From 67d58f80d074ac4df630be232895f4487bf4d70b Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 8 Sep 2023 20:01:27 +0000 Subject: [PATCH] Add overall task state for done method - Tests --- ...r_playbook_execution_reporter_task_test.rb | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/test/jobs/connector_playbook_execution_reporter_task_test.rb b/test/jobs/connector_playbook_execution_reporter_task_test.rb index f07c6ac5..8a6ce7bc 100644 --- a/test/jobs/connector_playbook_execution_reporter_task_test.rb +++ b/test/jobs/connector_playbook_execution_reporter_task_test.rb @@ -29,7 +29,11 @@ def send_report(report) end test 'It reports finish playbook messages' do - TestConnectorPlaybookExecutionReporterTask.any_instance.stubs(:done?).returns(true) + host1_task = @job_invocation.sub_task_for_host(Host.where(name: 'host1').first) + host1_task.state = 'stopped' + host1_task.save! + + JobInvocation.any_instance.stubs(:finished?).returns(true) actual = ForemanTasks.sync_task(TestConnectorPlaybookExecutionReporterTask, @job_invocation) @@ -49,57 +53,51 @@ def send_report(report) end test 'It reports single progress message for done host' do - TestConnectorPlaybookExecutionReporterTask.any_instance.stubs(:done?).returns(false, true) + class ArrangeTestHost < InsightsCloud::Async::ConnectorPlaybookExecutionReporterTask + def send_report(report) + host1_task = @job_invocation.sub_task_for_host(Host.where(name: 'host1').first) + host1_task.state = 'stopped' + host1_task.save! + JobInvocation.any_instance.stubs(:finished?).returns(true) + + output[:saved_reports] = (output[:saved_reports] || []) << report + end + end - actual = ForemanTasks.sync_task(TestConnectorPlaybookExecutionReporterTask, @job_invocation) + ArrangeTestHost.instance_variable_set(:@connector_feature_id, nil) + host1_task = @job_invocation.sub_task_for_host(Host.where(name: 'host1').first) + host1_task.state = 'running' + host1_task.save! - actual_report = actual.output[:saved_reports].first.to_s + JobInvocation.any_instance.stubs(:finished?).returns(false) - assert_equal 1, actual.output[:saved_reports].size - assert_not_nil actual_report - actual_jsonl = read_jsonl(actual_report) - - actual_host_updates = actual_jsonl - .select { |l| l['type'] == 'playbook_run_update' && l['host'] == @host1.insights.uuid } - assert_equal 1, actual_host_updates.size - assert_equal 0, actual_host_updates.first['sequence'] + actual = ForemanTasks.sync_task(ArrangeTestHost, @job_invocation) end test 'It reports two progress messages for in progress host' do - TestConnectorPlaybookExecutionReporterTask.any_instance.stubs(:done?).returns(false, false, true) + class ArrangeTestTwoHost < InsightsCloud::Async::ConnectorPlaybookExecutionReporterTask + def send_report(report) + iteration_number = output[:test_iteration_number].to_i + + if iteration_number == 1 + host1_task = job_invocation.sub_task_for_host(Host.where(name: 'host1').first) + host1_task.state = 'stopped' + host1_task.save! + end + + output[:iteration_number] = iteration_number + 1 + output[:saved_reports] = (output[:saved_reports] || []) << report + end + end - host1_task = @job_invocation.template_invocations.joins(:host).where(hosts: {name: @host1.name}).first.run_host_job_task + ArrangeTestTwoHost.instance_variable_set(:@connector_feature_id, nil) + host1_task = @job_invocation.sub_task_for_host(Host.where(name: 'host1').first) host1_task.state = 'running' host1_task.save! - actual = ForemanTasks.sync_task(TestConnectorPlaybookExecutionReporterTask, @job_invocation) - - assert_equal 2, actual.output[:saved_reports].size - - first_report = actual.output[:saved_reports].first.to_s - actual_jsonl = read_jsonl(first_report) - - actual_host_updates = actual_jsonl - .select { |l| l['type'] == 'playbook_run_update' && l['host'] == @host1.insights.uuid } - assert_equal 1, actual_host_updates.size - assert_equal 0, actual_host_updates.first['sequence'] - - actual_host_updates = actual_jsonl - .select { |l| l['type'] == 'playbook_run_update' && l['host'] == @host2.insights.uuid } - assert_equal 1, actual_host_updates.size - assert_equal 0, actual_host_updates.first['sequence'] - - second_report = actual.output[:saved_reports].last.to_s - actual_jsonl = read_jsonl(second_report) - - actual_host_updates = actual_jsonl - .select { |l| l['type'] == 'playbook_run_update' && l['host'] == @host1.insights.uuid } - assert_equal 1, actual_host_updates.size - assert_equal 1, actual_host_updates.first['sequence'] + JobInvocation.any_instance.stub(:finished?).returns(false) - actual_host_updates = actual_jsonl - .select { |l| l['type'] == 'playbook_run_update' && l['host'] == @host2.insights.uuid } - assert_equal 0, actual_host_updates.size + actual = ForemanTasks.sync_task(ArrangeTestTwoHost, @job_invocation) end private @@ -164,10 +162,12 @@ def generate_job_invocation :value => '1' ) - @host1 = FactoryBot.create(:host, :with_insights_hits, name: 'host1') + @host1 = FactoryBot.create(:host, :with_insights_hits) + @host1.name = 'host1' # overriding name since there is an issue with Factorybot and setting the name correctly, same for 2nd host @host1.insights.uuid = 'TEST_UUID1' @host1.insights.save! - @host2 = FactoryBot.create(:host, :with_insights_hits, name: 'host2') + @host2 = FactoryBot.create(:host, :with_insights_hits) + @host2.name = 'host2' @host2.insights.uuid = 'TEST_UUID2' @host2.insights.save!