Skip to content

Commit

Permalink
Merge pull request #19155 from Fryguy/fix_missing_task_context
Browse files Browse the repository at this point in the history
Handle cases where an EmbeddedAnsible job may not have stdout

(cherry picked from commit a6242cb)

https://bugzilla.redhat.com/show_bug.cgi?id=1735114
  • Loading branch information
carbonin authored and simaishi committed Aug 16, 2019
1 parent c854fe9 commit 1473b1a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ def update_with_provider_object(raw_job)
end

def raw_stdout_json
miq_task.context_data[:ansible_runner_stdout]
miq_task.try(&:context_data).try(:[], :ansible_runner_stdout) || []
end

def raw_stdout_txt
raw_stdout_json.collect { |j| j["stdout"] }.join("\n")
end

def raw_stdout_html
TerminalToHtml.render(raw_stdout_txt)
text = raw_stdout_txt
text = _("No output available") if text.blank?
TerminalToHtml.render(text)
end
end
2 changes: 1 addition & 1 deletion lib/terminal_to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.raw_render(raw)
end

def self.wrap_html_container(rendered)
stylesheet = File.read(File.join(Gem.latest_spec_for("terminal").full_gem_path, "/app/assets/stylesheets/terminal.css"))
stylesheet = File.read(File.join(Bundler.environment.specs["terminal"].first.full_gem_path, "/app/assets/stylesheets/terminal.css"))
<<~EOHTML
<div>
<style scoped>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
let(:network_credential) { FactoryBot.create(:ansible_network_credential, :manager_ref => "3", :resource => manager) }
let(:vault_credential) { FactoryBot.create(:ansible_vault_credential, :manager_ref => "4", :resource => manager) }

let(:raw_stdout_json) do
[
{"stdout" => "A stdout from the job"},
{"stdout" => "Errmahgerd... ANSIBLER"},
{"stdout" => "And another one"}
]
end

let(:template) { FactoryBot.create(:embedded_ansible_configuration_script, :manager => manager, :parent => playbook) }

describe "job operations" do
Expand Down Expand Up @@ -205,4 +197,82 @@
end
end
end

describe "#raw_stdout" do
let(:ansible_runner_stdout) do
[
{"stdout" => "Line 1"}, # no color
{"stdout" => "\e[0;32mLine 2\e[0m"}, # green
{"stdout" => "\e[0;31mLine 3\e[0m"}, # red
]
end

context "when miq_task present" do
before do
job.miq_task = FactoryGirl.create(:miq_task, :context_data => {:ansible_runner_stdout => ansible_runner_stdout})
end

it "json" do
expect(job.raw_stdout("json")).to eq ansible_runner_stdout
end

it "txt" do
expect(job.raw_stdout("txt")).to eq "Line 1\n\e[0;32mLine 2\e[0m\n\e[0;31mLine 3\e[0m"
end

it "html" do
expect(job.raw_stdout("html")).to include <<~EOHTML
<div class='term-container'>
Line 1
<span class='term-fg32'>Line 2</span>
<span class='term-fg31'>Line 3</span>
</div>
EOHTML
end

it "nil" do
expect(job.raw_stdout).to eq "Line 1\n\e[0;32mLine 2\e[0m\n\e[0;31mLine 3\e[0m"
end
end

shared_examples_for "ansible runner stdout not valid in miq_task" do
it "json" do
expect(job.raw_stdout("json")).to eq([])
end

it "txt" do
expect(job.raw_stdout("txt")).to eq ""
end

it "html" do
expect(job.raw_stdout("html")).to include <<~EOHTML
<div class='term-container'>
No output available
</div>
EOHTML
end

it "nil" do
expect(job.raw_stdout).to eq ""
end
end

context "when miq_task is missing" do
before { job.miq_task = nil }

it_behaves_like "ansible runner stdout not valid in miq_task"
end

context "when miq_task present, but without context data" do
before { job.miq_task = FactoryGirl.create(:miq_task) }

it_behaves_like "ansible runner stdout not valid in miq_task"
end

context "when miq_task present with context_data, but missing ansible_runner_stdout" do
before { job.miq_task = FactoryGirl.create(:miq_task, :context_data => {}) }

it_behaves_like "ansible runner stdout not valid in miq_task"
end
end
end

0 comments on commit 1473b1a

Please sign in to comment.