Exemplo n.º 1
0
def execute(tag, matrix_tags, pipeline, args):
    """Execute a pipeline definition."""
    LOGGER.info("Processing pipeline definition '%s'", pipeline.name)

    try:
        document = ordered_load(pipeline)
    except Exception as e:  # noqa: E722
        LOGGER.exception("Failed validation", e)
        document = None

    if TopLevel.valid(document):
        nodes = TopLevel.build(document)

        context = Context()
        context.args = args
        context.tags = tag
        if isinstance(matrix_tags, six.string_types):
            context.matrix_tags = [i.strip() for i in matrix_tags.split(',') if i != '']
        else:  # noqa: no-cover
            LOGGER.critical("Matrix tags must be a string.")
            sys.exit(3)

        for node in nodes:
            result = node.execute(context)

            if not result:
                sys.exit(1)
    else:
        sys.exit(2)
Exemplo n.º 2
0
def test_plugin_matrix_runs_with_two_elements_and_contains_tags(caplog):
    stream = StringIO('''
    - name: test1
      matrix:
        tags:
          - m1
          - m2
        tasks:
          - name: Testing task
            echo: Hello world.
    ''')
    document = loader.ordered_load(stream)

    assert_that(TopLevel.valid(document), equal_to(True))

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

        assert_that(context.variables.last(),
                    not has_entry('matrix_tag', 'm2'))
        assert_that(context.variables.last(),
                    not has_entry('matrix_list', ['m2']))

    assert_that(caplog.text, contains_string('entry: m1'))
    assert_that(caplog.text, contains_string('entry: m2'))
    assert_that(caplog.text, contains_string('Hello world.'))

    assert_that(context.variables.last(), not has_entry('matrix_tag', 'm2'))
    assert_that(context.variables.last(), not has_entry('matrix_list', ['m2']))
Exemplo n.º 3
0
def test_plugin_continue_will_continue(caplog):
    stream = StringIO('''
    - name: test1
      matrix:
        tags:
          - m1
          - m2
          - m3
        tasks:
          - name: test1
            continue:
              when:
                - 'False'

          - echo: "---{{ matrix_tag }}---"

    - echo: '---WORKS---'
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('---m1---'))
    assert_that(caplog.text, contains_string('---m2---'))
    assert_that(caplog.text, contains_string('---m3---'))
    assert_that(caplog.text, contains_string('---WORKS---'))
Exemplo n.º 4
0
def test_plugin_continue_with_stage_works(caplog):
    stream = StringIO('''
    - name: test1
      stage:
        tasks:
          - name: test1
            continue:
              when:
                - '1 == 1'

          - echo: "---FAILED---"

    - echo: '---WORKS---'
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, not contains_string('---FAILED---'))
    assert_that(caplog.text, contains_string('---WORKS---'))
Exemplo n.º 5
0
def test_plugin_echo_renders_node(caplog):
    stream = StringIO('''
    - name: test0
      echo: "{{ node }}"
    - name: test1
      echo: "{{ node }}"
    - name: test2
      echo: "{{ node }}"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for index, node in enumerate(nodes):
        node.execute(context)

        assert_that(len(caplog.records), equal_to(11))
        assert_that(caplog.records[1].message,
                    starts_with("test%d is starting" % index))
        assert_that(caplog.records[2].message,
                    starts_with("| %s" % platform.node()))
        assert_that(caplog.records[3].message,
                    starts_with("test%d has finished" % index))

        caplog.clear()
Exemplo n.º 6
0
def test_plugin_set_list_with_items(caplog):
    stream = StringIO('''
    - name: test1
      set:
        bits:
          - 1
          - 2
          - 3

    - name: test2
      echo: '--{{ item }}--'
      with_items: "{{ bits }}"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| --1--'))
    assert_that(caplog.text, contains_string('| --2--'))
    assert_that(caplog.text, contains_string('| --3--'))
Exemplo n.º 7
0
def test_plugin_matrix_runs_with_two_matrices_with_multiple_attempts(caplog):
    stream = StringIO('''
    - name: test1
      matrix:
        tags:
          - m1
          - m2
        tasks:
          - name: test2
            matrix:
              tags:
                - m3
              tasks:
                - name: Testing task
                  echo: Hello world.
                  attempts: 3
    ''')
    document = loader.ordered_load(stream)

    assert_that(TopLevel.valid(document), equal_to(True))

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('Hello world.'))
    assert_that(caplog.text, is_not(contains_string('failure')))
Exemplo n.º 8
0
def test_plugin_command_on_win_bad(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: sdfsdfdsf329909092
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string("'failure'"))
Exemplo n.º 9
0
def test_plugin_command_on_win(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: \'\Windows\System32\cmd.exe /c echo Hello world\'
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| Hello world'))
Exemplo n.º 10
0
def test_plugin_command_on_unix(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: echo Hello
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| Hello'))
Exemplo n.º 11
0
def test_plugin_shell_on_unix_bad(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: /nonexistent
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string("'failure'"))
Exemplo n.º 12
0
def test_plugin_echo_with_simple_when(caplog):
    stream = StringIO('''
    - name: test0
      echo: "{{ node }}"
      when: nbcpus == 0
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for index, node in enumerate(nodes):
        node.execute(context)

        assert_that(len(caplog.records), equal_to(2))
Exemplo n.º 13
0
def test_plugin_echo_renders_node(capenv):
    stream = StringIO('''
    - name: test0
      env:
        set:
          "Joe": "{{ node }}"
    ''')
    document = loader.ordered_load(stream)

    node = next(TopLevel.build(document))

    context = Context()

    node.execute(context)

    assert_that(os.getenv('Joe', None), equal_to(platform.node()))
Exemplo n.º 14
0
def test_plugin_command_on_unix_nonzero_return(caplog,
                                               reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: "false"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string("'failure'"))
Exemplo n.º 15
0
def test_plugin_shell_on_win(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: \'echo Hello world\'
        executable: cmd
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| Hello world'))
Exemplo n.º 16
0
def test_plugin_stage_fails_inner_task_with_context(caplog):
    stream = StringIO('''
    - name: test1
      stage:
        tasks:
          - fail: Exiting with style
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('Exiting with style'))
Exemplo n.º 17
0
def test_plugin_shell_on_win_bad_with_retry(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: sdfsdfdsf329909092
        executable: cmd
      attempts: 2
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string("'failure'"))
Exemplo n.º 18
0
def test_plugin_shell_on_unix_nonzero_return_with_retries(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: "false"
      attempts: 2
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string("'failure'"))
    assert_that(caplog.text, contains_string("Task failed all retry attempts"))
Exemplo n.º 19
0
def test_plugin_env_will_render_node(capenv, caplog):
    stream = StringIO('''
    - name: test0
      env:
        set:
          "Joe": "{{ node }}"
    - name: test case
      echo: "{{ env.Joe }}"
    ''')
    document = loader.ordered_load(stream)

    context = Context()

    for node in TopLevel.build(document):
        node.execute(context)

    assert_that(os.getenv('Joe', None), equal_to(platform.node()))
    assert_that(caplog.text, contains_string("| %s" % platform.node()))
Exemplo n.º 20
0
def test_plugin_shell_timeout_on_unix(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: "sleep 5 && echo Hello"
        executable: bash
        timeout: 0.1
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, not contains_string('| Hello'))
Exemplo n.º 21
0
def test_plugin_set_works(caplog):
    stream = StringIO('''
    - name: test1
      set:
        joe: benden

    - name: test2
      echo: '{{ joe }}'
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| benden'))
Exemplo n.º 22
0
def test_plugin_shell_silent_failure_on_unix(caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      shell:
        script: 'echo Hello >&2 && false'
        executable: bash
        silent: true
        timeout: 5.0
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('failure'))
    assert_that(caplog.text, contains_string('| Hello'))
Exemplo n.º 23
0
def test_plugin_command_with_register_result_on_unix(
        caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: echo Hello
      register: test1

    - name: result1
      echo: "--{{ test1 }}--"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('--Hello--'))
Exemplo n.º 24
0
def test_plugin_command_with_register_result_method_on_win(
        caplog, reactor):  # noqa: no-cover
    stream = StringIO('''
    - name: test1
      command: \'\Windows\System32\cmd.exe /c echo Hello world\'
      register: test1

    - name: result1
      echo: "--{{ test1.succeeded() }}--"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('--True--'))
Exemplo n.º 25
0
def test_plugin_stage_does_not_create_scope(caplog):
    stream = StringIO('''
    - name: test1
      stage:
        scope: false
        tasks:
          - set:
              joe: benden

    - name: test2
      echo: "{{ joe }}"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for node in nodes:
        node.execute(context)

    assert_that(caplog.text, contains_string('| benden'))
Exemplo n.º 26
0
def test_plugin_fail_renders_multiple_items(caplog):
    stream = StringIO('''
    - name: test0
      fail: "{{ item }}"
      with_items:
        - a-0
        - b-0
        - c-0
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for index, node in enumerate(nodes):
        node.execute(context)

    assert_that(len(caplog.records), equal_to(5))
    assert_that(caplog.text, contains_string("| a-0"))
    assert_that(caplog.text, not contains_string("| b-0"))
    assert_that(caplog.text, not contains_string("| c-0"))
Exemplo n.º 27
0
def test_plugin_matrix_renders_node(caplog):
    stream = StringIO('''
    - name: A simple test
      matrix:
        tags:
          - m1
        tasks:
          - name: test0
            echo: "{{ node }}"
          - name: test1
            echo: "{{ node }}"
          - name: test2
            echo: "{{ node }}"
    ''')
    document = loader.ordered_load(stream)

    nodes = TopLevel.build(document)

    context = Context()

    for index, node in enumerate(nodes):
        node.execute(context)

    assert_that(caplog.text, contains_string("| %s" % platform.node()))
Exemplo n.º 28
0
def test_rendering_simple_context():
    context = Context()
    fixture = """{{ platform }}"""
    subject = render(fixture, **context.variables.last())

    assert_that(subject, equal_to(sys.platform.lower()))
Exemplo n.º 29
0
def test_rendering_simple_boolean_false():
    context = Context()
    fixture = BooleanExpression("""False""")
    subject = fixture.evaluate(context)

    assert_that(subject, equal_to(False))
Exemplo n.º 30
0
def test_rendering_invalid_expression_raises():
    context = Context()
    fixture = BooleanExpression("""{{ False }}""")

    assert_that(
        calling(fixture.evaluate).with_args(context), raises(RuntimeError))