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_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_write_to_pickle_file_with_merge(self): with TempDirectory() as temp_dir: pickle_file = temp_dir.path + '/test.pkl' on_disk_wfr = setup_test() on_disk_wfr.write_to_pickle_file(pickle_file) in_mem_wfr1 = WorkflowResult.load_from_pickle_file(pickle_file) in_mem_wfr2 = WorkflowResult.load_from_pickle_file(pickle_file) sr1 = StepResult('compliance-scan', 'scan with stackrox', 'stackrox', 'prod') sr1.add_artifact('compliance-scan-result', 'pass') sr2 = StepResult('vulnerability-scan', 'scan with stackrox', 'stackrox', 'prod') sr2.add_artifact('vulnerability-scan-result', 'fail') in_mem_wfr1.add_step_result(sr1) in_mem_wfr2.add_step_result(sr2) in_mem_wfr1.merge_with_pickle_file(pickle_file) in_mem_wfr1.write_to_pickle_file(pickle_file) in_mem_wfr2.merge_with_pickle_file(pickle_file) in_mem_wfr2.write_to_pickle_file(pickle_file) resulting_wfr = WorkflowResult.load_from_pickle_file(pickle_file) # ensure: # - both our new StepResults are present # - all StepResults from the original pickle file are present self.assertIn(sr1, resulting_wfr.workflow_list) self.assertIn(sr2, resulting_wfr.workflow_list) for sr in on_disk_wfr.workflow_list: self.assertIn(sr, resulting_wfr.workflow_list)
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(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 with mock failure mock_run_maven_step.side_effect = StepRunnerException( 'Mock error running maven') 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') expected_step_result.message = "Error running maven. " \ "More details maybe found in 'maven-output' report artifact: "\ "Mock error running maven" expected_step_result.success = False # 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_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_success_no_tls_verify_str(self, mock_create_working_dir_sub_dir, mock_write_working_file, mock_container_registries_login, mock_inspect_container_image, mock_s2i): # setup step_config = { 's2i-builder-image': 'mock.io/awesome-image:v42', 'tls-verify': 'false' } step_implementer = self.create_step_implementer( step_config=step_config) # setup mocks mock_inspect_container_image.return_value = { 'OCIv1': { 'config': { 'Labels': { 'io.openshift.s2i.scripts-url': 'image:///moock-image-label/s2i' } } } } # run test actual_step_result = step_implementer._run_step() # validate expected_step_result = StepResult( step_name='create-container-image', sub_step_name='SourceToImage', sub_step_implementer_name='SourceToImage') expected_step_result.add_artifact( name='imagespecfile', value='/mock/working/s2i-context/Containerfile.s2i-gen', description= 'File defining the container image to build generated by s2i') expected_step_result.add_artifact( name='context', value='/mock/working/s2i-context', description= 'Context to use when building the imagespecfile generated by S2I.') self.assertEqual(actual_step_result, expected_step_result) mock_inspect_container_image.assert_called_once_with( container_image_address='mock.io/awesome-image:v42', containers_config_auth_file= 'create-container-image/container-auth.json') mock_s2i.build.assert_called_once_with( '.', 'mock.io/awesome-image:v42', '--loglevel', 1, '--as-dockerfile', '/mock/working/s2i-context/Containerfile.s2i-gen', '--image-scripts-url', 'image:///moock-image-label/s2i', _out=ANY, _err=ANY, _tee='err')
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__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__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_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_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_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__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_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_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_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_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_fail_set_version(self, mock_settings_file, mock_write_working_file, mock_run_maven_step, mock_run_maven): with TempDirectory() as test_dir: parent_work_dir_path = os.path.join(test_dir.path, 'working') pom_file = os.path.join(test_dir.path, 'mock-pom.xml') maven_push_artifact_repo_id = 'mock-repo-id' maven_push_artifact_repo_url = 'https://mock-repo.ploigos.com' version = '0.42.0-mock' step_config = { 'pom-file': pom_file, 'maven-push-artifact-repo-id': maven_push_artifact_repo_id, 'maven-push-artifact-repo-url': maven_push_artifact_repo_url, 'version': version } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # run step with mvn version:set failure mock_run_maven.side_effect = StepRunnerException( 'mock error setting new pom version') actual_step_result = step_implementer._run_step() # create expected step result expected_step_result = StepResult( step_name='deploy', sub_step_name='MavenDeploy', sub_step_implementer_name='MavenDeploy') expected_step_result.success = False expected_step_result.message = "Error running 'maven deploy' to push artifacts. " \ "More details maybe found in 'maven-output' report artifact: " \ "mock error setting new pom version" expected_step_result.add_artifact( description= "Standard out and standard error from running maven to update version.", name='maven-update-version-output', value='/mock/mvn_versions_set_output.txt') expected_step_result.add_artifact( description="Standard out and standard error from running maven to " \ "push artifacts to repository.", name='maven-push-artifacts-output', value='/mock/mvn_deploy_output.txt' ) # verify step result self.assertEqual(actual_step_result, expected_step_result) mock_write_working_file.assert_called() mock_run_maven.assert_called_with( mvn_output_file_path='/mock/mvn_versions_set_output.txt', settings_file='/fake/settings.xml', pom_file=pom_file, phases_and_goals=['versions:set'], additional_arguments=[f'-DnewVersion={version}']) mock_run_maven_step.assert_not_called()
def test_success(self, mock_settings_file, mock_write_working_file, mock_run_maven_step, mock_run_maven): with TempDirectory() as test_dir: parent_work_dir_path = os.path.join(test_dir.path, 'working') pom_file = os.path.join(test_dir.path, 'mock-pom.xml') maven_push_artifact_repo_id = 'mock-repo-id' maven_push_artifact_repo_url = 'https://mock-repo.ploigos.com' version = '0.42.0-mock' step_config = { 'pom-file': pom_file, 'maven-push-artifact-repo-id': maven_push_artifact_repo_id, 'maven-push-artifact-repo-url': maven_push_artifact_repo_url, 'version': version } 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='deploy', sub_step_name='MavenDeploy', sub_step_implementer_name='MavenDeploy') expected_step_result.add_artifact( description= "Standard out and standard error from running maven to update version.", name='maven-update-version-output', value='/mock/mvn_versions_set_output.txt') expected_step_result.add_artifact( description="Standard out and standard error from running maven to " \ "push artifacts to repository.", name='maven-push-artifacts-output', value='/mock/mvn_deploy_output.txt' ) # verify step result self.assertEqual(actual_step_result, expected_step_result) mock_write_working_file.assert_called() mock_run_maven.assert_called_with( mvn_output_file_path='/mock/mvn_versions_set_output.txt', settings_file='/fake/settings.xml', pom_file=pom_file, phases_and_goals=['versions:set'], additional_arguments=[f'-DnewVersion={version}']) mock_run_maven_step.assert_called_with( mvn_output_file_path='/mock/mvn_deploy_output.txt', step_implementer_additional_arguments=[ '-DaltDeploymentRepository=' \ f'{maven_push_artifact_repo_id}::default::{maven_push_artifact_repo_url}' ] )
def test_success_with_report_dir_deployed_host_urls_not_list( self, mock_gather_evidence, mock_write_working_file, mock_run_npm_step ): with TempDirectory() as test_dir: # setup test parent_work_dir_path = os.path.join(test_dir.path, 'working') step_config = { 'deployed-host-urls': 'https://mock.ploigos.org/mock-app-1', 'npm-envs': { 'VAR1': 'VAL1', 'VAR2': 'VAL2' }, 'test-reports-dir': '/mock/test-results-dir', "target-host-env-var-name": "APP_ROUTE" } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='unit-test', sub_step_name='NpmXunitIntegrationTest', sub_step_implementer_name='NpmXunitIntegrationTest' ) expected_step_result.add_artifact( description="Standard out and standard error from npm.", name='npm-output', value='/mock/npm_output.txt' ) expected_step_result.add_artifact( description="Test report generated when running tests.", name='test-report', value='/mock/test-results-dir' ) self.assertEqual(actual_step_result, expected_step_result) mock_run_npm_step.assert_called_once_with( npm_output_file_path='/mock/npm_output.txt', step_implementer_additional_envs={ 'APP_ROUTE': 'https://mock.ploigos.org/mock-app-1'} ) mock_gather_evidence.assert_called_once_with( step_result=Any(StepResult), test_report_dirs='/mock/test-results-dir' ) self.assertEqual(step_implementer.npm_args, ['run', 'test:uat'])
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_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 __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_success_with_report_dir_deployed_host_urls_list_multiple_entries( self, mock_gather_evidence, mock_write_working_file, mock_run_npm_step): with TempDirectory() as test_dir: # setup test parent_work_dir_path = os.path.join(test_dir.path, 'working') deployed_host_urls = [ 'https://mock.ploigos.org/mock-app-1', 'https://mock.ploigos.org/mock-app-2' ] step_config = { 'npm-test-script': 'myscript', 'deployed-host-urls': deployed_host_urls, 'npm-envs': { 'VAR1': 'VAL1', 'VAR2': 'VAL2' }, 'test-reports-dir': '/mock/test-results-dir', "target-host-env-var-name": "APP_ROUTE" } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='shared', sub_step_name='NpmXunitGeneric', sub_step_implementer_name='NpmXunitGeneric') expected_step_result.add_artifact( description="Standard out and standard error from npm.", name='npm-output', value='/mock/npm_output.txt') expected_step_result.add_artifact( description="Test report generated when running tests.", name='test-report', value='/mock/test-results-dir') expected_step_result.message = f"Given more than one deployed host URL ({deployed_host_urls})," \ f" targeting first one ({deployed_host_urls[0]}) for test." self.assertEqual(actual_step_result, expected_step_result) mock_run_npm_step.assert_called_once_with( npm_output_file_path='/mock/npm_output.txt', step_implementer_additional_envs={ 'APP_ROUTE': 'https://mock.ploigos.org/mock-app-1' }) mock_gather_evidence.assert_called_once_with( step_result=Any(StepResult), test_report_dirs='/mock/test-results-dir')
def test_fail_sign_image(self, mock_sign_image, mock_import_pgp_key, mock_upload_file, mock_container_registries_login, mock_get_deploy_time_container_image_address): with TempDirectory() as temp_dir: # setup parent_work_dir_path = os.path.join(temp_dir.path, 'working') pgp_private_key_fingerprint = 'abc123' step_config = TestStepImplementerSignContainerImagePodmanBase.generate_config( ) # setup mocks def import_pgp_key_side_effect(pgp_private_key): return pgp_private_key_fingerprint mock_import_pgp_key.side_effect = import_pgp_key_side_effect mock_sign_image.side_effect = StepRunnerException( 'mock error signing image') # run test step_implementer = self.create_step_implementer( step_config=step_config, step_name='sign-container-image', implementer='PodmanSign', parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() # validate mock_import_pgp_key.assert_called_once_with( pgp_private_key=step_config['signer-pgp-private-key']) mock_sign_image.assert_called_once_with( pgp_private_key_fingerprint=pgp_private_key_fingerprint, image_signatures_directory=os.path.join( parent_work_dir_path, 'sign-container-image/image-signature'), container_image_address= 'mock-deploy-time-container-image-address') mock_container_registries_login.assert_called_once_with( registries=None, containers_config_tls_verify=True, container_command_short_name='podman') mock_get_deploy_time_container_image_address.assert_called_once() expected_step_result = StepResult( step_name='sign-container-image', sub_step_name='PodmanSign', sub_step_implementer_name='PodmanSign') expected_step_result.add_artifact( name='container-image-signature-signer-private-key-fingerprint', value=pgp_private_key_fingerprint) expected_step_result.success = False expected_step_result.message = 'mock error signing image' self.assertEqual(expected_step_result, result)
def test__run_step_pass_upload_to_remote_with_auth_failure( self, create_archive_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', 'results-archive-destination-url': 'https://ploigos.com/mock/results-archives', 'results-archive-destination-username': '******', 'results-archive-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.side_effect = RuntimeError('mock upload error') # run the step step_result = step_implementer._run_step() # verify results 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.' ) expected_step_result.add_artifact( name='results-archive-uri', description='URI of the uploaded results archive.', value= 'https://ploigos.com/mock/results-archives/test-ORG/test-APP/test-SERVICE/foo.zip' ) expected_step_result.success = False expected_step_result.message = 'mock upload error' # verify mocks called create_archive_mock.assert_called_once() upload_file_mock.assert_called_once_with( file_path=mock.ANY, destination_uri= 'https://ploigos.com/mock/results-archives/test-ORG/test-APP/test-SERVICE/foo.zip', username='******', password='******')
def test_run_step_error_git_push(self, git_repo_mock, git_url_mock, get_tag_value_mock): # Test data setup tag = '1.0+69442c8' url = '[email protected]:ploigos/ploigos-step-runner.git' error = 'oh no. ohhhhh no.' branch_name = 'feature/no-feature' # Mock setup get_tag_value_mock.return_value = tag git_url_mock.return_value = url git_repo_mock.active_branch.name = branch_name with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') 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_config = { 'url': url, 'git-username': '******', 'git-password': '******' } step_implementer = self.create_step_implementer( step_config=step_config, workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path ) # this is the test here git_repo_mock.git.push.side_effect = Exception(error) result = step_implementer._run_step() # verify the test results 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) expected_step_result.success = False expected_step_result.message = f"Error tagging and pushing tags: Error pushing tags to remote ({url})" \ f" on current branch ({branch_name}): {error}" # verifying all mocks were called git_repo_mock.create_tag.assert_called_once_with(tag, force=True) git_url_mock.assert_called_once_with() git_repo_mock.git.push.assert_called_once_with(url, '--tag') self.assertEqual(result, expected_step_result)
def test_fail_with_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, 'target-host-url-maven-argument-name': 'mock.target-host-url-param', 'deployed-host-urls': ['https://mock.ploigos.org/mock-app-1'] } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) # setup mocks mock_run_maven_step.side_effect = StepRunnerException('mock error') # run test actual_step_result = step_implementer._run_step() # verify results expected_step_result = StepResult( step_name='unit-test', sub_step_name='MavenIntegrationTest', sub_step_implementer_name='MavenIntegrationTest') expected_step_result.success = False expected_step_result.message = "Error running maven. " \ "More details maybe found in report artifacts: " \ "mock error" expected_step_result.add_artifact( description="Standard out and standard error from maven.", name='maven-output', value='/mock/mvn_output.txt') expected_step_result.add_artifact( description="Test report generated when running unit tests.", name='test-report', value='/mock/test-results-dir') self.assertEqual(actual_step_result, expected_step_result) mock_run_maven_step.assert_called_once_with( mvn_output_file_path='/mock/mvn_output.txt', step_implementer_additional_arguments=[ '-Dmock.target-host-url-param=https://mock.ploigos.org/mock-app-1' ]) mock_gather_evidence.assert_called_once_with( step_result=Any(StepResult), test_report_dirs='/mock/test-results-dir')
def test_success_error_finding_label_on_build_image_inspection_details( self, mock_create_working_dir_sub_dir, mock_write_working_file, mock_container_registries_login, mock_inspect_container_image, mock_s2i): # setup step_config = {'s2i-builder-image': 'mock.io/awesome-image:v42'} step_implementer = self.create_step_implementer( step_config=step_config) # setup mocks mock_inspect_container_image.return_value = {} # run test actual_step_result = step_implementer._run_step() # validate expected_step_result = StepResult( step_name='create-container-image', sub_step_name='SourceToImage', sub_step_implementer_name='SourceToImage') expected_step_result.message = "WARNING: failed to find s2i scripts url label" \ " (io.openshift.s2i.scripts-url) on s2i builder image" \ " (mock.io/awesome-image:v42) to dynamically determine image scripts url." \ " S2I default will be used: Could not find key ('OCIv1').\n" expected_step_result.add_artifact( name='imagespecfile', value='/mock/working/s2i-context/Containerfile.s2i-gen', description= 'File defining the container image to build generated by s2i') expected_step_result.add_artifact( name='context', value='/mock/working/s2i-context', description= 'Context to use when building the imagespecfile generated by S2I.') self.assertEqual(actual_step_result, expected_step_result) mock_inspect_container_image.assert_called_once_with( container_image_address='mock.io/awesome-image:v42', containers_config_auth_file= 'create-container-image/container-auth.json') mock_s2i.build.assert_called_once_with( '.', 'mock.io/awesome-image:v42', '--loglevel', 1, '--tlsverify', '--as-dockerfile', '/mock/working/s2i-context/Containerfile.s2i-gen', _out=ANY, _err=ANY, _tee='err')