Skip to content

Commit

Permalink
test queue merge with ids, not objects
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Nov 9, 2020
1 parent 88c2db4 commit aac1a84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/models/ems_refresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def self.queue_merge(targets, ems, create_task = false)
MiqQueue.put_or_update(queue_options) do |msg, item|
targets = msg.nil? ? targets : msg.data.concat(targets)
targets = uniq_targets(targets)
obj = targets.select { |t| t.kind_of?(ApplicationRecord) }
if obj.present?
obj.each { |i| _log.warn("queue_merge called with class: #{i.class.name}, id: #{i.id}") }
raise ArgumentError, "cannot call queue_merge with an activerecord object"
end

# If we are merging with an existing queue item we don't need a new
# task, just use the original one
Expand Down
10 changes: 7 additions & 3 deletions spec/models/ems_refresh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,27 @@ def assert_queue_item(expected_targets)
let(:vm) { FactoryBot.create(:vm_vmware, :name => "vm_vmware1", :ext_management_system => ems) }

it 'sends the command to queue' do
EmsRefresh.queue_merge([vm], ems)
EmsRefresh.queue_merge([vm.class.to_s, vm.id], ems)
expect(MiqQueue.count).to eq(1)
end

it 'raises arg error if passed an object' do
expect { EmsRefresh.queue_merge([vm], ems) }.to raise_error(ArgumentError)
end

context "task creation" do
before do
@miq_task = FactoryBot.create(:miq_task)
allow(EmsRefresh).to receive(:create_refresh_task).and_return(@miq_task)
end

it 'returns id of MiqTask linked to queued item' do
task_id = EmsRefresh.queue_merge([vm], ems, true)
task_id = EmsRefresh.queue_merge([vm.class.to_s, vm.id], ems, true)
expect(task_id).to eq @miq_task.id
end

it 'links created task with queued item' do
task_id = EmsRefresh.queue_merge([vm], ems)
task_id = EmsRefresh.queue_merge([vm.class.to_s, vm.id], ems)
queue_item = MiqQueue.find_by(:method_name => 'refresh', :role => "ems_inventory")
expect(queue_item.miq_task_id).to eq task_id
end
Expand Down

0 comments on commit aac1a84

Please sign in to comment.