def test_tasks_ordered_having_with(self): """Testing with two task only (ordered).""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=False) pipeline.model = {'foo': [4, 5, 6]} document = [{ 'shell': { 'script': '''echo test:{{ item }}''', 'when': '', 'with': [1, 2, 3] } }, { 'shell': { 'script': '''echo test:{{ item }}''', 'when': '', 'with': "{{ model.foo }}" } }] result = tasks.process(document) output = [line for line in result['output'] if line.find("test:") >= 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(6)) assert_that(output[0], equal_to('test:1')) assert_that(output[1], equal_to('test:2')) assert_that(output[2], equal_to('test:3')) assert_that(output[3], equal_to('test:4')) assert_that(output[4], equal_to('test:5')) assert_that(output[5], equal_to('test:6'))
def test_dry_run(self): """Testing dry run mode.""" pipeline = FakePipeline() pipeline.options.dry_run = True tasks = Tasks(pipeline, parallel=True) definition = [{ 'shell': { 'script': '''echo hello1''', 'when': '' } }, { 'shell': { 'script': '''echo hello2''', 'when': '' } }] result = tasks.process(definition) output = [line for line in result['output'] if len(line.strip()) > 0] assert_that(result['success'], equal_to(True)) assert_that(len(output), equal_to(4)) assert_that(tasks.parallel, equal_to(False)) 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'''))
def test_tasks_ordered(self): """Testing with two task only (ordered).""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=False) document = [{ 'shell': { 'script': '''echo hello1''', 'when': '' } }, { 'shell': { 'script': '''echo hello2''', 'when': '' } }, { 'python': { 'script': '''print("hello3")''', 'when': '' } }] result = tasks.process(document) 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('hello1')) assert_that(output[1], equal_to('hello2')) assert_that(output[2], equal_to('hello3'))
def test_env_ordered(self): """Testing environment variables (ordered).""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=False) definition = [{ 'env': { 'message': 'hello' } }, { 'shell': { 'script': '''echo "1:{{env.message}}"''', 'when': '' } }, { 'shell': { 'script': '''echo "2:$message"''', 'when': '' } }] result = tasks.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(2)) assert_that(output[0], equal_to('1:hello')) assert_that(output[1], equal_to('2:hello'))
def test_tags_ordered(self): """Testing for filtering of tags.""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=False) definition = [{ 'shell': { 'script': '''echo hello1''', 'when': '', 'tags': ['first'] } }, { 'shell': { 'script': '''echo hello2''', 'when': '', 'tags': ['second'] } }] pipeline.options.tags = ['first'] result = tasks.process(definition) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(len(output), equal_to(1)) assert_that(output[0], equal_to('hello1')) pipeline.options.tags = ['second'] result = tasks.process(definition) output = [line for line in result['output'] if line.find("hello") >= 0] assert_that(len(output), equal_to(1)) assert_that(output[0], equal_to('hello2'))
def test_failed_two_blocks(self): """Testing cleanup when a task has failed (ordered with two blocks).""" hooks = Hooks() hooks.cleanup = '''echo cleanup hello''' pipeline = FakePipeline(hooks=hooks) tasks = Tasks(pipeline, parallel=False) definition = [{ 'shell': { 'script': '''exit 123''', 'when': '' } }, { 'shell': { 'script': '''echo hello1''', 'when': '' } }, { 'env': { 'block': 'two' } }, { 'shell': { 'script': '''echo hello2''', 'when': '' } }] result = tasks.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(1)) assert_that(output[0], equal_to('cleanup hello'))
def test_failed_parallel(self): """Testing cleanup when a task has failed (parallel).""" hooks = Hooks() hooks.cleanup = '''echo cleanup 123''' pipeline = FakePipeline(hooks=hooks) tasks = Tasks(pipeline, parallel=True) definition = [{ 'shell': { 'script': '''exit 123''', 'when': '' } }, { 'shell': { 'script': '''echo hello''', 'when': '' } }] result = tasks.process(definition) output = sorted([ line for line in result['output'] if line.find("hello") >= 0 or line.find("cleanup") >= 0 ]) assert_that(result['success'], equal_to(False)) assert_that(len(output), equal_to(2)) assert_that(output[0], equal_to('cleanup 123')) assert_that(output[1], equal_to('hello'))
def test_two_tasks_parallel(self): """Testing with two task only (parallel).""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=True) definition = [{ 'shell': { 'script': '''echo hello1''', 'when': '' } }, { 'shell': { 'script': '''echo hello2''', 'when': '' } }] result = tasks.process(definition) 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('hello2'))
def test_variables(self): """Testing variables.""" pipeline = FakePipeline() tasks = Tasks(pipeline, parallel=False) document = [{ 'shell': { 'script': '''echo hello1''', 'variable': 'hello1', 'when': '' } }, { 'shell': { 'script': '''echo {{ variables.hello1 }}''', 'when': '' } }] result = tasks.process(document) 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('hello1')) assert_that(output[1], equal_to('hello1'))