-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods to fetch terraform stdout for showing in UI. #78
Merged
agrare
merged 16 commits into
ManageIQ:master
from
putmanoj:add-method-fetch-terraform-stdout-for-ui
Oct 21, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bf05f0f
add method for fetching terraform stdout from UI
putmanoj 48ef887
update comments
putmanoj ab35fce
code review - use MiqTask to Job association
putmanoj 11d5159
code review: raw_stdout_json is not required, raise NotImplementedError
putmanoj f1ddef8
remove raw_stdout_json, as don't need to support for 'json' stdout
putmanoj 2acad77
return empty string(not nil) if no miq_task or job in miq_task or no …
putmanoj 553cfe9
add test for Stack.raw_stdout : terraform runner without valid miq_task
putmanoj c3c0643
move code fetching data from terraform_runner to private method
putmanoj f914e2f
add tests for #raw_stdout, when miq_task.job present
putmanoj 92aaf35
remove unnecessary require
putmanoj ab7c97f
miq_task definition in single line
putmanoj 3ff9628
don't need to test here, if terraform_runner api is called
putmanoj 7185a41
use let instead of before in the test
putmanoj 58727f0
remove setting stack.miq_task in before, setting in let
putmanoj 77e70a9
don't need to return empty string, instead return nil, if terraform_r…
putmanoj 47707cc
add tests for raw_stdout_via_worker
putmanoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,161 @@ | |
end | ||
end | ||
end | ||
|
||
describe "#raw_stdout" do | ||
let(:stack) { FactoryBot.create(:terraform_stack, :miq_task => miq_task) } | ||
let(:template) { FactoryBot.create(:terraform_template) } | ||
|
||
context "when miq_task.job present" do | ||
let(:terraform_runner_url) { "https://1.2.3.4:7000" } | ||
let(:hello_world_retrieve_response) do | ||
require 'json' | ||
JSON.parse(File.read(File.join(__dir__, "../../../../../lib/terraform/runner/data/responses/hello-world-retrieve-success.json"))) | ||
end | ||
let(:miq_task) { FactoryBot.create(:miq_task, :job => job) } | ||
|
||
let(:job) do | ||
ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Job.create_job(template, {}, {}, []).tap do |job| | ||
job.state = "finished" | ||
job.options = { | ||
:terraform_stack_id => hello_world_retrieve_response['stack_id'] | ||
} | ||
Comment on lines
+47
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a FactoryBot factory would be a better way to set up these Job objects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
end | ||
end | ||
|
||
let(:terraform_runner_stdout) { hello_world_retrieve_response['message'] } | ||
let(:terraform_runner_stdout_html) { TerminalToHtml.render(terraform_runner_stdout) } | ||
|
||
before do | ||
stub_const("ENV", ENV.to_h.merge("TERRAFORM_RUNNER_URL" => terraform_runner_url)) | ||
|
||
stub_request(:post, "#{terraform_runner_url}/api/stack/retrieve") | ||
.with(:body => hash_including({:stack_id => hello_world_retrieve_response['stack_id']})) | ||
.to_return( | ||
:status => 200, | ||
:body => hello_world_retrieve_response.to_json | ||
) | ||
end | ||
|
||
it "json" do | ||
expect(stack.raw_stdout("json")).to eq terraform_runner_stdout | ||
end | ||
|
||
it "txt" do | ||
expect(stack.raw_stdout("txt")).to eq terraform_runner_stdout | ||
end | ||
|
||
it "html" do | ||
expect(stack.raw_stdout("html")).to eq terraform_runner_stdout_html | ||
end | ||
|
||
it "nil" do | ||
expect(stack.raw_stdout).to eq terraform_runner_stdout | ||
end | ||
end | ||
|
||
shared_examples_for "terraform runner stdout not available from miq_task" do | ||
it "json" do | ||
expect(stack.raw_stdout("json")).to be_nil | ||
end | ||
|
||
it "txt" do | ||
expect(stack.raw_stdout("txt")).to be_nil | ||
end | ||
|
||
it "html" do | ||
expect(stack.raw_stdout("html")).to include <<~EOHTML | ||
<div class='term-container'> | ||
No output available | ||
</div> | ||
EOHTML | ||
end | ||
|
||
it "nil" do | ||
expect(stack.raw_stdout).to be_nil | ||
end | ||
end | ||
|
||
context "when miq_task is missing" do | ||
let(:miq_task) { nil } | ||
|
||
it_behaves_like "terraform runner stdout not available from miq_task" | ||
end | ||
|
||
context "when miq_task present, but missing miq_task.job" do | ||
let(:miq_task) { FactoryBot.create(:miq_task, :job => nil) } | ||
|
||
it_behaves_like "terraform runner stdout not available from miq_task" | ||
end | ||
|
||
context "when miq_task.job.options present but missing terraform_stack_id" do | ||
let(:job) do | ||
ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Job.create_job(template, {}, {}, []).tap do |job| | ||
job.state = "waiting_to_start" | ||
job.options = {} | ||
end | ||
end | ||
|
||
let(:miq_task) { FactoryBot.create(:miq_task, :job => job) } | ||
|
||
it_behaves_like "terraform runner stdout not available from miq_task" | ||
end | ||
end | ||
|
||
describe "#raw_stdout_via_worker" do | ||
let(:stack) { FactoryBot.create(:terraform_stack) } | ||
|
||
context "when embedded_terraform role is enabled" do | ||
before do | ||
EmbeddedTerraformEvmSpecHelper.assign_embedded_terraform_role | ||
|
||
allow_any_instance_of(ManageIQ::Providers::EmbeddedTerraform::AutomationManager::ConfigurationScriptSource).to receive(:checkout_git_repository) | ||
end | ||
|
||
describe "#raw_stdout_via_worker with no errors" do | ||
before do | ||
EvmSpecHelper.local_miq_server | ||
allow(described_class).to receive(:find).and_return(stack) | ||
|
||
allow(MiqTask).to receive(:wait_for_taskid) do | ||
request = MiqQueue.find_by(:class_name => described_class.name) | ||
request.update(:state => MiqQueue::STATE_DEQUEUE) | ||
request.deliver_and_process | ||
end | ||
end | ||
|
||
it "gets stdout from the job" do | ||
expect(stack).to receive(:raw_stdout).and_return("A stdout from the job") | ||
taskid = stack.raw_stdout_via_worker("user") | ||
MiqTask.wait_for_taskid(taskid) | ||
expect(MiqTask.find(taskid)).to have_attributes( | ||
:task_results => "A stdout from the job", | ||
:status => "Ok" | ||
) | ||
end | ||
|
||
it "returns the error message" do | ||
expect(stack).to receive(:raw_stdout).and_throw("Failed to get stdout from the job") | ||
taskid = stack.raw_stdout_via_worker("user") | ||
MiqTask.wait_for_taskid(taskid) | ||
expect(MiqTask.find(taskid).message).to include("Failed to get stdout from the job") | ||
expect(MiqTask.find(taskid).status).to eq("Error") | ||
end | ||
end | ||
end | ||
|
||
context "when embedded_terraform role is disabled" do | ||
describe "#raw_stdout_via_worker return error" do | ||
let(:role_enabled) { false } | ||
|
||
it "returns an error message" do | ||
taskid = stack.raw_stdout_via_worker("user") | ||
expect(MiqTask.find(taskid)).to have_attributes( | ||
:message => "Cannot get standard output of this terraform-template because the embedded terraform role is not enabled", | ||
:status => "Error" | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to consider moving this up to
spec/data
or something if it is going to be used by more than just theTerraform::Runner
specs since this../../../etc...
makes it hard to read where this file lives. Without counting the parent dirs I thought this lived in the "production" lib/terraform directory not spec/lib/terraformThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes agree makes it harder to read,
could make it like
"../../../../../../spec/lib/terraform/runner/data/responses/hello-world-retrieve-success.json"
also maybe should move this common test data, to somewhere like
<base-dir>/spec/data
. then still look like"../../../../../../spec/data/responses/hello-world-retrieve-success.json"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't know if there more easier way to refer to test data resources without using "../../../../../../"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use the
ManageIQ::Providers::EmbeddedTerraform::Engine.root
Pathname to build a path from the root of the pluginThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok