def test_set_cancel_callback(mocker):
    mock_runner = mocker.patch('ansible_runner.interface.Runner', side_effect=AttributeError('Raised intentionally'))
    mock_runner_config = mocker.patch('ansible_runner.interface.RunnerConfig')
    mock_runner_config.prepare.return_value = None

    def custom_cancel_callback():
        return 'custom'

    with pytest.raises(AttributeError, match='Raised intentionally'):
        init_runner(ignore_logging=True, cancel_callback=custom_cancel_callback)

    assert mock_runner.call_args.kwargs['cancel_callback'] is custom_cancel_callback
def executor(tmpdir, request, is_pre_ansible28):
    private_data_dir = six.text_type(tmpdir.mkdir('foo'))

    playbooks = request.node.callspec.params.get('playbook')
    playbook = list(playbooks.values())[0]
    envvars = request.node.callspec.params.get('envvars')
    if envvars is None:
        envvars = {}
    # warning messages create verbose events and interfere with assertions
    envvars["ANSIBLE_DEPRECATION_WARNINGS"] = "False"
    # python interpreter used is not of much interest, we really want to silence warnings
    envvars['ANSIBLE_PYTHON_INTERPRETER'] = 'auto_silent'

    if is_pre_ansible28:
        inventory = 'localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"'
    else:
        inventory = 'localhost ansible_connection=local'


    r = init_runner(
        private_data_dir=private_data_dir,
        inventory=inventory,
        envvars=envvars,
        playbook=yaml.safe_load(playbook)
    )

    return r
Beispiel #3
0
def test_process_isolation_executable_not_found(mock_subprocess, mock_sys,
                                                mock_prepare,
                                                executable_present):
    # Mock subprocess.Popen indicates if
    # process isolation executable is present
    mock_proc = Mock()
    mock_proc.returncode = (0 if executable_present else 1)
    mock_subprocess.Popen.return_value = mock_proc

    kwargs = {
        'process_isolation': True,
        'process_isolation_executable': 'fake_executable'
    }
    init_runner(**kwargs)
    if executable_present:
        assert not mock_sys.exit.called
    else:
        assert mock_sys.exit.called
def test_process_isolation_executable_not_found(executable_present, mocker):
    mocker.patch('ansible_runner.runner_config.RunnerConfig.prepare')
    mock_sys = mocker.patch('ansible_runner.interface.sys')
    mock_subprocess = mocker.patch('ansible_runner.utils.subprocess')
    # Mock subprocess.Popen indicates if
    # process isolation executable is present
    mock_proc = mocker.Mock()
    mock_proc.returncode = (0 if executable_present else 1)
    mock_subprocess.Popen.return_value = mock_proc

    kwargs = {
        'process_isolation': True,
        'process_isolation_executable': 'fake_executable'
    }
    init_runner(**kwargs)
    if executable_present:
        assert not mock_sys.exit.called
    else:
        assert mock_sys.exit.called
Beispiel #5
0
def executor(tmpdir, request):
    private_data_dir = six.text_type(tmpdir.mkdir('foo'))

    playbooks = request.node.callspec.params.get('playbook')
    playbook = list(playbooks.values())[0]

    r = init_runner(
        private_data_dir=private_data_dir,
        inventory="localhost ansible_connection=local",
        playbook=yaml.safe_load(playbook)
    )

    return r
Beispiel #6
0
def executor(tmpdir, request):
    private_data_dir = six.text_type(tmpdir.mkdir('foo'))

    playbooks = request.node.callspec.params.get('playbook')
    playbook = list(playbooks.values())[0]
    envvars = request.node.callspec.params.get('envvars')
    envvars = envvars.update({"ANSIBLE_DEPRECATION_WARNINGS": "False"}) if envvars is not None else {"ANSIBLE_DEPRECATION_WARNINGS": "False"}

    r = init_runner(
        private_data_dir=private_data_dir,
        inventory="localhost ansible_connection=local",
        envvars=envvars,
        playbook=yaml.safe_load(playbook)
    )

    return r
Beispiel #7
0
def test_output_when_given_non_playbook_script(tmp_path):
    # As shown in the following pull request:
    #
    #   https://github.com/ansible/ansible-runner/pull/256
    #
    # This ports some functionality that previously lived in awx and allows raw
    # lines of stdout to be treated as event lines.
    #
    # As mentioned in the pull request as well, there were no specs added, and
    # this is a retro-active test based on the sample repo provided in the PR:
    #
    #   https://github.com/AlanCoding/ansible-runner-examples/tree/master/non_playbook/sleep_with_writes
    private_data_dir = str(tmp_path)
    with open(os.path.join(private_data_dir, "args"), 'w') as args_file:
        args_file.write("bash sleep_and_write.sh\n")
    with open(os.path.join(private_data_dir, "sleep_and_write.sh"),
              'w') as script_file:
        script_file.write("echo 'hi world'\nsleep 0.5\necho 'goodbye world'\n")

    # Update the settings to make this test a bit faster :)
    os.mkdir(os.path.join(private_data_dir, "env"))
    with open(os.path.join(private_data_dir, "env", "settings"),
              'w') as settings_file:
        settings_file.write("pexpect_timeout: 0.2")

    executor = init_runner(
        private_data_dir=private_data_dir,
        inventory=
        'localhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"',
        envvars={"ANSIBLE_DEPRECATION_WARNINGS": "False"})

    executor.run()
    stdout = executor.stdout.readlines()
    assert stdout[0].strip() == "hi world"
    assert stdout[1].strip() == "goodbye world"

    events = list(executor.events)

    assert len(events) == 2
    assert events[0]['event'] == 'verbose'
    assert events[0]['stdout'] == 'hi world'
    assert events[1]['event'] == 'verbose'
    assert events[1]['stdout'] == 'goodbye world'
def test_output_when_given_invalid_playbook(tmpdir):
    # As shown in the following issue:
    #
    #   https://github.com/ansible/ansible-runner/issues/29
    #
    # There was a lack of output by runner when a playbook that doesn't exist
    # is provided.  This was fixed in this PR:
    #
    #   https://github.com/ansible/ansible-runner/pull/34
    #
    # But no test validated it.  This does that.
    private_data_dir = str(tmpdir)
    executor = init_runner(
        private_data_dir=private_data_dir,
        inventory="localhost ansible_connection=local",
        envvars={"ANSIBLE_DEPRECATION_WARNINGS": "False"},
        playbook=os.path.join(private_data_dir, 'fake_playbook.yml')
    )

    executor.run()
    stdout = executor.stdout.read()
    assert "ERROR! the playbook:" in stdout
    assert "could not be found" in stdout
def test_default_callback_set(mocker):
    mocker.patch('ansible_runner.interface.signal_handler', side_effect=AttributeError('Raised intentionally'))

    with pytest.raises(AttributeError, match='Raised intentionally'):
        init_runner(ignore_logging=True)