Make tower_inventory_source org optional, add tests

This commit is contained in:
AlanCoding
2019-10-31 09:24:41 -04:00
parent a2fee252f9
commit 24eae09ed9
3 changed files with 96 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ import datetime
import importlib
from contextlib import redirect_stdout
from unittest import mock
import logging
from requests.models import Response
@@ -13,6 +14,9 @@ from awx.main.tests.functional.conftest import _request
from awx.main.models import Organization, Project, Inventory, Credential, CredentialType
logger = logging.getLogger('awx.main.tests')
def sanitize_dict(din):
'''Sanitize Django response data to purge it of internal types
so it may be used to cast a requests response object
@@ -34,13 +38,22 @@ def sanitize_dict(din):
@pytest.fixture
def run_module():
def run_module(request):
def rf(module_name, module_params, request_user):
def new_request(self, method, url, **kwargs):
kwargs_copy = kwargs.copy()
if 'data' in kwargs:
kwargs_copy['data'] = json.loads(kwargs['data'])
if 'params' in kwargs and method == 'GET':
# query params for GET are handled a bit differently by
# tower-cli and python requests as opposed to REST framework APIRequestFactory
kwargs_copy.setdefault('data', {})
if isinstance(kwargs['params'], dict):
kwargs_copy['data'].update(kwargs['params'])
elif isinstance(kwargs['params'], list):
for k, v in kwargs['params']:
kwargs_copy['data'][k] = v
# make request
rf = _request(method.lower())
@@ -53,6 +66,13 @@ def run_module():
sanitize_dict(py_data)
resp._content = bytes(json.dumps(django_response.data), encoding='utf8')
resp.status_code = django_response.status_code
if request.config.getoption('verbose') > 0:
logger.info('{} {} by {}, code:{}'.format(
method, '/api/' + url.split('/api/')[1],
request_user.username, resp.status_code
))
return resp
stdout_buffer = io.StringIO()

View File

@@ -0,0 +1,56 @@
import pytest
from awx.main.models import Organization, Inventory, InventorySource
@pytest.mark.django_db
def test_create_inventory_source_implied_org(run_module, admin_user):
org = Organization.objects.create(name='test-org')
inv = Inventory.objects.create(name='test-inv', organization=org)
result = run_module('tower_inventory_source', dict(
name='Test Inventory Source',
inventory='test-inv',
source='ec2',
state='present'
), admin_user)
assert result.pop('changed', None), result
inv_src = InventorySource.objects.get(name='Test Inventory Source')
assert inv_src.inventory == inv
result.pop('invocation')
assert result == {
"inventory_source": "Test Inventory Source",
"state": "present",
"id": inv_src.id,
}
@pytest.mark.django_db
def test_create_inventory_source_multiple_orgs(run_module, admin_user):
org = Organization.objects.create(name='test-org')
inv = Inventory.objects.create(name='test-inv', organization=org)
# make another inventory by same name in another org
org2 = Organization.objects.create(name='test-org-number-two')
Inventory.objects.create(name='test-inv', organization=org2)
result = run_module('tower_inventory_source', dict(
name='Test Inventory Source',
inventory='test-inv',
source='ec2',
organization='test-org',
state='present'
), admin_user)
assert result.pop('changed', None), result
inv_src = InventorySource.objects.get(name='Test Inventory Source')
assert inv_src.inventory == inv
result.pop('invocation')
assert result == {
"inventory_source": "Test Inventory Source",
"state": "present",
"id": inv_src.id,
}