def test_host_repr(self): assert repr( Host(hostname='host01.example.com') ) == '<Host: hostname=host01.example.com>' assert repr( Host(hostname='host01.example.com', ipv4='192.168.0.2') ) == '<Host: hostname=host01.example.com, ipv4=192.168.0.2>'
def test_search_ipv4(self): parser = Parser('/tmp') parser._hosts = [ Host('host01.example.com', ipv4='192.168.0.1'), Host('host02.example.com', ipv4='192.168.0.2') ] assert parser.search('192.168.0.1')[0].hostname == 'host01.example.com' assert len(parser.search('192.168.0')) == 2
def test_search_hostname(self): parser = Parser('/tmp') parser._hosts = [ Host('host01.example.com'), Host('host02.example.com') ] assert parser.search('host01')[0].hostname == 'host01.example.com' assert len(parser.search('example.com')) == 2
def test_search_fnmatch_questionmark(self): parser = Parser('/tmp') parser._hosts = [ Host('host01.example.com'), Host('host02.example.com') ] assert parser.search( 'host??.example.com')[0].hostname == 'host01.example.com' assert len(parser.search('host??.example.com')) == 2
def test_search_ipv6(self): parser = Parser('/tmp') parser._hosts = [ Host('host01.example.com', ipv6='fe80::41:dead:beef:cafe'), Host('host02.example.com', ipv6='fe80::41:dead:beef:daff') ] assert parser.search( 'dead:beef:cafe')[0].hostname == 'host01.example.com' assert len(parser.search('dead:beef:')) == 2
def test_select_host(self, mocker): hosts = [ Host(hostname='host01.example.com', ipv4='127.0.0.1'), Host(hostname='host02.example.com', ipv4='127.0.0.2', user='******') ] mocker.patch('ssht.ssht.get_answer', return_value='1') assert select_host(hosts).hostname == 'host01.example.com' assert select_host(hosts).user is None mocker.patch('ssht.ssht.get_answer', return_value='2') assert select_host(hosts).hostname == 'host02.example.com' assert select_host(hosts).user == 'admin' mocker.patch('ssht.ssht.get_answer', return_value='') assert select_host(hosts) is None
def test_user(self, mocker): mocker.patch('subprocess.call') ssh_connect(Host(hostname='host01.example.com', user='******'), []) subprocess.call.assert_called_with( ['ssh', 'host01.example.com', '-l', 'admin'])
def test_ipv6(self, mocker): mocker.patch('subprocess.call') ssh_connect(Host(hostname='host01.example.com', ipv6='::1'), ['-6']) subprocess.call.assert_called_with(['ssh', '::1', '-6'])
def test_ipv4_(self, mocker): mocker.patch('subprocess.call') ssh_connect(Host(hostname='host01.example.com', ipv4='192.168.1.2'), ['-4']) subprocess.call.assert_called_with(['ssh', '192.168.1.2', '-4'], )
def test_port(self, mocker): mocker.patch('subprocess.call') ssh_connect(Host(hostname='host01.example.com', port='22022'), []) subprocess.call.assert_called_with( ['ssh', 'host01.example.com', '-p', '22022'], )
def test_factory_exception(self): with pytest.raises(ValueError): Host.factory('host01.example.com')
def test_factory_tuple(self): host = Host.factory(('host01.example.com',)) assert host.hostname == 'host01.example.com'
def test_factory_dict(self): host = Host.factory(dict(hostname='host01.example.com')) assert host.hostname == 'host01.example.com'
def test_user(self): x = Host(hostname='host01.example.com', user='******') assert x.display == '*****@*****.**'
def test_default(self): x = Host(hostname='host01.example.com') assert x.display == 'host01.example.com'