def ssh_client(**kwargs): """Context manager that returns an SSH client. The SSH connection is opened before entering, and closed before leaving. """ client = SSHClient(**kwargs) if not client.connect(): raise Exception("Cannot create SSH connection with %s" % (client,)) try: yield client finally: client.close()
def ssh_client(**kwargs): """Context manager that returns an SSH client. The SSH connection is opened before entering, and closed before leaving. """ client = SSHClient(**kwargs) if not client.connect(): raise Exception("Cannot create SSH connection with %s" % (client, )) try: yield client finally: client.close()
def test_password_auth_not_supported(self): try: ShellOutSSHClient(hostname='localhost', username='******', password='******') except ValueError as e: msg = str(e) self.assertTrue('ShellOutSSHClient only supports key auth' in msg) else: self.fail('Exception was not thrown')
def test_password_auth_not_supported(self): try: ShellOutSSHClient(hostname="localhost", username="******", password="******") except ValueError as e: msg = str(e) self.assertTrue("ShellOutSSHClient only supports key auth" in msg) else: self.fail("Exception was not thrown")
def test_get_base_ssh_command(self): client1 = ShellOutSSHClient(hostname='localhost', username='******') client2 = ShellOutSSHClient(hostname='localhost', username='******', key='/home/my.key') client3 = ShellOutSSHClient(hostname='localhost', username='******', key='/home/my.key', timeout=5) cmd1 = client1._get_base_ssh_command() cmd2 = client2._get_base_ssh_command() cmd3 = client3._get_base_ssh_command() self.assertEqual(cmd1, ['ssh', 'root@localhost']) self.assertEqual(cmd2, ['ssh', '-i', '/home/my.key', 'root@localhost']) self.assertEqual(cmd3, ['ssh', '-i', '/home/my.key', '-oConnectTimeout=5', 'root@localhost'])
def test_get_base_ssh_command(self): client1 = ShellOutSSHClient(hostname="localhost", username="******") client2 = ShellOutSSHClient(hostname="localhost", username="******", key="/home/my.key") client3 = ShellOutSSHClient(hostname="localhost", username="******", key="/home/my.key", timeout=5) cmd1 = client1._get_base_ssh_command() cmd2 = client2._get_base_ssh_command() cmd3 = client3._get_base_ssh_command() self.assertEqual(cmd1, ["ssh", "root@localhost"]) self.assertEqual(cmd2, ["ssh", "-i", "/home/my.key", "root@localhost"]) self.assertEqual(cmd3, [ "ssh", "-i", "/home/my.key", "-oConnectTimeout=5", "root@localhost" ])
def test_ssh_executable_not_available(self): class MockChild(object): returncode = 127 def communicate(*args, **kwargs): pass def mock_popen(*args, **kwargs): return MockChild() with patch('subprocess.Popen', mock_popen): try: ShellOutSSHClient(hostname='localhost', username='******') except ValueError as e: msg = str(e) self.assertTrue('ssh client is not available' in msg) else: self.fail('Exception was not thrown')
def test_get_base_ssh_command(self): client1 = ShellOutSSHClient(hostname='localhost', username='******') client2 = ShellOutSSHClient(hostname='localhost', username='******', key='/home/my.key') client3 = ShellOutSSHClient(hostname='localhost', username='******', key='/home/my.key', timeout=5) cmd1 = client1._get_base_ssh_command() cmd2 = client2._get_base_ssh_command() cmd3 = client3._get_base_ssh_command() self.assertEquals(cmd1, ['ssh', 'root@localhost']) self.assertEquals(cmd2, ['ssh', '-i', '/home/my.key', 'root@localhost']) self.assertEquals(cmd3, ['ssh', '-i', '/home/my.key', '-oConnectTimeout=5', 'root@localhost'])
def test_close_success(self): client = ShellOutSSHClient(hostname='localhost', username='******') self.assertTrue(client.close())
def test_connect_success(self): client = ShellOutSSHClient(hostname="localhost", username="******") self.assertTrue(client.connect())