def test_git_repo_with_single_commit_on_master_branch(self):
        with TempDirectory() as temp_dir:
            repo = Repo.init(str(temp_dir.path))

            create_git_commit_with_sample_file(temp_dir, repo)

            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git'
                    }
                }
            }

            git_branch_last_commit_hash = str(repo.head.reference.commit)
            expected_step_results = {
                'tssc-results': {
                    'generate-metadata': {
                        'pre-release': 'master',
                        'build': git_branch_last_commit_hash[:7]
                    }
                }
            }

            run_step_test_with_result_validation(
                temp_dir,
                'generate-metadata',
                config,
                expected_step_results,
                runtime_args={'repo-root': str(temp_dir.path)})
    def test_package_file_missing(self):
        with TempDirectory() as temp_dir:
            package_file_path = 'does_not_exist.json'
            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Npm',
                        'config': {
                            'package-file': str(package_file_path)
                        }
                    }
                }
            }

            expected_step_results = {}

            with self.assertRaisesRegex(
                    ValueError,
                    r"Given npm package file does not exist: does_not_exist.json"
            ):
                run_step_test_with_result_validation(temp_dir,
                                                     'generate-metadata',
                                                     config,
                                                     expected_step_results,
                                                     runtime_args={
                                                         'repo-root':
                                                         str(temp_dir.path),
                                                         'build':
                                                         '1234'
                                                     })
Example #3
0
 def test_push_artifact_with_version_missing_from_results(self, mvn_mock):
     with TempDirectory() as temp_dir:
         temp_dir.makedir('tssc-results')
         temp_dir.write('tssc-results/tssc-results.yml', b'''tssc-results:
           generate-metadata:
             BADversion: 1.0-123abc
           ''')
         config = {
             'tssc-config': {
                 'push-artifacts': {
                     'implementer': 'Maven',
                     'config': {
                         'maven-push-artifact-repo-id': 'id',
                         'maven-push-artifact-repo-url': 'repo',
                     }
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {}
         with self.assertRaisesRegex(
                 ValueError,
                 'generate-metadata results missing version'):
             run_step_test_with_result_validation(temp_dir, 'push-artifacts',
                                                  config, expected_step_results, runtime_args)
    def test_package_file(self):
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'package.json', b'''{
              "name": "my-awesome-package",
              "version": "1.0.0"
            }''')
            package_file_path = os.path.join(temp_dir.path, 'package.json')
            results_dir_path = os.path.join(temp_dir.path, 'tssc-resutls')
            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Npm',
                        'config': {
                            'package-file': str(package_file_path)
                        }
                    }
                }
            }
            expected_step_results = {
                'tssc-results': {
                    'generate-metadata': {
                        'app-version': '1.0.0'
                    }
                }
            }

            run_step_test_with_result_validation(temp_dir, 'generate-metadata',
                                                 config, expected_step_results)
 def test_sonarqube_missing_properties_file(self, sonar_mock):
     with TempDirectory() as temp_dir:
         tssc_results = '''tssc-results:
             generate-metadata:
                 version: 1.0-123abc
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         config = {
             'tssc-config': {
                 'global-defaults': {
                     'application-name': 'tssc',
                     'service-name': 'tssc-reference-testcase'
                 },
                 'static-code-analysis': {
                     'implementer': 'SonarQube',
                     'config': {
                         'url':
                         'https://sonarqube-sonarqube.apps.tssc.rht-set.com',
                         'properties': 'missingfile'
                     }
                 }
             }
         }
         expected_step_results = {}
         with self.assertRaisesRegex(
                 ValueError, r'Properties file in tssc config not found.*'):
             run_step_test_with_result_validation(temp_dir,
                                                  'static-code-analysis',
                                                  config,
                                                  expected_step_results)
    def test_package_file_missing_version(self):
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'package.json', b'''{
              "name": "my-awesome-package"
            }''')
            package_file_path = os.path.join(temp_dir.path, 'package.json')
            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Npm',
                        'config': {
                            'package-file': str(package_file_path)
                        }
                    }
                }
            }

            expected_step_results = {}

            with self.assertRaisesRegex(
                    ValueError, r"Given npm package file: " +
                    package_file_path + " does not contain a \"version\" key"):
                run_step_test_with_result_validation(temp_dir,
                                                     'generate-metadata',
                                                     config,
                                                     expected_step_results,
                                                     runtime_args={
                                                         'repo-root':
                                                         str(temp_dir.path),
                                                         'build':
                                                         '1234'
                                                     })
    def test_no_repo_root(self):
        with TempDirectory() as temp_dir:
            repo = Repo.init(str(temp_dir.path))

            # create commit
            create_git_commit_with_sample_file(temp_dir, repo)

            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git',
                        'config': {
                            'repo-root': None
                        }
                    }
                }
            }

            git_branch_last_commit_hash = str(repo.head.reference.commit)
            expected_step_results = {}

            with self.assertRaisesRegex(
                    AssertionError,
                    r"The runtime step configuration \(\{'repo-root': None, 'build-string-length': 7\}\) is missing the required configuration keys \(\['repo-root'\]\)"
            ):
                run_step_test_with_result_validation(temp_dir,
                                                     'generate-metadata',
                                                     config,
                                                     expected_step_results)
    def test_pom_file_valid_old_empty_jar(self, mvn_mock):
        artifact_id = 'my-app'
        version = '42.1'
        package = 'jar'
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'pom.xml',
                bytes(
                    '''<project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany.app</groupId>
        <artifactId>{artifact_id}</artifactId>
        <version>{version}</version>
    </project>'''.format(artifact_id=artifact_id, version=version), 'utf-8'))
            pom_file_path = os.path.join(temp_dir.path, 'pom.xml')
            config = {
                'tssc-config': {
                    'package': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path)
                        }
                    }
                }
            }
            artifact_file_name = '{artifact_id}-{version}.{package}'.format(
                artifact_id=artifact_id, version=version, package=package)
            expected_step_results = {
                'tssc-results': {
                    'package': {
                        'artifacts': [{
                            'path':
                            os.path.join(temp_dir.path, 'target',
                                         artifact_file_name),
                            'artifact-id':
                            artifact_id,
                            'group-id':
                            'com.mycompany.app',
                            'package-type':
                            package,
                            'pom-path':
                            str(pom_file_path)
                        }]
                    }
                }
            }

            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, 'target', [artifact_file_name])
            run_step_test_with_result_validation(temp_dir, 'package', config,
                                                 expected_step_results)
            settings_file_path = temp_dir.path + "/tssc-working/package/settings.xml"
            mvn_mock.assert_called_once_with('clean',
                                             'install',
                                             '-f',
                                             pom_file_path,
                                             '-s',
                                             settings_file_path,
                                             _out=Any(IOBase),
                                             _err=Any(IOBase))
 def test_sonarqube_missing_url(self, sonar_mock):
     with TempDirectory() as temp_dir:
         tssc_results = '''tssc-results:
             generate-metadata:
                 version: 1.0-123abc
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         sonar_properties = '''
             used to test existence of file
         '''
         temp_dir.write('sonar-project.properties',
                        sonar_properties.encode())
         properties = os.path.join(temp_dir.path,
                                   'sonar-project.properties')
         config = {
             'tssc-config': {
                 'global-defaults': {
                     'application-name': 'tssc',
                     'service-name': 'tssc-reference-testcase'
                 },
                 'static-code-analysis': {
                     'implementer': 'SonarQube',
                     'config': {
                         'properties': properties
                     }
                 }
             }
         }
         expected_step_results = {}
         with self.assertRaisesRegex(AssertionError, r'is missing.*url*'):
             run_step_test_with_result_validation(temp_dir,
                                                  'static-code-analysis',
                                                  config,
                                                  expected_step_results)
    def test_directory_is_detached_head(self):
        with TempDirectory() as temp_dir:
            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^')

            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git'
                    }
                }
            }
            expected_step_results = {}

            with self.assertRaisesRegex(
                    ValueError,
                    r"Expected a Git branch in given directory \(.*\) but has a detached head."
            ):
                run_step_test_with_result_validation(
                    temp_dir,
                    'generate-metadata',
                    config,
                    expected_step_results,
                    runtime_args={'repo-root': str(temp_dir.path)})
    def test_sonar_missing_user_and_password(self, sonar_mock):
        with TempDirectory() as temp_dir:
            tssc_results = '''tssc-results:
                generate-metadata:
                    version: 1.0-123abc
            '''
            temp_dir.write('tssc-results/tssc-results.yml',
                           tssc_results.encode())
            sonar_properties = '''
                used to test existence of file
            '''
            temp_dir.write('sonar-project.properties',
                           sonar_properties.encode())
            properties = os.path.join(temp_dir.path,
                                      'sonar-project.properties')
            config = {
                'tssc-config': {
                    'global-defaults': {
                        'application-name': 'tssc',
                        'service-name': 'tssc-reference-testcase'
                    },
                    'static-code-analysis': {
                        'implementer': 'SonarQube',
                        'config': {
                            'url':
                            'https://sonarqube-sonarqube.apps.tssc.rht-set.com',
                            'properties': properties
                        }
                    }
                }
            }
            expected_step_results = {
                'tssc-results': {
                    'generate-metadata': {
                        'version': '1.0-123abc'
                    },
                    'static-code-analysis': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'sonarqube step completed - see report-artifacts',
                        },
                        'report-artifacts': [{
                            'name':
                            'sonarqube result set',
                            'path':
                            f'file://{temp_dir.path}' +
                            '/tssc-working/static-code-analysis/report-task.txt'
                        }]
                    }
                }
            }

            run_step_test_with_result_validation(temp_dir,
                                                 'static-code-analysis',
                                                 config, expected_step_results)
 def test_configlint_from_argocd_missing_name(self, configlint_mock):
     with TempDirectory() as temp_dir:
         yml_file = '''
            empty file
         '''
         temp_dir.write('file.yml', yml_file.encode())
         yml_path = str(os.path.join(temp_dir.path, 'file.yml'))
         file_yml_path = f'file://{yml_path}'
         tssc_results = '''tssc-results:
             deploy:
                'report-artifacts': [
                    {
                        'name': 'badargocd-result-set',
                        'path':''' + file_yml_path + '''
                    }
                ]
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         config = {
             'tssc-config': {
                 'validate-environment-configuration': {
                     'implementer': 'ConfiglintFromArgocd',
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {
             'tssc-results': {
                 'deploy': {
                     'report-artifacts': [{
                         'name': 'argocd-result-set',
                         'path': file_yml_path
                     }]
                 },
                 'validate-environment-configuration': {
                     'result': {
                         'success': True,
                         'message': 'configlint prep step completed'
                     },
                     'options': {
                         'yml_path': yml_path
                     }
                 }
             }
         }
         with self.assertRaisesRegex(
                 ValueError,
                 r'Deploy results missing yml element name=argocd-result-set'
         ):
             run_step_test_with_result_validation(
                 temp_dir, 'validate-environment-configuration', config,
                 expected_step_results, runtime_args)
Example #13
0
    def test_uat_mvn_error_return_code(self, _mvn_mock):
        """Test when maven returns an error code."""
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'src/main/java/com/mycompany/app/App.java',
                b'''package com.mycompany.app;
                    public class App {
                        public static void main( String[] args ) {
                            System.out.println( "Hello World!" );
                        }
                    }
                ''')
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': 'Failure message'
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error invoking mvn:.*'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)
Example #14
0
 def test_configlint_ok(self, configlint_mock):
     with TempDirectory() as temp_dir:
         yml_file = ''
         temp_dir.write('file.yml', yml_file.encode())
         yml_path = str(os.path.join(temp_dir.path, 'file.yml'))
         tssc_results = '''tssc-results:
             validate-environment-configuration:
                     'options':
                         {
                             'yml_path':''' + yml_path + '''
                         }
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         configlint_rules = ''
         temp_dir.write('config-lint.rules', configlint_rules.encode())
         rules = os.path.join(temp_dir.path, 'config-lint.rules')
         config = {
             'tssc-config': {
                 'validate-environment-configuration': {
                     'implementer': 'Configlint',
                     'config': {
                         'rules': rules
                     }
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {
             'tssc-results': {
                 'validate-environment-configuration': {
                     'result': {
                         'success': True,
                         'message': 'configlint step completed'
                     },
                     'options': {
                         'yml_path': yml_path
                     },
                     'report-artifacts': [{
                         'name':
                         'configlint-result-set',
                         'path':
                         f'file://{temp_dir.path}/tssc-working/validate-environment-configuration/configlint_results_file.txt'
                     }]
                 }
             }
         }
         run_step_test_with_result_validation(
             temp_dir, 'validate-environment-configuration', config,
             expected_step_results, runtime_args)
Example #15
0
    def test_configlint_missing_options_and_runtime_and_steps(
            self, configlint_mock):
        with TempDirectory() as temp_dir:
            yml_file = ''
            temp_dir.write('file.yml', yml_file.encode())
            yml_path = str(os.path.join(temp_dir.path, 'file.yml'))
            tssc_results = '''tssc-results:
                validate-environment-configuration:
                    'badoptions':
                            {
                                'yml_path':''' + yml_path + '''
                            }
            '''
            temp_dir.write('tssc-results/tssc-results.yml',
                           tssc_results.encode())
            configlint_rules = ''
            temp_dir.write('config-lint.rules', configlint_rules.encode())
            rules = os.path.join(temp_dir.path, 'config-lint.rules')
            config = {
                'tssc-config': {
                    'validate-environment-configuration': {
                        'implementer': 'Configlint',
                        'config': {
                            'rules': rules
                        }
                    }
                }
            }
            runtime_args = {}
            expected_step_results = {
                'tssc-results': {
                    'validate-environment-configuration': {
                        'result': {
                            'success': True,
                            'message': 'configlint step completed'
                        },
                        'options': {
                            'yml_path': yml_path
                        },
                        'report-artifacts': []
                    }
                }
            }

            with self.assertRaisesRegex(
                    ValueError,
                    r'yml_path not specified in runtime args or in options'):
                run_step_test_with_result_validation(
                    temp_dir, 'validate-environment-configuration', config,
                    expected_step_results, runtime_args)
Example #16
0
 def test_configlint_bad_sh_call(self, configlint_mock):
     with TempDirectory() as temp_dir:
         yml_file = ''
         temp_dir.write('file.yml', yml_file.encode())
         yml_path = str(os.path.join(temp_dir.path, 'file.yml'))
         tssc_results = '''tssc-results:
             validate-environment-configuration:
                     'options':
                         {
                             'yml_path':''' + yml_path + '''
                         }
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         configlint_rules = ''
         temp_dir.write('config-lint.rules', configlint_rules.encode())
         rules = os.path.join(temp_dir.path, 'config-lint.rules')
         config = {
             'tssc-config': {
                 'validate-environment-configuration': {
                     'implementer': 'Configlint',
                     'config': {
                         'rules': rules
                     }
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {
             'tssc-results': {
                 'validate-environment-configuration': {
                     'result': {
                         'success': True,
                         'message': 'configlint step completed'
                     },
                     'options': {
                         'yml_path': yml_path
                     },
                     'report-artifacts': []
                 }
             }
         }
         sh.config_lint.side_effect = sh.ErrorReturnCode(
             'config_lint', b'mock stdout', b'mock error')
         with self.assertRaisesRegex(RuntimeError,
                                     r'Error invoking config-lint: .*'):
             run_step_test_with_result_validation(
                 temp_dir, 'validate-environment-configuration', config,
                 expected_step_results, runtime_args)
Example #17
0
 def test_push_artifact_with_repo_id_missing_from_config(self, mvn_mock):
     with TempDirectory() as temp_dir:
         config = {
             'tssc-config': {
                 'push-artifacts': {
                     'implementer': 'Maven'
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {}
         with self.assertRaisesRegex(
                 AssertionError,
                 'The .* configuration .* is missing .*maven-push-artifact-repo-id.*'):
             run_step_test_with_result_validation(temp_dir, 'push-artifacts',
                                                  config, expected_step_results, runtime_args)
Example #18
0
    def test_uat_run_attempt_fails_fail_on_no_tests_flag_true(self, mvn_mock):
        """Test when failure on no tests with flag to ignore it set to true."""
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'fail-on-no-tests': True,
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, reports_dir, [])
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': "Failure message"
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error: No uat defined'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)
    def test_root_dir_is_not_git_repo(self):
        with TempDirectory() as temp_dir:
            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git'
                    }
                }
            }
            expected_step_results = {}

            with self.assertRaises(InvalidGitRepositoryError):
                run_step_test_with_result_validation(
                    temp_dir,
                    'generate-metadata',
                    config,
                    expected_step_results,
                    runtime_args={'repo-root': str(temp_dir.path)})
    def test_sonar_missing_user(self, sonar_mock):
        with TempDirectory() as temp_dir:
            tssc_results = '''tssc-results:
                generate-metadata:
                    version: 1.0-123abc
            '''
            temp_dir.write('tssc-results/tssc-results.yml',
                           tssc_results.encode())
            sonar_properties = '''
                used to test existence of file
            '''
            temp_dir.write('sonar-project.properties',
                           sonar_properties.encode())
            properties = os.path.join(temp_dir.path,
                                      'sonar-project.properties')
            config = {
                'tssc-config': {
                    'global-defaults': {
                        'application-name': 'tssc',
                        'service-name': 'tssc-reference-testcase'
                    },
                    'static-code-analysis': {
                        'implementer': 'SonarQube',
                        'config': {
                            'url':
                            'https://sonarqube-sonarqube.apps.tssc.rht-set.com',
                            'properties': properties
                        }
                    }
                }
            }
            runtime_args = {'password': '******'}
            expected_step_results = {}

            with self.assertRaisesRegex(
                    AssertionError,
                    r'Either username or password is not set. Neither or both must be set.'
            ):
                run_step_test_with_result_validation(temp_dir,
                                                     'static-code-analysis',
                                                     config,
                                                     expected_step_results,
                                                     runtime_args)
 def test_sonar_bad_sonar_scanner_results(self, sonar_mock):
     with TempDirectory() as temp_dir:
         tssc_results = '''tssc-results:
             generate-metadata:
                 version: 1.0-123abc
         '''
         temp_dir.write('tssc-results/tssc-results.yml',
                        tssc_results.encode())
         sonar_properties = '''
             used to test existence of file
         '''
         temp_dir.write('sonar-project.properties',
                        sonar_properties.encode())
         properties = os.path.join(temp_dir.path,
                                   'sonar-project.properties')
         config = {
             'tssc-config': {
                 'global-defaults': {
                     'application-name': 'tssc',
                     'service-name': 'tssc-reference-testcase'
                 },
                 'static-code-analysis': {
                     'implementer': 'SonarQube',
                     'config': {
                         'url':
                         'https://sonarqube-sonarqube.apps.tssc.rht-set.com',
                         'properties': properties
                     }
                 }
             }
         }
         expected_step_results = {}
         sh.sonar_scanner.side_effect = sh.ErrorReturnCode(
             'sonar_scanner', b'mock stdout', b'mock error')
         with self.assertRaisesRegex(RuntimeError,
                                     'Error invoking sonarscanner'):
             run_step_test_with_result_validation(temp_dir,
                                                  'static-code-analysis',
                                                  config,
                                                  expected_step_results)
Example #22
0
 def test_push_artifact_with_artifacts_missing_from_results(self, mvn_mock):
     with TempDirectory() as temp_dir:
         temp_dir.makedir('tssc-results')
         temp_dir.write('tssc-results/tssc-results.yml', b'''tssc-results:
           generate-metadata:
             version: 1.0-123abc
           ''')
         config = {
            'tssc-config': {
                'global-defaults': {
                    'maven-servers': [
                        {'id': 'ID1', 'username': '******', 'password': '******'},
                        {'id': 'ID2'}
                    ],
                    'maven-repositories': [
                        {'id': 'ID1', 'url': 'URL1', 'snapshots': 'true', 'releases': 'false'},
                        {'id': 'ID2', 'url': 'URL2'}
                    ],
                    'maven-mirrors': [
                        {'id': 'ID1', 'url': 'URL1', 'mirror-of': '*'},
                        {'id': 'ID2', 'url': 'URL2', 'mirror-of': '*'}
                    ],
                },
                'push-artifacts': {
                     'implementer': 'Maven',
                     'config': {
                         'maven-push-artifact-repo-id': 'repoid',
                         'maven-push-artifact-repo-url': 'repourl'
                     }
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {}
         with self.assertRaisesRegex(
                 ValueError,
                 'package results missing artifacts'):
             run_step_test_with_result_validation(temp_dir, 'push-artifacts',
                                                  config, expected_step_results, runtime_args)
Example #23
0
    def test_configlint_using_runtime(self, configlint_mock):
        with TempDirectory() as temp_dir:
            yml_file = ''
            temp_dir.write('file.yml', yml_file.encode())
            yml_path: str = os.path.join(temp_dir.path, 'file.yml')
            tssc_results = '''tssc-results:
            '''
            temp_dir.write('tssc-results/tssc-results.yml',
                           tssc_results.encode())
            configlint_rules = ''
            temp_dir.write('config-lint.rules', configlint_rules.encode())
            rules = os.path.join(temp_dir.path, 'config-lint.rules')
            config = {
                'tssc-config': {
                    'validate-environment-configuration': {
                        'implementer': 'Configlint',
                        'config': {
                            'rules': rules
                        }
                    }
                }
            }
            runtime_args = {'yml_path': yml_path}
            expected_step_results = {
                'tssc-results': {
                    'validate-environment-configuration': {
                        'result': {
                            'success': True,
                            'message': 'configlint step completed'
                        },
                        'report-artifacts': []
                    }
                }
            }

            with self.assertRaisesRegex(AttributeError, r'.*.'):
                run_step_test_with_result_validation(
                    temp_dir, 'validate-environment-configuration', config,
                    expected_step_results, runtime_args)
    def test_root_dir_is_bare_git_repo(self):
        with TempDirectory() as temp_dir:
            repo = Repo.init(str(temp_dir.path), bare=True)

            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git'
                    }
                }
            }
            expected_step_results = {}

            with self.assertRaisesRegex(
                    ValueError,
                    r"Given directory \(.*\) is a bare Git repository"):
                run_step_test_with_result_validation(
                    temp_dir,
                    'generate-metadata',
                    config,
                    expected_step_results,
                    runtime_args={'repo-root': str(temp_dir.path)})
    def test_no_commit_history(self):
        with TempDirectory() as temp_dir:
            repo = Repo.init(str(temp_dir.path))

            config = {
                'tssc-config': {
                    'generate-metadata': {
                        'implementer': 'Git'
                    }
                }
            }
            expected_step_results = {}

            with self.assertRaisesRegex(
                    ValueError,
                    r"Given directory \(.*\) is a Git branch \(master\) with no commit history"
            ):
                run_step_test_with_result_validation(
                    temp_dir,
                    'generate-metadata',
                    config,
                    expected_step_results,
                    runtime_args={'repo-root': str(temp_dir.path)})
Example #26
0
    def test_push_artifact_with_artifacts_results_bad(self, mock_mvn):

        with TempDirectory() as temp_dir:
            temp_dir.makedir('target')
            temp_dir.write('target/my-app-1.0-SNAPSHOT.jar', b'''sandbox''')
            jar_file_path = str(os.path.join(temp_dir.path, 'target/my-app-1.0-SNAPSHOT.jar'))
            tssc_results = '''tssc-results:
                generate-metadata:
                    version: 1.0-123abc
                package:
                    'artifacts': [{
                        'path':''' + jar_file_path + ''',
                        'artifact-id': 'my-app',
                        'group-id': 'com.mycompany.app',
                        'package-type': 'jar',
                        'pom-path': 'pom.xml'
                    }]
            '''
            temp_dir.write('tssc-results/tssc-results.yml', tssc_results.encode())
            runtime_args = {}
            config = {
                'tssc-config': {
                    'global-defaults' : {
                        'maven-servers': [
                            {'id': 'ID1', 'username': '******', 'password': '******'},
                            {'id': 'ID2'}
                        ],
                        'maven-repositories': [
                            {'id': 'ID1', 'url': 'URL1', 'snapshots': 'true', 'releases': 'false'},
                            {'id': 'ID2', 'url': 'URL2'}
                        ],
                        'maven-mirrors': [
                            {'id': 'ID1', 'url': 'URL1', 'mirror-of': '*'},
                            {'id': 'ID2', 'url': 'URL2', 'mirror-of': '*'}
                        ],
                    },
                    'push-artifacts': {
                        'implementer': 'Maven',
                        'config': {
                            'maven-push-artifact-repo-id': 'repoid',
                            'maven-push-artifact-repo-url': 'repourl',
                        }
                    }
                 }
            }
            expected_step_results = {
                'tssc-results':
                    {
                        'generate-metadata':
                        {
                            'version': '1.0-123abc'
                        },
                        'package':
                        {
                            'artifacts':
                            [
                                {
                                    'artifact-id': 'my-app',
                                    'group-id': 'com.mycompany.app',
                                    'package-type': 'jar',
                                    'path': jar_file_path,
                                    'pom-path': 'pom.xml'
                                }
                            ]
                        },
                        'push-artifacts':
                        {
                            'result': {
                                'success': True,
                                'message': 'push artifacts step completed - see report-artifacts',
                            },
                            'report-artifacts':
                            [
                                {
                                    'artifact-id': 'my-app',
                                    'group-id': 'com.mycompany.app',
                                    'path': jar_file_path,
                                    'version': '1.0-123abc'
                                }
                            ]
                        }
                    }
            }
            expected_step_results = {}
            sh.mvn.side_effect = sh.ErrorReturnCode('mvn', b'mock stdout', b'mock error')
            with self.assertRaisesRegex(
                    RuntimeError,
                    'Error invoking mvn'):
                run_step_test_with_result_validation(temp_dir, 'push-artifacts',
                                                     config, expected_step_results, runtime_args)
Example #27
0
 def test_push_artifact_with_artifacts_results_multi_missing_id(self, mvn_mock):
     with TempDirectory() as temp_dir:
         temp_dir.makedir('target')
         temp_dir.write('target/my-app-1.0-SNAPSHOT.jar', b'''sandbox''')
         jar_file_path = str(os.path.join(temp_dir.path, 'target/my-app-1.0-SNAPSHOT.jar'))
         tssc_results = '''tssc-results:
             generate-metadata:
                 version: 1.0-123abc
             package:
                 'artifacts': [{
                     'path':''' + jar_file_path + ''',
                     'artifact-id': 'my-app',
                     'group-id': 'com.mycompany.app',
                     'package-type': 'jar',
                     'pom-path': 'pom.xml'
                 },
                 {
                     'path':''' + jar_file_path + ''',
                     'artifact-id': 'my-app',
                     'group-id': 'com.mycompany.app',
                     'package-type': 'jar',
                     'pom-path': 'pom.xml'
                 }]
         '''
         temp_dir.write('tssc-results/tssc-results.yml', tssc_results.encode())
         config = {
             'tssc-config': {
                 'push-artifacts': {
                     'implementer': 'Maven',
                     'config': {
                         'maven-push-artifact-repo-id': 'repoid',
                         'maven-push-artifact-repo-url': 'repourl',
                         'maven-servers': [
                             {'username': '******', 'password': '******'},
                         ],
                     }
                 }
             }
         }
         runtime_args = {}
         expected_step_results = {
             'tssc-results':
                 {
                     'generate-metadata':
                         {
                             'version': '1.0-123abc'
                         },
                     'package':
                         {
                             'artifacts':
                                 [
                                     {
                                         'artifact-id': 'my-app',
                                         'group-id': 'com.mycompany.app',
                                         'package-type': 'jar',
                                         'path': jar_file_path,
                                         'pom-path': 'pom.xml'
                                     },
                                     {
                                         'artifact-id': 'my-app',
                                         'group-id': 'com.mycompany.app',
                                         'package-type': 'jar',
                                         'path': jar_file_path,
                                         'pom-path': 'pom.xml'
                                     }
                                 ]
                         },
                     'push-artifacts':
                         {
                             'result': {
                                 'success': True,
                                 'message': 'push artifacts step completed - see report-artifacts',
                             },
                             'report-artifacts':
                                 [
                                     {
                                         'artifact-id': 'my-app',
                                         'group-id': 'com.mycompany.app',
                                         'path': jar_file_path,
                                         'version': '1.0-123abc'
                                     },
                                     {
                                         'artifact-id': 'my-app',
                                         'group-id': 'com.mycompany.app',
                                         'path': jar_file_path,
                                         'version': '1.0-123abc'
                                     }
                                 ]
                         }
                 }
         }
         with self.assertRaisesRegex(
                 ValueError,
                 'id is required for maven_servers.'):
             run_step_test_with_result_validation(temp_dir, 'push-artifacts',
                                                  config, expected_step_results, runtime_args)
    def test_mvn_quickstart_no_jar(self, mvn_mock):
        artifact_id = 'my-app'
        version = '1.0'
        package = 'jar'
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'src/main/java/com/mycompany/app/App.java',
                b'''package com.mycompany.app;
    public class App {
        public static void main( String[] args ) {
            System.out.println( "Hello World!" );
        }
    }''')
            temp_dir.write(
                'pom.xml',
                bytes(
                    '''<project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany.app</groupId>
        <artifactId>{artifact_id}</artifactId>
        <version>{version}</version>
    </project>'''.format(artifact_id=artifact_id, version=version), 'utf-8'))
            pom_file_path = os.path.join(temp_dir.path, 'pom.xml')
            config = {
                'tssc-config': {
                    'package': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path)
                        }
                    }
                }
            }
            artifact_file_name = '{artifact_id}-{version}.{package}'.format(
                artifact_id=artifact_id, version=version, package=package)
            expected_step_results = {
                'tssc-results': {
                    'package': {
                        'artifacts': [{
                            'path':
                            os.path.join(temp_dir.path, 'target',
                                         artifact_file_name),
                            'artifact-id':
                            'my-app',
                            'group-id':
                            'com.mycompany.app'
                        }]
                    }
                }
            }

            # NOTE:
            # sort of hacking this test by passing in no artifacts to cause the no artifacts error message
            # because can't figure out how to create a pom that doesn't generate any artifacts
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, 'target', [])

            with self.assertRaisesRegex(
                    ValueError,
                    'pom resulted in 0 with expected artifact extensions (.*), this is unsupported'
            ):
                run_step_test_with_result_validation(temp_dir, 'package',
                                                     config,
                                                     expected_step_results)
    def test_mvn_error_return_code(self, mvn_mock):
        artifact_id = 'my-app'
        version = '1.0'
        package = 'jar'
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'src/main/java/com/mycompany/app/App.java',
                b'''package com.mycompany.app;
    public class App {
        public static void main( String[] args ) {
            System.out.println( "Hello World!" );
        }
    }''')
            temp_dir.write(
                'pom.xml',
                bytes(
                    '''<project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany.app</groupId>
        <artifactId>{artifact_id}</artifactId>
        <version>{version}</version>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    </project>'''.format(artifact_id=artifact_id, version=version), 'utf-8'))
            pom_file_path = os.path.join(temp_dir.path, 'pom.xml')
            config = {
                'tssc-config': {
                    'package': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path)
                        }
                    }
                }
            }
            artifact_file_name = '{artifact_id}-{version}.{package}'.format(
                artifact_id=artifact_id, version=version, package=package)
            expected_step_results = {
                'tssc-results': {
                    'package': {
                        'artifacts': [{
                            'path':
                            os.path.join(temp_dir.path, 'target',
                                         artifact_file_name),
                            'artifact-id':
                            artifact_id,
                            'group-id':
                            'com.mycompany.app',
                            'pom-path':
                            str(pom_file_path),
                            'package-type':
                            'jar'
                        }]
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error invoking mvn:.*'):
                run_step_test_with_result_validation(temp_dir, 'package',
                                                     config,
                                                     expected_step_results)
    def test_mvn_quickstart_single_jar(self, mvn_mock):
        artifact_id = 'my-app'
        version = '1.0'
        package = 'jar'
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'src/main/java/com/mycompany/app/App.java',
                b'''package com.mycompany.app;
    public class App {
        public static void main( String[] args ) {
            System.out.println( "Hello World!" );
        }
    }''')
            temp_dir.write(
                'pom.xml',
                bytes(
                    '''<project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany.app</groupId>
        <artifactId>{artifact_id}</artifactId>
        <version>{version}</version>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    </project>'''.format(artifact_id=artifact_id, version=version), 'utf-8'))
            pom_file_path = os.path.join(temp_dir.path, 'pom.xml')
            config = {
                'tssc-config': {
                    'package': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path)
                        }
                    }
                }
            }
            artifact_file_name = '{artifact_id}-{version}.{package}'.format(
                artifact_id=artifact_id, version=version, package=package)
            expected_step_results = {
                'tssc-results': {
                    'package': {
                        'artifacts': [{
                            'path':
                            os.path.join(temp_dir.path, 'target',
                                         artifact_file_name),
                            'artifact-id':
                            artifact_id,
                            'group-id':
                            'com.mycompany.app',
                            'package-type':
                            package,
                            'pom-path':
                            str(pom_file_path)
                        }]
                    }
                }
            }

            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, 'target', [artifact_file_name])
            run_step_test_with_result_validation(temp_dir, 'package', config,
                                                 expected_step_results)
            settings_file_path = temp_dir.path + "/tssc-working/package/settings.xml"
            mvn_mock.assert_called_once_with('clean',
                                             'install',
                                             '-f',
                                             pom_file_path,
                                             '-s',
                                             settings_file_path,
                                             _out=Any(IOBase),
                                             _err=Any(IOBase))