def test_run_step_pass_alternate_java_truststore(self, sonar_mock): 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 ) sonar_mock.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.login=username', '-Dsonar.password=password', '-Dsonar.working.directory=' + step_implementer.work_dir_path, _env={'SONAR_SCANNER_OPTS': f'-Djavax.net.ssl.trustStore={java_truststore}'}, _out=sys.stdout, _err=sys.stderr ) self.assertEqual(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_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 __run__run_step_fail_sonar_scanner_error_test( self, sonar_scanner_error, expected_result_message_regex, sonar_mock ): 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, ) sonar_mock.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_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_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_run_step_pass_different_pre_release(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = {} artifact_config = { 'app-version': { 'description': '', 'value': '42.1.0' }, 'pre-release': { 'description': '', 'value': 'feature123' }, 'build': { 'description': '', 'value': 'abc123' } } workflow_result = self.setup_previous_result( parent_work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='SemanticVersion', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() 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='42.1.0-feature123+abc123') expected_step_result.add_artifact(name='container-image-version', value='42.1.0-feature123') expected_step_result.add_evidence(name='version', value='42.1.0-feature123+abc123') expected_step_result.add_evidence(name='container-image-version', value='42.1.0-feature123') self.assertEqual(result, expected_step_result)
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)
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 __run__run_step_test( self, test_dir, mvn_mock, write_effective_pom_mock, generate_maven_settings_mock, pom_content, group_id, artifact_id, surefire_reports_dir, selenium_hub_url, target_host_url=None, deployed_host_urls=None, write_mock_test_results=True, assert_mvn_called=True, assert_report_artifact=True, assert_evidence=True, expected_result_success=True, expected_result_message='', fail_on_no_tests=None, uat_maven_profile=None, pom_file_name='pom.xml', raise_error_on_tests=False, set_tls_verify_false=False, aggregate_xml_element_attribute_values_mock=False, aggregate_xml_element_attribute_values_mock_fail=False ): parent_work_dir_path = os.path.join(test_dir.path, 'working') cucumber_html_report_path = os.path.join(parent_work_dir_path, 'uat', 'cucumber.html') cucumber_json_report_path = os.path.join(parent_work_dir_path, 'uat', 'cucumber.json') test_dir.write(pom_file_name, pom_content) pom_file_path = os.path.join(test_dir.path, pom_file_name) step_config = { 'pom-file': pom_file_path, 'selenium-hub-url': selenium_hub_url, 'tls-verify': True } if set_tls_verify_false: step_config['tls-verify'] = False target_base_url = None if deployed_host_urls: step_config['deployed-host-urls'] = deployed_host_urls if isinstance(deployed_host_urls, list): target_base_url = deployed_host_urls[0] else: target_base_url = deployed_host_urls if target_host_url: step_config['target-host-url'] = target_host_url target_base_url = target_host_url if fail_on_no_tests is not None: step_config['fail-on-no-tests'] = fail_on_no_tests if uat_maven_profile is not None: step_config['uat-maven-profile'] = uat_maven_profile else: uat_maven_profile = 'integration-test' step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # mock generating settings settings_file_path = "/does/not/matter/settings.xml" def generate_maven_settings_side_effect(): return settings_file_path generate_maven_settings_mock.side_effect = generate_maven_settings_side_effect # mock effective pom def write_effective_pom_mock_side_effect(pom_file_path, output_path): create_parent_dir(pom_file_path) copyfile(pom_file_path, output_path) write_effective_pom_mock.side_effect = write_effective_pom_mock_side_effect # mock test results if write_mock_test_results: mvn_mock.side_effect = MaveStepImplementerTestCase.create_mvn_side_effect( pom_file=pom_file_path, artifact_parent_dir=surefire_reports_dir, artifact_names=[ f'{group_id}.{artifact_id}.CucumberTest.txt', f'TEST-{group_id}.{artifact_id}.CucumberTest.xml' ], raise_error_on_tests=raise_error_on_tests ) # mock evidence if aggregate_xml_element_attribute_values_mock and not aggregate_xml_element_attribute_values_mock_fail: aggregate_xml_element_attribute_values_mock.return_value = { 'time': '42', 'tests': '42', 'errors': '0', 'skipped': '0', 'failures': '0' } elif aggregate_xml_element_attribute_values_mock_fail: aggregate_xml_element_attribute_values_mock.return_value = { 'time': '42' } result = step_implementer._run_step() if assert_mvn_called: if not set_tls_verify_false: mvn_mock.assert_called_once_with( 'clean', 'test', f'-P{uat_maven_profile}', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=' \ f'html:{cucumber_html_report_path},' \ f'json:{cucumber_json_report_path}', '-f', pom_file_path, '-s', settings_file_path, _out=Any(IOBase), _err=Any(IOBase) ) else: mvn_mock.assert_called_once_with( 'clean', 'test', f'-P{uat_maven_profile}', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=' \ f'html:{cucumber_html_report_path},' \ f'json:{cucumber_json_report_path}', '-f', pom_file_path, '-s', settings_file_path, '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', _out=Any(IOBase), _err=Any(IOBase) ) expected_step_result = StepResult( step_name='uat', sub_step_name='MavenSeleniumCucumber', sub_step_implementer_name='MavenSeleniumCucumber' ) expected_step_result.success = expected_result_success expected_step_result.message = expected_result_message if assert_report_artifact: mvn_test_output_file_path = os.path.join( step_implementer.work_dir_path, 'mvn_test_output.txt' ) expected_step_result.add_artifact( description=f"Standard out and standard error by 'mvn -P{uat_maven_profile} test'.", name='maven-output', value=mvn_test_output_file_path ) expected_step_result.add_artifact( description=f"Surefire reports generated by 'mvn -P{uat_maven_profile} test'.", name='surefire-reports', value=surefire_reports_dir ) expected_step_result.add_artifact( description=f"Cucumber (HTML) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-html', value=cucumber_html_report_path ) expected_step_result.add_artifact( description=f"Cucumber (JSON) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-json', value=cucumber_json_report_path ) if assert_evidence and not aggregate_xml_element_attribute_values_mock_fail: expected_step_result.add_evidence( name='uat-evidence-time', description='Surefire report value for time', value='42' ) expected_step_result.add_evidence( name='uat-evidence-tests', description='Surefire report value for tests', value='42' ) expected_step_result.add_evidence( name='uat-evidence-errors', description='Surefire report value for errors', value='0' ) expected_step_result.add_evidence( name='uat-evidence-skipped', description='Surefire report value for skipped', value='0' ) expected_step_result.add_evidence( name='uat-evidence-failures', description='Surefire report value for failures', value='0' ) elif assert_evidence and aggregate_xml_element_attribute_values_mock_fail: expected_step_result.add_evidence( name='uat-evidence-time', description='Surefire report value for time', value='42' ) print(result) self.assertEqual(expected_step_result, result)