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_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_root_dir_is_bare_git_repo(self): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') Repo.init(str(temp_dir.path), bare=True) step_config = { 'repo-root': str(temp_dir.path) } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git', parent_work_dir_path=parent_work_dir_path, ) result = step_implementer._run_step() 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 = 'Given directory (repo_root) is a bare Git repository' self.assertEqual(result, expected_step_result)
def test_success(self, mock_write_working_file, mock_run_maven_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='MavenGeneric', sub_step_implementer_name='MavenGeneric') expected_step_result.add_artifact( description="Standard out and standard error from maven.", name='maven-output', value='/mock/mvn_output.txt') # verify step result self.assertEqual(actual_step_result, expected_step_result) mock_write_working_file.assert_called_once() mock_run_maven_step.assert_called_with( mvn_output_file_path='/mock/mvn_output.txt')
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_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_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_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_pom_file(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> </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.success = False expected_step_result.message = f"Given pom file ({pom_file_path}) does not contain " + \ "a \"version\" key." self.assertEqual(result, expected_step_result)
def test__run_step_fail_no_image_spec_file(self, buildah_mock): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') artifact_config = { 'container-image-version': {'description': '', 'value': '1.0-123abc'}, } workflow_result = self.setup_previous_result(parent_work_dir_path, artifact_config) step_config = { 'service-name': 'service-name', 'application-name': 'app-name' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='create-container-image', implementer='Buildah', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='create-container-image', sub_step_name='Buildah', sub_step_implementer_name='Buildah' ) expected_step_result.success = False expected_step_result.message = 'Image specification file does not exist in location: ./Containerfile' 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__run_step_pass_with_evidence(self, generate_evidence_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='generate_evidence', sub_step_name='GenerateEvidence', sub_step_implementer_name='GenerateEvidence') expected_step_result.message = 'Evidence successfully packaged ' \ 'in JSON file but was not uploaded to data store (no '\ 'destination URI specified).' 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.') print(str(step_result) + "\n\n" + str(expected_step_result)) self.assertEqual(step_result, expected_step_result) generate_evidence_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_run_step_fail_no_pom(self, mvn_mock): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' work_dir_path = os.path.join(temp_dir.path, 'working') step_config = {} step_implementer = self.create_step_implementer( step_config=step_config, step_name='package', implementer='Maven', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='package', sub_step_name='Maven', sub_step_implementer_name='Maven') expected_step_result.success = False expected_step_result.message = 'Given pom file does not exist: pom.xml' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
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_missing_required_config_key(self): config = { 'step-runner-config': { 'required-step-config-test': { 'implementer': 'tests.helpers.sample_step_implementers.RequiredStepConfigStepImplementer', 'config': {} } } } expected_results = StepResult( step_name='required-step-config-test', sub_step_name= 'tests.helpers.sample_step_implementers.RequiredStepConfigStepImplementer', sub_step_implementer_name= 'tests.helpers.sample_step_implementers.RequiredStepConfigStepImplementer' ) expected_results.success = False expected_results.message = "Missing required step configuration" \ " or previous step result artifact keys: ['required-config-key']" with TempDirectory() as test_dir: self._run_step_implementer_test(config, 'required-step-config-test', expected_results, test_dir)
def test_run_step_fail_no_properties(self, sonar_mock): 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' } 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_root_dir_is_not_git_repo(self): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'repo-root': '/' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) result = step_implementer._run_step() 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 = 'Given directory (repo_root) is not a Git repository' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_pass(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') 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) 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' ) 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': '-Djavax.net.ssl.trustStore=/etc/pki/java/cacerts' }, _out=sys.stdout, _err=sys.stderr) self.assertEqual(result, expected_step_result)
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_boolean_false_config_variable(self): config = { 'step-runner-config': { 'write-config-as-results': { 'implementer': 'tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer', 'config': { 'required-config-key': False } } } } config_expected_step_results = StepResult( step_name='write-config-as-results', sub_step_name='tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer', sub_step_implementer_name='tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer') config_expected_step_results.success = True config_expected_step_results.add_artifact(name='required-config-key', value=False) with TempDirectory() as test_dir: self._run_step_implementer_test(config, 'write-config-as-results', config_expected_step_results, test_dir)
def test_directory_is_detached(self): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') repo = Repo.init(str(temp_dir.path)) # create commits create_git_commit_with_sample_file(temp_dir, repo, 'test0') create_git_commit_with_sample_file(temp_dir, repo, 'test1') # detach head repo.git.checkout('master^') step_config = {'repo-root': str(temp_dir.path)} step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git', work_dir_path=work_dir_path, ) result = step_implementer._run_step() 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 = 'Expected a Git branch in given directory (repo_root) ' \ 'but has a detached head' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
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 test___create_archive_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_no_username_and_password(self, sonar_mock): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' 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') step_config = { 'properties': properties_path, 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='static-code-analysis', implementer='SonarQube', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) artifact_config = { 'version': { 'description': '', 'value': '1.0-123abc' }, } self.setup_previous_result(work_dir_path, artifact_config) 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/report-task.txt') 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.working.directory=' + work_dir_path, _out=sys.stdout, _err=sys.stderr) self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test___create_archive_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, 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_run_step_pass(self, git_push_mock, git_tag_mock, git_url_mock, get_tag_mock): with TempDirectory() as temp_dir: tag = '1.0+69442c8' url = '[email protected]:ploigos/ploigos-step-runner.git' parent_work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'url': url, 'git-username': '******', 'git-password': '******' } artifact_config = { 'version': { 'description': '', 'value': tag }, 'container-image-version': { 'description': '', 'value': tag } } workflow_result = self.setup_previous_result( parent_work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path, ) def get_tag_side_effect(): return tag get_tag_mock.side_effect = get_tag_side_effect def git_url_side_effect(): return url git_url_mock.side_effect = git_url_side_effect 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 get_tag_mock.assert_called_once_with() git_tag_mock.assert_called_once_with(tag) git_url_mock.assert_called_once_with() git_push_mock.assert_called_once_with(None) self.assertEqual(result, expected_step_result)
def __run__run_step_fail_sonar_scanner_error_test( self, sonar_scanner_error, expected_result_message_regex, sonar_mock): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' 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') 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', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) artifact_config = { 'version': { 'description': '', 'value': '1.0-123abc' }, } self.setup_previous_result(work_dir_path, artifact_config) 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/report-task.txt') 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_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_one_step_writes_to_empty_results_file(self): config1 = { 'step-runner-config': { 'write-config-as-results': { 'implementer': 'tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1', 'required-config-key': 'required' } } } } config1_expected_step_results = StepResult( step_name='write-config-as-results', sub_step_name='tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer', sub_step_implementer_name='tests.helpers.sample_step_implementers.' 'WriteConfigAsResultsStepImplementer') config1_expected_step_results.success = True config1_expected_step_results.add_artifact(name='config-1', value='config-1') config1_expected_step_results.add_artifact(name='config-overwrite-me', value='config-1') config1_expected_step_results.add_artifact(name='required-config-key', value='required') with TempDirectory() as test_dir: self._run_step_implementer_test(config1, 'write-config-as-results', config1_expected_step_results, test_dir)