From e1d9a52ac4bf4ae4f233b0bffee783375ac0b839 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 11:32:19 -0600 Subject: [PATCH 01/14] Added more exception handling. --- SoftLayer/transports.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 3aa896f11..b9249adb4 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -379,7 +379,12 @@ def __call__(self, request): request.url = resp.url resp.raise_for_status() - result = json.loads(resp.text) + + if resp.text != "": + result = json.loads(resp.text) + else: + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response." ) + request.result = result if isinstance(result, list): @@ -388,8 +393,14 @@ def __call__(self, request): else: return result except requests.HTTPError as ex: - message = json.loads(ex.response.text)['error'] - request.url = ex.response.url + try: + message = json.loads(ex.response.text)['error'] + request.url = ex.response.url + except: + if ex.response.text == "": + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response." ) + else: + raise exceptions.SoftLayerAPIError(resp.status_code, ex.response.text ) raise exceptions.SoftLayerAPIError(ex.response.status_code, message) except requests.RequestException as ex: raise exceptions.TransportError(0, str(ex)) From 3b5c37fe405740a73d56e58ecf0063996c7e8396 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 13:48:17 -0600 Subject: [PATCH 02/14] Formating changes. --- SoftLayer/transports.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index b9249adb4..2bb4455a8 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -384,7 +384,7 @@ def __call__(self, request): result = json.loads(resp.text) else: raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response." ) - + request.result = result if isinstance(result, list): From ba14a925bc6ca16e5ad0c6cd5e8f281d1a64c497 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 14:03:59 -0600 Subject: [PATCH 03/14] More minor changes. --- SoftLayer/transports.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 2bb4455a8..74371a2ed 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -381,9 +381,9 @@ def __call__(self, request): resp.raise_for_status() if resp.text != "": - result = json.loads(resp.text) + result = json.loads(resp.text) else: - raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response." ) + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") request.result = result @@ -394,14 +394,14 @@ def __call__(self, request): return result except requests.HTTPError as ex: try: - message = json.loads(ex.response.text)['error'] - request.url = ex.response.url - except: - if ex.response.text == "": - raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response." ) - else: - raise exceptions.SoftLayerAPIError(resp.status_code, ex.response.text ) - raise exceptions.SoftLayerAPIError(ex.response.status_code, message) + message = json.loads(ex.response.text)['error'] + request.url = ex.response.url + except Exception: + if ex.response.text == "": + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") + else: + raise exceptions.SoftLayerAPIError(resp.status_code, ex.response.text) + raise exceptions.SoftLayerAPIError(ex.response.status_code, message) except requests.RequestException as ex: raise exceptions.TransportError(0, str(ex)) From c81e791cdee2508c559c8b05a68e43c6b5f128c4 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 14:53:58 -0600 Subject: [PATCH 04/14] Fixes for tox issues. --- SoftLayer/exceptions.py | 7 ------- SoftLayer/shell/core.py | 1 - SoftLayer/testing/__init__.py | 2 -- SoftLayer/transports.py | 2 +- 4 files changed, 1 insertion(+), 11 deletions(-) diff --git a/SoftLayer/exceptions.py b/SoftLayer/exceptions.py index 5652730fa..b3530aa8c 100644 --- a/SoftLayer/exceptions.py +++ b/SoftLayer/exceptions.py @@ -57,34 +57,27 @@ class TransportError(SoftLayerAPIError): # XMLRPC Errors class NotWellFormed(ParseError): """Request was not well formed.""" - pass class UnsupportedEncoding(ParseError): """Encoding not supported.""" - pass class InvalidCharacter(ParseError): """There was an invalid character.""" - pass class SpecViolation(ServerError): """There was a spec violation.""" - pass class MethodNotFound(SoftLayerAPIError): """Method name not found.""" - pass class InvalidMethodParameters(SoftLayerAPIError): """Invalid method paramters.""" - pass class InternalError(ServerError): """Internal Server Error.""" - pass diff --git a/SoftLayer/shell/core.py b/SoftLayer/shell/core.py index ed90f9c95..32c250584 100644 --- a/SoftLayer/shell/core.py +++ b/SoftLayer/shell/core.py @@ -26,7 +26,6 @@ class ShellExit(Exception): """Exception raised to quit the shell.""" - pass @click.command() diff --git a/SoftLayer/testing/__init__.py b/SoftLayer/testing/__init__.py index 477815725..d5279c03f 100644 --- a/SoftLayer/testing/__init__.py +++ b/SoftLayer/testing/__init__.py @@ -96,11 +96,9 @@ def tearDownClass(cls): def set_up(self): """Aliased from setUp.""" - pass def tear_down(self): """Aliased from tearDown.""" - pass def setUp(self): # NOQA testtools.TestCase.setUp(self) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 74371a2ed..a17da8f8c 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -396,7 +396,7 @@ def __call__(self, request): try: message = json.loads(ex.response.text)['error'] request.url = ex.response.url - except Exception: + except json.JSONDecodeError: if ex.response.text == "": raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") else: From e4a51a9f19c84951d6485dedabb7b018d6bcdeb7 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 15:18:36 -0600 Subject: [PATCH 05/14] More updates due to changes in TOX. --- SoftLayer/CLI/order/place.py | 2 +- SoftLayer/CLI/virt/capacity/__init__.py | 1 - SoftLayer/__init__.py | 2 +- SoftLayer/testing/xmlrpc.py | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/SoftLayer/CLI/order/place.py b/SoftLayer/CLI/order/place.py index 6d51ab935..4e9608c98 100644 --- a/SoftLayer/CLI/order/place.py +++ b/SoftLayer/CLI/order/place.py @@ -83,7 +83,7 @@ def cli(env, package_keyname, location, preset, verify, billing, complex_type, 'extras': extras, 'quantity': 1, 'complex_type': complex_type, - 'hourly': True if billing == 'hourly' else False} + 'hourly': bool(billing == 'hourly')} if verify: table = formatting.Table(COLUMNS) diff --git a/SoftLayer/CLI/virt/capacity/__init__.py b/SoftLayer/CLI/virt/capacity/__init__.py index 2b10885df..3f891c194 100644 --- a/SoftLayer/CLI/virt/capacity/__init__.py +++ b/SoftLayer/CLI/virt/capacity/__init__.py @@ -45,4 +45,3 @@ def get_command(self, ctx, cmd_name): @click.group(cls=CapacityCommands, context_settings=CONTEXT) def cli(): """Base command for all capacity related concerns""" - pass diff --git a/SoftLayer/__init__.py b/SoftLayer/__init__.py index 3e79f6cd4..a3787f556 100644 --- a/SoftLayer/__init__.py +++ b/SoftLayer/__init__.py @@ -14,7 +14,7 @@ :license: MIT, see LICENSE for more details. """ -# pylint: disable=w0401,invalid-name +# pylint: disable=r0401,invalid-name from SoftLayer import consts from SoftLayer.API import * # NOQA diff --git a/SoftLayer/testing/xmlrpc.py b/SoftLayer/testing/xmlrpc.py index 257a6be75..bd74afe93 100644 --- a/SoftLayer/testing/xmlrpc.py +++ b/SoftLayer/testing/xmlrpc.py @@ -80,7 +80,6 @@ def do_POST(self): def log_message(self, fmt, *args): """Override log_message.""" - pass def _item_by_key_postfix(dictionary, key_prefix): From 4660a2dd0df536ccb3bf223aef58fc27ae222fbd Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 15:30:00 -0600 Subject: [PATCH 06/14] Fixed exception login after failing unit tests. --- SoftLayer/transports.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index a17da8f8c..36abdcee8 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -401,7 +401,8 @@ def __call__(self, request): raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") else: raise exceptions.SoftLayerAPIError(resp.status_code, ex.response.text) - raise exceptions.SoftLayerAPIError(ex.response.status_code, message) + + raise exceptions.SoftLayerAPIError(ex.response.status_code, message) except requests.RequestException as ex: raise exceptions.TransportError(0, str(ex)) From e9b68617d0a734575d9e5306cdb776e40d8fdf26 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 15:41:33 -0600 Subject: [PATCH 07/14] Updates to message handling. --- SoftLayer/transports.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 36abdcee8..2fc6902cc 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -396,11 +396,11 @@ def __call__(self, request): try: message = json.loads(ex.response.text)['error'] request.url = ex.response.url - except json.JSONDecodeError: + except Exception as json_ex: if ex.response.text == "": raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") else: - raise exceptions.SoftLayerAPIError(resp.status_code, ex.response.text) + raise exceptions.SoftLayerAPIError(resp.status_code, str(json_ex)) raise exceptions.SoftLayerAPIError(ex.response.status_code, message) except requests.RequestException as ex: From 0ceab623e3270f2ae99feeea04d100e57d6a1cc6 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 15:55:59 -0600 Subject: [PATCH 08/14] Adjusted exception handler. --- SoftLayer/transports.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 2fc6902cc..e72c08080 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -396,7 +396,7 @@ def __call__(self, request): try: message = json.loads(ex.response.text)['error'] request.url = ex.response.url - except Exception as json_ex: + except ValueError as json_ex: if ex.response.text == "": raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") else: From 08b6ee49713a31f08a0518652908f43f5fb675a5 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Thu, 31 Jan 2019 16:06:11 -0600 Subject: [PATCH 09/14] Renforced a pylint exception. --- SoftLayer/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SoftLayer/__init__.py b/SoftLayer/__init__.py index a3787f556..04ba36aaa 100644 --- a/SoftLayer/__init__.py +++ b/SoftLayer/__init__.py @@ -14,7 +14,8 @@ :license: MIT, see LICENSE for more details. """ -# pylint: disable=r0401,invalid-name +# pylint: disable=r0401,invalid-name,wildcard-import +# NOQA appears to no longer be working. The code might have been upgraded. from SoftLayer import consts from SoftLayer.API import * # NOQA From 63012e8a2d5f54961ab95bdc39315604fc7bf32c Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Sun, 3 Feb 2019 11:50:15 -0600 Subject: [PATCH 10/14] Added unit tests, and updated exception handling. --- SoftLayer/transports.py | 5 ++++- tests/transport_tests.py | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index e72c08080..616339738 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -381,7 +381,10 @@ def __call__(self, request): resp.raise_for_status() if resp.text != "": - result = json.loads(resp.text) + try: + result = json.loads(resp.text) + except ValueError as json_ex: + raise exceptions.SoftLayerAPIError(resp.status_code, str(json_ex)) else: raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") diff --git a/tests/transport_tests.py b/tests/transport_tests.py index 87a43de62..a14ec9238 100644 --- a/tests/transport_tests.py +++ b/tests/transport_tests.py @@ -349,15 +349,29 @@ def test_basic(self, request): timeout=None) @mock.patch('SoftLayer.transports.requests.Session.request') - def test_error(self, request): + def test_http_and_json_error(self, request): # Test JSON Error e = requests.HTTPError('error') e.response = mock.MagicMock() e.response.status_code = 404 - e.response.text = '''{ + e.response.text = ''' "error": "description", "code": "Error Code" - }''' + ''' + request().raise_for_status.side_effect = e + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_http_and_empty_error(self, request): + # Test JSON Error + e = requests.HTTPError('error') + e.response = mock.MagicMock() + e.response.status_code = 404 + e.response.text = '' request().raise_for_status.side_effect = e req = transports.Request() @@ -365,6 +379,26 @@ def test_error(self, request): req.method = 'Resource' self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_empty_error(self, request): + # Test empty response error. + request().text = '' + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_json_error(self, request): + # Test non-json response error. + request().text = 'Not JSON' + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + def test_proxy_without_protocol(self): req = transports.Request() req.service = 'SoftLayer_Service' From 980d11c41ba6406687385f79a8d3d603c5d65699 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Mon, 4 Feb 2019 09:56:51 -0600 Subject: [PATCH 11/14] Added initial unit tests for percentages. --- tests/CLI/modules/shell_tests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/CLI/modules/shell_tests.py diff --git a/tests/CLI/modules/shell_tests.py b/tests/CLI/modules/shell_tests.py new file mode 100644 index 000000000..349ee4094 --- /dev/null +++ b/tests/CLI/modules/shell_tests.py @@ -0,0 +1,26 @@ +""" + SoftLayer.tests.CLI.modules.summary_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. +""" +from SoftLayer import testing + +import json +import mock +import io +import shlex + +from prompt_toolkit.shortcuts import prompt +from SoftLayer.shell import core + +class ShellTests(testing.TestCase): + def test_shell(self): + result = self.run_command(['shell']) + self.assertIsInstance(result.exception, io.UnsupportedOperation) + + @mock.patch('prompt_toolkit.shortcuts.prompt') + def test_shell_quit(self, prompt): + prompt.return_value = "quit" + result = self.run_command(['shell']) + self.assertEqual(result.exit_code, 0) From effc9ffbef956d3a9cabf3e8c050b763e1a3c330 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Mon, 4 Feb 2019 10:02:33 -0600 Subject: [PATCH 12/14] Format changes. --- tests/CLI/modules/shell_tests.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/CLI/modules/shell_tests.py b/tests/CLI/modules/shell_tests.py index 349ee4094..a8979bd2c 100644 --- a/tests/CLI/modules/shell_tests.py +++ b/tests/CLI/modules/shell_tests.py @@ -6,13 +6,9 @@ """ from SoftLayer import testing -import json -import mock import io -import shlex +import mock -from prompt_toolkit.shortcuts import prompt -from SoftLayer.shell import core class ShellTests(testing.TestCase): def test_shell(self): From 7c2362712300b9d48c884b1ab26c86857157466c Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Mon, 4 Feb 2019 10:15:34 -0600 Subject: [PATCH 13/14] More changes for unit tests and lent. --- tests/CLI/modules/shell_tests.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/CLI/modules/shell_tests.py b/tests/CLI/modules/shell_tests.py index a8979bd2c..98ff8e60d 100644 --- a/tests/CLI/modules/shell_tests.py +++ b/tests/CLI/modules/shell_tests.py @@ -6,15 +6,10 @@ """ from SoftLayer import testing -import io import mock class ShellTests(testing.TestCase): - def test_shell(self): - result = self.run_command(['shell']) - self.assertIsInstance(result.exception, io.UnsupportedOperation) - @mock.patch('prompt_toolkit.shortcuts.prompt') def test_shell_quit(self, prompt): prompt.return_value = "quit" From 5e6d45f01f6a8adfc830164cda2b5022314a4b17 Mon Sep 17 00:00:00 2001 From: Erick Sapp Date: Mon, 4 Feb 2019 10:25:57 -0600 Subject: [PATCH 14/14] Updated documentation line. --- tests/CLI/modules/shell_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CLI/modules/shell_tests.py b/tests/CLI/modules/shell_tests.py index 98ff8e60d..5f4b82c03 100644 --- a/tests/CLI/modules/shell_tests.py +++ b/tests/CLI/modules/shell_tests.py @@ -1,5 +1,5 @@ """ - SoftLayer.tests.CLI.modules.summary_tests + SoftLayer.tests.CLI.modules.shell_tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :license: MIT, see LICENSE for more details.