Skip to content

Commit f69c341

Browse files
author
Dan
committed
Made fake server port an optional parameter. Added test for expected output behaviour when pool size is less than number of hosts - resolves #25
1 parent f568e30 commit f69c341

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

fake_server/fake_server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ def _long_running_response(self, channel, responder):
119119
channel.send_exit_status(0)
120120
channel.close()
121121

122-
def make_socket(listen_ip):
122+
def make_socket(listen_ip, port=0):
123123
"""Make socket on given address and available port chosen by OS"""
124124
try:
125125
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
126126
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
127-
sock.bind((listen_ip, 0))
127+
sock.bind((listen_ip, port))
128128
except Exception, e:
129129
logger.error('Failed to bind to address - %s' % (str(e),))
130130
traceback.print_exc()

pssh_local.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ def test():
4444

4545
def test_parallel():
4646
"""Perform ls and copy file with ParallelSSHClient on localhost"""
47-
client = ParallelSSHClient(['localhost'])
48-
cmds = client.exec_command('ls -ltrh')
49-
output = [client.get_stdout(cmd, return_buffers=True) for cmd in cmds]
47+
client = ParallelSSHClient(['localhost', 'localhost'])
48+
output = client.run_command('ls -ltrh')
5049
print output
51-
cmds = client.copy_file('../test', 'test_dir/test')
52-
client.pool.join()
50+
# cmds = client.copy_file('../test', 'test_dir/test')
51+
# client.pool.join()
5352

5453
if __name__ == "__main__":
5554
_setup_logger(logger)
56-
test()
55+
# test()
5756
test_parallel()

tests/test_pssh_client.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_pssh_client_exec_command_password(self):
231231
msg="Got unexpected command output - %s" % (output,))
232232
del client
233233
server.join()
234-
234+
235235
def test_pssh_client_long_running_command(self):
236236
expected_lines = 5
237237
server = start_server({ self.long_running_cmd :
@@ -314,8 +314,32 @@ def test_pssh_pool_size(self):
314314
def test_pssh_hosts_more_than_pool_size(self):
315315
"""Test we can successfully run on more hosts than our pool size and
316316
get logs for all hosts"""
317-
raise NotImplementedError
318-
317+
# Make a second server on the same port as the first one
318+
server2_socket = make_socket('127.0.0.2', port=self.listen_port)
319+
server2_port = server2_socket.getsockname()[1]
320+
server1 = start_server({ self.fake_cmd : self.fake_resp },
321+
self.listen_socket)
322+
server2 = start_server({ self.fake_cmd : self.fake_resp },
323+
server2_socket)
324+
hosts = ['127.0.0.1', '127.0.0.2']
325+
client = ParallelSSHClient(hosts,
326+
port=self.listen_port,
327+
pkey=self.user_key,
328+
pool_size=1,
329+
)
330+
output = client.run_command(self.fake_cmd)
331+
stdout = [list(output[k]['stdout']) for k in output]
332+
expected_stdout = [[self.fake_resp], [self.fake_resp]]
333+
self.assertEqual(len(hosts), len(output),
334+
msg="Did not get output from all hosts. Got output for \
335+
%s/%s hosts" % (len(output), len(hosts),))
336+
self.assertEqual(expected_stdout, stdout,
337+
msg="Did not get expected output from all hosts. \
338+
Got %s - expected %s" % (stdout, expected_stdout,))
339+
del client
340+
server1.kill()
341+
server2.kill()
342+
319343
def test_ssh_proxy(self):
320344
"""Test connecting to remote destination via SSH proxy
321345
client -> proxy -> destination

0 commit comments

Comments
 (0)