Beispiel #1
0
 def test_per_host_dict_args(self):
     server2_socket = make_socket('127.0.0.2', port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket)
     server3_socket = make_socket('127.0.0.3', port=self.listen_port)
     server3_port = server3_socket.getsockname()[1]
     server3 = start_server(server3_socket)
     hosts = [self.host, '127.0.0.2', '127.0.0.3']
     hosts_gen = (h for h in hosts)
     host_args = [dict(zip(('host_arg1', 'host_arg2',),
                           ('arg1-%s' % (i,), 'arg2-%s' % (i,),)))
                  for i, _ in enumerate(hosts)]
     cmd = 'echo %(host_arg1)s %(host_arg2)s'
     client = ParallelSSHClient(hosts, port=self.listen_port,
                                pkey=self.user_key)
     output = client.run_command(cmd, host_args=host_args)
     for i, host in enumerate(hosts):
         expected = ["%(host_arg1)s %(host_arg2)s" % host_args[i]]
         stdout = list(output[host]['stdout'])
         self.assertEqual(expected, stdout)
         self.assertTrue(output[host]['exit_code'] == 0)
     self.assertRaises(HostArgumentException, client.run_command,
                       cmd, host_args=[host_args[0]])
     # Host list generator should work also
     client.hosts = hosts_gen
     output = client.run_command(cmd, host_args=host_args)
     for i, host in enumerate(hosts):
         expected = ["%(host_arg1)s %(host_arg2)s" % host_args[i]]
         stdout = list(output[host]['stdout'])
         self.assertEqual(expected, stdout)
         self.assertTrue(output[host]['exit_code'] == 0)
     client.hosts = (h for h in hosts)
     self.assertRaises(HostArgumentException, client.run_command,
                       cmd, host_args=[host_args[0]])
Beispiel #2
0
 def test_per_host_tuple_args(self):
     server2_socket = make_socket('127.0.0.2', port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket)
     server3_socket = make_socket('127.0.0.3', port=self.listen_port)
     server3_port = server3_socket.getsockname()[1]
     server3 = start_server(server3_socket)
     hosts = [self.host, '127.0.0.2', '127.0.0.3']
     host_args = ('arg1', 'arg2', 'arg3')
     cmd = 'echo %s'
     client = ParallelSSHClient(hosts, port=self.listen_port,
                                pkey=self.user_key)
     output = client.run_command(cmd, host_args=host_args)
     for i, host in enumerate(hosts):
         expected = [host_args[i]]
         stdout = list(output[host]['stdout'])
         self.assertEqual(expected, stdout)
         self.assertTrue(output[host]['exit_code'] == 0)
     host_args = (('arg1', 'arg2'), ('arg3', 'arg4'), ('arg5', 'arg6'),)
     cmd = 'echo %s %s'
     output = client.run_command(cmd, host_args=host_args)
     for i, host in enumerate(hosts):
         expected = ["%s %s" % host_args[i]]
         stdout = list(output[host]['stdout'])
         self.assertEqual(expected, stdout)
         self.assertTrue(output[host]['exit_code'] == 0)
     self.assertRaises(HostArgumentException, client.run_command,
                       cmd, host_args=[host_args[0]])
 def test_ssh_proxy_auth_fail(self):
     """Test failures while connecting via proxy"""
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     self.server.kill()
     server = start_server(listen_socket,
                           fail_auth=True)
     proxy_server_socket = make_socket('127.0.0.2')
     proxy_server_port = proxy_server_socket.getsockname()[1]
     proxy_server = start_server(proxy_server_socket)
     proxy_user = '******'
     proxy_password = '******'
     gevent.sleep(2)
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                proxy_host='127.0.0.2',
                                proxy_port=proxy_server_port,
                                proxy_user=proxy_user,
                                proxy_password='******',
                                proxy_pkey=self.user_key,
                                )
     gevent.sleep(2)
     self.assertRaises(AuthenticationException, client.run_command, self.fake_cmd)
     del client
     server.kill()
     proxy_server.kill()
Beispiel #4
0
 def test_ssh_proxy_auth_fail(self):
     """Test failures while connecting via proxy"""
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     self.server.kill()
     server = start_server(listen_socket,
                           fail_auth=True)
     proxy_server_socket = make_socket('127.0.0.2')
     proxy_server_port = proxy_server_socket.getsockname()[1]
     proxy_server = start_server(proxy_server_socket)
     proxy_user = '******'
     proxy_password = '******'
     gevent.sleep(2)
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                proxy_host='127.0.0.2',
                                proxy_port=proxy_server_port,
                                proxy_user=proxy_user,
                                proxy_password='******',
                                proxy_pkey=self.user_key,
                                )
     gevent.sleep(2)
     self.assertRaises(AuthenticationException, client.run_command, self.fake_cmd)
     del client
     server.kill()
     proxy_server.kill()
 def test_pssh_hosts_iterator_hosts_modification(self):
     """Test using iterator as host list and modifying host list in place"""
     server2_socket = make_socket("127.0.0.2", port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket)
     server3_socket = make_socket("127.0.0.3", port=self.listen_port)
     server3_port = server3_socket.getsockname()[1]
     server3 = start_server(server3_socket)
     hosts = [self.host, "127.0.0.2"]
     client = ParallelSSHClient(iter(hosts), port=self.listen_port, pkey=self.user_key, pool_size=1)
     output = client.run_command(self.fake_cmd)
     stdout = [list(output[k]["stdout"]) for k in output]
     expected_stdout = [[self.fake_resp], [self.fake_resp]]
     self.assertEqual(
         len(hosts),
         len(output),
         msg="Did not get output from all hosts. Got output for " "%s/%s hosts" % (len(output), len(hosts)),
     )
     # Run again without re-assigning host list, should do nothing
     output = client.run_command(self.fake_cmd)
     self.assertFalse(hosts[0] in output, msg="Expected no host output, got %s" % (output,))
     self.assertFalse(output, msg="Expected empty output, got %s" % (output,))
     # Re-assigning host list with new hosts should work
     hosts = ["127.0.0.2", "127.0.0.3"]
     client.hosts = iter(hosts)
     output = client.run_command(self.fake_cmd)
     self.assertEqual(
         len(hosts),
         len(output),
         msg="Did not get output from all hosts. Got output for " "%s/%s hosts" % (len(output), len(hosts)),
     )
     self.assertTrue(hosts[1] in output, msg="Did not get output for new host %s" % (hosts[1],))
     del client, server2, server3
 def test_identical_host_output(self):
     """Test that we get output when running with duplicated hosts"""
     # Make socket with no server listening on it just for testing output
     _socket1, _socket2 = make_socket(self.host), make_socket(self.host)
     port = _socket1.getsockname()[1]
     hosts = [self.host, self.host, self.host]
     client = ParallelSSHClient(hosts, port=port,
                                pkey=self.user_key)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertEqual(len(hosts), len(output.keys()),
                      msg="Host list contains %s identical hosts, only got output for %s" % (
                          len(hosts), len(output.keys())))
     del _socket1, _socket2
 def test_identical_host_output(self):
     """Test that we get output when running with duplicated hosts"""
     # Make socket with no server listening on it just for testing output
     _socket1, _socket2 = make_socket(self.host), make_socket(self.host)
     port = _socket1.getsockname()[1]
     hosts = [self.host, self.host, self.host]
     client = ParallelSSHClient(hosts, port=port,
                                pkey=self.user_key)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertEqual(len(hosts), len(output.keys()),
                      msg="Host list contains %s identical hosts, only got output for %s" % (
                          len(hosts), len(output.keys())))
     del _socket1, _socket2
Beispiel #8
0
 def test_host_config(self):
     """Test per-host configuration functionality of ParallelSSHClient"""
     hosts = ['127.0.0.%01d' % n for n in xrange(1,3)]
     host_config = dict.fromkeys(hosts)
     servers = []
     user = '******'
     password = '******'
     for host in hosts:
         _socket = make_socket(host)
         port = _socket.getsockname()[1]
         host_config[host] = {}
         host_config[host]['port'] = port
         host_config[host]['user'] = user
         host_config[host]['password'] = password
         server = start_server(_socket, fail_auth=hosts.index(host))
         servers.append((server, port))
     pkey_data = load_private_key(PKEY_FILENAME)
     host_config[hosts[0]]['private_key'] = pkey_data
     client = ParallelSSHClient(hosts, host_config=host_config)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.join(output)
     for host in hosts:
         self.assertTrue(host in output)
     try:
         raise output[hosts[1]]['exception']
     except AuthenticationException, ex:
         pass
Beispiel #9
0
 def test_ssh_proxy_auth(self):
     """Test connecting to remote destination via SSH proxy
     client -> proxy -> destination
     Proxy SSH server accepts no commands and sends no responses, only
     proxies to destination. Destination accepts a command as usual."""
     proxy_server_socket = make_socket('127.0.0.2')
     proxy_server_port = proxy_server_socket.getsockname()[1]
     proxy_server = start_server(proxy_server_socket)
     proxy_user = '******'
     proxy_password = '******'
     gevent.sleep(2)
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key,
                                proxy_host='127.0.0.2',
                                proxy_port=proxy_server_port,
                                proxy_user=proxy_user,
                                proxy_password='******',
                                proxy_pkey=self.user_key,
                                )
     gevent.sleep(2)
     output = client.run_command(self.fake_cmd)
     stdout = list(output[self.host]['stdout'])
     expected_stdout = [self.fake_resp]
     self.assertEqual(expected_stdout, stdout,
                      msg="Got unexpected stdout - %s, expected %s" % (
                          stdout, expected_stdout,))
     self.assertEqual(client.host_clients[self.host].proxy_user, proxy_user)
     self.assertEqual(client.host_clients[self.host].proxy_password, proxy_password)
     self.assertTrue(client.host_clients[self.host].proxy_pkey)
     self.server.kill()
     proxy_server.kill()
Beispiel #10
0
 def test_openssh_config(self):
     """Test reading and using OpenSSH config file"""
     config_file = tempfile.NamedTemporaryFile()
     _host = "127.0.0.2"
     _user = "******"
     _listen_socket = make_socket(_host)
     _server = start_server(_listen_socket)
     _port = _listen_socket.getsockname()[1]
     _key = USER_KEY_PATH
     content = [("""Host %s\n""" % (_host,)),
                ("""  User %s\n""" % (_user,)),
                ("""  Port %s\n""" % (_port,)),
                ("""  IdentityFile %s\n""" % (_key,)),
                ]
     config_file.writelines([s.encode('utf-8') for s in content])
     config_file.flush()
     host, user, port, pkey = utils.read_openssh_config(
         _host, config_file=config_file.name)
     client = SSHClient(_host, _openssh_config_file=config_file.name)
     config_file.close()
     self.assertEqual(host, _host)
     self.assertEqual(user, _user)
     self.assertEqual(port, _port)
     self.assertTrue(pkey)
     self.assertEqual(client.host, _host)
     self.assertEqual(client.user, _user)
     self.assertEqual(client.port, _port)
     self.assertTrue(client.pkey)
     del _server, _listen_socket
Beispiel #11
0
 def test_openssh_config(self):
     """Test reading and using OpenSSH config file"""
     config_file = tempfile.NamedTemporaryFile()
     _host = "127.0.0.2"
     _user = "******"
     _listen_socket = make_socket(_host)
     _server = start_server(_listen_socket)
     _port = _listen_socket.getsockname()[1]
     _key = USER_KEY_PATH
     content = [
         ("""Host %s\n""" % (_host, )),
         ("""  User %s\n""" % (_user, )),
         ("""  Port %s\n""" % (_port, )),
         ("""  IdentityFile %s\n""" % (_key, )),
     ]
     config_file.writelines([s.encode('utf-8') for s in content])
     config_file.flush()
     host, user, port, pkey = utils.read_openssh_config(
         _host, config_file=config_file.name)
     client = SSHClient(_host, _openssh_config_file=config_file.name)
     config_file.close()
     self.assertEqual(host, _host)
     self.assertEqual(user, _user)
     self.assertEqual(port, _port)
     self.assertTrue(pkey)
     self.assertEqual(client.host, _host)
     self.assertEqual(client.user, _user)
     self.assertEqual(client.port, _port)
     self.assertTrue(client.pkey)
     del _server, _listen_socket
Beispiel #12
0
 def test_ssh_proxy_auth(self):
     """Test connecting to remote destination via SSH proxy
     client -> proxy -> destination
     Proxy SSH server accepts no commands and sends no responses, only
     proxies to destination. Destination accepts a command as usual."""
     proxy_server_socket = make_socket('127.0.0.2')
     proxy_server_port = proxy_server_socket.getsockname()[1]
     proxy_server = start_server(proxy_server_socket)
     proxy_user = '******'
     proxy_password = '******'
     gevent.sleep(2)
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key,
                                proxy_host='127.0.0.2',
                                proxy_port=proxy_server_port,
                                proxy_user=proxy_user,
                                proxy_password='******',
                                proxy_pkey=self.user_key,
                                )
     gevent.sleep(2)
     output = client.run_command(self.fake_cmd)
     stdout = list(output[self.host]['stdout'])
     expected_stdout = [self.fake_resp]
     self.assertEqual(expected_stdout, stdout,
                      msg="Got unexpected stdout - %s, expected %s" % (
                          stdout, expected_stdout,))
     self.assertEqual(client.host_clients[self.host].proxy_user, proxy_user)
     self.assertEqual(client.host_clients[self.host].proxy_password, proxy_password)
     self.assertTrue(client.host_clients[self.host].proxy_pkey)
     self.server.kill()
     proxy_server.kill()
Beispiel #13
0
 def test_ssh_exception(self):
     """Test that we get ssh exception in output with correct arguments"""
     self.server.kill()
     host = '127.0.0.10'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     server = start_server(_socket, ssh_exception=True)
     hosts = [host]
     client = ParallelSSHClient(hosts, port=port,
                                user='******', password='******',
                                pkey=paramiko.RSAKey.generate(1024))
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     gevent.sleep(1)
     client.pool.join()
     self.assertTrue('exception' in output[host],
                     msg="Got no exception for host %s - expected connection error" % (
                         host,))
     try:
         raise output[host]['exception']
     except SSHException, ex:
         self.assertEqual(ex.args[1], host,
                          msg="Exception host argument is %s, should be %s" % (
                              ex.args[1], host,))
         self.assertEqual(ex.args[2], port,
                          msg="Exception port argument is %s, should be %s" % (
                              ex.args[2], port,))
Beispiel #14
0
 def test_pssh_client_hosts_list_part_failure(self):
     """Test getting output for remainder of host list in the case where one
     host in the host list has a failure"""
     server2_socket = make_socket('127.0.0.2', port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket, fail_auth=True)
     hosts = [self.host, '127.0.0.2']
     client = ParallelSSHClient(hosts,
                                port=self.listen_port,
                                pkey=self.user_key,
                                agent=self.agent)
     output = client.run_command(self.fake_cmd,
                                 stop_on_errors=False)
     self.assertFalse(client.finished(output))
     client.join(output)
     self.assertTrue(client.finished(output))
     self.assertTrue(hosts[0] in output,
                     msg="Successful host does not exist in output - output is %s" % (output,))
     self.assertTrue(hosts[1] in output,
                     msg="Failed host does not exist in output - output is %s" % (output,))
     self.assertTrue('exception' in output[hosts[1]],
                     msg="Failed host %s has no exception in output - %s" % (hosts[1], output,))
     try:
         raise output[hosts[1]]['exception']
     except AuthenticationException:
         pass
     else:
         raise Exception("Expected AuthenticationException, got %s instead" % (
             output[hosts[1]]['exception'],))
     del client
     server2.kill()
Beispiel #15
0
 def test_host_config(self):
     """Test per-host configuration functionality of ParallelSSHClient"""
     hosts = ['127.0.0.%01d' % n for n in xrange(1,3)]
     host_config = dict.fromkeys(hosts)
     servers = []
     user = '******'
     password = '******'
     for host in hosts:
         _socket = make_socket(host)
         port = _socket.getsockname()[1]
         host_config[host] = {}
         host_config[host]['port'] = port
         host_config[host]['user'] = user
         host_config[host]['password'] = password
         server = start_server(_socket, fail_auth=hosts.index(host))
         servers.append((server, port))
     pkey_data = load_private_key(PKEY_FILENAME)
     host_config[hosts[0]]['private_key'] = pkey_data
     client = ParallelSSHClient(hosts, host_config=host_config)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.join(output)
     for host in hosts:
         self.assertTrue(host in output)
     try:
         raise output[hosts[1]]['exception']
     except AuthenticationException, ex:
         pass
Beispiel #16
0
 def test_pssh_client_hosts_list_part_failure(self):
     """Test getting output for remainder of host list in the case where one
     host in the host list has a failure"""
     server2_socket = make_socket('127.0.0.2', port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket, fail_auth=True)
     hosts = [self.host, '127.0.0.2']
     client = ParallelSSHClient(hosts,
                                port=self.listen_port,
                                pkey=self.user_key,
                                agent=self.agent)
     output = client.run_command(self.fake_cmd,
                                 stop_on_errors=False)
     client.join(output)
     self.assertTrue(hosts[0] in output,
                     msg="Successful host does not exist in output - output is %s" % (output,))
     self.assertTrue(hosts[1] in output,
                     msg="Failed host does not exist in output - output is %s" % (output,))
     self.assertTrue('exception' in output[hosts[1]],
                     msg="Failed host %s has no exception in output - %s" % (hosts[1], output,))
     try:
         raise output[hosts[1]]['exception']
     except AuthenticationException:
         pass
     else:
         raise Exception("Expected AuthenticationException, got %s instead" % (
             output[hosts[1]]['exception'],))
     del client
     server2.kill()
Beispiel #17
0
 def test_ssh_exception(self):
     """Test that we get ssh exception in output with correct arguments"""
     self.server.kill()
     host = '127.0.0.10'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     server = start_server(_socket, ssh_exception=True)
     hosts = [host]
     client = ParallelSSHClient(hosts, port=port,
                                user='******', password='******',
                                pkey=paramiko.RSAKey.generate(1024))
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     gevent.sleep(.2)
     client.pool.join()
     self.assertTrue('exception' in output[host],
                     msg="Got no exception for host %s - expected connection error" % (
                         host,))
     try:
         raise output[host]['exception']
     except SSHException, ex:
         self.assertEqual(ex.args[1], host,
                          msg="Exception host argument is %s, should be %s" % (
                              ex.args[1], host,))
         self.assertEqual(ex.args[2], port,
                          msg="Exception port argument is %s, should be %s" % (
                              ex.args[2], port,))
 def test_pssh_hosts_more_than_pool_size(self):
     """Test we can successfully run on more hosts than our pool size and
     get logs for all hosts"""
     # Make a second server on the same port as the first one
     server2_socket = make_socket("127.0.0.2", port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket)
     hosts = [self.host, "127.0.0.2"]
     client = ParallelSSHClient(hosts, port=self.listen_port, pkey=self.user_key, pool_size=1)
     output = client.run_command(self.fake_cmd)
     stdout = [list(output[k]["stdout"]) for k in output]
     expected_stdout = [[self.fake_resp], [self.fake_resp]]
     self.assertEqual(
         len(hosts),
         len(output),
         msg="Did not get output from all hosts. Got output for " "%s/%s hosts" % (len(output), len(hosts)),
     )
     self.assertEqual(
         expected_stdout,
         stdout,
         msg="Did not get expected output from all hosts. \
                      Got %s - expected %s"
         % (stdout, expected_stdout),
     )
     del client
     del server2
Beispiel #19
0
 def test_pssh_client_timeout(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server_timeout=0.2
     client_timeout=server_timeout-0.1
     server = start_server(listen_socket,
                           timeout=server_timeout)
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                timeout=client_timeout)
     output = client.run_command(self.fake_cmd)
     # Handle exception
     try:
         gevent.sleep(server_timeout+0.2)
         client.pool.join()
         if not server.exception:
             raise Exception(
                 "Expected gevent.Timeout from socket timeout, got none")
         raise server.exception
     except gevent.Timeout:
         pass
     # chan_timeout = output[self.host]['channel'].gettimeout()
     # self.assertEqual(client_timeout, chan_timeout,
     #                  msg="Channel timeout %s does not match requested timeout %s" %(
     #                      chan_timeout, client_timeout,))
     del client
     server.join()
Beispiel #20
0
 def test_authentication_exception(self):
     """Test that we get authentication exception in output with correct arguments"""
     self.server.kill()
     _socket = make_socket(self.host)
     port = _socket.getsockname()[1]
     server = start_server(_socket, fail_auth=True)
     hosts = [self.host]
     client = ParallelSSHClient(hosts,
                                port=port,
                                pkey=self.user_key,
                                agent=self.agent)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertTrue(
         'exception' in output[self.host],
         msg="Got no exception for host %s - expected connection error" %
         (self.host, ))
     try:
         raise output[self.host]['exception']
     except AuthenticationException, ex:
         self.assertEqual(
             ex.args[1],
             self.host,
             msg="Exception host argument is %s, should be %s" % (
                 ex.args[1],
                 self.host,
             ))
         self.assertEqual(
             ex.args[2],
             port,
             msg="Exception port argument is %s, should be %s" % (
                 ex.args[2],
                 port,
             ))
Beispiel #21
0
 def test_connection_error_exception(self):
     """Test that we get connection error exception in output with correct arguments"""
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     hosts = [host]
     client = ParallelSSHClient(hosts, port=port, pkey=self.user_key)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertTrue(
         'exception' in output[host],
         msg="Got no exception for host %s - expected connection error" %
         (host, ))
     try:
         raise output[host]['exception']
     except ConnectionErrorException, ex:
         self.assertEqual(
             ex.args[1],
             host,
             msg="Exception host argument is %s, should be %s" % (
                 ex.args[1],
                 host,
             ))
         self.assertEqual(
             ex.args[2],
             port,
             msg="Exception port argument is %s, should be %s" % (
                 ex.args[2],
                 port,
             ))
Beispiel #22
0
    def test_pssh_hosts_more_than_pool_size(self):
        """Test we can successfully run on more hosts than our pool size and
        get logs for all hosts"""
        # Make a second server on the same port as the first one
        server2_socket = make_socket('127.0.0.2', port=self.listen_port)
        server2_port = server2_socket.getsockname()[1]
        server2 = start_server(server2_socket)
        hosts = [self.host, '127.0.0.2']
        client = ParallelSSHClient(
            hosts,
            port=self.listen_port,
            pkey=self.user_key,
            pool_size=1,
        )
        output = client.run_command(self.fake_cmd)
        stdout = [list(output[k]['stdout']) for k in output]
        expected_stdout = [[self.fake_resp], [self.fake_resp]]
        self.assertEqual(
            len(hosts),
            len(output),
            msg="Did not get output from all hosts. Got output for \
%s/%s hosts" % (
                len(output),
                len(hosts),
            ))
        self.assertEqual(expected_stdout,
                         stdout,
                         msg="Did not get expected output from all hosts. \
                         Got %s - expected %s" % (
                             stdout,
                             expected_stdout,
                         ))
        del client
        del server2
Beispiel #23
0
 def test_pssh_client_timeout(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server_timeout=0.2
     client_timeout=server_timeout-0.1
     server = start_server(listen_socket,
                           timeout=server_timeout)
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                timeout=client_timeout)
     output = client.run_command(self.fake_cmd)
     # Handle exception
     try:
         gevent.sleep(server_timeout+0.2)
         client.pool.join()
         if not server.exception:
             raise Exception(
                 "Expected gevent.Timeout from socket timeout, got none")
         raise server.exception
     except gevent.Timeout:
         pass
     # chan_timeout = output[self.host]['channel'].gettimeout()
     # self.assertEqual(client_timeout, chan_timeout,
     #                  msg="Channel timeout %s does not match requested timeout %s" %(
     #                      chan_timeout, client_timeout,))
     del client
     server.kill()
Beispiel #24
0
 def setUp(self):
     self.fake_cmd = 'echo me'
     self.fake_resp = 'me'
     self.user_key = USER_KEY
     self.host = '127.0.0.1'
     self.listen_socket = make_socket(self.host)
     self.listen_port = self.listen_socket.getsockname()[1]
     self.server = start_server(self.listen_socket)
 def setUp(self):
     self.fake_cmd = "echo me"
     self.fake_resp = "me"
     self.user_key = USER_KEY
     self.host = "127.0.0.1"
     self.listen_socket = make_socket(self.host)
     self.listen_port = self.listen_socket.getsockname()[1]
     self.server = start_server(self.listen_socket)
Beispiel #26
0
 def test_sftp_exceptions(self):
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     self.assertRaises(ConnectionErrorException, SSHClient,
                       host, port=port, num_retries=1)
     del _socket
 def setUp(self):
     self.fake_cmd = 'echo "me"'
     self.fake_resp = 'me'
     self.long_cmd = lambda lines: 'for (( i=0; i<%s; i+=1 )) do echo $i; sleep 1; done' % (lines,)
     self.user_key = USER_KEY
     self.host = '127.0.0.1'
     self.listen_socket = make_socket(self.host)
     self.listen_port = self.listen_socket.getsockname()[1]
     self.server = start_server(self.listen_socket)
Beispiel #28
0
 def test_sftp_exceptions(self):
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     client = ParallelSSHClient([self.host], port=port, num_retries=1)
     cmds = client.copy_file("test", "test")
     client.pool.join()
     for cmd in cmds:
         self.assertRaises(ConnectionErrorException, cmd.get)
Beispiel #29
0
 def test_sftp_exceptions(self):
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     client = ParallelSSHClient([self.host], port=port, num_retries=1)
     cmds = client.copy_file("test", "test")
     client.pool.join()
     for cmd in cmds:
         self.assertRaises(ConnectionErrorException, cmd.get)
 def setUp(self):
     self.fake_cmd = 'echo "me"'
     self.fake_resp = "me"
     self.long_cmd = lambda lines: "for (( i=0; i<%s; i+=1 )) do echo $i; sleep 1; done" % (lines,)
     self.user_key = USER_KEY
     self.host = "127.0.0.1"
     self.listen_socket = make_socket(self.host)
     self.listen_port = self.listen_socket.getsockname()[1]
     self.server = start_server(self.listen_socket)
     self.agent = FakeAgent()
     self.agent.add_key(USER_KEY)
     self.client = ParallelSSHClient([self.host], port=self.listen_port, pkey=self.user_key, agent=self.agent)
Beispiel #31
0
 def test_pssh_client_ssh_exception(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket,
                           ssh_exception=True)
     client = ParallelSSHClient([self.host],
                                user='******', password='******',
                                port=listen_port,
                                pkey=paramiko.RSAKey.generate(1024),
                                )
     self.assertRaises(SSHException, client.run_command, self.fake_cmd)
     del client
     server.kill()
Beispiel #32
0
 def test_pssh_client_ssh_exception(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket,
                           ssh_exception=True)
     client = ParallelSSHClient([self.host],
                                user='******', password='******',
                                port=listen_port,
                                pkey=RSAKey.generate(1024),
                                )
     self.assertRaises(SSHException, client.run_command, self.fake_cmd)
     del client
     server.kill()
Beispiel #33
0
 def test_pssh_hosts_iterator_hosts_modification(self):
     """Test using iterator as host list and modifying host list in place"""
     server2_socket = make_socket('127.0.0.2', port=self.listen_port)
     server2_port = server2_socket.getsockname()[1]
     server2 = start_server(server2_socket)
     server3_socket = make_socket('127.0.0.3', port=self.listen_port)
     server3_port = server3_socket.getsockname()[1]
     server3 = start_server(server3_socket)
     hosts = [self.host, '127.0.0.2']
     client = ParallelSSHClient(
         iter(hosts),
         port=self.listen_port,
         pkey=self.user_key,
         pool_size=1,
     )
     output = client.run_command(self.fake_cmd)
     stdout = [list(output[k]['stdout']) for k in output]
     expected_stdout = [[self.fake_resp], [self.fake_resp]]
     self.assertEqual(len(hosts), len(output),
                      msg="Did not get output from all hosts. Got output for " \
                      "%s/%s hosts" % (len(output), len(hosts),))
     # Run again without re-assigning host list, should do nothing
     output = client.run_command(self.fake_cmd)
     self.assertFalse(hosts[0] in output,
                      msg="Expected no host output, got %s" % (output, ))
     self.assertFalse(output,
                      msg="Expected empty output, got %s" % (output, ))
     # Re-assigning host list with new hosts should work
     hosts = ['127.0.0.2', '127.0.0.3']
     client.hosts = iter(hosts)
     output = client.run_command(self.fake_cmd)
     self.assertEqual(len(hosts), len(output),
                      msg="Did not get output from all hosts. Got output for " \
                      "%s/%s hosts" % (len(output), len(hosts),))
     self.assertTrue(hosts[1] in output,
                     msg="Did not get output for new host %s" %
                     (hosts[1], ))
     del client, server2, server3
Beispiel #34
0
 def setUp(self):
     self.fake_cmd = 'echo "me"'
     self.fake_resp = 'me'
     self.long_cmd = lambda lines: 'for (( i=0; i<%s; i+=1 )) do echo $i; sleep 1; done' % (lines,)
     self.user_key = USER_KEY
     self.host = '127.0.0.1'
     self.listen_socket = make_socket(self.host)
     self.listen_port = self.listen_socket.getsockname()[1]
     self.server = start_server(self.listen_socket)
     self.agent = FakeAgent()
     self.agent.add_key(USER_KEY)
     self.client = ParallelSSHClient([self.host], port=self.listen_port,
                                     pkey=self.user_key,
                                     agent=self.agent)
 def test_pssh_client_auth_failure(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket, fail_auth=True)
     client = ParallelSSHClient([self.host], port=listen_port, pkey=self.user_key, agent=self.agent)
     cmd = client.exec_command(self.fake_cmd)[0]
     # Handle exception
     try:
         cmd.get()
         raise Exception("Expected AuthenticationException, got none")
     except AuthenticationException:
         pass
     del client
     server.join()
 def test_pssh_client_ssh_exception(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket, ssh_exception=True)
     client = ParallelSSHClient(
         [self.host], user="******", password="******", port=listen_port, pkey=paramiko.RSAKey.generate(1024)
     )
     # Handle exception
     try:
         client.run_command(self.fake_cmd)
         raise Exception("Expected SSHException, got none")
     except SSHException:
         pass
     del client
     server.join()
Beispiel #37
0
def example_hosts():
    '''Fixture providing two temporary SSH servers

    Returns a nightbus.ssh_config.SSHConfig instance.

    '''
    server_host_1 = '127.0.0.1'

    server_socket_1 = embedded_server.make_socket(server_host_1)
    server_listen_port_1 = server_socket_1.getsockname()[1]
    server_1 = embedded_server.start_server(server_socket_1)

    server_host_2 = '127.0.0.2'
    server_socket_2 = embedded_server.make_socket(server_host_2)
    server_listen_port_2 = server_socket_2.getsockname()[1]
    server_2 = embedded_server.start_server(server_socket_2)

    hosts = '''
        %s: { port: %s }
        %s: { port: %s }
    ''' % (server_host_1, server_listen_port_1, server_host_2,
           server_listen_port_2)

    return nightbus.ssh_config.SSHConfig(hosts)
Beispiel #38
0
 def test_pssh_client_retries(self):
     """Test connection error retries"""
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     expected_num_tries = 2
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                num_retries=expected_num_tries)
     self.assertRaises(ConnectionErrorException, client.run_command, 'blah')
     try:
         client.run_command('blah')
     except ConnectionErrorException, ex:
         num_tries = ex.args[-1:][0]
         self.assertEqual(expected_num_tries, num_tries,
                          msg="Got unexpected number of retries %s - expected %s"
                          % (num_tries, expected_num_tries,))
Beispiel #39
0
 def test_pssh_client_retries(self):
     """Test connection error retries"""
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     expected_num_tries = 2
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                num_retries=expected_num_tries)
     self.assertRaises(ConnectionErrorException, client.run_command, 'blah')
     try:
         client.run_command('blah')
     except ConnectionErrorException, ex:
         num_tries = ex.args[-1:][0]
         self.assertEqual(expected_num_tries, num_tries,
                          msg="Got unexpected number of retries %s - expected %s"
                          % (num_tries, expected_num_tries,))
Beispiel #40
0
 def test_pssh_client_auth_failure(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket, fail_auth=True)
     client = ParallelSSHClient([self.host], port=listen_port,
                                pkey=self.user_key,
                                agent=self.agent)
     cmd = client.exec_command(self.fake_cmd)[0]
     # Handle exception
     try:
         cmd.get()
         raise Exception("Expected AuthenticationException, got none")
     except AuthenticationException:
         pass
     del client
     server.kill()
 def test_connection_error_exception(self):
     """Test that we get connection error exception in output with correct arguments"""
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = "127.0.0.3"
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     hosts = [host]
     client = ParallelSSHClient(hosts, port=port, pkey=self.user_key)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertTrue(
         "exception" in output[host], msg="Got no exception for host %s - expected connection error" % (host,)
     )
     try:
         raise output[host]["exception"]
     except ConnectionErrorException, ex:
         self.assertEqual(ex.args[1], host, msg="Exception host argument is %s, should be %s" % (ex.args[1], host))
         self.assertEqual(ex.args[2], port, msg="Exception port argument is %s, should be %s" % (ex.args[2], port))
Beispiel #42
0
 def test_pssh_client_ssh_exception(self):
     listen_socket = make_socket(self.host)
     listen_port = listen_socket.getsockname()[1]
     server = start_server(listen_socket, ssh_exception=True)
     client = ParallelSSHClient(
         [self.host],
         user='******',
         password='******',
         port=listen_port,
         pkey=paramiko.RSAKey.generate(1024),
     )
     # Handle exception
     try:
         client.run_command(self.fake_cmd)
         raise Exception("Expected SSHException, got none")
     except SSHException:
         pass
     del client
     server.join()
 def test_authentication_exception(self):
     """Test that we get authentication exception in output with correct arguments"""
     self.server.kill()
     _socket = make_socket(self.host)
     port = _socket.getsockname()[1]
     server = start_server(_socket, fail_auth=True)
     hosts = [self.host]
     client = ParallelSSHClient(hosts, port=port, pkey=self.user_key, agent=self.agent)
     output = client.run_command(self.fake_cmd, stop_on_errors=False)
     client.pool.join()
     self.assertTrue(
         "exception" in output[self.host],
         msg="Got no exception for host %s - expected connection error" % (self.host,),
     )
     try:
         raise output[self.host]["exception"]
     except AuthenticationException, ex:
         self.assertEqual(
             ex.args[1], self.host, msg="Exception host argument is %s, should be %s" % (ex.args[1], self.host)
         )
         self.assertEqual(ex.args[2], port, msg="Exception port argument is %s, should be %s" % (ex.args[2], port))
 def test_ssh_proxy(self):
     """Test connecting to remote destination via SSH proxy
     client -> proxy -> destination
     Proxy SSH server accepts no commands and sends no responses, only
     proxies to destination. Destination accepts a command as usual."""
     proxy_server_socket = make_socket("127.0.0.2")
     proxy_server_port = proxy_server_socket.getsockname()[1]
     proxy_server = start_server(proxy_server_socket)
     gevent.sleep(2)
     client = ParallelSSHClient(
         [self.host], port=self.listen_port, pkey=self.user_key, proxy_host="127.0.0.2", proxy_port=proxy_server_port
     )
     gevent.sleep(2)
     output = client.run_command(self.fake_cmd)
     stdout = list(output[self.host]["stdout"])
     expected_stdout = [self.fake_resp]
     self.assertEqual(
         expected_stdout, stdout, msg="Got unexpected stdout - %s, expected %s" % (stdout, expected_stdout)
     )
     self.server.kill()
     proxy_server.kill()
 def make_random_port(self, host=None):
     host = self.host if not host else host
     listen_socket = make_socket(host)
     listen_port = listen_socket.getsockname()[1]
     del listen_socket
     return listen_port