From 9143e21ef371ece2c203ff4f34361c7ee023f219 Mon Sep 17 00:00:00 2001 From: Ian Tewksbury Date: Mon, 2 Aug 2021 10:47:32 -0400 Subject: [PATCH] get latest result artifact value not first --- .../results/workflow_result.py | 2 +- tests/results/test_workflow_result.py | 8 ++--- tests/test_step_implementer.py | 34 +++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/ploigos_step_runner/results/workflow_result.py b/src/ploigos_step_runner/results/workflow_result.py index 4cd17da8..4109ead9 100644 --- a/src/ploigos_step_runner/results/workflow_result.py +++ b/src/ploigos_step_runner/results/workflow_result.py @@ -60,7 +60,7 @@ def get_artifact_value( """ value = None - for step_result in self.workflow_list: + for step_result in reversed(self.workflow_list): if ( \ (not step_name or step_result.step_name == step_name) and \ (not sub_step_name or step_result.sub_step_name == sub_step_name) and \ diff --git a/tests/results/test_workflow_result.py b/tests/results/test_workflow_result.py index f44ceb2f..8fa240bb 100644 --- a/tests/results/test_workflow_result.py +++ b/tests/results/test_workflow_result.py @@ -23,7 +23,7 @@ def setup_test(): step_result1.add_evidence('evidence3', 'value3') step_result1.add_evidence('evidence4', False) step_result1.add_evidence('same-evidence-all-env-and-no-env', 'result1') - + step_result2 = StepResult('step2', 'sub2', 'implementer2') step_result2.add_artifact('artifact1', True) @@ -103,7 +103,7 @@ class TestStepWorkflowResultTest(BaseTestCase): def test_get_artifact_value_without_step(self): wfr = setup_test() - expected_artifact = 'value1' + expected_artifact = True self.assertEqual( expected_artifact, wfr.get_artifact_value(artifact='artifact1') @@ -237,7 +237,7 @@ def test_get_artifact_value_in_step_executed_in_different_environments_and_no_en wfr.get_artifact_value( artifact='same-artifact-all-env-and-no-env' ), - 'result1' + 'result4-test-env' ) self.assertEqual( @@ -289,7 +289,7 @@ def test_get_artifact_value_in_step_executed_in_different_environments(self): wfr.get_artifact_value( artifact='same-artifact-diff-env' ), - 'value-dev-env' + 'value-test-env' ) self.assertEqual( diff --git a/tests/test_step_implementer.py b/tests/test_step_implementer.py index b2e61810..c11a1068 100644 --- a/tests/test_step_implementer.py +++ b/tests/test_step_implementer.py @@ -57,6 +57,17 @@ def _setup_get_value_with_env_test(self, test_dir, environment): workflow_result = WorkflowResult() + step_result_no_evn = StepResult( + step_name='foo', + sub_step_name='Mock', + sub_step_implementer_name='Mock' + ) + step_result_no_evn.add_artifact( + name='container-image-tag', + value='localhost/mock:0.42.0-weird' + ) + workflow_result.add_step_result(step_result=step_result_no_evn) + step_result_deploy_test = StepResult( step_name='deploy', sub_step_name='ArgoCD', @@ -67,6 +78,10 @@ def _setup_get_value_with_env_test(self, test_dir, environment): name='deployed-host-urls', value='https://awesome-app.test.ploigos.xyz' ) + step_result_deploy_test.add_artifact( + name='container-image-tag', + value='localhost/test/mock:0.42.0-weird' + ) workflow_result.add_step_result(step_result=step_result_deploy_test) step_result_deploy_prod = StepResult( @@ -79,6 +94,10 @@ def _setup_get_value_with_env_test(self, test_dir, environment): name='deployed-host-urls', value='https://awesome-app.prod.ploigos.xyz' ) + step_result_deploy_test.add_artifact( + name='container-image-tag', + value='localhost/prod/mock:0.42.0-weird' + ) workflow_result.add_step_result(step_result=step_result_deploy_prod) pickle_filename = os.path.join(parent_work_dir_path, 'step-runner-results.pkl') workflow_result.write_to_pickle_file(pickle_filename=pickle_filename) @@ -910,12 +929,21 @@ def test_get_value_no_env(self): self.assertEqual(step.get_value('fake-previous-step-artifact'), 'world hello') self.assertIsNone(step.get_value('does-not-exist')) - def test_get_value_from_step_not_run_against_specific_environment(self): + def test_get_value_from_artifact_without_specifing_environment_where_last_artifact_was_from_environment_and_artifact_never_set_without_environment(self): with TempDirectory() as test_dir: step = self._setup_get_value_with_env_test(test_dir=test_dir, environment=None) self.assertEqual( step.get_value('deployed-host-urls'), - 'https://awesome-app.test.ploigos.xyz' + 'https://awesome-app.prod.ploigos.xyz' + ) + + # NOTE: maybe this should actually return the first instance where the environment was not set? + def test_get_value_from_artifact_without_specifing_environment_where_last_artifact_was_from_environment_and_artifact_was_set_without_environment(self): + with TempDirectory() as test_dir: + step = self._setup_get_value_with_env_test(test_dir=test_dir, environment=None) + self.assertEqual( + step.get_value('container-image-tag'), + 'localhost/prod/mock:0.42.0-weird' ) def test_get_value_from_step_run_in_environment_that_has_result_value_for_that_environment_1(self): @@ -939,7 +967,7 @@ def test_get_value_from_step_run_in_environment_that_nodes_not_have_result_value step = self._setup_get_value_with_env_test(test_dir=test_dir, environment='RANDOM') self.assertEqual( step.get_value('deployed-host-urls'), - 'https://awesome-app.test.ploigos.xyz' + 'https://awesome-app.prod.ploigos.xyz' ) def test_get_value_multiple_keys_1(self):