Skip to content

Commit

Permalink
Merge pull request #20738 from djberg96/ansible_playbook_log_stdout
Browse files Browse the repository at this point in the history
Update playbook_log_stdout to handle possible nil job

(cherry picked from commit 3493002)

https://bugzilla.redhat.com/show_bug.cgi?id=1835226
  • Loading branch information
kbrock authored and simaishi committed Nov 9, 2020
1 parent 3f931e3 commit 3ae0f41
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/models/mixins/ansible_playbook_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ def hosts_array(hosts_string)
end

def playbook_log_stdout(log_option, job)
raise ArgumentError, "invalid job object" if job.nil?
return unless %(on_error always).include?(log_option)
return if log_option == 'on_error' && job.raw_status.succeeded?

$log.info("Stdout from ansible job #{job.name}: #{job.raw_stdout('txt_download')}")
rescue StandardError => err
$log.error("Failed to get stdout from ansible job #{job.name}")
if job.nil?
$log.error("Job was nil, must pass a valid job")
else
$log.error("Failed to get stdout from ansible job #{job.name}")
end
$log.log_backtrace(err)
end
end
7 changes: 6 additions & 1 deletion app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ def decrypt_options(opts)

def log_stdout(action)
log_option = options.fetch_path(:config_info, action.downcase.to_sym, :log_output) || 'on_error'
playbook_log_stdout(log_option, job(action))
job = job(action)
if job.nil?
$log.info("No stdout available due to missing job")
else
playbook_log_stdout(log_option, job)
end
end
end
37 changes: 37 additions & 0 deletions spec/models/mixins/ansible_playbook_mixin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe AnsiblePlaybookMixin do
let(:test_instance) do
Class.new(ActiveRecord::Base) do
def self.name; "TestClass"; end
self.table_name = "services"
include AnsiblePlaybookMixin
end.new
end

let(:job) { FactoryBot.create(:embedded_ansible_job, :name => 'ansible_playbook_mixin_test') }
let(:task) { FactoryBot.create(:miq_task) }
let(:status) { ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job::Status.new(task, MiqTask::STATUS_OK) }

context "#playbook_log_stdout" do
it "returns if the log option is not on_error or always" do
expect(test_instance.playbook_log_stdout('test', job)).to be_nil
end

it "returns if the log option is on_error but the job's raw_status is succeeded" do
allow(status).to receive(:succeeded?).and_return(true)
allow(job).to receive(:raw_status).and_return(status)
expect(test_instance.playbook_log_stdout('on_error', job)).to be_nil
end

it "logs the expected message if the log option is not on_error, and the job has succeeded" do
allow(job).to receive(:raw_stdout).with('txt_download').and_return("<test ansible text>")
expect($log).to receive(:info).with("Stdout from ansible job ansible_playbook_mixin_test: <test ansible text>")
test_instance.playbook_log_stdout('always', job)
end

it "raises an error if the job is nil" do
expect($log).to receive(:error).with("Job was nil, must pass a valid job")
expect($log).to receive(:log_backtrace)
test_instance.playbook_log_stdout('always', nil)
end
end
end

0 comments on commit 3ae0f41

Please sign in to comment.