Skip to content

Commit

Permalink
Merge pull request #11718 from lfu/event_no_set_type_1379854
Browse files Browse the repository at this point in the history
Fix the issue that user can't add alerts.
(cherry picked from commit 8b43521)

https://bugzilla.redhat.com/show_bug.cgi?id=1379854
  • Loading branch information
gmcculloug authored and chessbyte committed Oct 19, 2016
1 parent e42e425 commit d88e922
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
3 changes: 1 addition & 2 deletions app/controllers/miq_policy_controller/alerts.rb
Expand Up @@ -277,8 +277,7 @@ def alert_build_edit_screen

# Build hash of arrays of all events by event type
@edit[:events] = {}
MiqEventDefinition.all_events.each do |e|
next if e.name.ends_with?("compliance_check")
MiqEventDefinition.all_control_events.each do |e|
@edit[:events][e.id] = (e.etype.description + ": " + e.description)
end

Expand Down
8 changes: 5 additions & 3 deletions app/models/miq_event_definition.rb
Expand Up @@ -21,6 +21,10 @@ def self.all_events
where(:event_type => "Default")
end

def self.all_control_events
all_events.where.not("name like ?", "%compliance_check").select { |e| e.etype }
end

def miq_policies
p_ids = MiqPolicyContent.where(:miq_event_definition_id => id).uniq.pluck(:miq_policy_id)
MiqPolicy.where(:id => p_ids).to_a
Expand Down Expand Up @@ -63,9 +67,7 @@ def self.import_from_hash(event, options = {})
end

def etype
set = memberof.first
raise "unexpected error, no type found for event #{name}" if set.nil?
set
memberof.first.tap { |set| _log.error("No type found for event #{name}") if set.nil? }
end

def self.etypes
Expand Down
44 changes: 44 additions & 0 deletions spec/models/miq_event_definition_spec.rb
Expand Up @@ -55,4 +55,48 @@ def create_set!(name)
end
end
end

describe '#etype' do
it "returns event set type" do
set_type = 'set_testing'
set = MiqEventDefinitionSet.create(:name => set_type, :description => "Set testing")
event = FactoryGirl.create(:miq_event_definition, :name => "vm_start")
set.add_member(event)

expect(event.etype.name).to eq(set_type)
end

it "returns nil when not belong to any event set" do
event = FactoryGirl.create(:miq_event_definition, :name => "test_event")
expect(event.etype).to be_nil
end
end

describe '.all_control_events' do
subject { MiqEventDefinition.all_control_events }

before do
com_set = MiqEventDefinitionSet.create(:name => "compliance", :description => "Compliance Events")
FactoryGirl.create(:miq_event_definition,
:name => "host_compliance_check",
:event_type => "Default").tap { |e| com_set.add_member(e) }
end

it 'has all default control policy events with set type' do
event = FactoryGirl.create(:miq_event_definition, :name => "some_event", :event_type => "Default")
set = MiqEventDefinitionSet.create(:name => "evm_operations", :description => "EVM Events")
set.add_member(event)

expect(subject.include?(event)).to be true
end

it 'has not the events for compliance policy' do
expect(subject.any? { |e| e.name.ends_with?("compliance_check") }).to be false
end

it 'has not the events without a set type' do
event = FactoryGirl.create(:miq_event_definition, :name => "test_event", :event_type => "Default")
expect(subject.include?(event)).to be false
end
end
end

0 comments on commit d88e922

Please sign in to comment.