def test_pipeline_runner_main_with_context_with_failure():
    """Run main with context with failure argument as expected."""
    expected_notify_output = ['sg3', 'fh']
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        with pytest.raises(ValueError) as err:
            pipelinerunner.main_with_context(
                pipeline_name='tests/pipelines/api/main-all',
                groups=['sg3'],
                failure_group='fh')

    assert str(err.value) == "err from sg3"
    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
def test_pipeline_runner_main_with_context_with_failure_handled():
    """Run main with context with failure argument as expected."""
    expected_notify_output = ['sg3', 'on_failure']
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        out = pipelinerunner.main_with_context(
            pipeline_name='tests/pipelines/api/main-all',
            groups=['sg3'],
            failure_group='on_failure')

    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
    assert out.pipeline_name == 'tests/pipelines/api/main-all'
    assert out.working_dir == Path.cwd()

    assert len(out) == 2
    assert out['py'] == "raise ValueError('err from sg3')"

    assert len(out['runErrors']) == 1
    out_run_error = out['runErrors'][0]
    assert out_run_error
    assert out_run_error['col'] == 5
    assert out_run_error['customError'] == {}
    assert out_run_error['description'] == 'err from sg3'
    assert repr(out_run_error['exception']) == repr(ValueError('err from sg3'))
    assert out_run_error['line'] == 50
    assert out_run_error['name'] == 'ValueError'
    assert out_run_error['step'] == 'pypyr.steps.py'
    assert out_run_error['swallowed'] is False

    # somewhat arbitrary check if behaves like Context()
    with pytest.raises(KeyNotInContextError) as err:
        out.assert_key_has_value('set_in_pipe', 'arbcaller')

    assert str(err.value) == ("context['set_in_pipe'] doesn't exist. It must "
                              "exist for arbcaller.")
def test_pipeline_runner_main_with_context_minimal_with_failure_handled():
    """Run main with context minimal with failure argument as expected."""
    expected_notify_output = ['steps', 'on_success', 'on_failure']
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        out = pipelinerunner.main_with_context(
            pipeline_name='tests/pipelines/api/main-all',
            dict_in={'argList': ['A', 'B', 'C', 'raise on success']})

    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
    assert out.pipeline_name == 'tests/pipelines/api/main-all'
    assert out.working_dir == Path.cwd()

    assert len(out) == 4
    assert out['argList'] == ['A', 'B', 'C', 'raise on success']
    assert out['set_in_pipe'] == 456
    assert out['py'] == "raise ValueError('err from on_success')"

    assert len(out['runErrors']) == 1
    out_run_error = out['runErrors'][0]
    assert out_run_error
    assert out_run_error['col'] == 5
    assert out_run_error['customError'] == {}
    assert out_run_error['description'] == 'err from on_success'
    assert repr(out_run_error['exception']) == repr(
        ValueError('err from on_success'))
    assert out_run_error['line'] == 74
    assert out_run_error['name'] == 'ValueError'
    assert out_run_error['step'] == 'pypyr.steps.py'
    assert out_run_error['swallowed'] is False
    # somewhat arbitrary check if behaves like Context()
    out.assert_key_has_value('set_in_pipe', 'caller')
def test_pipeline_runner_main_with_context_all_with_failure(
        pipeline_cache_reset):
    """Run main with context - all arguments as expected with runtime error."""
    expected_notify_output = ['sg2', 'success_handler', 'fh']
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        with pytest.raises(ValueError) as err:
            pipelinerunner.main_with_context(
                pipeline_name='pipelines/api/main-all',
                dict_in={'argList': ['A', 'B', 'C', 'raise on sh']},
                working_dir=working_dir_tests,
                groups=['sg2'],
                success_group='sh',
                failure_group='fh',
                loader='arbpack.naivefileloader')

    assert str(err.value) == "err from sh"
    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
Esempio n. 5
0
def test_foreach_enumerables():
    """Builtin generators, on the fly generators, lists & dicts in foreach."""
    context = pipelinerunner.main_with_context(
        pipeline_name='pipelines/loops/foreach',
        working_dir=working_dir_tests)

    assert context['just_list'] == [0, 1, 2]
    assert context['just_dict'] == ['a', 'c']
    assert context['just_dict_items'] == [('a', 'b'), ('c', 'd')]
    assert context['generator_out'] == [4, 5, 6]
    assert context['product_out'] == [('A', 0), ('A', 1),
                                      ('B', 0), ('B', 1)]
def test_pipeline_runner_main_with_context_minimal():
    """Run main with context with minimal arguments as expected."""
    # Not having argList==None proves context_parser didn't run.
    expected_notify_output = ['steps', 'argList not exist', 'on_success']

    # working_dir will default to repo root rather than test root
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        out = pipelinerunner.main_with_context('tests/pipelines/api/main-all')

    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
    assert out.pipeline_name == 'tests/pipelines/api/main-all'
    assert out.working_dir == Path.cwd()
    assert out == {'set_in_pipe': 456}
    # somewhat arbitrary check if behaves like Context()
    out.assert_key_has_value('set_in_pipe', 'caller')
def test_pipeline_runner_main_with_context_all(pipeline_cache_reset):
    """Run main with context with all arguments as expected."""
    expected_notify_output = ['sg1', 'sg1.2', 'success_handler']
    with patch_logger('pypyr.steps.echo', logging.NOTIFY) as mock_log:
        out = pipelinerunner.main_with_context(
            pipeline_name='pipelines/api/main-all',
            dict_in={'argList': ['A', 'B', 'C']},
            working_dir=working_dir_tests,
            groups=['sg1'],
            success_group='sh',
            failure_group='fh',
            loader='arbpack.naivefileloader')

    assert mock_log.mock_calls == [call(v) for v in expected_notify_output]
    assert out.pipeline_name == 'pipelines/api/main-all'
    assert out.working_dir == working_dir_tests
    assert out == {'argList': ['A', 'B', 'C'], 'set_in_pipe': 123}
Esempio n. 8
0
def test_retry_with_yaml_anchor(mock_sleep, mock_random):
    """Retry uses common shared anchors."""
    out = pipelinerunner.main_with_context(
        pipeline_name='pipelines/retries/retry-anchors',
        dict_in={'outList': []},
        working_dir=working_dir_tests)

    assert out['outList'] == ['s1.1',
                              's1.2',
                              's1.3',
                              's2.1',
                              's2.2',
                              's3.1',
                              's3.2',
                              's3.3']

    assert mock_random.mock_calls == [call(1.5, 3), call(4.25, 8.5)]
    assert mock_sleep.mock_calls == [call(2),
                                     call(4),
                                     call(2),
                                     call(123),
                                     call(123)]