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
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'
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
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)
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' )
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, )
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)
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)
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
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
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
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)
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
def test_list_args(): api = Api('localhost') # api.assert is not valid Python syntax getattr(api, 'assert')(that=[ "'bar' != 'foo'", "'bar' == 'bar'" ])
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
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')
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"
def test_ignore_unreachable(server): host = Api(server, ignore_unreachable=True) assert server in host.servers host.command('whoami') assert server in host.servers
def test_servers_list(server): host = Api((server, )) assert host.command('whoami').rc(server) == 0
def test_results_single_server(): result = Api('localhost').command('whoami') assert result.rc() == 0
def test_servers_list(): host = Api(('localhost', )) assert host.command('whoami').rc('localhost') == 0
def test_module_error(): with pytest.raises(ModuleError): # command cannot include pipes Api('localhost').command('whoami | less')
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'
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')
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']
def test_ignore_errors(): host = Api('localhost', ignore_errors=True) result = host.command('whoami | less') assert result.rc() == 1 assert result.cmd() == ['whoami', '|', 'less']
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
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
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
def test_servers_list(self): host = Api(('localhost', )) assert host.command('whoami').rc('localhost') == 0
def test_results_multiple_servers(self): result = Api(['localhost', '127.0.0.1']).command('whoami') with pytest.raises(KeyError): result.rc()
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
def test_results_single_server(server): result = Api(server).command('whoami') assert result.rc() == 0 assert result.rc(server) == 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')
def test_dict_args(tempdir): api = Api('localhost') api.set_stats(data={'foo': 'bar'})
def test_auto_localhost(): host = Api('localhost') assert host.options.connection == 'local' host = Api('localhost', connection='smart') assert host.options.connection == 'smart'