Remove silence_warning (because of autouse), add version/type warning tests to module_utils unit test file

This commit is contained in:
beeankha 2020-04-27 15:43:44 -04:00 committed by AlanCoding
parent 8c5d236066
commit 4321c63165
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B
4 changed files with 77 additions and 11 deletions

View File

@ -108,6 +108,7 @@ def run_module(request, collection_import):
sanitize_dict(py_data)
resp._content = bytes(json.dumps(django_response.data), encoding='utf8')
resp.status_code = django_response.status_code
resp.headers = {'X-API-Product-Name': 'AWX', 'X-API-Product-Version': '11.0.0'}
if request.config.getoption('verbose') > 0:
logger.info(
@ -120,7 +121,11 @@ def run_module(request, collection_import):
def new_open(self, method, url, **kwargs):
r = new_request(self, method, url, **kwargs)
return mock.MagicMock(read=mock.MagicMock(return_value=r._content), status=r.status_code)
m = mock.MagicMock(read=mock.MagicMock(return_value=r._content),
status=r.status_code,
getheader=mock.MagicMock(side_effect=r.headers.get)
)
return m
stdout_buffer = io.StringIO()
# Requies specific PYTHONPATH, see docs

View File

@ -152,7 +152,7 @@ def test_make_use_of_custom_credential_type(run_module, organization, admin_user
@pytest.mark.django_db
def test_secret_field_write_twice(run_module, organization, admin_user, cred_type, silence_warning):
def test_secret_field_write_twice(run_module, organization, admin_user, cred_type):
val1 = '7rEZK38DJl58A7RxA6EC7lLvUHbBQ1'
result = run_module('tower_credential', dict(
name='Galaxy Token for Steve',

View File

@ -1,11 +1,66 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import sys
from requests.models import Response
from unittest import mock
import json
def getheader(self, header_name, default):
mock_headers = {'X-API-Product-Name': 'not-junk', 'X-API-Product-Version': '1.2.3'}
return mock_headers.get(header_name, default)
def read(self):
return json.dumps({})
def status(self):
return 200
def mock_ping_response(self, method, url, **kwargs):
r = Response()
r.getheader = getheader.__get__(r)
r.read = read.__get__(r)
r.status = status.__get__(r)
return r
def test_version_warning(collection_import, silence_warning):
TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response):
my_module = TowerModule(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.get_endpoint('ping')
mock_warn.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):
TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response):
my_module = TowerModule(argument_spec={})
my_module._COLLECTION_VERSION = "1.2.3"
my_module._COLLECTION_TYPE = "junk"
my_module.collection_to_version['junk'] = 'junk'
my_module.get_endpoint('ping')
mock_warn.assert_called_once_with(
'You are using the junk version of this collection but connecting to not-junk'
)
def test_duplicate_config(collection_import):
@ -17,17 +72,23 @@ def test_duplicate_config(collection_import):
'tower_username': 'bob',
'tower_config_file': 'my_config'
}
class DuplicateTestTowerModule(TowerModule):
def load_config(self, config_path):
assert config_path == 'my_config'
def _load_params(self):
self.params = data
cli_data = {'ANSIBLE_MODULE_ARGS': data}
testargs = ['module_file.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs):
with mock.patch.object(TowerModule, 'load_config') as mock_load:
argument_spec = dict(
name=dict(required=True),
zig=dict(type='str'),
)
TowerModule(argument_spec=argument_spec)
mock_load.mock_calls[-1] == mock.call('my_config')
argument_spec = dict(
name=dict(required=True),
zig=dict(type='str'),
)
DuplicateTestTowerModule(argument_spec=argument_spec)
mock_warn.assert_called_once_with(
'The parameter(s) tower_username were provided at the same time as '
'tower_config_file. Precedence may be unstable, '

View File

@ -17,7 +17,7 @@ from awx.main.models import (
# warns based on password_management param, but not security issue
@pytest.mark.django_db
def test_receive_send_jt(run_module, admin_user, mocker, silence_deprecation, silence_warning):
def test_receive_send_jt(run_module, admin_user, mocker, silence_deprecation):
org = Organization.objects.create(name='SRtest')
proj = Project.objects.create(
name='SRtest',