Skip to content

Commit

Permalink
Merge pull request #20426 from lfu/compiance_action_loop
Browse files Browse the repository at this point in the history
Improvement to action_check_compliance to avoid infinite loop.
  • Loading branch information
gtanzillo authored Aug 10, 2020
2 parents a2634bc + 1357bcb commit 484cf2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/miq_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ def action_check_compliance(action, rec, inputs)
return
end

if inputs[:policy].mode == 'compliance'
MiqPolicy.logger.warn("MIQ(action_check_compliance): Invoking action [#{action.description}] for event [#{inputs[:event].description}] would cause infinite loop, skipping")
return
end

if inputs[:synchronous]
MiqPolicy.logger.info("MIQ(action_check_compliance): Now executing [#{action.description}] of #{rec.class.name} [#{rec.name}]")
rec.check_compliance
Expand Down Expand Up @@ -982,6 +987,10 @@ def self.create_or_update(action_attributes)
end
end

def self.allowed_for_policies(mode)
mode == 'compliance' ? where.not(:name => 'check_compliance') : all
end

def self.fixture_path
FIXTURE_DIR.join("#{to_s.pluralize.underscore}.csv")
end
Expand Down
19 changes: 19 additions & 0 deletions spec/models/miq_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@
end
end

context '#action_check_compliance' do
let(:vm) { FactoryBot.create(:vm_infra) }
let(:action) { FactoryBot.create(:miq_action, :name => 'check_compliance', :description => 'Check Host or Vm Compliance') }
let(:policy) { FactoryBot.create(:miq_policy, :mode => 'compliance') }
let(:event) { FactoryBot.create(:miq_event_definition, :description => 'vm_compliance_check') }

it 'will not execute for compliance polices' do
expect(vm).not_to receive(:check_compliance)
expect(MiqPolicy.logger).to receive(:warn)
action.action_check_compliance(action, vm, :policy => policy, :synchronous => true, :event => event)
end

it 'will execute for control polices' do
policy.update(:mode => 'control')
expect(vm).to receive(:check_compliance)
action.action_check_compliance(action, vm, :policy => policy, :synchronous => true, :event => event)
end
end

context '.create_default_actions' do
context 'seeding default actions from a file with 3 csv rows and some comments' do
before do
Expand Down

0 comments on commit 484cf2c

Please sign in to comment.