def test_pom_file_missing_version(self): with TempDirectory() as temp_dir: 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') config = { 'tssc-config': { 'generate-metadata': { 'implementer': 'Maven', 'config': { 'pom-file': str(pom_file_path) } } } } factory = TSSCFactory(config) with self.assertRaisesRegex(ValueError, r"Given pom file does not exist:"): factory.run_step('generate-metadata')
def test_unit_test_missing_surefire_plugin_in_pom(self): group_id = 'com.mycompany.app' artifact_id = 'my-app' version = '1.0' with TempDirectory() as temp_dir: temp_dir.write( 'pom.xml', bytes( '''<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>{group_id}</groupId> <artifactId>{artifact_id}</artifactId> <version>{version}</version> </project>'''.format(group_id=group_id, artifact_id=artifact_id, version=version), 'utf-8')) pom_file_path = os.path.join(temp_dir.path, 'pom.xml') config = { 'tssc-config': { 'unit-test': { 'implementer': 'Maven', 'config': { 'pom-file': str(pom_file_path) } } } } factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, 'Unit test dependency "maven-surefire-plugin" missing from POM.' ): factory.run_step('unit-test')
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 run_step_test_with_result_validation(self, temp_dir, step_name, config, expected_step_results, runtime_args=None, environment=None, expected_stdout=None, expected_stderr=None): results_dir_path = os.path.join(temp_dir.path, 'tssc-results') working_dir_path = os.path.join(temp_dir.path, 'tssc-working') factory = TSSCFactory(config, results_dir_path, work_dir_path=working_dir_path) if runtime_args: factory.config.set_step_config_overrides(step_name, runtime_args) out = StringIO() err = StringIO() with redirect_stdout(out), redirect_stderr(err): factory.run_step(step_name, environment) if expected_stdout is not None: self.assertRegex(out.getvalue(), expected_stdout) if expected_stderr is not None: self.assertRegex(err.getvalue(), expected_stderr) results_file_name = "tssc-results.yml" with open(os.path.join(results_dir_path, results_file_name), 'r') as step_results_file: actual_step_results = yaml.safe_load(step_results_file.read()) self.assertEqual(actual_step_results, expected_step_results)
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_mvn_quickstart_single_jar_java_error(self, mvn_mock): with TempDirectory() as temp_dir: temp_dir.write( 'src/main/java/com/mycompany/app/App.java', b'''package com.mycompany.app; public class Fail { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }''') 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') config = { 'tssc-config': { 'package': { 'implementer': 'Maven', 'config': { 'pom-file': str(pom_file_path) } } } } factory = TSSCFactory(config) with self.assertRaisesRegex(RuntimeError, 'Error invoking mvn:.*'): factory.run_step('package')
def test_mvn_quickstart_single_jar_no_pom(): 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!" ); } }''') pom_file_path = os.path.join(temp_dir.path, 'pom.xml') config = { 'tssc-config': { 'package': { 'implementer': 'Maven', } } } expected_step_results = { 'tssc-results': { 'package': { 'artifacts': { 'my-app-1.0-SNAPSHOT.jar': os.path.join(temp_dir.path, 'target', 'my-app-1.0-SNAPSHOT.jar') } } } } factory = TSSCFactory(config) with pytest.raises(ValueError): factory.run_step('package')
def _run_step_implementer_test(config, step, expected_step_results, test_dir): results_dir_path = os.path.join(test_dir.path, 'tssc-results') factory = TSSCFactory(config, results_dir_path) factory.run_step(step) with open(os.path.join(results_dir_path, "tssc-results.yml"), 'r') as step_results_file: step_results = yaml.safe_load(step_results_file.read()) assert step_results == expected_step_results
def test_uat_mandatory_selenium_hub_url_missing(self, _mvn_mock): """Test when mandatory selenium hub url is missing.""" config = {'tssc-config': {'uat': {'implementer': 'Maven'}}} factory = TSSCFactory(config) error_message = '.* is missing the required configuration keys ' \ '\\(\\[\'selenium-hub-url\'\\]\\).*' with self.assertRaisesRegex(AssertionError, error_message): factory.run_step('uat')
def test_unit_test_runtime_pom_file_missing(self, mvn_mock): config = {'tssc-config': {'unit-test': {'implementer': 'Maven'}}} factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, "Given pom file does not exist: does-not-exist-pom.xml"): factory.config.set_step_config_overrides( 'unit-test', {'pom-file': 'does-not-exist-pom.xml'}) factory.run_step('unit-test')
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_uat_mandatory_target_base_url_missing(self, _mvn_mock): """Test when mandatory target base url is missing.""" config = {'tssc-config': {'uat': {'implementer': 'Maven'}}} factory = TSSCFactory(config) with self.assertRaisesRegex(ValueError, 'No target base url was specified'): factory.config.set_step_config_overrides( 'uat', { 'selenium-hub-url': SELENIUM_HUB_URL, }) factory.run_step('uat')
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_run_step_config_only_sub_step_and_is_dict_rather_then_array(self): config = { 'tssc-config': { 'foo': { 'implementer': 'tests.helpers.sample_step_implementers.FooStepImplementer' } } } factory = TSSCFactory(config, 'results.yml') factory.run_step('foo')
def test_runtime_pom_file_missing (): config = { 'tssc-config': { 'package': { 'implementer': 'Maven' } } } factory = TSSCFactory(config) with pytest.raises(ValueError): factory.run_step('package', {'pom-file': 'does-not-exist-pom.xml'})
def test_run_step_with_no_config(self): config = { 'tssc-config': { } } factory = TSSCFactory(config, 'results.yml') with self.assertRaisesRegex( AssertionError, r"Can not run step \(foo\) because no step configuration provided."): factory.run_step('foo')
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_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_run_step_config_implementer_specfied_and_no_sub_step_config_specified_StepImplementer(self): config = { 'tssc-config': { 'foo': [ { 'implementer': 'tests.helpers.sample_step_implementers.FooStepImplementer' } ] } } factory = TSSCFactory(config, 'results.yml') factory.run_step('foo')
def test_config_file_pom_file_none_value(): config = { 'tssc-config': { 'generate-metadata': { 'implementer': 'Maven', 'config': { 'pom-file': None } } } } factory = TSSCFactory(config) with pytest.raises(ValueError): factory.run_step('generate-metadata')
def test_config_file_pom_file_missing(): config = { 'tssc-config': { 'generate-metadata': { 'implementer': 'Maven', 'config': { 'pom-file': 'does-not-exist.pom' } } } } factory = TSSCFactory(config) with pytest.raises(ValueError): factory.run_step('generate-metadata')
def test_uat_runtime_pom_file_missing(self, _mvn_mock): """Test when pom file is invalid.""" config = {'tssc-config': {'uat': {'implementer': 'Maven'}}} factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, 'Given pom file does not exist: does-not-exist-pom.xml'): factory.config.set_step_config_overrides( 'uat', { 'pom-file': 'does-not-exist-pom.xml', 'selenium-hub-url': SELENIUM_HUB_URL, 'target-base-url': TARGET_BASE_URL }) factory.run_step('uat')
def test__get_step_implementer_name_class_is_not_subclass_of_StepImplementer(self): with self.assertRaisesRegex( TSSCException, re.compile( "Step \(foo\) is configured to use step implementer " "\(tests.helpers.sample_step_implementers.NotSubClassOfStepImplementer\) " "from module \(tests.helpers.sample_step_implementers\) with class name " "\(NotSubClassOfStepImplementer\), and dynamically loads as class " "\(<class 'tests.helpers.sample_step_implementers.NotSubClassOfStepImplementer'>\) " "which is not a subclass of required parent class " "\(<class 'tssc.step_implementer.StepImplementer'>\)." ) ): TSSCFactory._TSSCFactory__get_step_implementer_class('foo', 'tests.helpers.sample_step_implementers.NotSubClassOfStepImplementer')
def _run_step_implementer_test(self, config, step, expected_step_results, test_dir, environment=None): results_dir_path = os.path.join(test_dir.path, 'tssc-results') factory = TSSCFactory(config, results_dir_path) factory.run_step(step_name=step, environment=environment) with open(os.path.join(results_dir_path, "tssc-results.yml"), 'r') as step_results_file: step_results = yaml.safe_load(step_results_file.read()) self.assertEqual(step_results, expected_step_results)
def test_unit_test_config_file_pom_file_missing(self, mvn_mock): config = { 'tssc-config': { 'unit-test': { 'implementer': 'Maven', 'config': { 'pom-file': 'does-not-exist.pom' } } } } factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, 'Given pom file does not exist: does-not-exist.pom'): factory.run_step('unit-test')
def test_runtime_pom_file_missing(self): config = { 'tssc-config': { 'generate-metadata': { 'implementer': 'Maven' } } } factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, r"Given pom file does not exist: does-not-exist-pom.xml"): factory.config.set_step_config_overrides( 'generate-metadata', {'pom-file': 'does-not-exist-pom.xml'}) factory.run_step('generate-metadata')
def test_config_file_pom_file_missing(self): config = { 'tssc-config': { 'generate-metadata': { 'implementer': 'Maven', 'config': { 'pom-file': 'does-not-exist.pom' } } } } factory = TSSCFactory(config) with self.assertRaisesRegex( ValueError, r"Given pom file does not exist: does-not-exist.pom"): factory.run_step('generate-metadata')
def test_run_step_config_specfied_StepImplementer_does_not_exist(self): config = { 'tssc-config': { 'foo': [ { 'implementer': 'DoesNotExist' } ] } } factory = TSSCFactory(config, 'results.yml') with self.assertRaisesRegex( TSSCException, r"Could not dynamically load step \(foo\) step implementer \(DoesNotExist\) from module \(tssc.step_implementers.foo\) with class name \(DoesNotExist\)"): factory.run_step('foo')
def test__get_step_implementer_class_exists_include_module(self): self.assertIsNotNone( TSSCFactory._TSSCFactory__get_step_implementer_class( 'foo', 'tests.helpers.sample_step_implementers.FooStepImplementer' ) )