From e3687406f08f5775b8879b627f357f9c60ac0646 Mon Sep 17 00:00:00 2001 From: Mooli Tayer Date: Thu, 3 Aug 2017 14:34:59 +0300 Subject: [PATCH] Add target to event existence check --- app/models/ems_event.rb | 10 ++++++---- spec/models/ems_event_spec.rb | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/models/ems_event.rb b/app/models/ems_event.rb index 5037bf1201c..cf9cad5cd0c 100644 --- a/app/models/ems_event.rb +++ b/app/models/ems_event.rb @@ -234,10 +234,12 @@ def self.create_event(event) event.delete_if { |k,| k.to_s.ends_with?("_ems_ref") } new_event = EmsEvent.create(event) unless EmsEvent.exists?( - :event_type => event[:event_type], - :timestamp => event[:timestamp], - :chain_id => event[:chain_id], - :ems_id => event[:ems_id] + :event_type => event[:event_type], + :timestamp => event[:timestamp], + :chain_id => event[:chain_id], + :ems_id => event[:ems_id], + :target_id => event[:target_id], + :target_type => event[:target_type], ) new_event.handle_event if new_event new_event diff --git a/spec/models/ems_event_spec.rb b/spec/models/ems_event_spec.rb index 00dcc3032a6..473b0946db8 100644 --- a/spec/models/ems_event_spec.rb +++ b/spec/models/ems_event_spec.rb @@ -169,10 +169,12 @@ context ".add" do before :each do @event_hash = { - :event_type => "event_with_availability_zone", - :vm_ems_ref => @vm.ems_ref, - :timestamp => Time.now, - :ems_id => @ems.id + :event_type => "event_with_availability_zone", + :target_type => @vm.class.name, + :target_id => @vm.id, + :vm_ems_ref => @vm.ems_ref, + :timestamp => Time.now, + :ems_id => @ems.id } end @@ -196,6 +198,31 @@ expect(new_event.availability_zone_id).to eq @availability_zone.id end end + + context "when an event was previously added" do + before do + EmsEvent.add(@ems.id, @event_hash) + end + + it "should reject duplicates" do + ems_event = EmsEvent.add(@ems.id, @event_hash) + expect( + EmsEvent.where(@event_hash.except(:target_id)).count + ).to eq(1) + expect(ems_event).to be_nil + end + + it "should add an identical event if it is for a different target" do + ems_event = EmsEvent.add( + @ems.id, + @event_hash.merge(:target_id => FactoryGirl.build(:vm_openstack).id) + ) + expect( + EmsEvent.where(@event_hash.except(:target_id)).count + ).to eq(2) + expect(ems_event).to_not be_nil + end + end end end