Esempio n. 1
0
def test_same_server_multiple_ports():
    api = Api(('localhost', 'localhost:22'))
    assert len(api.servers) == 2

    # Ansible groups these calls, so we only get one result back
    result = api.command('whoami')
    assert len(result['contacted']) == 1
Esempio n. 2
0
def test_same_server_multiple_ports():
    api = Api(('localhost', 'localhost:22'))
    assert len(api.servers) == 2

    # Ansible groups these calls, so we only get one result back
    result = api.command('whoami')
    assert len(result['contacted']) == 1
Esempio n. 3
0
def test_auto_localhost():
    host = Api('localhost')
    assert host.inventory['localhost']['ansible_connection'] == 'local'

    host = Api('localhost', connection='smart')
    assert 'ansible_connection' not in host.inventory['localhost']
    assert host.options.connection == 'smart'
Esempio n. 4
0
    def test_results(self):
        result = Api('localhost').command('whoami')
        assert result.rc('localhost') == 0
        assert result.stdout('localhost') is not None
        assert result['contacted']['localhost']['rc'] == 0

        result['contacted'] = []
        assert result.rc('localhost') is None
Esempio n. 5
0
    def test_unreachable(self):
        host = Api('255.255.255.255')

        assert '255.255.255.255' in host.servers

        try:
            host.command('whoami')
        except UnreachableError, e:
            assert '255.255.255.255' in str(e)
Esempio n. 6
0
def test_escaping(tempdir):
    special_dir = os.path.join(tempdir, 'special dir with "-char')
    os.mkdir(special_dir)

    api = Api('localhost')
    api.file(
        dest=os.path.join(special_dir, 'foo.txt'),
        state='touch'
    )
Esempio n. 7
0
def test_valid_return_codes():
    host = Api('localhost')
    assert host._valid_return_codes == (0, )

    with host.valid_return_codes(0, 1):
        assert host._valid_return_codes == (0, 1)
        host.shell('whoami | grep -q asdfasdfasdf')

    assert host._valid_return_codes == (0, )
Esempio n. 8
0
def test_escaping(tempdir):
    special_dir = os.path.join(tempdir, 'special dir with "-char')
    os.mkdir(special_dir)

    api = Api('localhost')
    api.file(
        dest=os.path.join(special_dir, 'foo.txt'),
        state='touch'
    )
Esempio n. 9
0
def test_valid_return_codes():
    host = Api('localhost')
    assert host._valid_return_codes == (0, )

    with host.valid_return_codes(0, 1):
        assert host._valid_return_codes == (0, 1)
        host.shell('whoami | grep -q asdfasdfasdf')

    assert host._valid_return_codes == (0, )
Esempio n. 10
0
def test_extra_vars():
    tempdir = tempfile.mkdtemp()

    try:
        api = Api('localhost', extra_vars={'path': tempdir})
        api.file(dest="{{ path }}/foo.txt", state='touch')

        assert os.path.exists(tempdir + '/foo.txt')
    finally:
        shutil.rmtree(tempdir)
Esempio n. 11
0
def test_escaping():
    tempdir = tempfile.mkdtemp()

    try:
        special_dir = os.path.join(tempdir, 'special dir with "-char')
        os.mkdir(special_dir)

        api = Api('localhost')
        api.file(dest=os.path.join(special_dir, 'foo.txt'), state='touch')
    finally:
        shutil.rmtree(tempdir)
Esempio n. 12
0
def test_unreachable(server):
    host = Api(server)

    assert server in host.inventory

    try:
        host.command('whoami')
    except UnreachableError as e:
        assert server in str(e)
    else:
        assert False, "an error should have been thrown"

    assert server not in host.inventory
Esempio n. 13
0
def test_unreachable():
    host = Api('255.255.255.255')

    assert '255.255.255.255' in host.servers

    try:
        host.command('whoami')
    except UnreachableError as e:
        assert '255.255.255.255' in str(e)
    else:
        assert False, "an error should have been thrown"

    assert '255.255.255.255' not in host.servers
Esempio n. 14
0
def test_unreachable(server):
    host = Api(server)

    assert server in host.servers

    try:
        host.command('whoami')
    except UnreachableError as e:
        assert server in str(e)
    else:
        assert False, "an error should have been thrown"

    assert server not in host.servers
Esempio n. 15
0
def test_escaping():
    tempdir = tempfile.mkdtemp()

    try:
        special_dir = os.path.join(tempdir, 'special dir with "-char')
        os.mkdir(special_dir)

        api = Api('localhost')
        api.file(
            dest=os.path.join(special_dir, 'foo.txt'),
            state='touch'
        )
    finally:
        shutil.rmtree(tempdir)
Esempio n. 16
0
def test_server_with_port():
    # ideally we would test ipv6 here as well, but that doesn't work
    # on travis at the moment:
    # see https://github.com/travis-ci/travis-ci/issues/8891
    for definition in ('localhost:22', ['localhost:22']):
        api = Api(definition)

        assert api.servers == ['localhost:22']

        hosts, ports = zip(tuple(*api.hosts_with_ports))
        assert hosts == ('localhost', )
        assert ports == (22, )

        result = Api('localhost').command('whoami')
        assert result.rc() == 0
Esempio n. 17
0
def test_server_with_port():
    # ideally we would test ipv6 here as well, but that doesn't work
    # on travis at the moment:
    # see https://github.com/travis-ci/travis-ci/issues/8891
    for definition in ('localhost:22', ['localhost:22']):
        api = Api(definition)

        assert api.servers == ['localhost:22']

        hosts, ports = zip(tuple(*api.hosts_with_ports))
        assert hosts == ('localhost', )
        assert ports == (22, )

        result = Api('localhost').command('whoami')
        assert result.rc() == 0
Esempio n. 18
0
def test_list_args():
    api = Api('localhost')

    # api.assert is not valid Python syntax
    getattr(api, 'assert')(that=[
        "'bar' != 'foo'",
        "'bar' == 'bar'"
    ])
Esempio n. 19
0
def test_module_args():
    upgrade = ('apt-get upgrade -y -o Dpkg::Options::="--force-confdef" '
               '-o Dpkg::Options::="--force-confold"')

    try:
        Api('localhost').command(upgrade)
    except ModuleError as e:
        assert e.result['invocation']['module_args']['_raw_params'] == upgrade
Esempio n. 20
0
def test_results():
    result = Api('localhost').command('whoami')
    assert result.rc('localhost') == 0
    assert result.stdout('localhost') is not None
    assert result['contacted']['localhost']['rc'] == 0

    with pytest.raises(AttributeError):
        result.asdf('localhost')

    result['contacted'] = []

    with pytest.raises(KeyError):
        result.rc('localhost')
Esempio n. 21
0
def test_results():
    result = Api('localhost').command('whoami')
    assert result.rc('localhost') == 0
    assert result.stdout('localhost') is not None
    assert result['contacted']['localhost']['rc'] == 0

    with pytest.raises(AttributeError):
        result.asdf('localhost')

    result['contacted'] = []

    with pytest.raises(KeyError):
        result.rc('localhost')
Esempio n. 22
0
def test_error_string():
    try:
        Api('localhost').command('whoami | less')
    except ModuleError as e:
        # we don't have a msg so we mock that out, for coverage!
        e.result['msg'] = '0xdeadbeef'
        error_string = text_type(e)

        # we don't make many guarantees with the string messages, so
        # a basic somke test suffices here. This is not something to
        # depend on.

        assert '0xdeadbeef' in error_string
        assert 'command: whoami | less' in error_string
        assert 'Returncode: 1' in error_string
    else:
        assert False, "this needs to trigger an exception"
Esempio n. 23
0
def test_ignore_unreachable(server):
    host = Api(server, ignore_unreachable=True)

    assert server in host.servers
    host.command('whoami')
    assert server in host.servers
Esempio n. 24
0
def test_ignore_unreachable(server):
    host = Api(server, ignore_unreachable=True)

    assert server in host.servers
    host.command('whoami')
    assert server in host.servers
Esempio n. 25
0
def test_servers_list(server):
    host = Api((server, ))
    assert host.command('whoami').rc(server) == 0
Esempio n. 26
0
def test_results_single_server():
    result = Api('localhost').command('whoami')
    assert result.rc() == 0
Esempio n. 27
0
def test_servers_list():
    host = Api(('localhost', ))
    assert host.command('whoami').rc('localhost') == 0
Esempio n. 28
0
def test_module_error():
    with pytest.raises(ModuleError):
        # command cannot include pipes
        Api('localhost').command('whoami | less')
Esempio n. 29
0
def test_environment():
    api = Api('localhost', environment={'FOO': 'BAR'})
    assert api.shell('echo $FOO').stdout() == 'BAR'

    api.environment['FOO'] = 'BAZ'
    assert api.shell('echo $FOO').stdout() == 'BAZ'
Esempio n. 30
0
def test_extra_vars(tempdir):
    api = Api('localhost', extra_vars={'path': tempdir})
    api.file(dest="{{ path }}/foo.txt", state='touch')

    assert os.path.exists(tempdir + '/foo.txt')
Esempio n. 31
0
def test_sudo():
    host = Api('localhost', sudo=True)
    try:
        assert host.command('whoami').stdout() == 'root'
    except ModuleError as e:
        assert 'password' in e.result['module_stderr']
Esempio n. 32
0
def test_ignore_errors():
    host = Api('localhost', ignore_errors=True)
    result = host.command('whoami | less')

    assert result.rc() == 1
    assert result.cmd() == ['whoami', '|', 'less']
Esempio n. 33
0
def test_whoami_multiple_servers(server):
    host = Api(server)
    results = host.command('whoami')
    assert results.rc(server[0]) == 0
    assert results.rc(server[1]) == 0
Esempio n. 34
0
def test_ignore_unreachable():
    host = Api('255.255.255.255', ignore_unreachable=True)

    assert '255.255.255.255' in host.servers
    host.command('whoami')
    assert '255.255.255.255' in host.servers
Esempio n. 35
0
def test_ignore_unreachable(server):
    host = Api(server, ignore_unreachable=True)
    assert server in host.inventory
    result = host.command('whoami')
    assert server in result['unreachable']
    assert server in host.inventory
Esempio n. 36
0
def test_results_single_server():
    result = Api('localhost').command('whoami')
    assert result.rc() == 0
Esempio n. 37
0
def test_ignore_errors():
    host = Api('localhost', ignore_errors=True)
    result = host.command('whoami | less')

    assert result.rc() == 1
    assert result.cmd() == ['whoami', '|', 'less']
Esempio n. 38
0
 def test_servers_list(self):
     host = Api(('localhost', ))
     assert host.command('whoami').rc('localhost') == 0
Esempio n. 39
0
 def test_results_multiple_servers(self):
     result = Api(['localhost', '127.0.0.1']).command('whoami')
     with pytest.raises(KeyError):
         result.rc()
Esempio n. 40
0
    def test_ignore_unreachable(self):
        host = Api('255.255.255.255', ignore_unreachable=True)

        assert '255.255.255.255' in host.servers
        host.command('whoami')
        assert '255.255.255.255' in host.servers
Esempio n. 41
0
def test_environment():
    api = Api('localhost', environment={'FOO': 'BAR'})
    assert api.shell('echo $FOO').stdout() == 'BAR'

    api.environment['FOO'] = 'BAZ'
    assert api.shell('echo $FOO').stdout() == 'BAZ'
Esempio n. 42
0
def test_servers_list(server):
    host = Api((server, ))
    assert host.command('whoami').rc(server) == 0
Esempio n. 43
0
def test_results_single_server(server):
    result = Api(server).command('whoami')
    assert result.rc() == 0
    assert result.rc(server) == 0
Esempio n. 44
0
def test_no_global_state():
    # turn the cached value into something unusable, if we don't clear it
    # correctly, an error will arise
    inventory.HOSTS_PATTERNS_CACHE['all'] = None
    Api('localhost').command('whoami')
Esempio n. 45
0
def test_dict_args(tempdir):
    api = Api('localhost')
    api.set_stats(data={'foo': 'bar'})
Esempio n. 46
0
def test_sudo():
    host = Api('localhost', sudo=True)
    try:
        assert host.command('whoami').stdout() == 'root'
    except ModuleError as e:
        assert 'password' in e.result['module_stderr']
Esempio n. 47
0
def test_results_single_server(server):
    result = Api(server).command('whoami')
    assert result.rc() == 0
    assert result.rc(server) == 0
Esempio n. 48
0
def test_extra_vars(tempdir):
    api = Api('localhost', extra_vars={'path': tempdir})
    api.file(dest="{{ path }}/foo.txt", state='touch')

    assert os.path.exists(tempdir + '/foo.txt')
Esempio n. 49
0
def test_auto_localhost():
    host = Api('localhost')
    assert host.options.connection == 'local'

    host = Api('localhost', connection='smart')
    assert host.options.connection == 'smart'
Esempio n. 50
0
def test_dict_args(tempdir):
    api = Api('localhost')
    api.set_stats(data={'foo': 'bar'})