diff --git a/awx_collection/plugins/module_utils/controller_api.py b/awx_collection/plugins/module_utils/controller_api.py index aefe0d5d01..51020336bc 100644 --- a/awx_collection/plugins/module_utils/controller_api.py +++ b/awx_collection/plugins/module_utils/controller_api.py @@ -261,22 +261,29 @@ 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] - 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: + if not controller_version: self.warn( - "You are running collection version {0} but connecting to {2} version {1}".format( - self._COLLECTION_VERSION, controller_version, controller_type + "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': + 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 + ) + ) self.version_checked = True 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': {}}