def test_per_host_dict_args(self): host2, host3 = '127.0.0.2', '127.0.0.3' server2, _ = start_server_from_ip(host2, port=self.listen_port) server3, _ = start_server_from_ip(host3, port=self.listen_port) hosts = [self.host, host2, host3] 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, num_retries=1) 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]]) client.hosts = hosts
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]])
def test_pssh_hosts_iterator_hosts_modification(self): """Test using iterator as host list and modifying host list in place""" host2, host3 = '127.0.0.2', '127.0.0.3' server2, _ = start_server_from_ip(host2, port=self.listen_port) server3, _ = start_server_from_ip(host3, port=self.listen_port) 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.kill() server3.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