def test_get_step_result_json(self): step_result_expected = '{"step1": {"sub1": {"sub-step-implementer-name": "implementer1", "success": true, "message": "", "artifacts": {"artifact1": {"description": "description1", "value": "value1"}}}}}' step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') print(step_result) self.assertEqual(step_result.get_step_result_json(), step_result_expected)
def test_add_artifact(self): step_result_expected = { 'step1': { 'sub1': { 'sub-step-implementer-name': 'implementer1', 'success': True, 'message': '', 'artifacts': { 'artifact1': { 'description': 'description1', 'value': 'value1' }, 'artifact2': { 'description': 'description2', 'value': 'value2' }, 'artifact3': { 'description': '', 'value': 'value3' } } } } } step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') step_result.add_artifact('artifact2', 'value2', 'description2') step_result.add_artifact('artifact3', 'value3') self.assertEqual(step_result.get_step_result_dict(), step_result_expected)
def test_run_step_pass(self): with TempDirectory() as temp_dir: 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', work_dir_path=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.get_step_result_dict(), expected_step_result.get_step_result_dict())
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)
def test_run_step_pass(self): with TempDirectory() as temp_dir: 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( 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, work_dir_path=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.get_step_result_dict(), result.get_step_result_dict())
def test_run_step_error_git_url(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' 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 = { 'url': url, 'git-username': '******', 'git-password': '******' } artifact_config = { 'version': { 'description': '', 'value': tag }, 'container-image-version': { 'description': '', 'value': tag } } self.setup_previous_result(work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) def get_tag_side_effect(): return tag get_tag_mock.side_effect = get_tag_side_effect # this is the test here git_url_mock.side_effect = StepRunnerException('mock error') result = step_implementer._run_step() # verify 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 = "mock error" # verifying correct mocks were called git_tag_mock.assert_called_once_with(tag) git_url_mock.assert_called_once_with() self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_pass_no_username_password(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' 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 = {'url': url} artifact_config = { 'version': { 'description': '', 'value': tag }, 'container-image-version': { 'description': '', 'value': tag } } self.setup_previous_result(work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=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.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_fail_sign_image(self, sign_image_mock, import_pgp_key_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') pgp_private_key_fingerprint = 'abc123' step_config = TestStepImplementerSignContainerImagePodman.generate_config() container_image_tag = 'does/not/matter:v0.42.0' # Previous (fake) results artifact_config = { 'container-image-tag': {'value': container_image_tag} } self.setup_previous_result(work_dir_path, artifact_config) def import_pgp_key_side_effect(pgp_private_key): return pgp_private_key_fingerprint import_pgp_key_mock.side_effect = import_pgp_key_side_effect sign_image_mock.side_effect = StepRunnerException('mock error signing image') # Actual results step_implementer = self.create_step_implementer( step_config=step_config, step_name='sign-container-image', implementer='PodmanSign', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) result = step_implementer._run_step() import_pgp_key_mock.assert_called_once_with( pgp_private_key=step_config['container-image-signer-pgp-private-key'] ) sign_image_mock.assert_called_once_with( pgp_private_key_fingerprint=pgp_private_key_fingerprint, image_signatures_directory= os.path.join( work_dir_path, 'sign-container-image/image-signature' ), container_image_tag=container_image_tag ) 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-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.get_step_result_dict(), result.get_step_result_dict())
def test_get_artifact(self): step_result_expected = { 'description': 'description1', 'value': 'value1' } step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') self.assertEqual(step_result.get_artifact('artifact1'), step_result_expected)
def test_run_step_fail_scan(self, configlint_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') file_to_validate_contents = 'notused' temp_dir.write('config-file-to-validate.yml', file_to_validate_contents.encode()) file_to_validate_file_path = str( os.path.join(temp_dir.path, 'config-file-to-validate.yml')) # write config-lint rules file configlint_rules_content = 'not used' config_lint_rules_file_name = 'config-lint-test-rules.yml' temp_dir.write(config_lint_rules_file_name, configlint_rules_content.encode()) config_lint_rules_file_path = os.path.join( temp_dir.path, config_lint_rules_file_name) step_config = { 'configlint-yml-path': file_to_validate_file_path, 'rules': config_lint_rules_file_path } configlint_mock.side_effect = \ TestStepImplementerConfiglint.create_config_lint_side_effect( config_lint_fail=True ) step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='Configlint', 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='validate-environment-configuration', sub_step_name='Configlint', sub_step_implementer_name='Configlint') expected_step_result.success = False expected_step_result.message = 'Failed config-lint scan.' expected_step_result.add_artifact( name='configlint-result-set', value= f'{work_dir_path}/validate-environment-configuration/configlint_results_file.txt' ) expected_step_result.add_artifact(name='configlint-yml-path', value=file_to_validate_file_path) self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def test_add_step_result_duplicate_no_env(self): duplicate_step_result = StepResult('step1', 'sub1', 'implementer1') duplicate_step_result.add_artifact('newartifact', 'newvalue', 'newdescription') wfr = setup_test() with self.assertRaisesRegex( StepRunnerException, r"Can not add duplicate StepResult for step \(step1\)," r" sub step \(sub1\), and environment \(None\)."): wfr.add_step_result(duplicate_step_result)
def test_add_step_result_duplicate_with_env(self): duplicate_step_result = StepResult('deploy', 'deploy-sub', 'helm', 'dev') duplicate_step_result.add_artifact('newartifact', 'newvalue', 'newdescription') wfr = setup_test() with self.assertRaisesRegex( StepRunnerException, r"Can not add duplicate StepResult for step \(deploy\)," r" sub step \(deploy-sub\), and environment \(dev\)."): wfr.add_step_result(duplicate_step_result)
def test_run_step_pass(self, curl_mock): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') signature_file_path = 'signature-1' temp_dir.write(signature_file_path, b'bogus signature') container_image_signature_file_path = os.path.join(temp_dir.path, signature_file_path) container_image_signature_name = 'jkeam/hello-node@sha256=2cbdb73c9177e63e85d267f738e' \ '99e368db3f806eab4c541f5c6b719e69f1a2b/signature-1' step_config = { 'container-image-signature-server-url': 'https://sigserver/signatures', 'container-image-signature-server-username': '******', 'container-image-signature-server-password': '******', 'with-fips': True } # Previous (fake) results artifact_config = { 'container-image-signature-file-path': {'value': container_image_signature_file_path}, 'container-image-signature-name': {'value': container_image_signature_name}, } workflow_result = self.setup_previous_result(work_dir_path, artifact_config) # Actual results step_implementer = self.create_step_implementer( step_config=step_config, step_name='sign-container-image', implementer='CurlPush', workflow_result=workflow_result, work_dir_path=work_dir_path ) result = step_implementer._run_step() # Expected results expected_step_result = StepResult(step_name='sign-container-image', sub_step_name='CurlPush', sub_step_implementer_name='CurlPush') expected_step_result.add_artifact(name='container-image-signature-url', value=f'https://sigserver/signatures/{container_image_signature_name}') self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict()) curl_mock.assert_called_once_with( '-sSfv', '-X', 'PUT', '--user', "admin:adminPassword", '--upload-file', container_image_signature_file_path, f"https://sigserver/signatures/{container_image_signature_name}", _out=Any(IOBase), _err_to_out=True, _tee='out' )
def test_get_artifacts_property(self): step_result_expected = { 'artifact1': { 'description': 'description1', 'value': 'value1' }, 'artifact2': { 'description': '', 'value': 'value2' } } step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') step_result.add_artifact('artifact2', 'value2') self.assertEqual(step_result.artifacts, step_result_expected)
def test_run_step_pass_different_pre_release(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 = {} artifact_config = { 'app-version': { 'description': '', 'value': '42.1.0' }, 'pre-release': { 'description': '', 'value': 'feature123' }, 'build': { 'description': '', 'value': 'abc123' } } self.setup_previous_result(work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='SemanticVersion', 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='SemanticVersion', sub_step_implementer_name='SemanticVersion') expected_step_result.add_artifact(name='version', value='42.1.0-feature123+abc123') expected_step_result.add_artifact(name='container-image-version', value='42.1.0-feature123') self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_get_step_result_yaml(self): step_result_expected = '''step1: sub1: artifacts: artifact1: description: description1 value: value1 message: '' sub-step-implementer-name: implementer1 success: true ''' step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') print(step_result.get_step_result_yaml()) self.assertEqual(step_result.get_step_result_yaml(), step_result_expected)
def test_run_step_pass_prev_step(self, config_lint_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') 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 = { 'rules': test_file_path, } artifact_config = { 'configlint-yml-path': { 'value': f'{test_file_path}' } } self.setup_previous_result(work_dir_path, artifact_config) step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='Configlint', 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='validate-environment-configuration', sub_step_name='Configlint', sub_step_implementer_name='Configlint') expected_step_result.add_artifact( name='configlint-result-set', value= f'{work_dir_path}/validate-environment-configuration/configlint_results_file.txt' ) expected_step_result.add_artifact(name='configlint-yml-path', value=test_file_path) self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def test_run_step_pass_https(self, git_push_mock, git_tag_mock, git_url_mock, get_tag_mock): with TempDirectory() as temp_dir: tag = '1.0+69442c8' url = 'https://git.ploigos.xyz/ploigos-references/ploigos-reference-app-quarkus-rest-json.git' 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(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, work_dir_path=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('https://*****:*****@' + url[8:]) self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_pass(self, config_lint_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') test_file_name = 'file.txt' test_file_path = os.path.join(temp_dir.path, test_file_name) temp_dir.write(test_file_path, b'ignored') step_config = { 'rules': test_file_path, 'configlint-yml-path': test_file_path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='validate-environment-configuration', implementer='Configlint', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) config_lint_mock.side_effect = sh.ErrorReturnCode( 'config_lint', b'mock out', b'mock error') 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 = 'Unexpected Error invoking config-lint.' expected_step_result.add_artifact( name='configlint-result-set', value= f'{work_dir_path}/validate-environment-configuration/configlint_results_file.txt' ) expected_step_result.add_artifact(name='configlint-yml-path', value=test_file_path) self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def test__get_tag_no_version(self, git_push_mock, git_tag_mock, git_url_mock): with TempDirectory() as temp_dir: tag = 'latest' url = '[email protected]:ploigos/ploigos-step-runner.git' work_dir_path = os.path.join(temp_dir.path, 'working') artifact_config = { 'container-image-version': {'description': '', 'value': tag} } workflow_result = self.setup_previous_result(work_dir_path, artifact_config) step_config = { 'url': url } step_implementer = self.create_step_implementer( step_config=step_config, workflow_result=workflow_result, work_dir_path=work_dir_path ) 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 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.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_add_duplicate(self): step_result_expected = { 'artifact1': { 'description': 'description1', 'value': 'value1' }, 'artifact2': { 'description': '', 'value': 'lastonewins' } } step_result = StepResult('step1', 'sub1', 'implementer1') step_result.add_artifact('artifact1', 'value1', 'description1') step_result.add_artifact('artifact2', 'here') step_result.add_artifact('artifact2', 'andhere') step_result.add_artifact('artifact2', 'lastonewins') self.assertEqual(step_result.artifacts, step_result_expected)
def test_run_step_pass(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') 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', 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='Maven', sub_step_implementer_name='Maven' ) expected_step_result.add_artifact(name='app-version', value='42.1') self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_add_artifact_missing_value(self): step_result = StepResult('step1', 'sub1', 'implementer1') with self.assertRaisesRegex(StepRunnerException, r"Value is required to add artifact"): step_result.add_artifact('name1', '', 'description1')
def setup_test(): step_result1 = StepResult('step1', 'sub1', 'implementer1') step_result1.add_artifact('artifact1', 'value1', 'description1') step_result1.add_artifact('artifact2', 'value2', 'description2') step_result1.add_artifact('artifact3', 'value3') step_result1.add_artifact('artifact4', False) step_result1.add_artifact('same-artifact-all-env-and-no-env', 'result1') step_result2 = StepResult('step2', 'sub2', 'implementer2') step_result2.add_artifact('artifact1', True) step_result2.add_artifact('artifact2', False) step_result2.add_artifact('artifact5', 'value5') step_result3 = StepResult('deploy', 'deploy-sub', 'helm', 'dev') step_result3.add_artifact('same-artifact-diff-env', 'value-dev-env') step_result3.add_artifact('unique-artifact-to-step-and-environment-1', 'value1-dev-env') step_result3.add_artifact('same-artifact-all-env-and-no-env', 'result3-dev-env') step_result4 = StepResult('deploy', 'deploy-sub', 'helm', 'test') step_result4.add_artifact('artifact1', True) step_result4.add_artifact('artifact2', False) step_result4.add_artifact('artifact5', 'value5') step_result4.add_artifact('same-artifact-diff-env', 'value-test-env') step_result4.add_artifact('unique-artifact-to-step-and-environment-2', 'value2-test-env') step_result4.add_artifact('same-artifact-all-env-and-no-env', 'result4-test-env') wfr = WorkflowResult() wfr.add_step_result(step_result1) wfr.add_step_result(step_result2) wfr.add_step_result(step_result3) wfr.add_step_result(step_result4) return wfr
def test_run_step_pass(self, sign_image_mock, import_pgp_key_mock): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') pgp_private_key_fingerprint = 'abc123' step_config = TestStepImplementerSignContainerImagePodman.generate_config( ) container_image_tag = 'does/not/matter:v0.42.0' signature_name = 'does/not/matter/signature-0' # Previous (fake) results artifact_config = { 'container-image-tag': { 'value': container_image_tag } } workflow_result = self.setup_previous_result( work_dir_path, artifact_config) def import_pgp_key_side_effect(pgp_private_key): return pgp_private_key_fingerprint import_pgp_key_mock.side_effect = import_pgp_key_side_effect def sign_image_side_effect(pgp_private_key_fingerprint, image_signatures_directory, container_image_tag): return os.path.join(image_signatures_directory, signature_name) sign_image_mock.side_effect = sign_image_side_effect # Actual results step_implementer = self.create_step_implementer( step_config=step_config, step_name='sign-container-image', implementer='PodmanSign', workflow_result=workflow_result, work_dir_path=work_dir_path) result = step_implementer._run_step() import_pgp_key_mock.assert_called_once_with( pgp_private_key=step_config[ 'container-image-signer-pgp-private-key']) sign_image_mock.assert_called_once_with( pgp_private_key_fingerprint=pgp_private_key_fingerprint, image_signatures_directory=os.path.join( work_dir_path, 'sign-container-image/image-signature'), container_image_tag=container_image_tag) 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-file-path', value=os.path.join(work_dir_path, 'sign-container-image/image-signature', signature_name)) expected_step_result.add_artifact( name='container-image-signature-name', value=signature_name) expected_step_result.add_artifact( name='container-image-signature-private-key-fingerprint', value=pgp_private_key_fingerprint) self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def test__run_step_pass(self, buildah_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('Dockerfile', b'''testing''') step_config = { 'containers-config-auth-file': 'buildah-auth.json', 'imagespecfile': 'Dockerfile', 'context': temp_dir.path, 'tls-verify': True, 'format': 'oci', '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', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) artifact_config = { 'container-image-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='create-container-image', sub_step_name='Buildah', sub_step_implementer_name='Buildah') expected_step_result.add_artifact( name='container-image-version', value='localhost/app-name/service-name:1.0-123abc') expected_step_result.add_artifact( name='image-tar-file', value=work_dir_path + '/create-container-image/image-app-name-service-name-1.0-123abc.tar' ) buildah_mock.bud.assert_called_once_with( '--storage-driver=vfs', '--format=oci', '--tls-verify=true', '--layers', '-f', 'Dockerfile', '-t', 'localhost/app-name/service-name:1.0-123abc', '--authfile', 'buildah-auth.json', temp_dir.path, _out=sys.stdout, _err=sys.stderr, _tee='err') buildah_mock.push.assert_called_once_with( '--storage-driver=vfs', 'localhost/app-name/service-name:1.0-123abc', 'docker-archive:' + work_dir_path + '/create-container-image/image-app-name-service-name-1.0-123abc.tar', _out=sys.stdout, _err=sys.stderr, _tee='err') self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def __run__run_step_test(self, test_dir, mvn_mock, write_effective_pom_mock, generate_maven_settings_mock, pom_content, group_id, artifact_id, surefire_reports_dir, selenium_hub_url, target_host_url=None, deployed_host_urls=None, write_mock_test_results=True, assert_mvn_called=True, assert_report_artifact=True, expected_result_success=True, expected_result_message='', fail_on_no_tests=None, uat_maven_profile=None, pom_file_name='pom.xml', raise_error_on_tests=False, set_tls_verify_false=False): work_dir_path = os.path.join(test_dir.path, 'working') cucumber_html_report_path = os.path.join(work_dir_path, 'cucumber.html') cucumber_json_report_path = os.path.join(work_dir_path, 'cucumber.json') test_dir.write(pom_file_name, pom_content) pom_file_path = os.path.join(test_dir.path, pom_file_name) step_config = { 'pom-file': pom_file_path, 'selenium-hub-url': selenium_hub_url, 'tls-verify': True } if set_tls_verify_false: step_config['tls-verify'] = False target_base_url = None if deployed_host_urls: step_config['deployed-host-urls'] = deployed_host_urls if isinstance(deployed_host_urls, list): target_base_url = deployed_host_urls[0] else: target_base_url = deployed_host_urls if target_host_url: step_config['target-host-url'] = target_host_url target_base_url = target_host_url if fail_on_no_tests is not None: step_config['fail-on-no-tests'] = fail_on_no_tests if uat_maven_profile is not None: step_config['uat-maven-profile'] = uat_maven_profile else: uat_maven_profile = 'integration-test' step_implementer = self.create_step_implementer( step_config=step_config, work_dir_path=work_dir_path, ) # mock generating settings settings_file_path = "/does/not/matter/settings.xml" def generate_maven_settings_side_effect(): return settings_file_path generate_maven_settings_mock.side_effect = generate_maven_settings_side_effect # mock effective pom def write_effective_pom_mock_side_effect(pom_file_path, output_path): create_parent_dir(pom_file_path) copyfile(pom_file_path, output_path) write_effective_pom_mock.side_effect = write_effective_pom_mock_side_effect # mock test results if write_mock_test_results: mvn_mock.side_effect = MaveStepImplementerTestCase.create_mvn_side_effect( pom_file=pom_file_path, artifact_parent_dir=surefire_reports_dir, artifact_names=[ f'{group_id}.{artifact_id}.CucumberTest.txt', f'TEST-{group_id}.{artifact_id}.CucumberTest.xml' ], raise_error_on_tests=raise_error_on_tests) result = step_implementer._run_step() if assert_mvn_called: if not set_tls_verify_false: mvn_mock.assert_called_once_with( 'clean', 'test', f'-P{uat_maven_profile}', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=' \ f'html:{cucumber_html_report_path},' \ f'json:{cucumber_json_report_path}', '-f', pom_file_path, '-s', settings_file_path, _out=Any(IOBase), _err=Any(IOBase) ) else: mvn_mock.assert_called_once_with( 'clean', 'test', f'-P{uat_maven_profile}', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=' \ f'html:{cucumber_html_report_path},' \ f'json:{cucumber_json_report_path}', '-f', pom_file_path, '-s', settings_file_path, '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', _out=Any(IOBase), _err=Any(IOBase) ) expected_step_result = StepResult( step_name='uat', sub_step_name='MavenSeleniumCucumber', sub_step_implementer_name='MavenSeleniumCucumber') expected_step_result.success = expected_result_success expected_step_result.message = expected_result_message if assert_report_artifact: mvn_test_output_file_path = os.path.join(work_dir_path, 'uat', 'mvn_test_output.txt') expected_step_result.add_artifact( description= f"Standard out and standard error by 'mvn -P{uat_maven_profile} test'.", name='maven-output', value=mvn_test_output_file_path) expected_step_result.add_artifact( description= f"Surefire reports generated by 'mvn -P{uat_maven_profile} test'.", name='surefire-reports', value=surefire_reports_dir) expected_step_result.add_artifact( description= f"Cucumber (HTML) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-html', value=cucumber_html_report_path) expected_step_result.add_artifact( description= f"Cucumber (JSON) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-json', value=cucumber_json_report_path) self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def setup_test_sub_steps(): step_result1 = StepResult('step1', 'sub1', 'implementer1') step_result1.add_artifact('artifact1', 'value1', 'description1') step_result1.add_artifact('artifact2', 'value2', 'description2') step_result1.add_artifact('artifact3', 'value3') step_result1.add_artifact('artifact4', False) step_result2 = StepResult('step1', 'sub2', 'implementer2') step_result2.add_artifact('artifact1', True) step_result2.add_artifact('artifact2', False) step_result2.add_artifact('artifact5', 'value5') wfr = WorkflowResult() wfr.add_step_result(step_result1) wfr.add_step_result(step_result2) return wfr
def test_run_step_pass(self, skopeo_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') image_tar_file = 'fake-image.tar' image_version = '1.0-69442c8' image_tag = f'fake-registry.xyz/fake-org/fake-app-fake-service:{image_version}' step_config = { 'destination-url': 'fake-registry.xyz', 'service-name': 'fake-service', 'application-name': 'fake-app', 'organization': 'fake-org', 'container-image-version': image_version, 'image-tar-file': image_tar_file } step_implementer = self.create_step_implementer( step_config=step_config, step_name='push-container-image', implementer='Skopeo', 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='push-container-image', sub_step_name='Skopeo', sub_step_implementer_name='Skopeo') expected_step_result.add_artifact( name='container-image-registry-uri', value='fake-registry.xyz') expected_step_result.add_artifact( name='container-image-registry-organization', value='fake-org') expected_step_result.add_artifact( name='container-image-repository', value='fake-app-fake-service') expected_step_result.add_artifact(name='container-image-name', value='fake-app-fake-service') expected_step_result.add_artifact(name='container-image-version', value=image_version) expected_step_result.add_artifact( name='container-image-tag', value= 'fake-registry.xyz/fake-org/fake-app-fake-service:1.0-69442c8') self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict()) containers_config_auth_file = os.path.join(Path.home(), '.skopeo-auth.json') skopeo_mock.copy.assert_called_once_with( "--src-tls-verify=true", "--dest-tls-verify=true", f"--authfile={containers_config_auth_file}", f'docker-archive:{image_tar_file}', f'docker://{image_tag}', _out=Any(IOBase), _err=Any(IOBase), _tee='err')
def test_run_step_pass(self, curl_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') signature_file_path = 'signature-1' temp_dir.write(signature_file_path, b'bogus signature') container_image_signature_file_path = os.path.join( temp_dir.path, signature_file_path) container_image_signature_name = 'jkeam/hello-node@sha256=2cbdb73c9177e63e85d267f738e' \ '99e368db3f806eab4c541f5c6b719e69f1a2b/signature-1' step_config = { 'container-image-signature-server-url': 'https://sigserver/signatures', 'container-image-signature-server-username': '******', 'container-image-signature-server-password': '******' } # Previous (fake) results artifact_config = { 'container-image-signature-file-path': { 'value': container_image_signature_file_path }, 'container-image-signature-name': { 'value': container_image_signature_name }, } self.setup_previous_result(work_dir_path, artifact_config) # Actual results step_implementer = self.create_step_implementer( step_config=step_config, step_name='sign-container-image', implementer='CurlPush', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) result = step_implementer._run_step() # # Expected results expected_step_result = StepResult( step_name='sign-container-image', sub_step_name='CurlPush', sub_step_implementer_name='CurlPush') expected_step_result.add_artifact( name='container-image-signature-url', value= f'https://sigserver/signatures/{container_image_signature_name}' ) expected_step_result.add_artifact( name='container-image-signature-file-sha1', value='d9ba1fc747829392883c48adfe4bb688239dc8b2') expected_step_result.add_artifact( name='container-image-signature-file-md5', value='b66c5c3d4ab37a50e69a05d72ba302fa') self.assertEqual(expected_step_result.get_step_result_dict(), result.get_step_result_dict()) curl_mock.assert_called_once_with( '-sSfv', '-X', 'PUT', '--header', StringRegexParam(r'X-Checksum-Sha1:.+'), '--header', StringRegexParam(r'X-Checksum-MD5:.+'), '--user', "admin:adminPassword", '--upload-file', container_image_signature_file_path, f"https://sigserver/signatures/{container_image_signature_name}", _out=Any(IOBase), _err_to_out=True, _tee='out')