From d4971eb7b7c0f58d6d0b8f8d57d2751a736a2f2d Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Wed, 8 Sep 2021 12:28:52 -0400 Subject: [PATCH 1/6] Preventing error if we were unable to get an API version --- .../plugins/module_utils/controller_api.py | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index aefe0d5d01..c27086d74f 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -261,22 +261,25 @@ class ControllerAPIModule(ControllerModule): controller_version = response.info().getheader('X-API-Product-Version', None) parsed_collection_version = Version(self._COLLECTION_VERSION).version - parsed_controller_version = Version(controller_version).version - if controller_type == 'AWX': - collection_compare_ver = parsed_collection_version[0] - controller_compare_ver = parsed_controller_version[0] + if(not controller_version): + self.warn("You are using the {0} version of this collection but connecting to a controller that did not return a version".format(self._COLLECTION_TYPE)) else: - collection_compare_ver = "{0}.{1}".format(parsed_collection_version[0], parsed_collection_version[1]) - controller_compare_ver = '{0}.{1}'.format(parsed_controller_version[0], parsed_controller_version[1]) + parsed_controller_version = Version(controller_version).version + if controller_type == 'AWX': + collection_compare_ver = parsed_collection_version[0] + controller_compare_ver = parsed_controller_version[0] + else: + collection_compare_ver = "{0}.{1}".format(parsed_collection_version[0], parsed_collection_version[1]) + controller_compare_ver = '{0}.{1}'.format(parsed_controller_version[0], parsed_controller_version[1]) - if self._COLLECTION_TYPE not in self.collection_to_version or self.collection_to_version[self._COLLECTION_TYPE] != controller_type: - self.warn("You are using the {0} version of this collection but connecting to {1}".format(self._COLLECTION_TYPE, controller_type)) - elif collection_compare_ver != controller_compare_ver: - self.warn( - "You are running collection version {0} but connecting to {2} version {1}".format( - self._COLLECTION_VERSION, controller_version, controller_type + if self._COLLECTION_TYPE not in self.collection_to_version or self.collection_to_version[self._COLLECTION_TYPE] != controller_type: + self.warn("You are using the {0} version of this collection but connecting to {1}".format(self._COLLECTION_TYPE, controller_type)) + elif collection_compare_ver != controller_compare_ver: + self.warn( + "You are running collection version {0} but connecting to {2} version {1}".format( + self._COLLECTION_VERSION, controller_version, controller_type + ) ) - ) self.version_checked = True From aad432aaa3b04f16b0dcfcd10623e7296029fc0f Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Wed, 8 Sep 2021 12:38:38 -0400 Subject: [PATCH 2/6] Changing to Version instead of Type --- awx_collection/plugins/module_utils/controller_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index c27086d74f..fc4d73d69b 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -262,7 +262,7 @@ class ControllerAPIModule(ControllerModule): parsed_collection_version = Version(self._COLLECTION_VERSION).version if(not controller_version): - self.warn("You are using the {0} version of this collection but connecting to a controller that did not return a version".format(self._COLLECTION_TYPE)) + self.warn("You are using the {0} version of this collection but connecting to a controller that did not return a version".format(self._COLLECTION_VERSION)) else: parsed_controller_version = Version(controller_version).version if controller_type == 'AWX': From b9758f5c1ad260a161bbd5576581e41e3554ebff Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 9 Sep 2021 12:31:33 -0400 Subject: [PATCH 3/6] Adding unit test for no header response --- awx_collection/test/awx/test_module_utils.py | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/awx_collection/test/awx/test_module_utils.py b/awx_collection/test/awx/test_module_utils.py index d5be56d6cc..088b5368a7 100644 --- a/awx_collection/test/awx/test_module_utils.py +++ b/awx_collection/test/awx/test_module_utils.py @@ -24,6 +24,11 @@ def getAWXheader(self, header_name, default): return mock_headers.get(header_name, default) +def getNoheader(self, header_name, default): + mock_headers = {} + return mock_headers.get(header_name, default) + + def read(self): return json.dumps({}) @@ -48,6 +53,14 @@ def mock_awx_ping_response(self, method, url, **kwargs): return r +def mock_no_ping_response(self, method, url, **kwargs): + r = Response() + r.getheader = getNoheader.__get__(r) + r.read = read.__get__(r) + r.status = status.__get__(r) + return r + + def test_version_warning(collection_import, silence_warning): ControllerAPIModule = collection_import('plugins.module_utils.controller_api').ControllerAPIModule cli_data = {'ANSIBLE_MODULE_ARGS': {}} @@ -63,6 +76,21 @@ def test_version_warning(collection_import, silence_warning): ) +def test_no_version_warning(collection_import, silence_warning): + ControllerAPIModule = collection_import('plugins.module_utils.controller_api').ControllerAPIModule + cli_data = {'ANSIBLE_MODULE_ARGS': {}} + testargs = ['module_file2.py', json.dumps(cli_data)] + with mock.patch.object(sys, 'argv', testargs): + with mock.patch('ansible.module_utils.urls.Request.open', new=mock_no_ping_response): + my_module = ControllerAPIModule(argument_spec=dict()) + my_module._COLLECTION_VERSION = "2.0.0" + my_module._COLLECTION_TYPE = "awx" + my_module.get_endpoint('ping') + silence_warning.assert_called_once_with( + 'You are using the {0} version of this collection but connecting to a controller that did not return a version'.format(my_module._COLLECTION_VERSION) + ) + + def test_version_warning_strictness_awx(collection_import, silence_warning): ControllerAPIModule = collection_import('plugins.module_utils.controller_api').ControllerAPIModule cli_data = {'ANSIBLE_MODULE_ARGS': {}} From 5607c350cd283ef2f48276a589486ffeef9c29b9 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 9 Sep 2021 12:58:29 -0400 Subject: [PATCH 4/6] Removing parens --- awx_collection/plugins/module_utils/controller_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index fc4d73d69b..d2105e17ac 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -261,7 +261,7 @@ class ControllerAPIModule(ControllerModule): controller_version = response.info().getheader('X-API-Product-Version', None) parsed_collection_version = Version(self._COLLECTION_VERSION).version - if(not controller_version): + if not controller_version: self.warn("You are using the {0} version of this collection but connecting to a controller that did not return a version".format(self._COLLECTION_VERSION)) else: parsed_controller_version = Version(controller_version).version From 515c3450c2f8854e075b4ca3977de67dd99fd148 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 9 Sep 2021 15:51:15 -0400 Subject: [PATCH 5/6] Fixing linting issue --- awx_collection/plugins/module_utils/controller_api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index d2105e17ac..22e646c93d 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -262,7 +262,11 @@ class ControllerAPIModule(ControllerModule): parsed_collection_version = Version(self._COLLECTION_VERSION).version if not controller_version: - self.warn("You are using the {0} version of this collection but connecting to a controller that did not return a version".format(self._COLLECTION_VERSION)) + self.warn( + "You are using the {0} version of this collection but connecting to a controller that did not return a version".format( + self._COLLECTION_VERSION + ) + ) else: parsed_controller_version = Version(controller_version).version if controller_type == 'AWX': From 9b66bda8b9567300b0eebd0eb9c1fc38b928b02b Mon Sep 17 00:00:00 2001 From: beeankha Date: Fri, 10 Sep 2021 09:02:30 -0400 Subject: [PATCH 6/6] Fix pep8 error --- awx_collection/plugins/module_utils/controller_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index 22e646c93d..51020336bc 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -263,9 +263,9 @@ class ControllerAPIModule(ControllerModule): parsed_collection_version = Version(self._COLLECTION_VERSION).version if not controller_version: self.warn( - "You are using the {0} version of this collection but connecting to a controller that did not return a version".format( - self._COLLECTION_VERSION - ) + "You are using the {0} version of this collection but connecting to a controller that did not return a version".format( + self._COLLECTION_VERSION + ) ) else: parsed_controller_version = Version(controller_version).version