def test_environment_variables(self): """Testing of a simple valid pipeline with environment variables.""" definition = [{ 'env': { 'message': 'pipeline hello' } }, { 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo $message''', 'when': '' } }, { 'shell': { 'script': '''echo tasks1:hello''', 'when': '' } }] }] }] hooks = Hooks() hooks.cleanup = '''echo cleanup hello''' pipeline = Pipeline(options=ApplicationOptions(definition='fake.yaml')) pipeline.hooks = hooks result = pipeline.process(definition) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(3)) assert_that(output[0], equal_to('pipeline hello')) assert_that(output[1], equal_to('tasks1:hello')) assert_that(output[2], equal_to('cleanup hello'))
def test_simple_failed_pipeline(self): """Testing of a simple failed pipeline.""" definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo tasks1:hello1''', 'when': '' } }, { 'shell': { 'script': '''exit 123''', 'when': '' } }, { 'shell': { 'script': '''echo tasks1:hello3''', 'when': '' } }] }] }] hooks = Hooks() hooks.cleanup = '''echo cleanup hello''' pipeline = Pipeline(options=ApplicationOptions(definition='fake.yaml')) pipeline.hooks = hooks result = pipeline.process(definition) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(result['success'], equal_to(False)) assert_that(len(output), equal_to(2)) assert_that(output[0], equal_to('tasks1:hello1')) assert_that(output[1], equal_to('cleanup hello')) assert_that(pipeline.hooks, equal_to(hooks))
def test_failed_parallel(self): """Testing failed parallel.""" matrix_definition = [{ 'name': 'one', 'env': { 'message': 'hello1' } }, { 'name': 'two', 'env': { 'message': 'hello2' } }] pipeline_definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''exit 123''', 'when': '' } }] }] }] process_data = MatrixProcessData() process_data.pipeline = pipeline_definition process_data.options = ApplicationOptions(definition='fake.yaml') matrix = Matrix(matrix_definition, parallel=True) result = matrix.process(process_data) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(result['success'], equal_to(False)) assert_that(len(output), equal_to(0))
def test_matrix_worker(self): """Testing worker for matrix used in multiprocessing.""" pipeline_definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo $message''', 'when': '' } }] }] }] result = matrix_worker({ 'matrix': { 'name': 'one', 'env': { 'message': 'hello1' } }, 'pipeline': pipeline_definition, 'model': {}, 'options': ApplicationOptions(definition='fake.yaml'), 'hooks': None }) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(1))
def test_run_matrix(self): """Testing method Application.run_matrix.""" mdef = [{ 'name': 'test1', 'env': { 'message': 'hello 1' } }, { 'name': 'test2', 'env': { 'message': 'hello 2' } }] pdef = { 'pipeline': [{ 'stage(Test)': [{ 'tasks': [{ 'shell': { 'script': 'echo "{{ env.message }}"', 'when': '' } }] }] }] } application = Application(ApplicationOptions(definition='fake.yaml')) result = application.run_matrix(mdef, pdef) assert_that(result['success'], equal_to(True)) output = [line for line in result['output'] if line.find('hello') >= 0] assert_that(len(output), equal_to(2)) assert_that(output[0], equal_to('hello 1')) assert_that(output[1], equal_to('hello 2'))
def test_simple_with_one_entry(self): """Testing simple matrix with one entry.""" matrix_definition = [{'name': 'one', 'env': {'message': 'hello'}}] pipeline_definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo tasks1:hello1''', 'when': '' } }, { 'shell': { 'script': '''echo tasks1:hello2''', 'when': '' } }] }] }] process_data = MatrixProcessData() process_data.pipeline = pipeline_definition process_data.options = ApplicationOptions(definition='fake.yaml') matrix = Matrix(matrix_definition, parallel=False) result = matrix.process(process_data) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(2)) assert_that(output[0], equal_to('tasks1:hello1')) assert_that(output[1], equal_to('tasks1:hello2'))
def test_missing_mandatory(self): """Testing missing mandatory parameter.""" try: ApplicationOptions() self.assertFalse("RuntimeError expected") except RuntimeError as exception: assert_that(str(exception), equal_to("Missing keys: 'definition'"))
def test_simple(self): """Testing application for a very simple example.""" filename = os.path.join(os.path.dirname(__file__), 'data/simple.yaml') options = ApplicationOptions(definition=filename) application = Application(options) output = application.run(options.definition) assert_that(len(output), equal_to(1)) assert_that(output[0], equal_to("hello world!"))
def test_invalidate_document(self): """Testing invalid document.""" application = Application( ApplicationOptions(definition='data/invalid.yaml')) with patch('sys.exit') as mocked_exit: path = os.path.dirname(__file__) application.validate_document( os.path.join(path, "data/invalid.yaml")) mocked_exit.assert_called_once_with(1)
def test_report(self, item): """Testing missing mandatory parameter.""" try: options = {'definition': 'fake.yml'} options.update({'report': item[0]}) ApplicationOptions(**options) if not item[1]: self.assertFalse("RuntimeError expected") except RuntimeError as exception: if item[1]: self.assertFalse("Unexpected exception %s" % exception)
def test_validate_document(self): """Testing valid document.""" application = Application( ApplicationOptions(definition='data/simple.yaml')) application.validate_only = True with patch('sys.exit') as mocked_exit: path = os.path.dirname(__file__) document = application.validate_document( os.path.join(path, "data/simple.yaml")) mocked_exit.assert_not_called() assert_that(isinstance(document, dict), equal_to(True))
def test_init(self): """Testing c'tor only.""" tags = 'prepare,build' matrix_tags = "py27,py35" options = ApplicationOptions(definition='fake.yaml', matrix_tags=matrix_tags, tags=tags) application = Application(options) assert_that(application.options.tags, equal_to(['prepare', 'build'])) assert_that(application.options.matrix_tags, equal_to(['py27', 'py35'])) assert_that(application.options.validate_only, equal_to(False)) assert_that(application.options.logging_config, equal_to(''))
def test_minimal_valid(self): """Testing to provide mandatory parameters only.""" options = ApplicationOptions(definition='fake.yml') assert_that(options.definition, equal_to('fake.yml')) assert_that(options.matrix_tags, equal_to([])) assert_that(options.tags, equal_to([])) assert_that(options.logging_config, equal_to('')) assert_that(options.event_logging, equal_to(False)) assert_that(options.validate_only, equal_to(False)) assert_that(options.dry_run, equal_to(False)) assert_that(options.debug, equal_to(False)) assert_that(options.strict, equal_to(False)) assert_that(options.report, equal_to('off')) assert_that(options.temporary_scripts_path, equal_to(''))
def test_run_collector(self): """Test create, run and stop of collector.""" # we have to disable the spawning of the process otherwise # the coverage won't work ... with patch("spline.application.Collector.start") as mocked_start: collector = Application.create_and_run_collector( document={}, options=ApplicationOptions(definition='fake.html', report='html')) assert_that(collector.is_alive(), equal_to(False)) mocked_start.assert_called_once() with patch("logging.Logger.info") as mocked_logging: collector.queue.put(None) collector.run() mocked_logging.assert_called_once_with( "Stopping collector process ...")
def test_with_tags_and_filter_parallel(self): """Testing simple matrix with tags and filtering.""" matrix_definition = [{ 'name': 'one', 'env': { 'message': 'hello1' }, 'tags': ['group-a'] }, { 'name': 'two', 'env': { 'message': 'hello2' }, 'tags': ['group-b'] }, { 'name': 'three', 'env': { 'message': 'hello3' }, 'tags': ['group-a'] }] pipeline_definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo $message''', 'when': '' } }] }] }] process_data = MatrixProcessData() process_data.pipeline = pipeline_definition process_data.options = ApplicationOptions(definition='fake.yaml', matrix_tags='group-a') matrix = Matrix(matrix_definition, parallel=True) result = matrix.process(process_data) output = sorted( [line for line in result['output'] if line.find("hello") >= 0]) assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(2)) assert_that(output[0], equal_to('hello1')) assert_that(output[1], equal_to('hello3'))
def test_dry_run(self): """Testing simple matrix with tags and filtering.""" matrix_definition = [{ 'name': 'one', 'env': { 'message': 'hello1' } }, { 'name': 'two', 'env': { 'message': 'hello2' } }, { 'name': 'three', 'env': { 'message': 'hello3' } }] pipeline_definition = [{ 'stage(test)': [{ 'tasks': [{ 'shell': { 'script': '''echo {{ env.message }}''', 'when': '' } }] }] }] process_data = MatrixProcessData() process_data.pipeline = pipeline_definition process_data.options = ApplicationOptions(definition='fake.yaml', dry_run=True) matrix = Matrix(matrix_definition, parallel=True) result = matrix.process(process_data) output = [line for line in result['output'] if len(line) > 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(6)) assert_that(output[0], equal_to('#!/bin/bash')) assert_that(output[1], equal_to('echo hello1')) assert_that(output[2], equal_to('#!/bin/bash')) assert_that(output[3], equal_to('echo hello2')) assert_that(output[4], equal_to('#!/bin/bash')) assert_that(output[5], equal_to('echo hello3'))
def main(**kwargs): """The Pipeline tool.""" options = ApplicationOptions(**kwargs) Event.configure(is_logging_enabled=options.event_logging) application = Application(options) application.run(options.definition)
def __init__(self, hooks=None): """Initialization of fake pipeline.""" self.data = PipelineData(hooks) self.model = {} self.options = ApplicationOptions(definition='fake.yaml') self.variables = {}