def test_merge(self): sr1 = StepResult('step1', 'sub1', 'implementer1') sr1.add_artifact('artifact1', 'value1', 'description1') sr1.add_artifact('artifact2', 'value2', 'description2') sr1.add_evidence('evidence1', 'value1', 'description1') sr1.add_evidence('evidence2', 'value2', 'description2') sr2 = StepResult('step1', 'sub1', 'implementer1') sr2.add_artifact('artifact1', 'changed-value1', 'changed-description1') sr2.add_evidence('evidence1', 'changed-value1', 'changed-description1') sr1.merge(sr2) artifact1 = sr1.get_artifact('artifact1') artifact2 = sr1.get_artifact('artifact2') evidence1 = sr1.get_evidence('evidence1') evidence2 = sr1.get_evidence('evidence2') # changed by SR2 self.assertEqual(artifact1.value, 'changed-value1') self.assertEqual(artifact1.description, 'changed-description1') self.assertEqual(evidence1.value, 'changed-value1') self.assertEqual(evidence1.description, 'changed-description1') # unchanged by SR2 self.assertEqual(artifact2.value, 'value2') self.assertEqual(artifact2.description, 'description2') self.assertEqual(evidence2.value, 'value2') self.assertEqual(evidence2.description, 'description2')
def test_pass_alternate_java_truststore(self, mock_sonar, mock_is_release_branch): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write('sonar-project.properties', b'''testing''') properties_path = os.path.join(temp_dir.path, 'sonar-project.properties') temp_dir.write('alternate.jks', b'''testing''') java_truststore = os.path.join(temp_dir.path, 'alternate.jks') artifact_config = { 'version': {'description': '', 'value': '1.0-123abc'}, } workflow_result = self.setup_previous_result(parent_work_dir_path, artifact_config) step_config = { 'properties': properties_path, 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name', 'username': '******', 'password': '******', 'java-truststore': java_truststore } step_implementer = self.create_step_implementer( step_config=step_config, step_name='static-code-analysis', implementer='SonarQube', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='static-code-analysis', sub_step_name='SonarQube', sub_step_implementer_name='SonarQube' ) expected_step_result.add_artifact( name='sonarqube-result-set', value=f'{temp_dir.path}/working/static-code-analysis/report-task.txt' ) expected_step_result.add_evidence( name='sonarqube-quality-gate-pass', value=True ) mock_sonar.assert_called_once_with( '-Dproject.settings=' + properties_path, '-Dsonar.host.url=https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', '-Dsonar.projectVersion=1.0-123abc', '-Dsonar.projectKey=app-name:service-name', '-Dsonar.working.directory=' + step_implementer.work_dir_path, '-Dsonar.login=username', '-Dsonar.password=password', _env={'SONAR_SCANNER_OPTS': f'-Djavax.net.ssl.trustStore={java_truststore}'}, _out=sys.stdout, _err=sys.stderr ) self.assertEqual(result, expected_step_result)
def test_app_version_and_is_pre_release_with_given_pre_release_identifiers_and_build( self, mock_build, mock_pre_release): # setup step_config = {'app-version': '0.42.1', 'is-pre-release': True} step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='SemanticVersion') # setup mocks mock_build.return_value = 'mock3' mock_pre_release.return_value = 'feature-mock1' # run test actual_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='SemanticVersion', sub_step_implementer_name='SemanticVersion') expected_step_result.add_artifact( name='version', value='0.42.1-feature-mock1+mock3', description='Full constructured semantic version') expected_step_result.add_artifact( name='container-image-tag', value='0.42.1-feature-mock1_mock3', description='Constructed semantic version without build identifier' \ ' since not compatible with container image tags' ) expected_step_result.add_artifact( name='semantic-version-core', value='0.42.1', description='Semantic version version core portion') expected_step_result.add_artifact( name='semantic-version-pre-release', value='feature-mock1', description='Semantic version pre-release portion') expected_step_result.add_artifact( name='semantic-version-build', value='mock3', description='Semantic version build portion') expected_step_result.add_evidence( name='version', value='0.42.1-feature-mock1+mock3', description='Full constructured semantic version') expected_step_result.add_evidence( name='container-image-tag', value='0.42.1-feature-mock1_mock3', description='semantic version without build identifier' \ ' since not compatible with container image tags' ) self.assertEqual(actual_result, expected_step_result) mock_pre_release.assert_called_once() mock_build.assert_called_once()
def test_app_version_and_build_custom_container_image_tag_build_deliminator( self, mock_build, mock_pre_release): # setup step_config = { 'app-version': '0.42.1', 'container-image-tag-build-deliminator': '_-_' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='SemanticVersion') # setup mocks mock_build.return_value = 'mock1' mock_pre_release.return_value = None # run test actual_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='SemanticVersion', sub_step_implementer_name='SemanticVersion') expected_step_result.add_artifact( name='version', value='0.42.1+mock1', description='Full constructured semantic version') expected_step_result.add_artifact( name='container-image-tag', value='0.42.1_-_mock1', description='Constructed semantic version without build identifier' \ ' since not compatible with container image tags' ) expected_step_result.add_artifact( name='semantic-version-core', value='0.42.1', description='Semantic version version core portion') expected_step_result.add_artifact( name='semantic-version-build', value='mock1', description='Semantic version build portion') expected_step_result.add_evidence( name='version', value='0.42.1+mock1', description='Full constructured semantic version') expected_step_result.add_evidence( name='container-image-tag', value='0.42.1_-_mock1', description='semantic version without build identifier' \ ' since not compatible with container image tags' ) self.assertEqual(actual_result, expected_step_result) mock_pre_release.assert_not_called() mock_build.assert_called_once()
def __run__fail_sonar_scanner_error_test( self, sonar_scanner_error, expected_result_message_regex, mock_sonar ): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write('sonar-project.properties',b'''testing''') properties_path = os.path.join(temp_dir.path, 'sonar-project.properties') artifact_config = { 'version': {'description': '', 'value': '1.0-123abc'}, } workflow_result = self.setup_previous_result(parent_work_dir_path, artifact_config) step_config = { 'properties': properties_path, 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name', 'username': '******', 'password': '******' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='static-code-analysis', implementer='SonarQube', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path, ) mock_sonar.side_effect = sonar_scanner_error result = step_implementer._run_step() expected_step_result = StepResult( step_name='static-code-analysis', sub_step_name='SonarQube', sub_step_implementer_name='SonarQube' ) expected_step_result.success = False expected_step_result.add_artifact( name='sonarqube-result-set', value=f'{temp_dir.path}/working/static-code-analysis/report-task.txt' ) expected_step_result.add_evidence( name='sonarqube-quality-gate-pass', value=False ) self.assertEqual(result.success, expected_step_result.success) self.assertEqual(result.artifacts, expected_step_result.artifacts) self.assertRegex(result.message, expected_result_message_regex)
def test_add_step_result_duplicate_no_env(self): duplicate_step_result = StepResult('step1', 'sub1', 'implementer1') duplicate_step_result.add_artifact('newartifact', 'newvalue', 'newdescription') duplicate_step_result.add_evidence('newevidence', 'newvalue', 'newdescription') wfr = setup_test() with self.assertRaisesRegex( StepRunnerException, r"Can not add duplicate StepResult for step \(step1\)," r" sub step \(sub1\), and environment \(None\)."): wfr.add_step_result(duplicate_step_result)
def test_add_step_result_duplicate_with_env(self): duplicate_step_result = StepResult('deploy', 'deploy-sub', 'helm', 'dev') duplicate_step_result.add_artifact('newartifact', 'newvalue', 'newdescription') duplicate_step_result.add_evidence('newevidence', 'newvalue', 'newdescription') wfr = setup_test() with self.assertRaisesRegex( StepRunnerException, r"Can not add duplicate StepResult for step \(deploy\)," r" sub step \(deploy-sub\), and environment \(dev\)."): wfr.add_step_result(duplicate_step_result)
def test_found_dir_found_some_attributes(self, mock_collect_report_results): with TempDirectory() as test_dir: # setup test actual_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') test_report_dir = os.path.join(test_dir.path, 'mock-test-results') # setup mocks mock_collect_report_results.return_value = [{ "time": 1.42, "tests": 42, }, []] # run test MavenTestReportingMixin._gather_evidence_from_test_report_directory_testsuite_elements( step_result=actual_step_result, test_report_dirs=test_report_dir) # verify results expected_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') expected_step_result.add_evidence(name='time', value=1.42) expected_step_result.add_evidence(name='tests', value=42) not_found_attribs = ["failures"] expected_step_result.message += "\nWARNING: could not find expected evidence" \ f" attributes ({not_found_attribs}) on a recognized xml root element" \ f" (['testsuites', 'testsuite']) in test report" \ f" directory (['{test_report_dir}'])." self.assertEqual(actual_step_result, expected_step_result) mock_collect_report_results.assert_called_once_with( test_report_dirs=[test_report_dir])
def test_found_dir_found_no_attributes( self, aggregate_xml_element_attribute_values_mock): with TempDirectory() as test_dir: # setup test actual_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') test_report_dir = os.path.join(test_dir.path, 'mock-test-results') # setup mocks os.mkdir(test_report_dir) aggregate_xml_element_attribute_values_mock.return_value = { "time": 1.42, "tests": 42 } # run test MavenTestReportingMixin._gather_evidence_from_test_report_directory_testsuite_elements( step_result=actual_step_result, test_report_dir=test_report_dir) # verify results expected_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') expected_step_result.add_evidence(name='time', value=1.42) expected_step_result.add_evidence(name='tests', value=42) test_report_evidence_element = 'testsuite' not_found_attribs = ["errors", "skipped", "failures"] expected_step_result.message += "\nWARNING: could not find expected evidence" \ f" attributes ({not_found_attribs}) on xml element" \ f" ({test_report_evidence_element}) in test report" \ f" directory ({test_report_dir})." self.assertEqual(actual_step_result, expected_step_result)
def __setup_evidence(self, parent_work_dir_path, evidence=True, environment=None): step_config = { 'organization': 'test-ORG', 'application-name': 'test-APP', 'service-name': 'test-SERVICE', 'version': '42.0-test', } step_result = StepResult( step_name='test-step', sub_step_name='test-sub-step', sub_step_implementer_name='test-sub-step-implementer', environment=environment ) step_result.add_evidence( name='test-evidence', value="test-value", description="test-description" ) step_result.add_evidence( name='test-evidence2', value="test-value2", description="test-description2" ) workflow_result = WorkflowResult() workflow_result.add_step_result(step_result) step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, workflow_result=workflow_result, ) return step_implementer
def test_found_all_attributes_multiple_test_dirs( self, mock_collect_report_results): with TempDirectory() as test_dir: # setup test actual_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') test_report_dir1 = os.path.join(test_dir.path, 'mock-test-results1') os.mkdir(test_report_dir1) test_report_dir2 = os.path.join(test_dir.path, 'mock-test-results2') os.mkdir(test_report_dir2) test_report_dirs = [test_report_dir1, test_report_dir2] # setup mocks mock_collect_report_results.return_value = [{ "time": 1.42, "tests": 42, "errors": 3, "skipped": 2, "failures": 1 }, []] # run test MavenTestReportingMixin._gather_evidence_from_test_report_directory_testsuite_elements( step_result=actual_step_result, test_report_dirs=test_report_dirs) # verify results expected_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') expected_step_result.add_evidence(name='time', value=1.42) expected_step_result.add_evidence(name='tests', value=42) expected_step_result.add_evidence(name='errors', value=3) expected_step_result.add_evidence(name='skipped', value=2) expected_step_result.add_evidence(name='failures', value=1) self.assertEqual(actual_step_result, expected_step_result) mock_collect_report_results.assert_called_once_with( test_report_dirs=test_report_dirs)
def test_found_warning(self, mock_collect_report_results): with TempDirectory() as test_dir: # setup test actual_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') test_report_dir = os.path.join(test_dir.path, 'mock-test-results') # setup mocks mock_collect_report_results.return_value = [{ "time": 1.42, "tests": 42, "errors": 3, "skipped": 2, "failures": 1 }, ['WARNING: mock warning about nothing']] # run test MavenTestReportingMixin._gather_evidence_from_test_report_directory_testsuite_elements( step_result=actual_step_result, test_report_dirs=test_report_dir) # verify results expected_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') expected_step_result.message = '\nWARNING: mock warning about nothing' expected_step_result.add_evidence(name='time', value=1.42) expected_step_result.add_evidence(name='tests', value=42) expected_step_result.add_evidence(name='errors', value=3) expected_step_result.add_evidence(name='skipped', value=2) expected_step_result.add_evidence(name='failures', value=1) self.assertEqual(actual_step_result, expected_step_result) mock_collect_report_results.assert_called_once_with( test_report_dirs=[test_report_dir])
def test_found_all_attributes(self, aggregate_xml_element_attribute_values_mock): with TempDirectory() as test_dir: # setup test actual_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') test_report_dir = os.path.join(test_dir.path, 'mock-test-results') # setup mocks os.mkdir(test_report_dir) aggregate_xml_element_attribute_values_mock.return_value = { "time": 1.42, "tests": 42, "errors": 3, "skipped": 2, "failures": 1 } # run test MavenTestReportingMixin._gather_evidence_from_test_report_directory_testsuite_elements( step_result=actual_step_result, test_report_dir=test_report_dir) # verify results expected_step_result = StepResult( step_name='mock-maven-test-step', sub_step_name='mock-maven-test-sub-step', sub_step_implementer_name= 'MockMavenTestReportingMixinStepImplementer') expected_step_result.add_evidence(name='time', value=1.42) expected_step_result.add_evidence(name='tests', value=42) expected_step_result.add_evidence(name='errors', value=3) expected_step_result.add_evidence(name='skipped', value=2) expected_step_result.add_evidence(name='failures', value=1) self.assertEqual(actual_step_result, expected_step_result)
def setup_test_sub_steps(): step_result1 = StepResult('step1', 'sub1', 'implementer1') step_result1.add_artifact('artifact1', 'value1', 'description1') step_result1.add_artifact('artifact2', 'value2', 'description2') step_result1.add_artifact('artifact3', 'value3') step_result1.add_artifact('artifact4', False) step_result1.add_evidence('evidence1', 'value1', 'description1') step_result1.add_evidence('evidence2', 'value2', 'description2') step_result1.add_evidence('evidence3', 'value3') step_result1.add_evidence('evidence4', False) step_result2 = StepResult('step1', 'sub2', 'implementer2') step_result2.add_artifact('artifact1', True) step_result2.add_artifact('artifact2', False) step_result2.add_artifact('artifact5', 'value5') step_result2.add_evidence('evidence1', True) step_result2.add_evidence('evidence2', False) step_result2.add_evidence('evidence5', 'value5') wfr = WorkflowResult() wfr.add_step_result(step_result1) wfr.add_step_result(step_result2) return wfr
def setup_test(): step_result1 = StepResult('step1', 'sub1', 'implementer1') step_result1.add_artifact('artifact1', 'value1', 'description1') step_result1.add_artifact('artifact2', 'value2', 'description2') step_result1.add_artifact('artifact3', 'value3') step_result1.add_artifact('artifact4', False) step_result1.add_artifact('same-artifact-all-env-and-no-env', 'result1') step_result1.add_evidence('evidence1', 'value1', 'description1') step_result1.add_evidence('evidence2', 'value2', 'description2') 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) step_result2.add_artifact('artifact2', False) step_result2.add_artifact('artifact5', 'value5') step_result2.add_evidence('evidence1', True) step_result2.add_evidence('evidence2', False) step_result2.add_evidence('evidence5', 'value5') step_result3 = StepResult('deploy', 'deploy-sub', 'helm', 'dev') step_result3.add_artifact('same-artifact-diff-env', 'value-dev-env') step_result3.add_artifact('unique-artifact-to-step-and-environment-1', 'value1-dev-env') step_result3.add_artifact('same-artifact-all-env-and-no-env', 'result3-dev-env') step_result3.add_evidence('same-evidence-diff-env', 'value-dev-env') step_result3.add_evidence('unique-evidence-to-step-and-environment-1', 'value1-dev-env') step_result3.add_evidence('same-evidence-all-env-and-no-env', 'result3-dev-env') step_result4 = StepResult('deploy', 'deploy-sub', 'helm', 'test') step_result4.add_artifact('artifact1', True) step_result4.add_artifact('artifact2', False) step_result4.add_artifact('artifact5', 'value5') step_result4.add_artifact('same-artifact-diff-env', 'value-test-env') step_result4.add_artifact('unique-artifact-to-step-and-environment-2', 'value2-test-env') step_result4.add_artifact('same-artifact-all-env-and-no-env', 'result4-test-env') step_result4.add_evidence('evidence1', True) step_result4.add_evidence('evidence2', False) step_result4.add_evidence('evidence5', 'value5') step_result4.add_evidence('same-evidence-diff-env', 'value-test-env') step_result4.add_evidence('unique-evidence-to-step-and-environment-2', 'value2-test-env') step_result4.add_evidence('same-evidence-all-env-and-no-env', 'result4-test-env') wfr = WorkflowResult() wfr.add_step_result(step_result1) wfr.add_step_result(step_result2) wfr.add_step_result(step_result3) wfr.add_step_result(step_result4) return wfr
def test___eq__(self): step_result1 = StepResult( step_name='foo', sub_step_name= 'tests.helpers.sample_step_implementers.FooStepImplementer', sub_step_implementer_name= 'tests.helpers.sample_step_implementers.FooStepImplementer', environment='blarg') step_result1.add_artifact(name='art-str', value='hello') step_result1.add_artifact(name='art-bool-t', value=True) step_result1.add_artifact(name='art-bool-f', value=False) step_result1.add_artifact(name='art-desc', value='world', description='test artifact') step_result1.add_evidence(name='evi-str', value='hello') step_result1.add_evidence(name='evi-bool-t', value=True) step_result1.add_evidence(name='evi-bool-f', value=False) step_result1.add_evidence(name='evi-desc', value='world', description='test evidence') step_result2 = StepResult( step_name='foo', sub_step_name= 'tests.helpers.sample_step_implementers.FooStepImplementer', sub_step_implementer_name= 'tests.helpers.sample_step_implementers.FooStepImplementer', environment='blarg') step_result2.add_artifact(name='art-str', value='hello') step_result2.add_artifact(name='art-bool-t', value=True) step_result2.add_artifact(name='art-bool-f', value=False) step_result2.add_artifact(name='art-desc', value='world', description='test artifact') step_result2.add_evidence(name='evi-str', value='hello') step_result2.add_evidence(name='evi-bool-t', value=True) step_result2.add_evidence(name='evi-bool-f', value=False) step_result2.add_evidence(name='evi-desc', value='world', description='test evidence') self.assertEqual(step_result1, step_result2)
def test_merge_matching_stepresults_on_pickle(self): """When we pickle to disk, make sure we merge the artifacts and evidence from in-memory StepResult instances with those on disk. Whatever is in memory wins. """ with TempDirectory() as temp_dir: pickle_file = temp_dir.path + '/test.pkl' expected_wfr = setup_test() expected_wfr.write_to_pickle_file(pickle_file) # now adjust our expected WorkflowResult to include the # amended evidence/artifacts we expect to see. All other # StepResults and their artifacts/evidence are left the same. expected_sr_no_env = expected_wfr.get_step_result('step1', 'sub1') expected_sr_no_env.add_artifact('artifact1', 'changed-value1', 'description1') expected_sr_no_env.add_evidence('evidence1', 'changed-value1', 'description1') expected_sr_env = expected_wfr.get_step_result( 'deploy', 'deploy-sub', 'dev') expected_sr_env.add_artifact('artifact1', 'changed-value1') expected_sr_env.add_evidence('evidence1', 'changed-value1', 'description1') # now build our 'in memory' WFR that we're going to write # to disk. this has the amended evidence/artifacts. # one StepResult with environment, one without. in_mem_wfr = WorkflowResult() sr_no_env = StepResult('step1', 'sub1', 'implementer1') sr_no_env.add_artifact('artifact1', 'changed-value1', 'description1') sr_no_env.add_evidence('evidence1', 'changed-value1', 'description1') sr_env = StepResult('deploy', 'deploy-sub', 'helm', 'dev') sr_env.add_artifact('artifact1', 'changed-value1') sr_env.add_evidence('evidence1', 'changed-value1', 'description1') in_mem_wfr.add_step_result(sr_no_env) in_mem_wfr.add_step_result(sr_env) # write in-memory to disk post-merging in_mem_wfr.merge_with_pickle_file(pickle_file) in_mem_wfr.write_to_pickle_file(pickle_file) resulting_wfr = WorkflowResult.load_from_pickle_file(pickle_file) resulting_workflow_list = resulting_wfr.workflow_list for expected_result in expected_wfr.workflow_list: merged_result = resulting_wfr.get_step_result( step_name=expected_result.step_name, sub_step_name=expected_result.sub_step_name, environment=expected_result.environment) self.assertEqual(expected_result, merged_result) resulting_workflow_list.remove(expected_result) # nothing duplicate or extra should be in the result self.assertEqual(len(resulting_workflow_list), 0)