Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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_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)
Example #5
0
    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_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_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)
Example #8
0
    def test__run_step_pass_with_evidence(self, generate_evidence_mock):
        with TempDirectory() as temp_dir:
            parent_work_dir_path = os.path.join(temp_dir.path, 'working')
            step_config = {
                'organization': 'test-ORG',
                'application-name': 'test-APP',
                'service-name': 'test-SERVICE',
                'version': '42.0-test'
            }
            step_implementer = self.create_step_implementer(
                step_config=step_config,
                parent_work_dir_path=parent_work_dir_path)

            step_result = step_implementer._run_step()
            expected_step_result = StepResult(
                step_name='generate_evidence',
                sub_step_name='GenerateEvidence',
                sub_step_implementer_name='GenerateEvidence')

            expected_step_result.message = 'Evidence successfully packaged ' \
            'in JSON file but was not uploaded to data store (no '\
            'destination URI specified).'

            expected_step_result.add_artifact(
                name='evidence-path',
                value=os.path.join(
                    parent_work_dir_path, 'generate_evidence',
                    "test-ORG-test-APP-test-SERVICE-42.0-test-evidence.json"),
                description='File path of evidence.')

            print(str(step_result) + "\n\n" + str(expected_step_result))

            self.assertEqual(step_result, expected_step_result)

            generate_evidence_mock.assert_called_once()
Example #9
0
    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)
Example #10
0
    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()
Example #11
0
    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())
Example #12
0
    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())
Example #13
0
    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_run_step_fail_missing_version_in_package_file(self):
        with TempDirectory() as temp_dir:
            parent_work_dir_path = os.path.join(temp_dir.path, 'working')
            temp_dir.write(
                'package.json', b'''{
              "name": "my-awesome-package"
            }''')
            package_file_path = os.path.join(temp_dir.path, 'package.json')

            step_config = {'package-file': package_file_path}

            step_implementer = self.create_step_implementer(
                step_config=step_config,
                step_name='generate-metadata',
                implementer='Npm',
                parent_work_dir_path=parent_work_dir_path,
            )

            result = step_implementer._run_step()

            expected_step_result = StepResult(
                step_name='generate-metadata',
                sub_step_name='Npm',
                sub_step_implementer_name='Npm',
            )
            expected_step_result.success = False
            expected_step_result.message = f"Given npm package file ({package_file_path})" + \
                ' does not contain a \"version\" key.'

            self.assertEqual(result, expected_step_result)
    def test_run_step_fail_missing_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)
Example #16
0
    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)
Example #17
0
    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)
Example #18
0
    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_uri='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'
        )
Example #19
0
    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()
Example #20
0
    def test__upload_to_remote_with_auth(self, gather_evidence_mock,
                                         upload_file_mock):
        with TempDirectory() as temp_dir:

            parent_work_dir_path = os.path.join(temp_dir.path, 'working')

            step_config = {
                'organization': 'test-ORG',
                'application-name': 'test-APP',
                'service-name': 'test-SERVICE',
                'version': '42.0-test',
                'evidence-destination-url': self.DEST_URL,
                'evidence-destination-username': '******',
                'evidence-destination-password': '******'
            }
            step_implementer = self.create_step_implementer(
                step_config=step_config,
                parent_work_dir_path=parent_work_dir_path)

            # mock the upload results
            upload_file_mock.return_value = "mock upload results"

            # run the step
            step_result = step_implementer._run_step()

            # verify results
            expected_step_result = StepResult(
                step_name='generate_evidence',
                sub_step_name='GenerateEvidence',
                sub_step_implementer_name='GenerateEvidence')
            expected_step_result.add_artifact(
                name='evidence-upload-results',
                description='Results of uploading the evidence JSON file ' \
                            'to the given destination.',
                value='mock upload results'
                )
            expected_step_result.add_artifact(
                name='evidence-uri',
                description='URI of the uploaded results archive.',
                value=self.DEST_URI)
            expected_step_result.add_artifact(
                name='evidence-path',
                value=os.path.join(
                    parent_work_dir_path, 'generate_evidence',
                    "test-ORG-test-APP-test-SERVICE-42.0-test-evidence.json"),
                description='File path of evidence.')
            expected_step_result.message = "Evidence successfully packaged in JSON file and uploaded to data store."

            self.assertEqual(step_result, expected_step_result)

            # verify mocks called
            gather_evidence_mock.assert_called_once()
            upload_file_mock.assert_called_once_with(
                file_path=mock.ANY,
                destination_uri=self.DEST_URI,
                username='******',
                password='******')
    def test_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)
Example #22
0
    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'
            )
Example #23
0
    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='******')
Example #24
0
    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())
Example #25
0
    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)
Example #27
0
    def test_success_with_report_dir_deployed_host_urls_list_multiple_entries(
            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',
                    'https://mock.ploigos.org/mock-app-2'
                ]
            }
            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='MavenIntegrationTest',
                sub_step_implementer_name='MavenIntegrationTest')
            expected_step_result.message = \
                f"Given more then one deployed host URL ({step_config['deployed-host-urls']})," \
                f" targeting first one (https://mock.ploigos.org/mock-app-1) for user acceptance test (UAT)."
            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_dir='/mock/test-results-dir')
    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)
Example #29
0
    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()
Example #30
0
    def test_fail_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,
                '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')
            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='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')
            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_not_called()