From bf05f0ffeffd943efe383975c90e3121ce71c1fe Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Wed, 16 Oct 2024 19:45:20 +0530 Subject: [PATCH 01/16] add method for fetching terraform stdout from UI --- .../automation_manager/stack.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index c608285..beb5999 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -54,4 +54,59 @@ def refresh def raw_status Status.new(miq_task) end + + # Intend to be called by UI to display stdout. The stdout is stored in MiqTask#task_results or #message if error + # Since the task_results may contain a large block of data, it is desired to remove the task upon receiving the data + def raw_stdout_via_worker(userid, format = 'txt') + unless MiqRegion.my_region.role_active?("embedded_terraform") + msg = "Cannot get standard output of this terraform-template because the embedded terraform role is not enabled" + return MiqTask.create( + :name => 'terraform_stdout', + :userid => userid || 'system', + :state => MiqTask::STATE_FINISHED, + :status => MiqTask::STATUS_ERROR, + :message => msg + ).id + end + + options = {:userid => userid || 'system', :action => 'terraform_stdout'} + queue_options = { + :class_name => self.class, + :method_name => 'raw_stdout', + :instance_id => id, + :args => [format], + :priority => MiqQueue::HIGH_PRIORITY, + :role => nil + } + + MiqTask.generic_action_with_callback(options, queue_options) + end + + def raw_stdout(format = 'txt') + case format + when "json" then raw_stdout_json + when "html" then raw_stdout_html + else raw_stdout_txt + end + end + + def raw_stdout_json + raw_stdout_txt.split("\n") + end + + def raw_stdout_txt + job = Job.find_by(:miq_task_id => miq_task.id) + terraform_stack_id = job.options[:terraform_stack_id] + + if terraform_stack_id + response = Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) + response.message + end + end + + def raw_stdout_html + text = raw_stdout_txt + text = _("No output available") if text.blank? + TerminalToHtml.render(text) + end end From 48ef8871178592a95dceba6d9a9caf767cf400ed Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Wed, 16 Oct 2024 19:51:24 +0530 Subject: [PATCH 02/16] update comments --- .../providers/embedded_terraform/automation_manager/stack.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index beb5999..b26eb52 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -55,8 +55,7 @@ def raw_status Status.new(miq_task) end - # Intend to be called by UI to display stdout. The stdout is stored in MiqTask#task_results or #message if error - # Since the task_results may contain a large block of data, it is desired to remove the task upon receiving the data + # Intend to be called by UI to display stdout. The stdout is stored in TerraformRunner(api/stack#message) def raw_stdout_via_worker(userid, format = 'txt') unless MiqRegion.my_region.role_active?("embedded_terraform") msg = "Cannot get standard output of this terraform-template because the embedded terraform role is not enabled" From ab35fce6dd09e6ccd14a56d161b8cf47ae482545 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Wed, 16 Oct 2024 23:43:41 +0530 Subject: [PATCH 03/16] code review - use MiqTask to Job association --- .../embedded_terraform/automation_manager/stack.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index b26eb52..8892a2e 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -94,13 +94,13 @@ def raw_stdout_json end def raw_stdout_txt - job = Job.find_by(:miq_task_id => miq_task.id) + job = miq_task.job terraform_stack_id = job.options[:terraform_stack_id] - if terraform_stack_id - response = Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) - response.message - end + return if terraform_stack_id.blank? + + response = Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) + response.message end def raw_stdout_html From 11d51590caebebe158888b53c4a121380adc7b28 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Thu, 17 Oct 2024 10:31:08 +0530 Subject: [PATCH 04/16] code review: raw_stdout_json is not required, raise NotImplementedError --- .../providers/embedded_terraform/automation_manager/stack.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index 8892a2e..0e02540 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -90,7 +90,7 @@ def raw_stdout(format = 'txt') end def raw_stdout_json - raw_stdout_txt.split("\n") + raise NotImplementedError, _("raw_stdout_json must be implemented, if required") end def raw_stdout_txt From f1ddef8ba89f176bb2d6d205049214db7699a2b7 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 17:28:09 +0530 Subject: [PATCH 05/16] remove raw_stdout_json, as don't need to support for 'json' stdout --- .../providers/embedded_terraform/automation_manager/stack.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index 0e02540..e3638f4 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -83,16 +83,11 @@ def raw_stdout_via_worker(userid, format = 'txt') def raw_stdout(format = 'txt') case format - when "json" then raw_stdout_json when "html" then raw_stdout_html else raw_stdout_txt end end - def raw_stdout_json - raise NotImplementedError, _("raw_stdout_json must be implemented, if required") - end - def raw_stdout_txt job = miq_task.job terraform_stack_id = job.options[:terraform_stack_id] From 2acad777a7300b82fc81d7774b0db5c17eeebcbd Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 17:32:24 +0530 Subject: [PATCH 06/16] return empty string(not nil) if no miq_task or job in miq_task or no terraform_stack_id in job --- .../providers/embedded_terraform/automation_manager/stack.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index e3638f4..35788ad 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -89,10 +89,12 @@ def raw_stdout(format = 'txt') end def raw_stdout_txt + return '' if miq_task.nil? || miq_task.job.nil? + job = miq_task.job terraform_stack_id = job.options[:terraform_stack_id] - return if terraform_stack_id.blank? + return '' if terraform_stack_id.blank? response = Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) response.message From 553cfe9d86b7c827e2a620a8beedfc32e672daee Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 17:48:46 +0530 Subject: [PATCH 07/16] add test for Stack.raw_stdout : terraform runner without valid miq_task --- .../automation_manager/stack_spec.rb | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index 3014951..e99bfa4 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -30,4 +30,60 @@ end end end + + describe "#raw_stdout" do + let(:stack) { FactoryBot.create(:terraform_stack) } + let(:template) { FactoryBot.create(:terraform_template) } + + shared_examples_for "terraform runner stdout not valid in miq_task" do + it "json" do + expect(stack.raw_stdout("json")).to eq("") + end + + it "txt" do + expect(stack.raw_stdout("txt")).to eq "" + end + + it "html" do + expect(stack.raw_stdout("html")).to include <<~EOHTML +
+ No output available +
+ EOHTML + end + + it "nil" do + expect(stack.raw_stdout).to eq "" + end + end + + context "when miq_task is missing" do + before do + stack.miq_task = nil + end + + it_behaves_like "terraform runner stdout not valid in miq_task" + end + + context "when miq_task present, but missing miq_task.job" do + before do + stack.miq_task = FactoryBot.create(:miq_task) + stack.miq_task.job = nil + end + + it_behaves_like "terraform runner stdout not valid in miq_task" + end + + context "when miq_task.job.options present but missing terraform_stack_id" do + before do + stack.miq_task = FactoryBot.create(:miq_task) + stack.miq_task.job = ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Job.create_job(template, {}, {}, []).tap do |job| + job.state = "waiting_to_start" + job.options = {} + end + end + + it_behaves_like "terraform runner stdout not valid in miq_task" + end + end end From c3c06438dc1193991a62d7000297a02738b1e5ed Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 21:14:27 +0530 Subject: [PATCH 08/16] move code fetching data from terraform_runner to private method --- .../automation_manager/stack.rb | 23 +++++++++++++------ .../automation_manager/stack_spec.rb | 8 +++---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index 35788ad..27e48df 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -89,15 +89,11 @@ def raw_stdout(format = 'txt') end def raw_stdout_txt - return '' if miq_task.nil? || miq_task.job.nil? + data = terraform_runner_stack_data - job = miq_task.job - terraform_stack_id = job.options[:terraform_stack_id] - - return '' if terraform_stack_id.blank? + return '' if data.nil? - response = Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) - response.message + data.message end def raw_stdout_html @@ -105,4 +101,17 @@ def raw_stdout_html text = _("No output available") if text.blank? TerminalToHtml.render(text) end + + private + + def terraform_runner_stack_data + return if miq_task.nil? || miq_task.job.nil? + + job = miq_task.job + terraform_stack_id = job.options[:terraform_stack_id] + + return if terraform_stack_id.blank? + + Terraform::Runner.fetch_result_by_stack_id(terraform_stack_id) + end end diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index e99bfa4..b7745b0 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -35,7 +35,7 @@ let(:stack) { FactoryBot.create(:terraform_stack) } let(:template) { FactoryBot.create(:terraform_template) } - shared_examples_for "terraform runner stdout not valid in miq_task" do + shared_examples_for "terraform runner stdout not available from miq_task" do it "json" do expect(stack.raw_stdout("json")).to eq("") end @@ -62,7 +62,7 @@ stack.miq_task = nil end - it_behaves_like "terraform runner stdout not valid in miq_task" + it_behaves_like "terraform runner stdout not available from miq_task" end context "when miq_task present, but missing miq_task.job" do @@ -71,7 +71,7 @@ stack.miq_task.job = nil end - it_behaves_like "terraform runner stdout not valid in miq_task" + 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 @@ -83,7 +83,7 @@ end end - it_behaves_like "terraform runner stdout not valid in miq_task" + it_behaves_like "terraform runner stdout not available from miq_task" end end end From f914e2fab533d725b85c7f21f8661b45b4dcfbb4 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 22:06:05 +0530 Subject: [PATCH 09/16] add tests for #raw_stdout, when miq_task.job present --- .../automation_manager/stack_spec.rb | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index b7745b0..d4be0b9 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -35,6 +35,71 @@ let(:stack) { FactoryBot.create(:terraform_stack) } let(:template) { FactoryBot.create(:terraform_template) } + context "when miq_task.job present" do + require 'webmock/rspec' + require 'json' + let(:terraform_runner_url) { "https://1.2.3.4:7000" } + let(:hello_world_retrieve_response) do + JSON.parse(File.read(File.join(__dir__, "../../../../../lib/terraform/runner/data/responses/hello-world-retrieve-success.json"))) + end + let(:miq_task) do + miq_task = FactoryBot.create(:miq_task) + miq_task.job = job + miq_task + end + + 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'] + } + end + end + + let(:terraform_runner_stdout) { hello_world_retrieve_response['message'] } + let(:terraform_runner_stdout_html) { TerminalToHtml.render(terraform_runner_stdout) } + + retrieve_stub = nil + + before do + stack.miq_task = miq_task + + stub_const("ENV", ENV.to_h.merge("TERRAFORM_RUNNER_URL" => terraform_runner_url)) + + retrieve_stub = 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 + + expect(retrieve_stub).to(have_been_requested.times(1)) + end + + it "txt" do + expect(stack.raw_stdout("txt")).to eq terraform_runner_stdout + + expect(retrieve_stub).to(have_been_requested.times(1)) + end + + it "html" do + expect(stack.raw_stdout("html")).to eq terraform_runner_stdout_html + + expect(retrieve_stub).to(have_been_requested.times(1)) + end + + it "nil" do + expect(stack.raw_stdout).to eq terraform_runner_stdout + + expect(retrieve_stub).to(have_been_requested.times(1)) + end + end + shared_examples_for "terraform runner stdout not available from miq_task" do it "json" do expect(stack.raw_stdout("json")).to eq("") @@ -57,7 +122,7 @@ end end - context "when miq_task is missing" do + context "when miq_task.job is missing" do before do stack.miq_task = nil end From 92aaf352ecb42878722c0c91a077b550289dc51f Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 22:23:03 +0530 Subject: [PATCH 10/16] remove unnecessary require --- .../embedded_terraform/automation_manager/stack_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index d4be0b9..6aad37d 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -36,10 +36,9 @@ let(:template) { FactoryBot.create(:terraform_template) } context "when miq_task.job present" do - require 'webmock/rspec' - require 'json' 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) do From ab7c97f11cc2a4e609eaea0b7d9835a41db605c3 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 22:33:01 +0530 Subject: [PATCH 11/16] miq_task definition in single line --- .../embedded_terraform/automation_manager/stack_spec.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index 6aad37d..7f8f0da 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -41,11 +41,7 @@ require 'json' JSON.parse(File.read(File.join(__dir__, "../../../../../lib/terraform/runner/data/responses/hello-world-retrieve-success.json"))) end - let(:miq_task) do - miq_task = FactoryBot.create(:miq_task) - miq_task.job = job - miq_task - end + let(:miq_task) { FactoryBot.create(:miq_task, :job => job) } let(:job) do ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Job.create_job(template, {}, {}, []).tap do |job| @@ -121,7 +117,7 @@ end end - context "when miq_task.job is missing" do + context "when miq_task is missing" do before do stack.miq_task = nil end From 3ff9628e4f74fc54b46dfc48cc141f65db491431 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 22:41:56 +0530 Subject: [PATCH 12/16] don't need to test here, if terraform_runner api is called --- .../automation_manager/stack_spec.rb | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index 7f8f0da..0ba1fd1 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -55,43 +55,33 @@ let(:terraform_runner_stdout) { hello_world_retrieve_response['message'] } let(:terraform_runner_stdout_html) { TerminalToHtml.render(terraform_runner_stdout) } - retrieve_stub = nil - before do stack.miq_task = miq_task stub_const("ENV", ENV.to_h.merge("TERRAFORM_RUNNER_URL" => terraform_runner_url)) - retrieve_stub = 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 - ) + 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 - - expect(retrieve_stub).to(have_been_requested.times(1)) end it "txt" do expect(stack.raw_stdout("txt")).to eq terraform_runner_stdout - - expect(retrieve_stub).to(have_been_requested.times(1)) end it "html" do expect(stack.raw_stdout("html")).to eq terraform_runner_stdout_html - - expect(retrieve_stub).to(have_been_requested.times(1)) end it "nil" do expect(stack.raw_stdout).to eq terraform_runner_stdout - - expect(retrieve_stub).to(have_been_requested.times(1)) end end From 7185a41b913985288aba4dc7566445aa82ef7239 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 23:19:36 +0530 Subject: [PATCH 13/16] use let instead of before in the test --- .../automation_manager/stack_spec.rb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index 0ba1fd1..351411f 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -86,6 +86,10 @@ end shared_examples_for "terraform runner stdout not available from miq_task" do + before do + stack.miq_task = miq_task + end + it "json" do expect(stack.raw_stdout("json")).to eq("") end @@ -108,31 +112,27 @@ end context "when miq_task is missing" do - before do - stack.miq_task = nil - end + 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 - before do - stack.miq_task = FactoryBot.create(:miq_task) - stack.miq_task.job = nil - end + 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 - before do - stack.miq_task = FactoryBot.create(:miq_task) - stack.miq_task.job = ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Job.create_job(template, {}, {}, []).tap do |job| + 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 From 58727f003a6338bd893dae2a61a2f1f6adffd7e6 Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 23:38:36 +0530 Subject: [PATCH 14/16] remove setting stack.miq_task in before, setting in let --- .../automation_manager/stack_spec.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index 351411f..a96faa6 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -1,7 +1,7 @@ RSpec.describe ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Stack do - describe "#raw_status" do - let(:stack) { FactoryBot.create(:terraform_stack, :miq_task => miq_task) } + let(:stack) { FactoryBot.create(:terraform_stack, :miq_task => miq_task) } + describe "#raw_status" do context "with a running deployment" do let(:miq_task) { FactoryBot.create(:miq_task, :state => "Running", :status => "Ok", :message => "process initiated") } @@ -32,7 +32,6 @@ end describe "#raw_stdout" do - let(:stack) { FactoryBot.create(:terraform_stack) } let(:template) { FactoryBot.create(:terraform_template) } context "when miq_task.job present" do @@ -56,8 +55,6 @@ let(:terraform_runner_stdout_html) { TerminalToHtml.render(terraform_runner_stdout) } before do - stack.miq_task = miq_task - stub_const("ENV", ENV.to_h.merge("TERRAFORM_RUNNER_URL" => terraform_runner_url)) stub_request(:post, "#{terraform_runner_url}/api/stack/retrieve") @@ -86,10 +83,6 @@ end shared_examples_for "terraform runner stdout not available from miq_task" do - before do - stack.miq_task = miq_task - end - it "json" do expect(stack.raw_stdout("json")).to eq("") end From 77e70a9b1ccee9ca2b0bcfcc0bd6a985a4b4f37c Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Fri, 18 Oct 2024 23:44:29 +0530 Subject: [PATCH 15/16] don't need to return empty string, instead return nil, if terraform_runner_stack_data is not available --- .../embedded_terraform/automation_manager/stack.rb | 5 +---- .../embedded_terraform/automation_manager/stack_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb index 27e48df..d953615 100644 --- a/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb +++ b/app/models/manageiq/providers/embedded_terraform/automation_manager/stack.rb @@ -90,10 +90,7 @@ def raw_stdout(format = 'txt') def raw_stdout_txt data = terraform_runner_stack_data - - return '' if data.nil? - - data.message + data&.message end def raw_stdout_html diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index a96faa6..c74c22e 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -84,11 +84,11 @@ shared_examples_for "terraform runner stdout not available from miq_task" do it "json" do - expect(stack.raw_stdout("json")).to eq("") + expect(stack.raw_stdout("json")).to be_nil end it "txt" do - expect(stack.raw_stdout("txt")).to eq "" + expect(stack.raw_stdout("txt")).to be_nil end it "html" do @@ -100,7 +100,7 @@ end it "nil" do - expect(stack.raw_stdout).to eq "" + expect(stack.raw_stdout).to be_nil end end From 47707cc8325170daebd6b12ce6c9074512c1d1cd Mon Sep 17 00:00:00 2001 From: MANOJ PUTHRAN Date: Sat, 19 Oct 2024 01:04:31 +0530 Subject: [PATCH 16/16] add tests for raw_stdout_via_worker --- .../automation_manager/stack_spec.rb | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb index c74c22e..6c8447c 100644 --- a/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb +++ b/spec/models/manageiq/providers/embedded_terraform/automation_manager/stack_spec.rb @@ -1,7 +1,7 @@ RSpec.describe ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Stack do - let(:stack) { FactoryBot.create(:terraform_stack, :miq_task => miq_task) } - describe "#raw_status" do + let(:stack) { FactoryBot.create(:terraform_stack, :miq_task => miq_task) } + context "with a running deployment" do let(:miq_task) { FactoryBot.create(:miq_task, :state => "Running", :status => "Ok", :message => "process initiated") } @@ -32,6 +32,7 @@ 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 @@ -129,4 +130,61 @@ 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