Skip to content

Commit

Permalink
Utility function to update PxeMenu list
Browse files Browse the repository at this point in the history
With this commit we introduce a utility method on PxeServer model
to handle PxeMenus list update. Until now, miq-api was always
recreating all menus upon server update, but often we can actually
just preserve exisitng menus. This method calculates what menus to
add, what to preserve and what to remove, and then executes it.

Fixes #18940

Signed-off-by: Miha Pleško <[email protected]>
  • Loading branch information
miha-plesko committed Aug 12, 2019
1 parent 11ad9b8 commit e1afbe0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/pxe_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ def delete_provisioning_files(pxe_image, mac_address, windows_image = nil, custo
_log.info("#{log_message}...Complete")
end

def ensure_menu_list(menu_list)
current_menus = pxe_menus.map(&:file_name)
to_destroy = current_menus.reject { |menu| menu_list.include?(menu) }
to_create = menu_list.reject { |menu| current_menus.include?(menu) }
pxe_menus.where(:file_name => to_destroy).destroy_all
to_create.each { |menu| pxe_menus << PxeMenu.create(:file_name => menu) }
end

def self.display_name(number = 1)
n_('PXE Server', 'PXE Servers', number)
end
Expand Down
63 changes: 63 additions & 0 deletions spec/models/pxe_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@pxe_server = FactoryBot.create(:pxe_server)
end

subject { FactoryBot.create(:pxe_server) }

context "#sync_images_queue" do
it "should create a queue entry with the correct parameters" do
msg = @pxe_server.sync_images_queue
Expand Down Expand Up @@ -310,4 +312,65 @@ def file_write(file, contents)
expect(@pxe_server.discovered_pxe_images).to eq([@discovered_image])
end
end

describe '#ensure_menu_list' do
context 'when creating server' do
subject { FactoryBot.build(:pxe_server) }
it 'existing menus remain' do
subject.ensure_menu_list(%w[new])
expect(PxeMenu.count).to eq(1)
end
end

context 'when updating server' do
let!(:pxe_menu_1) { FactoryBot.create(:pxe_menu, :pxe_server => subject, :file_name => 'existing1') }
let!(:pxe_image_1) { FactoryBot.create(:pxe_image, :pxe_server => subject, :pxe_menu => pxe_menu_1) }
let!(:pxe_menu_2) { FactoryBot.create(:pxe_menu, :pxe_server => subject, :file_name => 'existing2') }
let!(:pxe_image_2) { FactoryBot.create(:pxe_image, :pxe_server => subject, :pxe_menu => pxe_menu_2) }

context 'when nothing changed' do
let(:menus) { %w[existing1 existing2] }
it 'all menus remain' do
subject.ensure_menu_list(menus)
expect(PxeMenu.count).to eq(2)
expect(PxeImage.count).to eq(2)
expect(PxeMenu.find_by(:id => pxe_menu_1.id)).to eq(pxe_menu_1)
expect(PxeMenu.find_by(:id => pxe_menu_2.id)).to eq(pxe_menu_2)
end
end

context 'when new menus are added' do
let(:menus) { %w[existing1 existing2 new] }
it 'existing menus remain' do
subject.ensure_menu_list(menus)
expect(PxeMenu.count).to eq(3)
expect(PxeImage.count).to eq(2)
expect(PxeMenu.find_by(:id => pxe_menu_1.id)).to eq(pxe_menu_1)
expect(PxeMenu.find_by(:id => pxe_menu_2.id)).to eq(pxe_menu_2)
expect(PxeMenu.find_by(:file_name => 'new')).not_to be_nil
end
end

context 'when menus are deleted' do
let(:menus) { %w[existing1] }
it 'menus are destroyed' do
subject.ensure_menu_list(menus)
expect(PxeMenu.count).to eq(1)
expect(PxeImage.count).to eq(1)
expect(PxeMenu.find_by(:id => pxe_menu_1.id)).to eq(pxe_menu_1)
expect(PxeMenu.find_by(:id => pxe_menu_2.id)).to be_nil
end
end

context 'when different menus are specified' do
let(:menus) { %w[new] }
it 'old menus are destroyed' do
subject.ensure_menu_list(menus)
expect(PxeMenu.count).to eq(1)
expect(PxeImage.count).to eq(0)
expect(PxeMenu.find_by(:file_name => 'new')).not_to be_nil
end
end
end
end
end

0 comments on commit e1afbe0

Please sign in to comment.