Skip to content

Commit 020af48

Browse files
committed
Made fake server asynchronous. Fixed exception handling in parallel tests. Added port to error message when connecting.
1 parent f28b71b commit 020af48

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

fake_server/fake_server.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
Server private key is hardcoded, server listen code inspired by demo_server.py in paramiko repository"""
77

88
import os
9-
import socket
9+
from gevent import socket
10+
import gevent.event
1011
import sys
11-
import threading
1212
import traceback
1313
import logging
1414
import paramiko
15-
import time
1615

1716
logger = logging.getLogger(__name__)
1817
paramiko_logger = logging.getLogger('paramiko.transport')
@@ -21,7 +20,7 @@
2120

2221
class Server (paramiko.ServerInterface):
2322
def __init__(self, cmd_req_response = {}, fail_auth = False):
24-
self.event = threading.Event()
23+
self.event = gevent.event.Event()
2524
self.cmd_req_response = cmd_req_response
2625
self.fail_auth = fail_auth
2726

@@ -68,10 +67,12 @@ def _make_socket(listen_ip, listen_port):
6867
def listen(cmd_req_response, listen_ip = '127.0.0.1', listen_port = 2200, fail_auth = False):
6968
"""Run a fake ssh server and given a cmd_to_run, send given response"""
7069
sock = _make_socket(listen_ip, listen_port)
71-
if not sock: return
70+
if not sock:
71+
logger.error("Could not establish listening connection on %s:%s", listen_ip, listen_port)
72+
return
7273
try:
7374
sock.listen(100)
74-
logger.info('Listening for connection ...')
75+
logger.info('Listening for connection on %s:%s..', listen_ip, listen_port)
7576
client, addr = sock.accept()
7677
except Exception, e:
7778
logger.error('*** Listen/accept failed: %s' % (str(e),))
@@ -103,7 +104,7 @@ def listen(cmd_req_response, listen_ip = '127.0.0.1', listen_port = 2200, fail_a
103104
chan.close()
104105
return
105106
while not chan.send_ready():
106-
time.sleep(.5)
107+
gevent.sleep(.5)
107108
chan.close()
108109

109110
except Exception, e:

pssh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _connect(self):
8181
raise UnknownHostException("%s - %s" % (str(e.args[1]), self.host,))
8282
except socket.error, e:
8383
logger.error("Error connecting to host '%s'" % (self.host,))
84-
raise ConnectionErrorException("%s for host '%s'" % (str(e.args[1]), self.host,))
84+
raise ConnectionErrorException("%s for host '%s:%s'" % (str(e.args[1]), self.host, self.port,))
8585

8686
def exec_command(self, command, sudo = False, **kwargs):
8787
"""Wrapper to paramiko.SSHClient.exec_command"""

tests/test_pssh_client.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import unittest
66
import gevent
7-
from gevent import monkey
8-
monkey.patch_all()
9-
from pssh import ParallelSSHClient, UnknownHostException, ConnectionErrorException
7+
from pssh import ParallelSSHClient, UnknownHostException, ConnectionErrorException, _setup_logger
108
from paramiko import AuthenticationException
11-
from fake_server.fake_server import listen
9+
from fake_server.fake_server import listen, logger as server_logger
1210
import random
11+
import logging
12+
13+
# _setup_logger(server_logger)
1314

1415
class ParallelSSHClientTest(unittest.TestCase):
1516

@@ -18,21 +19,31 @@ def setUp(self):
1819
self.fake_resp = 'fake response'
1920

2021
def test_pssh_client_exec_command(self):
21-
listen_port = random.randint(1025, 65534)
22+
listen_port = random.randint(1026, 65534)
2223
server = gevent.spawn(listen, { self.fake_cmd : self.fake_resp }, listen_port = listen_port)
23-
client = ParallelSSHClient(['localhost'], port = listen_port)
24+
gevent.sleep(.1)
25+
client = ParallelSSHClient(['localhost'], port=listen_port)
2426
cmd = client.exec_command(self.fake_cmd)[0]
2527
output = client.get_stdout(cmd)
2628
expected = {'localhost' : {'exit_code' : 0}}
27-
self.assertEqual(expected, output, msg = "Got unexpected command output - %s" % (output,))
29+
self.assertEqual(expected, output,
30+
msg = "Got unexpected command output - %s" % (output,))
2831
server.kill()
32+
del client
2933

3034
def test_pssh_client_auth_failure(self):
31-
listen_port = random.randint(1025, 65534)
32-
server = gevent.spawn(listen, { self.fake_cmd : self.fake_resp }, listen_port = listen_port, fail_auth = True)
33-
client = ParallelSSHClient(['localhost'], port = listen_port)
35+
listen_port = random.randint(2048, 65534)
36+
server = gevent.spawn(listen, { self.fake_cmd : self.fake_resp },
37+
listen_port=listen_port, fail_auth=True, )# link_callback=)
38+
gevent.sleep(.1)
39+
client = ParallelSSHClient(['localhost'], port=listen_port)
40+
cmd = client.exec_command(self.fake_cmd)[0]
41+
# Handle exception
3442
try:
35-
cmd = client.exec_command(self.fake_cmd)[0]
43+
cmd.get()
44+
raise Exception("Expected AuthenticationException, got none")
3645
except AuthenticationException:
3746
pass
3847
server.kill()
48+
del client
49+

0 commit comments

Comments
 (0)