Splitting out AWX and Tower versions

This commit is contained in:
John Westcott IV 2021-03-24 10:16:53 -04:00 committed by beeankha
parent e2b290ff99
commit bb43ecb0b5
2 changed files with 66 additions and 18 deletions

View File

@ -260,13 +260,21 @@ class TowerAPIModule(TowerModule):
tower_type = response.info().getheader('X-API-Product-Name', None)
tower_version = response.info().getheader('X-API-Product-Version', None)
collection_major = Version(self._COLLECTION_VERSION).version[0]
tower_major = Version(tower_version).version[0]
parsed_collection_version = Version(self._COLLECTION_VERSION).version
parsed_tower_version = Version(tower_version).version
if tower_type != 'AWX':
collection_compare_ver = parsed_collection_version[0]
tower_compare_ver = parsed_tower_version[0]
else:
collection_compare_ver = "{}.{}".format(parsed_collection_version[0], parsed_collection_version[1])
tower_major = '{}.{}'.format(parsed_tower_version[0], parsed_tower_version[1])
if self._COLLECTION_TYPE not in self.collection_to_version or self.collection_to_version[self._COLLECTION_TYPE] != tower_type:
self.warn("You are using the {0} version of this collection but connecting to {1}".format(self._COLLECTION_TYPE, tower_type))
elif collection_major != tower_major:
elif collection_compare_ver != tower_compare_ver:
self.warn("You are running collection version {0} but connecting to tower version {1}".format(self._COLLECTION_VERSION, tower_version))
self.version_checked = True
response_body = ''

View File

@ -10,8 +10,12 @@ from requests.models import Response
from unittest import mock
def getheader(self, header_name, default):
mock_headers = {'X-API-Product-Name': 'not-junk', 'X-API-Product-Version': '1.2.3'}
def getTowerheader(self, header_name, default):
mock_headers = {'X-API-Product-Name': 'Red Hat Ansible Tower', 'X-API-Product-Version': '1.2.3'}
return mock_headers.get(header_name, default)
def getAWXheader(self, header_name, default):
mock_headers = {'X-API-Product-Name': 'AWX', 'X-API-Product-Version': '1.2.3'}
return mock_headers.get(header_name, default)
@ -23,9 +27,16 @@ def status(self):
return 200
def mock_ping_response(self, method, url, **kwargs):
def mock_tower_ping_response(self, method, url, **kwargs):
r = Response()
r.getheader = getheader.__get__(r)
r.getheader = getTowerheader.__get__(r)
r.read = read.__get__(r)
r.status = status.__get__(r)
return r
def mock_awx_ping_response(self, method, url, **kwargs):
r = Response()
r.getheader = getAWXheader.__get__(r)
r.read = read.__get__(r)
r.status = status.__get__(r)
return r
@ -36,41 +47,70 @@ def test_version_warning(collection_import, silence_warning):
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_ping_response):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_awx_ping_response):
my_module = TowerAPIModule(argument_spec=dict())
my_module._COLLECTION_VERSION = "2.0.0"
my_module._COLLECTION_TYPE = "not-junk"
my_module.collection_to_version['not-junk'] = 'not-junk'
my_module._COLLECTION_TYPE = "awx"
my_module.get_endpoint('ping')
silence_warning.assert_called_once_with('You are running collection version 2.0.0 but connecting to tower version 1.2.3')
def test_version_warning_strictness(collection_import, silence_warning):
def test_version_warning_strictness_awx(collection_import, silence_warning):
TowerAPIModule = collection_import('plugins.module_utils.tower_api').TowerAPIModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)]
# Compare 1.0.0 to 1.2.3 (major matches)
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_awx_ping_response):
my_module = TowerAPIModule(argument_spec=dict())
my_module._COLLECTION_VERSION = "1.0.0"
my_module._COLLECTION_TYPE = "not-junk"
my_module.collection_to_version['not-junk'] = 'not-junk'
my_module._COLLECTION_TYPE = "awx"
my_module.get_endpoint('ping')
silence_warning.assert_not_called()
# Compare 1.2.0 to 1.2.3 (major matches minor does not count)
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_awx_ping_response):
my_module = TowerAPIModule(argument_spec=dict())
my_module._COLLECTION_VERSION = "1.2.0"
my_module._COLLECTION_TYPE = "awx"
my_module.get_endpoint('ping')
silence_warning.assert_not_called()
def test_version_warning_strictness_tower(collection_import, silence_warning):
TowerAPIModule = collection_import('plugins.module_utils.tower_api').TowerAPIModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)]
# Compare 1.2.0 to 1.2.3 (major/minor matches)
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_tower_ping_response):
my_module = TowerAPIModule(argument_spec=dict())
my_module._COLLECTION_VERSION = "1.2.0"
my_module.get_endpoint('ping')
silence_warning.assert_not_called()
# Compare 1.0.0 to 1.2.3 (major/minor fail to match)
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_tower_ping_response):
my_module = TowerAPIModule(argument_spec=dict())
my_module._COLLECTION_VERSION = "1.0.0"
my_module._COLLECTION_TYPE = "tower"
my_module.get_endpoint('ping')
silence_warning.assert_called_once_with('You are running collection version 1.0.0 but connecting to tower version 1.2.3')
def test_type_warning(collection_import, silence_warning):
TowerAPIModule = collection_import('plugins.module_utils.tower_api').TowerAPIModule
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_ping_response):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_awx_ping_response):
my_module = TowerAPIModule(argument_spec={})
my_module._COLLECTION_VERSION = "1.2.3"
my_module._COLLECTION_TYPE = "junk"
my_module.collection_to_version['junk'] = 'junk'
my_module._COLLECTION_TYPE = "tower"
my_module.get_endpoint('ping')
silence_warning.assert_called_once_with('You are using the junk version of this collection but connecting to not-junk')
silence_warning.assert_called_once_with('You are using the tower version of this collection but connecting to awx')
def test_duplicate_config(collection_import, silence_warning):