Skip to content

Commit c5b5790

Browse files
author
Pan
committed
Added native client tunnel initilisation exception handling and test. Show exception from tunnel in ProxyError exception on tunnel initilisation failures. Resolves #121
1 parent a3f0f47 commit c5b5790

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

Changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
============
33

4+
1.6.2
5+
++++++
6+
7+
Fixes
8+
------
9+
10+
* Native client proxy initilisation failures were not caught by ``stop_on_errors=False`` - #121.
11+
412
1.6.1
513
+++++++
614

pssh/clients/native/parallel.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,15 @@ def _start_tunnel(self, host):
301301
allow_agent=self.allow_agent)
302302
tunnel.daemon = True
303303
tunnel.start()
304-
self._tunnels[host] = tunnel
305304
while not tunnel.tunnel_open.is_set():
306305
logger.debug("Waiting for tunnel to become active")
307306
sleep(.1)
308307
if not tunnel.is_alive():
309-
msg = "Proxy authentication failed"
310-
logger.error(msg)
311-
raise ProxyError(msg)
308+
msg = "Proxy authentication failed. " \
309+
"Exception from tunnel client: %s"
310+
logger.error(msg, tunnel.exception)
311+
raise ProxyError(msg, tunnel.exception)
312+
self._tunnels[host] = tunnel
312313
return tunnel
313314

314315
def _make_ssh_client(self, host):

pssh/tunnel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(self, host, fw_host, fw_port, user=None,
5555
self.retry_delay = retry_delay
5656
self.allow_agent = allow_agent
5757
self.timeout = timeout
58+
self.exception = None
5859
self.tunnel_open = Event()
5960

6061
def _read_forward_sock(self):
@@ -120,8 +121,13 @@ def _init_tunnel_client(self):
120121
self.session = self.client.session
121122

122123
def run(self):
123-
self._init_tunnel_client()
124-
self._init_tunnel_sock()
124+
try:
125+
self._init_tunnel_client()
126+
self._init_tunnel_sock()
127+
except Exception as ex:
128+
logger.error("Tunnel initilisation failed with %s", ex)
129+
self.exception = ex
130+
return
125131
logger.debug("Hub in run function: %s", get_hub())
126132
try:
127133
while True:

tests/test_native_parallel_client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
from pssh.pssh2_client import ParallelSSHClient, logger as pssh_logger
4040
from pssh.exceptions import UnknownHostException, \
4141
AuthenticationException, ConnectionErrorException, SessionError, \
42-
HostArgumentException, SFTPError, SFTPIOError, Timeout, SCPError
42+
HostArgumentException, SFTPError, SFTPIOError, Timeout, SCPError, \
43+
ProxyError
4344

4445
from .embedded_server.embedded_server import make_socket
4546
from .embedded_server.openssh import OpenSSHServer
@@ -1387,6 +1388,7 @@ def test_scp_send_dir(self):
13871388
try:
13881389
cmds = self.client.scp_send(local_filename, remote_filename)
13891390
gevent.joinall(cmds, raise_error=True)
1391+
time.sleep(.2)
13901392
self.assertTrue(os.path.isdir(remote_test_dir_abspath))
13911393
self.assertTrue(os.path.isfile(remote_file_abspath))
13921394
remote_file_data = open(remote_file_abspath, 'r').read()
@@ -1507,8 +1509,22 @@ def test_tunnel(self):
15071509
proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
15081510
proxy_pkey=self.user_key,
15091511
timeout=2)
1510-
output = client.run_command('echo me', stop_on_errors=False)
1512+
output = client.run_command(self.cmd, stop_on_errors=False)
15111513
self.assertEqual(self.host, list(output.keys())[0])
1514+
del client
1515+
server.stop()
1516+
1517+
def test_tunnel_init_failure(self):
1518+
proxy_host = '127.0.0.20'
1519+
client = ParallelSSHClient(
1520+
[self.host], port=self.port, pkey=self.user_key,
1521+
proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
1522+
proxy_pkey=self.user_key,
1523+
timeout=2)
1524+
output = client.run_command(self.cmd, stop_on_errors=False)
1525+
exc = output[self.host].exception
1526+
self.assertIsInstance(exc, ProxyError)
1527+
self.assertIsInstance(exc.args[1], ConnectionErrorException)
15121528

15131529
# def test_proxy_remote_host_failure_timeout(self):
15141530
# """Test that timeout setting is passed on to proxy to be used for the

0 commit comments

Comments
 (0)