def test_TSSCFactory_run_step_config_implementer_specfied_and_no_sub_step_config_specified_StepImplementer( ): config = {'tssc-config': {'foo': [{'implementer': 'FooStepImplementer'}]}} factory = TSSCFactory(config, 'results.yml') TSSCFactory.register_step_implementer(FooStepImplementer) factory.run_step('foo')
def test_TSSCFactory_run_step_config_specfied_StepImplementer_does_not_exist(): config = {'tssc-config': {'foo': [{'implementer': 'DoesNotExist'}]}} factory = TSSCFactory(config, 'results.yml') TSSCFactory.register_step_implementer(FooStepImplementer) with pytest.raises(TSSCException): factory.run_step('foo')
def test_one_step_writes_to_empty_results_file(): config1 = { 'tssc-config': { 'write-config-as-results': { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } } config1_expected_step_results = { 'tssc-results': { 'write-config-as-results': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } TSSCFactory.register_step_implementer(WriteConfigAsResultsStepImplementer) with TempDirectory() as test_dir: _run_step_implementer_test( config1, 'write-config-as-results', config1_expected_step_results, test_dir )
def test_one_step_existing_results_file_empty(): config = { 'tssc-config': { 'write-config-as-results': { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } } config_expected_step_results = { 'tssc-results': { 'write-config-as-results': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' }, } } TSSCFactory.register_step_implementer(WriteConfigAsResultsStepImplementer) with TempDirectory() as test_dir: results_dir_path = os.path.join(test_dir.path, 'tssc-results') results_file_path = os.path.join(results_dir_path, 'tssc-results.yml') test_dir.write(results_file_path,b'''''') _run_step_implementer_test( config, 'write-config-as-results', config_expected_step_results, test_dir )
def test_one_step_existing_results_file_missing_key(): config = { 'tssc-config': { 'write-config-as-results': { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } } TSSCFactory.register_step_implementer(WriteConfigAsResultsStepImplementer) with TempDirectory() as test_dir: results_dir_path = os.path.join(test_dir.path, 'tssc-results') results_file_path = os.path.join(results_dir_path, 'tssc-results.yml') test_dir.write(results_file_path,b'''not-expected-root-key-for-results: {}''') with pytest.raises(TSSCException): _run_step_implementer_test( config, 'write-config-as-results', None, test_dir )
def test_TSSCFactory_run_step_config_only_sub_step_and_is_dict_rather_then_array( ): config = {'tssc-config': {'foo': {'implementer': 'FooStepImplementer'}}} factory = TSSCFactory(config, 'results.yml') TSSCFactory.register_step_implementer(FooStepImplementer) factory.run_step('foo')
def test_TSSCFactory_run_step_default_StepImplementer_for_step_without_config( ): config = {'tssc-config': {}} factory = TSSCFactory(config, 'results.yml') TSSCFactory.register_step_implementer(FooStepImplementer, True) factory.run_step('foo')
def test_TSSCFactory_run_step_no_default_StepImplementer_for_step_without_config( ): config = {'tssc-config': {}} factory = TSSCFactory(config, 'results.yml') TSSCFactory.register_step_implementer(FooStepImplementer) with pytest.raises(TSSCException): factory.run_step('foo')
def test_merge_results_from_running_same_step_twice_with_different_config(): config1 = { 'tssc-config': { 'write-config-as-results': { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } } config1_expected_step_results = { 'tssc-results': { 'write-config-as-results': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } } } config2 = { 'tssc-config': { 'write-config-as-results': { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-2': 'config-2', 'config-overwrite-me': 'config-2' } } } } config2_expected_step_results = { 'tssc-results': { 'write-config-as-results': { 'config-1': "config-1", 'config-2': 'config-2', 'config-overwrite-me': 'config-2' } } } TSSCFactory.register_step_implementer(WriteConfigAsResultsStepImplementer) with TempDirectory() as test_dir: _run_step_implementer_test( config1, 'write-config-as-results', config1_expected_step_results, test_dir ) _run_step_implementer_test( config2, 'write-config-as-results', config2_expected_step_results, test_dir )
def test_required_step_config_pass_via_config_file(): TSSCFactory.register_step_implementer(RequiredStepConfigStepImplementer, True) _run_main_test(['--step', 'required-step-config-test'], None, '''--- tssc-config: required-step-config-test: implementer: RequiredStepConfigStepImplementer config: required-config-key: "hello world" ''' )
def test_required_step_config_pass_via_runtime_arg_missing(): TSSCFactory.register_step_implementer(RequiredRuntimeStepConfigStepImplementer, True) _run_main_test( [ '--step', 'required-runtime-step-config-test', '--step-config', 'wrong-config="hello world"' ], 200, '''--- tssc-config: {} ''' )
def test_required_step_config_pass_via_runtime_arg_valid(): TSSCFactory.register_step_implementer(RequiredRuntimeStepConfigStepImplementer, True) _run_main_test( [ '--step', 'required-runtime-step-config-test', '--step-config', 'required-rutnime-config-key="hello world"' ], None, '''--- tssc-config: {} ''' )
def test_merge_results_from_two_sub_steps(): config = { 'tssc-config': { 'write-config-as-results': [ { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-1': "config-1", 'config-overwrite-me': 'config-1' } }, { 'implementer': 'WriteConfigAsResultsStepImplementer', 'config': { 'config-2': 'config-2', 'config-overwrite-me': 'config-2' } } ] } } config_expected_step_results = { 'tssc-results': { 'write-config-as-results': { 'config-1': "config-1", 'config-2': 'config-2', 'config-overwrite-me': 'config-2' } } } TSSCFactory.register_step_implementer(WriteConfigAsResultsStepImplementer) with TempDirectory() as test_dir: _run_step_implementer_test( config, 'write-config-as-results', config_expected_step_results, test_dir )
class SonarQube(StepImplementer): """ StepImplementer for the tag-source step for SonarQube. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.LINTING_STATIC_CODE_ANALYSIS def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = {} return results # register step implementer TSSCFactory.register_step_implementer(SonarQube)
except sh.ErrorReturnCode: # pylint: disable=undefined-variable raise RuntimeError('Issue invoking buildah bud with given image ' 'specification file (' + image_spec_file + ')') results = {'image_tag': tag} if 'image_tar_file' in runtime_step_config and runtime_step_config[ 'image_tar_file']: image_tar_file = runtime_step_config['image_tar_file'] rm_f = sh.rm.bake("-f") # pylint: disable=no-member print(rm_f(image_tar_file)) buildah_push = sh.buildah.bake("push") # pylint: disable=no-member try: print( buildah_push(tag, "docker-archive:" + image_tar_file, _out=sys.stdout)) except sh.ErrorReturnCode: # pylint: disable=undefined-variable raise RuntimeError('Issue invoking buildah push to tar file ' + image_tar_file) results.update({'image_tar_file': image_tar_file}) return results # register step implementer TSSCFactory.register_step_implementer(Buildah, True)
version = "latest" if(self.get_step_results('generate-metadata') and \ self.get_step_results('generate-metadata').get('image-tag')): version = self.get_step_results('generate-metadata')['image-tag'] else: print('No version found in metadata. Using latest') destination_with_version = (runtime_step_config['destination'] + ':' + version).lower() skopeo_copy = sh.skopeo.bake("copy") # pylint: disable=no-member try: print( skopeo_copy('--src-tls-verify=' + runtime_step_config['src-tls-verify'], '--dest-tls-verify=' + runtime_step_config['dest-tls-verify'], runtime_step_config['source'], destination_with_version, _out=sys.stdout)) except sh.ErrorReturnCode: # pylint: disable=undefined-variable raise RuntimeError('Error invoking skopeo') results = {'image_tag': destination_with_version} return results # register step implementer TSSCFactory.register_step_implementer(Skopeo, True)
class Git(StepImplementer): """ StepImplementer for the tag-source step for Git. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.TAG_SOURCE def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = { } return results # register step implementer TSSCFactory.register_step_implementer(Git, True)
class NPM(StepImplementer): """ StepImplementer for the package step for NPM. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.PACKAGE def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = {} return results # register step implementer TSSCFactory.register_step_implementer(NPM)
def test_required_step_config_missing(): TSSCFactory.register_step_implementer(RequiredStepConfigStepImplementer, True) _run_main_test(['--step', 'required-step-config-test'], 200, '''{"tssc-config":{}}''' )
pre_release = current_step_results['pre-release'] else: raise ValueError( """No value for (pre_release) provided via runtime flag (pre-release) or from prior step implementer ({0}) """.format(self.step_name())) if 'build' in runtime_step_config: build = runtime_step_config['build'] elif 'build' in current_step_results: build = current_step_results['build'] else: raise ValueError("""No value for (build) provided via runtime flag (build) or from prior step implementer ({0}) """.format(self.step_name())) if pre_release == release_branch: version = "{0}+{1}".format(app_version, build) image_tag = "{0}-{1}".format(app_version, build) else: version = "{0}-{1}+{2}".format(app_version, pre_release, build) image_tag = "{0}-{1}-{2}".format(app_version, pre_release, build) results = {'version': version, 'image-tag': image_tag} return results # register step implementer TSSCFactory.register_step_implementer(SemanticVersion)
class Selenium(StepImplementer): """ StepImplementer for the canary-test step for Selenium. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.CANARY_TEST def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = {} return results # register step implementer TSSCFactory.register_step_implementer(Selenium)
class JUnit(StepImplementer): """ StepImplementer for the unit-test step for JUnit. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.UNIT_TEST def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = { } return results # register step implementer TSSCFactory.register_step_implementer(JUnit)
# verify runtime config if not os.path.exists(pom_file): raise ValueError('Given pom file does not exist: ' + pom_file) # parse the pom file and figure out the namespace if there is one pom_xml = ElementTree.parse(pom_file) pom_root = pom_xml.getroot() pom_namespace_match = re.match(r'\{.*}', str(pom_root.tag)) pom_namespace = '' if pom_namespace_match: pom_namespace = pom_namespace_match.group(0) # extract needed information from the pom file pom_version_element = pom_xml.getroot().find('./' + pom_namespace + 'version') # verify information from pom file if pom_version_element is None: raise ValueError('Given pom file (' + pom_file + ') does not have ./version element') pom_version = pom_version_element.text results = {'app-version': pom_version} return results # register step implementer TSSCFactory.register_step_implementer(Maven)
def test_config_file_valid_yaml(): TSSCFactory.register_step_implementer(FooStepImplementer, True) _run_main_test(['--step', 'foo'], None, '''tssc-config: {}''' )
class OpenSCAP(StepImplementer): """ StepImplementer for the container-image-static-vulnerability-scan step for OpenSCAP. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.CONTAINER_IMAGE_STATIC_VULNERABILITY_SCAN def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = { } return results # register step implementer TSSCFactory.register_step_implementer(OpenSCAP)
class Cucumber(StepImplementer): """ StepImplementer for the cucumber step for Cucumber. """ def __init__(self, config, results_dir, results_file_name): super().__init__(config, results_dir, results_file_name, DEFAULT_ARGS) @classmethod def step_name(cls): return DefaultSteps.UAT def _validate_step_config(self, step_config): """ Function for implementers to override to do custom step config validation. Parameters ---------- step_config : dict Step configuration to validate. """ def _run_step(self, runtime_step_config): results = { } return results # register step implementer TSSCFactory.register_step_implementer(Cucumber)