Skip to content

Commit 1f431b2

Browse files
author
Dan
committed
Moved pssh_local.py example script to examples dir. Removed unused embedded server code. Minor test code update for race condition avoidance. Added unit tests for pssh.utils package. Updated pssh_local example script
1 parent 2e61443 commit 1f431b2

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

embedded_server/embedded_server.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from .stub_sftp import StubSFTPServer
4242
from .tunnel import Tunneler
4343
import gevent.subprocess
44-
# import gipc
4544

4645
logger = logging.getLogger("embedded_server")
4746
paramiko_logger = logging.getLogger('paramiko.transport')
@@ -208,14 +207,6 @@ def start_server(sock, fail_auth=False, ssh_exception=False,
208207
return gevent.spawn(listen, sock, fail_auth=fail_auth,
209208
timeout=timeout, ssh_exception=ssh_exception)
210209

211-
# def start_server_process(listen_ip, fail_auth=False, ssh_exception=False,
212-
# timeout=None):
213-
# p = gipc.start_process(target=_make_sock_start_server, args=(listen_ip,),
214-
# kwargs={'fail_auth': fail_auth,
215-
# 'timeout': timeout,
216-
# 'ssh_exception': ssh_exception,})
217-
# return p
218-
219210
if __name__ == "__main__":
220211
logging.basicConfig()
221212
logger.setLevel(logging.DEBUG)

pssh_local.py renamed to examples/pssh_local.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,40 @@
2020
and ParallelSSHClient.
2121
"""
2222

23-
from pssh import SSHClient, ParallelSSHClient
23+
from pssh import SSHClient, ParallelSSHClient, utils
2424
import logging
25+
from pprint import pprint
2526

26-
logger = logging.getLogger('pssh')
27-
28-
def _setup_logger(_logger):
29-
"""Setup default logger"""
30-
_handler = logging.StreamHandler()
31-
log_format = logging.Formatter(
32-
'%(name)s - %(asctime)s - %(levelname)s - %(message)s')
33-
_handler.setFormatter(log_format)
34-
_logger.addHandler(_handler)
35-
_logger.setLevel(logging.DEBUG)
27+
utils.enable_host_logger()
28+
utils.enable_logger(utils.logger)
3629

3730
def test():
3831
"""Perform ls and copy file with SSHClient on localhost"""
3932
client = SSHClient('localhost')
4033
channel, host, stdout, stderr = client.exec_command('ls -ltrh')
4134
for line in stdout:
42-
print line.strip()
35+
pprint(line.strip())
4336
client.copy_file('../test', 'test_dir/test')
4437

4538
def test_parallel():
46-
"""Perform ls and copy file with ParallelSSHClient on localhost"""
39+
"""Perform ls and copy file with ParallelSSHClient on localhost.
40+
41+
Two identical hosts cause the same command to be executed
42+
twice on the same host in two parallel connections.
43+
In printed output there will be two identical lines per printed per
44+
line of `ls -ltrh` output as output is printed by host_logger as it
45+
becomes available and commands are executed in parallel
46+
47+
Host output key is de-duplicated so that output for the two
48+
commands run on the same host(s) is not lost
49+
"""
4750
client = ParallelSSHClient(['localhost', 'localhost'])
4851
output = client.run_command('ls -ltrh')
49-
print output
50-
# cmds = client.copy_file('../test', 'test_dir/test')
51-
# client.pool.join()
52+
client.join(output)
53+
pprint(output)
54+
cmds = client.copy_file('../test', 'test_dir/test')
55+
client.pool.join()
5256

5357
if __name__ == "__main__":
54-
_setup_logger(logger)
55-
# test()
58+
test()
5659
test_parallel()

tests/test_pssh_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def test_ssh_exception(self):
583583
user='fakey', password='fakey',
584584
pkey=paramiko.RSAKey.generate(1024))
585585
output = client.run_command(self.fake_cmd, stop_on_errors=False)
586-
gevent.sleep(.2)
586+
gevent.sleep(1)
587587
client.pool.join()
588588
self.assertTrue('exception' in output[host],
589589
msg="Got no exception for host %s - expected connection error" % (

tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pssh import utils
2+
import unittest
3+
4+
5+
class ParallelSSHUtilsTest(unittest.TestCase):
6+
7+
def test_enabling_host_logger(self):
8+
utils.enable_host_logger()
9+
# And again to test only one handler is attached
10+
utils.enable_host_logger()
11+
self.assertTrue(len(utils.host_logger.handlers)==1)
12+
13+
def test_enabling_pssh_logger(self):
14+
utils.enable_logger(utils.logger)
15+
self.assertTrue(len(utils.logger.handlers)==1)

0 commit comments

Comments
 (0)