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_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_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_no_commit_history(self): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') Repo.init(str(temp_dir.path)) 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.add_artifact(name='pre-release', value='master') expected_step_result.success = False expected_step_result.message = 'Given directory (repo_root) is a ' \ 'git branch (git_branch) with no commit history' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
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_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_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_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_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_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)
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__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_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_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_error_git_push(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') 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) 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 # this is the test here git_push_mock.side_effect = StepRunnerException('mock 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 = "mock error" # 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 test_run_step_multiple_sub_steps_first_sub_step_fail_contine_sub_steps_on_failure_str( self, foo_step_implementer_run_step_mock, foo_step_implementer2_run_step_mock): config = { 'step-runner-config': { 'foo': [{ 'name': 'Mock Sub Step 1', 'continue-sub-steps-on-failure': 'true', 'implementer': 'tests.helpers.sample_step_implementers.FooStepImplementer' }, { 'name': 'Mock Sub Step 2', 'implementer': 'tests.helpers.sample_step_implementers.FooStepImplementer2' }] } } # mock return value mock_sub_step_1_result = StepResult( step_name='foo', sub_step_name='Mock Sub Step 1', sub_step_implementer_name= 'tests.helpers.sample_step_implementers.FooStepImplementer') mock_sub_step_1_result.success = False mock_sub_step_2_result = StepResult( step_name='foo', sub_step_name='Mock Sub Step 2', sub_step_implementer_name= 'tests.helpers.sample_step_implementers.FooStepImplementer') mock_sub_step_2_result.success = True foo_step_implementer_run_step_mock.return_value = mock_sub_step_1_result foo_step_implementer2_run_step_mock.return_value = mock_sub_step_2_result # run test step_runner = StepRunner(config) actual_success = step_runner.run_step('foo') # validate self.assertFalse(actual_success) foo_step_implementer_run_step_mock.assert_called_once() foo_step_implementer2_run_step_mock.assert_called_once()
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 __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_run_step_pass_with_existing_tags(self, MockRepo, mock_sh): previous_dir = os.getcwd() try: with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') repo = Repo.init(str(temp_dir.path)) temp_dir.write('.cz.json', b'''{ "commitizen": { "bump_message": "build: bump $current_version \\u2192 $new_version [skip-ci]", "name": "cz_conventional_commits", "tag_format": "$version", "update_changelog_on_bump": true } }''') cz_json_path = os.path.join(temp_dir.path, '.cz.json') step_config = { 'cz-json': cz_json_path, 'repo-root': temp_dir.path } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Commitizen', parent_work_dir_path=parent_work_dir_path, ) mock_sh.cz.bump.side_effect = functools.partial( self._mock_cz_bump, path=os.path.join(temp_dir.path, '.cz.json'), increment_type='minor' ) tag = collections.namedtuple('Tag', 'name') MockRepo().tags = [ tag(name='v_0.1.0__'), tag(name='0.3.0'), tag(name='version_4.34.0'), tag(name='5.2.7-prerelease'), tag(name='v3.142.0-test_abcxyz') ] result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Commitizen', sub_step_implementer_name='Commitizen' ) expected_step_result.success = True expected_step_result.add_artifact(name='app-version', value='5.3.0') self.assertEqual(result, expected_step_result) finally: os.chdir(previous_dir)
def test_run_step_fail_mvn_error(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') temp_dir.write( 'pom.xml', b'''<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0</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='package', implementer='Maven', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) mvn_mock.side_effect = sh.ErrorReturnCode('mvn', b'mock out', b'mock error') result = step_implementer._run_step() expected_step_result = StepResult( step_name='package', sub_step_name='Maven', sub_step_implementer_name='Maven') mvn_output_file_path = os.path.join(work_dir_path, 'package', 'mvn_test_output.txt') expected_step_result.add_artifact( description= "Standard out and standard error from 'mvn install'.", name='maven-output', value=mvn_output_file_path) expected_step_result.success = False self.assertEqual(result.success, expected_step_result.success) self.assertRegex( result.message, re.compile( r"Package failures. See 'maven-output' report artifacts for details:" r".*RAN: mvn" r".*STDOUT:" r".*mock out" r".*STDERR:" r".*mock error", re.DOTALL)) self.assertEqual(result.artifacts, expected_step_result.artifacts)
def test_run_step_fail_sign_image(self, sign_image_mock, import_pgp_key_mock): with TempDirectory() as temp_dir: parent_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 } } workflow_result = self.setup_previous_result( parent_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', workflow_result=workflow_result, parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() import_pgp_key_mock.assert_called_once_with( pgp_private_key=step_config['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( parent_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-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_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 } 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='MavenTest', sub_step_implementer_name='MavenTest' ) 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' ) mock_gather_evidence.assert_called_once_with( step_result=Any(StepResult), test_report_dir='/mock/test-results-dir' )
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_fail_no_artifacts(self, mvn_mock): with TempDirectory() as temp_dir: artifact_id = '' version = '' package = '' 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>1.0</version> </project>''') pom_file_path = os.path.join(temp_dir.path, 'pom.xml') step_config = {'pom-file': pom_file_path} artifact_file_name = f'{artifact_id}-{version}.{package}' 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, ) mvn_mock.side_effect = TestStepImplementerMavenPackageBase.create_mvn_side_effect( pom_file_path, 'target', [artifact_file_name]) 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 = "pom resulted in 0 with expected artifact extensions (['jar', 'war', 'ear']), this is unsupported" mvn_output_file_path = os.path.join(work_dir_path, 'package', 'mvn_test_output.txt') expected_step_result.add_artifact( description= "Standard out and standard error from 'mvn install'.", name='maven-output', value=mvn_output_file_path) self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_fail_https_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 = 'https://git.ploigos.xyz/ploigos-references/ploigos-reference-app-quarkus-rest-json.git' 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} 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.success = False expected_step_result.message = 'For a https:// git url, you need to also provide username/password pair' # 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() self.assertEqual(result, expected_step_result)
def test_run_step_fail_scan(self, configlint_mock): with TempDirectory() as temp_dir: parent_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', 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 = 'Failed config-lint scan.' expected_step_result.add_artifact( name='configlint-result-set', value= f'{step_implementer.work_dir_path}/configlint_results_file.txt' ) expected_step_result.add_artifact(name='configlint-yml-path', value=file_to_validate_file_path) self.assertEqual(expected_step_result, result)
def test_run_step_fail_import_pgp_key(self, sign_image_mock, import_pgp_key_mock): with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') 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( parent_work_dir_path, artifact_config) import_pgp_key_mock.side_effect = RuntimeError( 'mock error importing pgp key') 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, parent_work_dir_path=parent_work_dir_path) result = step_implementer._run_step() import_pgp_key_mock.assert_called_once_with( pgp_private_key=step_config['signer-pgp-private-key']) expected_step_result = StepResult( step_name='sign-container-image', sub_step_name='PodmanSign', sub_step_implementer_name='PodmanSign') expected_step_result.success = False expected_step_result.message = 'mock error importing pgp key' print(expected_step_result) print(result) self.assertEqual(expected_step_result, result)
def test_fail_no_surefire_plugin(self, mock_effective_pom, mock_effective_pom_element, mock_write_working_file, mock_run_maven_step): 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') 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 sideeffects surefire_reports_dir = os.path.join(test_dir.path, 'target/surefire-reports') group_id = 'com.ploigos.app' artifact_id = 'my-app' surefire_artifact_names = [ f'{group_id}.{artifact_id}.ClassNameTest.txt', f'TEST-{group_id}.{artifact_id}.ClassNameTest.xml' ] def run_maven_side_effect(mvn_output_file_path): os.makedirs(surefire_reports_dir, exist_ok=True) for artifact_name in surefire_artifact_names: artifact_path = os.path.join(surefire_reports_dir, artifact_name) Path(artifact_path).touch() mock_run_maven_step.side_effect = run_maven_side_effect # run step actual_step_result = step_implementer._run_step() # create expected step result expected_step_result = StepResult( step_name='unit-test', sub_step_name='MavenTest', sub_step_implementer_name='MavenTest') expected_step_result.success = False expected_step_result.message = 'Unit test dependency "maven-surefire-plugin" ' \ f'missing from effective pom (mock-effective-pom.xml).' # verify step result self.assertEqual(actual_step_result, expected_step_result) mock_run_maven_step.assert_not_called()
def test_one_step_existing_results_file_empty(self): config = { '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' } } } } 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='config-1', value='config-1' ) config_expected_step_results.add_artifact( name='config-overwrite-me', value='config-1' ) config_expected_step_results.add_artifact( name='required-config-key', value='required' ) with TempDirectory() as test_dir: results_dir_path = os.path.join(test_dir.path, 'step-runner-working') results_file_path = os.path.join(results_dir_path, 'step-runner-results.pkl') test_dir.write(results_file_path, b'''''') self._run_step_implementer_test( config, 'write-config-as-results', config_expected_step_results, test_dir )