From e1d09fe4b0f4ab8eee5e923dd63ae08155334e80 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 4 Feb 2016 19:56:51 +0200 Subject: Make NoValidConnectionsError picklable correctly Fixes #617. --- tests/test_ssh_exception.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/test_ssh_exception.py (limited to 'tests/test_ssh_exception.py') diff --git a/tests/test_ssh_exception.py b/tests/test_ssh_exception.py new file mode 100644 index 00000000..2d1f899f --- /dev/null +++ b/tests/test_ssh_exception.py @@ -0,0 +1,15 @@ +import pickle +import unittest + +from paramiko.ssh_exception import NoValidConnectionsError + + +class NoValidConnectionsErrorTest (unittest.TestCase): + + def test_pickling(self): + # Regression test for https://github.com/paramiko/paramiko/issues/617 + exc = NoValidConnectionsError({'ab': ''}) + new_exc = pickle.loads(pickle.dumps(exc)) + self.assertEqual(type(exc), type(new_exc)) + self.assertEqual(str(exc), str(new_exc)) + self.assertEqual(exc.args, new_exc.args) -- cgit v1.2.3 From 0411010d55755913fa7bd5b0a9c719c8548549f4 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Fri, 5 Feb 2016 08:43:50 +0200 Subject: Improve NoValidConnectionsError formatting Because "Unable to connect to port 22 on or X.X.X.X" looks seriously _weird_ with the blank space between "on" and "or". --- paramiko/ssh_exception.py | 7 +++++-- tests/test_ssh_exception.py | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'tests/test_ssh_exception.py') diff --git a/paramiko/ssh_exception.py b/paramiko/ssh_exception.py index bdf97e24..fea3146a 100644 --- a/paramiko/ssh_exception.py +++ b/paramiko/ssh_exception.py @@ -164,10 +164,13 @@ class NoValidConnectionsError(socket.error): :param dict errors: The errors dict to store, as described by class docstring. """ - addrs = list(errors.keys()) + addrs = sorted(errors.keys()) body = ', '.join([x[0] for x in addrs[:-1]]) tail = addrs[-1][0] - msg = "Unable to connect to port {0} on {1} or {2}" + if body: + msg = "Unable to connect to port {0} on {1} or {2}" + else: + msg = "Unable to connect to port {0} on {2}" super(NoValidConnectionsError, self).__init__( None, # stand-in for errno msg.format(addrs[0][1], body, tail) diff --git a/tests/test_ssh_exception.py b/tests/test_ssh_exception.py index 2d1f899f..9c109de1 100644 --- a/tests/test_ssh_exception.py +++ b/tests/test_ssh_exception.py @@ -8,8 +8,23 @@ class NoValidConnectionsErrorTest (unittest.TestCase): def test_pickling(self): # Regression test for https://github.com/paramiko/paramiko/issues/617 - exc = NoValidConnectionsError({'ab': ''}) + exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception()}) new_exc = pickle.loads(pickle.dumps(exc)) self.assertEqual(type(exc), type(new_exc)) self.assertEqual(str(exc), str(new_exc)) self.assertEqual(exc.args, new_exc.args) + + def test_error_message_for_single_host(self): + exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception()}) + self.assertIn("Unable to connect to port 22 on 127.0.0.1", str(exc)) + + def test_error_message_for_two_hosts(self): + exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception(), + ('::1', '22'): Exception()}) + self.assertIn("Unable to connect to port 22 on 127.0.0.1 or ::1", str(exc)) + + def test_error_message_for_multiple_hosts(self): + exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception(), + ('::1', '22'): Exception(), + ('10.0.0.42', '22'): Exception()}) + self.assertIn("Unable to connect to port 22 on 10.0.0.42, 127.0.0.1 or ::1", str(exc)) -- cgit v1.2.3 From d3af63ac5edc06cb07ad3f2afb5a30a2f79713b6 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Sun, 24 Apr 2016 14:00:44 -0700 Subject: Python 2.6 fix re: assertIn --- tests/test_ssh_exception.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'tests/test_ssh_exception.py') diff --git a/tests/test_ssh_exception.py b/tests/test_ssh_exception.py index 9c109de1..18f2a97d 100644 --- a/tests/test_ssh_exception.py +++ b/tests/test_ssh_exception.py @@ -16,15 +16,16 @@ class NoValidConnectionsErrorTest (unittest.TestCase): def test_error_message_for_single_host(self): exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception()}) - self.assertIn("Unable to connect to port 22 on 127.0.0.1", str(exc)) + assert "Unable to connect to port 22 on 127.0.0.1" in str(exc) def test_error_message_for_two_hosts(self): exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception(), ('::1', '22'): Exception()}) - self.assertIn("Unable to connect to port 22 on 127.0.0.1 or ::1", str(exc)) + assert "Unable to connect to port 22 on 127.0.0.1 or ::1" in str(exc) def test_error_message_for_multiple_hosts(self): exc = NoValidConnectionsError({('127.0.0.1', '22'): Exception(), ('::1', '22'): Exception(), ('10.0.0.42', '22'): Exception()}) - self.assertIn("Unable to connect to port 22 on 10.0.0.42, 127.0.0.1 or ::1", str(exc)) + exp = "Unable to connect to port 22 on 10.0.0.42, 127.0.0.1 or ::1" + assert exp in str(exc) -- cgit v1.2.3