def test_merge_raises_exception_on_non_stepresult_other(self): sr1 = StepResult('step1', 'sub1', 'implementer1') other = "string" with self.assertRaisesRegex(StepRunnerException, f'expect StepResult instance type'): sr1.merge(other)
def test_test_report_dir_does_not_exist( 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') # 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.message += f"\nWARNING: test report directory ({test_report_dir})" \ " does not exist to gather evidence from" self.assertEqual(actual_step_result, expected_step_result)
def test_run_step_pass(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') test_file_name = 'yamlnotused' test_file_path = os.path.join(temp_dir.path, test_file_name) temp_dir.write(test_file_path, b'ignored') step_config = {} artifact_config = { 'argocd-deployed-manifest': { 'value': test_file_path } } workflow_result = self.setup_previous_result( parent_work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='ConfiglintArgocd', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() expected_step_result = StepResult( step_name='validate-environment-configuration', sub_step_name='ConfiglintArgocd', sub_step_implementer_name='ConfiglintArgocd') expected_step_result.add_artifact(name='configlint-yml-path', value=test_file_path) self.assertEqual(expected_step_result, result)
def test_test_report_dir_does_not_exist(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 = [{}, []] # 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: could not find expected evidence" \ " attributes (['time', 'tests', 'failures'])" \ f" on a recognized xml root element (['testsuites', 'testsuite']) in test report 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_run_step_pass(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write( 'package.json', b'''{ "name": "my-awesome-package", "version": "42.1" }''') package_file_path = os.path.join(temp_dir.path, 'package.json') step_config = {'package-file': package_file_path} step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Npm', parent_work_dir_path=parent_work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult(step_name='generate-metadata', sub_step_name='Npm', sub_step_implementer_name='Npm') expected_step_result.add_artifact(name='app-version', value='42.1') self.assertEqual(result, expected_step_result)
def test_run_step_fail_missing_version_in_package_file(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write( 'package.json', b'''{ "name": "my-awesome-package" }''') package_file_path = os.path.join(temp_dir.path, 'package.json') step_config = {'package-file': package_file_path} step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Npm', parent_work_dir_path=parent_work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Npm', sub_step_implementer_name='Npm', ) expected_step_result.success = False expected_step_result.message = f"Given npm package file ({package_file_path})" + \ ' does not contain a \"version\" key.' self.assertEqual(result, expected_step_result)
def test_run_step_fail_missing_path_file_from_deploy(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') test_file_name = 'yamlnotused' test_file_path = os.path.join(temp_dir.path, test_file_name) temp_dir.write(test_file_path, b'ignored') step_config = {} artifact_config = { 'argocd-deployed-manifest': { 'value': f'{test_file_path}.bad' } } workflow_result = self.setup_previous_result( parent_work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='ConfiglintArgocd', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() expected_step_result = StepResult( step_name='validate-environment-configuration', sub_step_name='ConfiglintArgocd', sub_step_implementer_name='ConfiglintArgocd') expected_step_result.success = False expected_step_result.message = f'File specified in argocd-deployed-manifest {test_file_path}.bad not found' self.assertEqual(expected_step_result, result)
def test_fail_no_properties_and_project_key(self, mock_sonar, mock_is_release_branch): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') artifact_config = { 'version': {'description': '', 'value': '1.0-123abc'}, } workflow_result = self.setup_previous_result(parent_work_dir_path, artifact_config) step_config = { 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name', 'project-key': 'project-key' } 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.success = False expected_step_result.message = 'Properties file not found: ./sonar-project.properties' self.assertEqual(result, expected_step_result)
def test_success_no_report_dir(self, mock_gather_evidence, mock_get_test_report_dir, mock_write_working_file, mock_run_maven_step): with TempDirectory() as test_dir: # setup test parent_work_dir_path = os.path.join(test_dir.path, 'working') pom_file = os.path.join(test_dir.path, 'mock-pom.xml') step_config = {'pom-file': pom_file} step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # setup mocks mock_get_test_report_dir.return_value = None # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='unit-test', sub_step_name='MavenTest', sub_step_implementer_name='MavenTest') expected_step_result.add_artifact( description="Standard out and standard error from maven.", name='maven-output', value='/mock/mvn_output.txt') self.assertEqual(actual_step_result, expected_step_result) mock_run_maven_step.assert_called_once_with( mvn_output_file_path='/mock/mvn_output.txt') mock_gather_evidence.assert_not_called()
def test_success(self, mock_write_working_file, mock_run_tox_step): with TempDirectory() as test_dir: parent_work_dir_path = os.path.join(test_dir.path, 'working') step_config = {} step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # run step actual_step_result = step_implementer._run_step() # create expected step result expected_step_result = StepResult( step_name='foo', sub_step_name='ToxGeneric', sub_step_implementer_name='ToxGeneric' ) expected_step_result.add_artifact( description="Standard out and standard error from tox.", name='tox-output', value='/mock/tox_output.txt' ) # verify step result self.assertEqual( actual_step_result, expected_step_result ) mock_run_tox_step.assert_called_with( tox_output_file_path='/mock/tox_output.txt' )
def test_fail_git_repo_bare( self, mock_repo, mock_git_url, mock_git_commit_utc_timestamp ): with TempDirectory() as temp_dir: # setup step_config = { 'repo-root': temp_dir.path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git' ) # setup mocks mock_repo().bare = True # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.success = False expected_step_result.message = f'Given git-repo-root is not a Git repository' self.assertEqual(actual_step_result, expected_step_result)
def test_fail_is_detached( self, mock_repo, mock_git_url, mock_git_commit_utc_timestamp ): with TempDirectory() as temp_dir: # setup step_config = { 'git-repo-root': temp_dir.path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git' ) # setup mocks mock_repo().bare = False mock_repo().head.is_detached = True # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.success = False expected_step_result.message = f'Expected a Git branch in given git repo root' \ f' but has a detached head' self.assertEqual(actual_step_result, expected_step_result)
def test__run_step_pass_no_evidence(self, generate_evidence_mock): step_config = { 'organization': 'test-ORG', 'application-name': 'test-APP', 'service-name': 'test-SERVICE', 'version': '42.0-test' } step_implementer = self.create_step_implementer( step_config=step_config, ) #Set mock method to return None generate_evidence_mock.return_value = None step_result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate_evidence', sub_step_name='GenerateEvidence', sub_step_implementer_name='GenerateEvidence' ) expected_step_result.add_artifact( name='result-generate-evidence', value='No evidence to generate.', description='Evidence from all previously run steps.' ) expected_step_result.message = "No evidence generated from previously run steps" self.assertEqual(step_result, expected_step_result) generate_evidence_mock.assert_called_once()
def test_string_result_with_env(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') 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='test-env1') step_result.add_artifact(name='test-step-result-str', value='hello world') 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) archive_path = step_implementer._ResultArtifactsArchive__create_archive( ) archive_zip = zipfile.ZipFile(archive_path) artifact_file_path = f"{step_config['organization']}-" \ f"{step_config['application-name']}-{step_config['service-name']}-" \ f"{step_config['version']}/test-step/test-sub-step/test-env1/test-step-result-str" with archive_zip.open(artifact_file_path, 'r') as artifact_file: artifact_file_contents = artifact_file.read().decode('UTF-8') self.assertEqual(artifact_file_contents, 'hello world')
def test_run_step_pass(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write('pom.xml', b'''<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>42.1</version> </project>''') pom_file_path = os.path.join(temp_dir.path, 'pom.xml') step_config = { 'pom-file': pom_file_path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Maven', parent_work_dir_path=parent_work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Maven', sub_step_implementer_name='Maven' ) expected_step_result.add_artifact(name='app-version', value='42.1') self.assertEqual(result, expected_step_result)
def test_run_step_fail_bad_rule_path(self, config_lint_mock): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') test_file_name = 'rules' test_file_path = os.path.join(temp_dir.path, test_file_name) temp_dir.write(test_file_path, b'ignored') step_config = { 'configlint-yml-path': test_file_path, 'rules': 'invalid_file' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='Configlint', parent_work_dir_path=parent_work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='validate-environment-configuration', sub_step_name='Configlint', sub_step_implementer_name='Configlint' ) expected_step_result.success = False expected_step_result.message = 'File specified in rules not found: invalid_file' self.assertEqual(expected_step_result, result)
def test_found_dir_found_some_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 = {} # 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') test_report_evidence_element = 'testsuite' not_found_attribs = [ "time", "tests", "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 test__get_tag_no_version(self, git_repo_mock, git_tag_mock, git_url_mock): with TempDirectory() as temp_dir: tag = 'latest' url = '[email protected]:ploigos/ploigos-step-runner.git' parent_work_dir_path = os.path.join(temp_dir.path, 'working') artifact_config = { 'container-image-version': {'description': '', 'value': tag} } workflow_result = self.setup_previous_result(parent_work_dir_path, artifact_config) step_config = { 'url': url } step_implementer = self.create_step_implementer( step_config=step_config, workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='tag-source', sub_step_name='Git', sub_step_implementer_name='Git') expected_step_result.add_artifact(name='tag', value=tag) # verifying all mocks were called git_tag_mock.assert_called_once_with(tag) git_repo_mock.git.push.assert_called_once_with(git_url_mock, '--tag') self.assertEqual(result, expected_step_result)
def test_from_step_implementer_with_env(self): config = Config({ 'step-runner-config': { 'foo': { 'implementer': 'tests.helpers.sample_step_implementers.FooStepImplementer', 'config': {} } } }) step_config = config.get_step_config('foo') sub_step = step_config.get_sub_step( 'tests.helpers.sample_step_implementers.FooStepImplementer') step = FooStepImplementer(workflow_result=WorkflowResult(), parent_work_dir_path=None, config=sub_step, environment='blarg') step_result = StepResult.from_step_implementer(step) expected_step_result = 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') self.assertEqual(step_result, expected_step_result)
def test_success_release_branch_default_release_branch_regexes( self, mock_os_env_get): with TempDirectory() as temp_dir: # setup step_config = {'repo-root': temp_dir.path} step_implementer = self.create_step_implementer( step_config=step_config) # setup mocks mock_os_env_get.return_value = '42' # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Jenkins', sub_step_implementer_name='Jenkins') expected_step_result.add_artifact( name='workflow-run-num', value='42', description='Incremental workflow run number') self.assertEqual(actual_step_result, expected_step_result)
def test__run_step_pass(self, create_archive_mock): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'organization': 'test-ORG', 'application-name': 'test-APP', 'service-name': 'test-SERVICE', 'version': '42.0-test' } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) step_result = step_implementer._run_step() expected_step_result = StepResult( step_name='report', sub_step_name='ResultArtifactsArchive', sub_step_implementer_name='ResultArtifactsArchive') expected_step_result.add_artifact( name='result-artifacts-archive', value='/fake/archive/path/foo.zip', description= 'Archive of all of the step result artifacts marked for archiving.' ) self.assertEqual(step_result, expected_step_result) create_archive_mock.assert_called_once()
def setup_previous_result(self, work_dir_path, artifact_config={}): step_result = StepResult( step_name='test-step', sub_step_name='test-sub-step-name', sub_step_implementer_name='test-step-implementer-name') for key1, val1 in artifact_config.items(): description = '' value = '' for key2, val2 in val1.items(): if key2 == 'description': description = val2 elif key2 == 'value': value = val2 else: raise StepRunnerException( f'Given field is not apart of an artifact: {key2}') step_result.add_artifact( name=key1, value=value, description=description, ) workflow_result = WorkflowResult() workflow_result.add_step_result(step_result=step_result) pickle_filename = os.path.join(work_dir_path, 'step-runner-results.pkl') workflow_result.write_to_pickle_file(pickle_filename=pickle_filename) return workflow_result
def test_fail_getting_git_repo( self, mock_repo, mock_git_url, mock_git_commit_utc_timestamp ): with TempDirectory() as temp_dir: # setup step_config = { 'repo-root': temp_dir.path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git' ) # setup mocks mock_repo.side_effect = StepRunnerException('mock error') # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.success = False expected_step_result.message = f'mock error' self.assertEqual(actual_step_result, expected_step_result)
def test_dir_result(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'organization': 'test-ORG', 'application-name': 'test-APP', 'service-name': 'test-SERVICE', 'version': '42.0-test' } artifact_dir_name = 'test-result-artifact-dir' temp_dir.makedir(artifact_dir_name) artifact_file_name_1 = f'{artifact_dir_name}/test-artifact1.txt' temp_dir.write(artifact_file_name_1, bytes('hello world 1', 'utf-8')) artifact_file_name_2 = f'{artifact_dir_name}/test-artifact2.txt' temp_dir.write(artifact_file_name_2, bytes('hello world 2', 'utf-8')) step_result = StepResult( step_name='test-step', sub_step_name='test-sub-step', sub_step_implementer_name='test-sub-step-implementer') step_result.add_artifact(name='test-step-result-dir', value=os.path.join( temp_dir.path, f'{artifact_dir_name}/')) 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) archive_path = step_implementer._ResultArtifactsArchive__create_archive( ) archive_zip = zipfile.ZipFile(archive_path) artifact_file_path_1 = f"{step_config['organization']}-" \ f"{step_config['application-name']}-{step_config['service-name']}-" \ f"{step_config['version']}/test-step/test-sub-step/test-step-result-dir/" \ f"{artifact_file_name_1}" with archive_zip.open(artifact_file_path_1, 'r') as artifact_file: artifact_file_contents = artifact_file.read().decode('UTF-8') self.assertEqual(artifact_file_contents, 'hello world 1') artifact_file_path_2 = f"{step_config['organization']}-" \ f"{step_config['application-name']}-{step_config['service-name']}-" \ f"{step_config['version']}/test-step/test-sub-step/test-step-result-dir/" \ f"{artifact_file_name_1}" with archive_zip.open(artifact_file_path_2, 'r') as artifact_file: artifact_file_contents = artifact_file.read().decode('UTF-8') self.assertEqual(artifact_file_contents, 'hello world 1')
def test_success_release_branch_default_prerelease_branch_regexes_commit_and_push_always_not_dirty( self, mock_repo, mock_git_url, mock_git_commit_utc_timestamp ): # Test data setup branch_name = "not-main" with TempDirectory() as temp_dir: # setup step_config = { 'repo-root': temp_dir.path, 'git-commit-and-push-changes': 'always' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git' ) # setup mocks mock_repo().bare = False mock_repo().is_dirty.return_value = False mock_repo().head.is_detached = False mock_repo().active_branch.name = branch_name mock_repo().head.reference.commit = 'a1b2c3d4e5f6g7h8i9' mock_git_url.return_value = "value doesn't matter" mock_git_commit_utc_timestamp.return_value = '42' # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.add_artifact( name='branch', value=branch_name ) expected_step_result.add_artifact( name='is-pre-release', value=True ) expected_step_result.add_artifact( name='commit-hash', value='a1b2c3d4e5f6g7h8i9' ) expected_step_result.add_artifact( name='commit-utc-timestamp', value='42' ) self.assertEqual(actual_step_result, expected_step_result)
def test_merge_raises_exception_for_mismatching_data(self): sr1 = StepResult('step1', 'sub1', 'implementer1') sr2 = StepResult('step2', 'sub1', 'implementer1') with self.assertRaisesRegex( StepRunnerException, f'Other StepResult does not have matching ' \ f'step name, sub step name, or environment' ): sr1.merge(sr2)
def test_run_step_pass_with_existing_tags(self, MockRepo, mock_sh): previous_dir = os.getcwd() try: with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') repo = Repo.init(str(temp_dir.path)) temp_dir.write( '.cz.json', b'''{ "commitizen": { "bump_message": "build: bump $current_version \\u2192 $new_version [skip-ci]", "name": "cz_conventional_commits", "tag_format": "$version", "update_changelog_on_bump": true } }''') cz_json_path = os.path.join(temp_dir.path, '.cz.json') step_config = { 'cz-json': cz_json_path, 'repo-root': temp_dir.path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Commitizen', parent_work_dir_path=parent_work_dir_path, ) mock_sh.cz.bump.side_effect = functools.partial( self._mock_cz_bump, path=os.path.join(temp_dir.path, '.cz.json'), increment_type='minor') tag = collections.namedtuple('Tag', 'name') MockRepo().tags = [ tag(name='v_0.1.0__'), tag(name='0.3.0'), tag(name='version_4.34.0'), tag(name='5.2.7-prerelease'), tag(name='v3.142.0-test_abcxyz') ] result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Commitizen', sub_step_implementer_name='Commitizen') expected_step_result.success = True expected_step_result.add_artifact(name='app-version', value='5.3.0') self.assertEqual(result, expected_step_result) finally: os.chdir(previous_dir)
def test__run_step_pass_audit_success(self, audit_attestation_mock, download_source_to_destination_mock): with TempDirectory() as temp_dir: workflow_attestation_uri = 'https://foo.bar/evidence.json' workflow_policy_uri = 'https://foo.bar/policy.json' parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'workflow-policy-uri': workflow_policy_uri } step_result = StepResult( step_name='test-step', sub_step_name='test-sub-step', sub_step_implementer_name='test-sub-step-implementer' ) step_result.add_artifact('evidence-uri', workflow_attestation_uri, 'URI of the uploaded results archive.') 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 ) download_source_to_destination_mock.side_effect = [ parent_work_dir_path + '/workflow_attestion.json', parent_work_dir_path + '/workflow_policy.rego'] audit_attestation_mock.return_value = "Audit was successful", 0 step_result = step_implementer._run_step() expected_step_result = StepResult( step_name='audit_attestation', sub_step_name='OpenPolicyAgent', sub_step_implementer_name='OpenPolicyAgent' ) expected_step_result.add_artifact( name='audit-results', value='Audit was successful' ) expected_step_result.message = 'Audit was successful' self.assertEqual(step_result, expected_step_result) audit_attestation_mock.assert_called_once_with(parent_work_dir_path + '/workflow_attestion.json', parent_work_dir_path + '/workflow_policy.rego', "data.workflowResult.passAll") download_source_to_destination_mock.assert_has_calls([ mock.call(workflow_attestation_uri, parent_work_dir_path + '/audit_attestation'), mock.call(workflow_policy_uri, parent_work_dir_path + '/audit_attestation') ] )
def test__upload_to_remote_with_auth(self, gather_evidence_mock, upload_file_mock): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'organization': 'test-ORG', 'application-name': 'test-APP', 'service-name': 'test-SERVICE', 'version': '42.0-test', 'evidence-destination-url': self.DEST_URL, 'evidence-destination-username': '******', 'evidence-destination-password': '******' } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path ) # mock the upload results upload_file_mock.return_value = "mock upload results" # run the step step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate_evidence', sub_step_name='GenerateEvidence', sub_step_implementer_name='GenerateEvidence' ) expected_step_result.add_artifact( name='evidence-upload-results', description='Results of uploading the evidence JSON file ' \ 'to the given destination.', value='mock upload results' ) expected_step_result.add_artifact( name='evidence-uri', description='URI of the uploaded results archive.', value=self.DEST_URI ) expected_step_result.add_artifact( name='evidence-path', value=os.path.join(parent_work_dir_path, 'generate_evidence', "test-ORG-test-APP-test-SERVICE-42.0-test-evidence.json"), description='File path of evidence.' ) expected_step_result.message = "Evidence successfully packaged in JSON file and uploaded to data store." self.assertEqual(step_result, expected_step_result) # verify mocks called gather_evidence_mock.assert_called_once() upload_file_mock.assert_called_once_with( file_path=mock.ANY, destination_uri=self.DEST_URI, username='******', password='******' )
def test_success_pre_release_branch_custom_release_branch_regexes_list( self, mock_repo, mock_git_url, mock_git_commit_utc_timestamp ): with TempDirectory() as temp_dir: # setup step_config = { 'repo-root': temp_dir.path, 'release-branch-regexes': [ '^release/.*$' ] } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git' ) # setup mocks mock_repo().bare = False mock_repo().head.is_detached = False mock_repo().active_branch.name = 'feature/mock1' mock_repo().head.reference.commit = 'a1b2c3d4e5f6g7h8i9' mock_git_commit_utc_timestamp.return_value = '42' # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.add_artifact( name='branch', value='feature/mock1' ) expected_step_result.add_artifact( name='is-pre-release', value=True ) expected_step_result.add_artifact( name='commit-hash', value='a1b2c3d4e5f6g7h8i9' ) expected_step_result.add_artifact( name='commit-utc-timestamp', value='42' ) self.assertEqual(actual_step_result, expected_step_result)