def test_root_dir_is_not_git_repo(self): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'repo-root': '/' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='generate-metadata', implementer='Git', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) result = step_implementer._run_step() expected_step_result = StepResult( step_name='generate-metadata', sub_step_name='Git', sub_step_implementer_name='Git' ) expected_step_result.success = False expected_step_result.message = 'Given directory (repo_root) is not a Git repository' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_pass(self, mvn_mock): with TempDirectory() as temp_dir: artifact_id = 'my-app' version = '1.0' package = 'war' 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> <package>war</package> </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() package_artifacts = { 'path': temp_dir.path + '/target/my-app-1.0.war', 'artifact-id': 'my-app', 'group-id': 'com.mycompany.app', 'package-type': 'war', 'pom-path': pom_file_path } expected_step_result = StepResult( step_name='package', sub_step_name='Maven', sub_step_implementer_name='Maven') expected_step_result.add_artifact(name='package-artifacts', value=[package_artifacts]) 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(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
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_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(self, upload_mock): """Testing extra_data in rekor_entry """ with TempDirectory() as temp_dir: parent_work_dir_path = os.path.join(temp_dir.path, 'working') signer_pgp_public_key_path = os.path.join( os.path.dirname(__file__), '../../helpers', 'files', 'ploigos-step-runner-tests-public.key') step_config = { 'rekor-server-url': TestStepImplementerAutomatedGovernanceRekor.TEST_REKOR_SERVER, 'signer-pgp-public-key-path': signer_pgp_public_key_path, 'signer-pgp-private-key-user': TestStepImplementerAutomatedGovernanceRekor. TEST_signer_pgp_private_key_user } step_implementer = self.create_step_implementer( step_config=step_config, parent_work_dir_path=parent_work_dir_path, ) expected_step_result = StepResult( step_name='automated-governance', sub_step_name='Rekor', sub_step_implementer_name='Rekor') expected_step_result.add_artifact( name='rekor-entry', value=TestStepImplementerAutomatedGovernanceRekor. TEST_REKOR_ENTRY) expected_step_result.add_artifact( name='rekor-uuid', value=TestStepImplementerAutomatedGovernanceRekor. TEST_REKOR_UUID) def upload_mock_side_effect(rekor_server, extra_data_file, signer_pgp_public_key_path, signer_pgp_private_key_user): return TestStepImplementerAutomatedGovernanceRekor.TEST_REKOR_ENTRY, TestStepImplementerAutomatedGovernanceRekor.TEST_REKOR_UUID upload_mock.side_effect = upload_mock_side_effect extra_data_file = os.path.join(step_implementer.work_dir_path, 'automated-governance.json') result = step_implementer._run_step() upload_mock.assert_called_once_with( rekor_server=TestStepImplementerAutomatedGovernanceRekor. TEST_REKOR_SERVER, extra_data_file=extra_data_file, signer_pgp_public_key_path=signer_pgp_public_key_path, signer_pgp_private_key_user= TestStepImplementerAutomatedGovernanceRekor. TEST_signer_pgp_private_key_user) 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_run_step_pass(self, sonar_mock): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write('sonar-project.properties', b'''testing''') properties_path = os.path.join(temp_dir.path, 'sonar-project.properties') artifact_config = { 'version': { 'description': '', 'value': '1.0-123abc' }, } workflow_result = self.setup_previous_result( work_dir_path, artifact_config) step_config = { 'properties': properties_path, 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name', 'username': '******', 'password': '******' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='static-code-analysis', implementer='SonarQube', workflow_result=workflow_result, work_dir_path=work_dir_path) result = step_implementer._run_step() expected_step_result = StepResult( step_name='static-code-analysis', sub_step_name='SonarQube', sub_step_implementer_name='SonarQube') expected_step_result.add_artifact( name='sonarqube-result-set', value=f'{temp_dir.path}/working/report-task.txt') sonar_mock.assert_called_once_with( '-Dproject.settings=' + properties_path, '-Dsonar.host.url=https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', '-Dsonar.projectVersion=1.0-123abc', '-Dsonar.projectKey=app-name:service-name', '-Dsonar.login=username', '-Dsonar.password=password', '-Dsonar.working.directory=' + work_dir_path, _env={ 'SONAR_SCANNER_OPTS': '-Djavax.net.ssl.trustStore=/etc/pki/java/cacerts' }, _out=sys.stdout, _err=sys.stderr) self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_tls_verify_false(self, mvn_mock): with TempDirectory() as temp_dir: work_dir_path = os.path.join(temp_dir.path, 'working') step_config = { 'maven-push-artifact-repo-url': 'pass', 'maven-push-artifact-repo-id': 'pass', 'tls-verify': False } # Previous (fake) results package_artifacts = [{ 'path': 'test-path', 'group-id': 'test-group-id', 'artifact-id': 'test-artifact-id', 'package-type': 'test-package-type' }] artifact_config = { 'package-artifacts': {'value': package_artifacts}, 'version': {'value': 'test-version'} } 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='push-artifacts', implementer='Maven', workflow_result=workflow_result, work_dir_path=work_dir_path ) result = step_implementer._run_step() # Expected results push_artifacts = [{ 'artifact-id': 'test-artifact-id', 'group-id': 'test-group-id', 'version': 'test-version', 'path': 'test-path', 'packaging': 'test-package-type', }] expected_step_result = StepResult( step_name='push-artifacts', sub_step_name='Maven', sub_step_implementer_name='Maven' ) expected_step_result.add_artifact(name='push-artifacts', value=push_artifacts) mvn_output_file_path = os.path.join( work_dir_path, 'push-artifacts', '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(expected_step_result.get_step_result_dict(), result.get_step_result_dict())
def test_run_step_pass_no_username_and_password(self, sonar_mock): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' work_dir_path = os.path.join(temp_dir.path, 'working') temp_dir.write('sonar-project.properties', b'''testing''') properties_path = os.path.join(temp_dir.path, 'sonar-project.properties') step_config = { 'properties': properties_path, 'url': 'https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', 'application-name': 'app-name', 'service-name': 'service-name' } step_implementer = self.create_step_implementer( step_config=step_config, step_name='static-code-analysis', implementer='SonarQube', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) artifact_config = { 'version': { 'description': '', 'value': '1.0-123abc' }, } self.setup_previous_result(work_dir_path, artifact_config) result = step_implementer._run_step() expected_step_result = StepResult( step_name='static-code-analysis', sub_step_name='SonarQube', sub_step_implementer_name='SonarQube') expected_step_result.add_artifact( name='sonarqube-result-set', value=f'{temp_dir.path}/working/report-task.txt') sonar_mock.assert_called_once_with( '-Dproject.settings=' + properties_path, '-Dsonar.host.url=https://sonarqube-sonarqube.apps.ploigos_step_runner.rht-set.com', '-Dsonar.projectVersion=1.0-123abc', '-Dsonar.projectKey=app-name:service-name', '-Dsonar.working.directory=' + work_dir_path, _out=sys.stdout, _err=sys.stderr) self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_run_step_pass(self, skopeo_mock): with TempDirectory() as temp_dir: parent_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', parent_work_dir_path=parent_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_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_no_properties(self, sonar_mock): with TempDirectory() as temp_dir: results_dir_path = os.path.join(temp_dir.path, 'step-runner-results') results_file_name = 'step-runner-results.yml' work_dir_path = os.path.join(temp_dir.path, 'working') 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', results_dir_path=results_dir_path, results_file_name=results_file_name, work_dir_path=work_dir_path, ) artifact_config = { 'version': { 'description': '', 'value': '1.0-123abc' }, } self.setup_previous_result(work_dir_path, artifact_config) result = step_implementer._run_step() expected_step_result = StepResult( step_name='static-code-analysis', sub_step_name='SonarQube', sub_step_implementer_name='SonarQube') expected_step_result.success = False expected_step_result.message = 'Properties file not found: ./sonar-project.properties' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())
def test_root_dir_is_bare_git_repo(self): with TempDirectory() as temp_dir: 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', 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 a bare Git repository' self.assertEqual(result.get_step_result_dict(), expected_step_result.get_step_result_dict())