Skip to content

Commit

Permalink
Fix the ems_events relation to return src and dest
Browse files Browse the repository at this point in the history
The query for vm.ems_events wasn't returning any events where the vm was
the dest_vm_or_template_id because an extra AND
ems_event.vm_or_template_id = id was being added to the where query.
  • Loading branch information
agrare committed Aug 22, 2019
1 parent e62ec0b commit 057fb9c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ class VmOrTemplate < ApplicationRecord
has_one :openscap_result, :as => :resource, :dependent => :destroy

# EMS Events
has_many :ems_events, ->(vmt) { where(["vm_or_template_id = ? OR dest_vm_or_template_id = ?", vmt.id, vmt.id]).order(:timestamp) },
:class_name => "EmsEvent"
def ems_events
EmsEvent.where("vm_or_template_id = ? OR dest_vm_or_template_id = ?", id, id).order(:timestamp)
end

has_many :ems_events_src, :class_name => "EmsEvent"
has_many :ems_events_dest, :class_name => "EmsEvent", :foreign_key => :dest_vm_or_template_id
Expand Down
15 changes: 9 additions & 6 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,15 @@
end

it "with ems_events" do
ems = FactoryBot.create(:ems_vmware_with_authentication)
vm = FactoryBot.create(:vm_vmware, :ext_management_system => ems)
ems_event = FactoryBot.create(:ems_event)
vm.ems_events << ems_event
expect(vm.ems_events.first).to be_kind_of(EmsEvent)
expect(vm.ems_events.first.id).to eq(ems_event.id)
ems = FactoryBot.create(:ems_vmware_with_authentication)
vm = FactoryBot.create(:vm_vmware, :ext_management_system => ems)
ems_event_src = FactoryBot.create(:ems_event, :vm_or_template => vm)
ems_event_dest = FactoryBot.create(:ems_event, :dest_vm_or_template => vm)

expect(vm.ems_events.count).to eq(2)
expect(vm.ems_events_src.first).to be_kind_of(EmsEvent)
expect(vm.ems_events_src.first.id).to eq(ems_event_src.id)
expect(vm.ems_events_dest.first.id).to eq(ems_event_dest.id)
end

it "#miq_provision_vms" do
Expand Down

0 comments on commit 057fb9c

Please sign in to comment.