diff options
-rw-r--r-- | awx/api/serializers.py | 2 | ||||
-rw-r--r-- | awx/main/tests/settings_for_test.py | 28 | ||||
-rw-r--r-- | awx/main/tests/unit/test_settings.py | 1 | ||||
-rw-r--r-- | awx/settings/defaults.py | 14 | ||||
-rw-r--r-- | awx/settings/development.py | 19 | ||||
-rw-r--r-- | awx_collection/test/awx/test_bulk.py | 8 | ||||
-rw-r--r-- | pytest.ini | 2 |
7 files changed, 41 insertions, 33 deletions
diff --git a/awx/api/serializers.py b/awx/api/serializers.py index cc40075a88..dc2eb69b22 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -2190,7 +2190,7 @@ class BulkHostCreateSerializer(serializers.Serializer): host_data = [] for r in result: item = {k: getattr(r, k) for k in return_keys} - if not settings.IS_TESTING_MODE: + if settings.DATABASES and ('sqlite3' not in settings.DATABASES.get('default', {}).get('ENGINE')): # sqlite acts different with bulk_create -- it doesn't return the id of the objects # to get it, you have to do an additional query, which is not useful for our tests item['url'] = reverse('api:host_detail', kwargs={'pk': r.id}) diff --git a/awx/main/tests/settings_for_test.py b/awx/main/tests/settings_for_test.py new file mode 100644 index 0000000000..373489de37 --- /dev/null +++ b/awx/main/tests/settings_for_test.py @@ -0,0 +1,28 @@ +# Python +from unittest import mock +import uuid + +# patch python-ldap +with mock.patch('__main__.__builtins__.dir', return_value=[]): + import ldap # NOQA + +# Load development settings for base variables. +from awx.settings.development import * # NOQA + +# Some things make decisions based on settings.SETTINGS_MODULE, so this is done for that +SETTINGS_MODULE = 'awx.settings.development' + +# Use SQLite for unit tests instead of PostgreSQL. If the lines below are +# commented out, Django will create the test_awx-dev database in PostgreSQL to +# run unit tests. +CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-{}'.format(str(uuid.uuid4()))}} +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'awx.sqlite3'), # noqa + 'TEST': { + # Test database cannot be :memory: for inventory tests. + 'NAME': os.path.join(BASE_DIR, 'awx_test.sqlite3') # noqa + }, + } +} diff --git a/awx/main/tests/unit/test_settings.py b/awx/main/tests/unit/test_settings.py index ef516dbefc..dae5929655 100644 --- a/awx/main/tests/unit/test_settings.py +++ b/awx/main/tests/unit/test_settings.py @@ -8,6 +8,7 @@ LOCAL_SETTINGS = ( 'BROADCAST_WEBSOCKET_PROTOCOL', 'BROADCAST_WEBSOCKET_SECRET', 'DATABASES', + 'CACHES', 'DEBUG', 'NAMED_URL_GRAPH', ) diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index b72147c91f..5608774686 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -1,24 +1,16 @@ # Copyright (c) 2015 Ansible, Inc. # All Rights Reserved. +# Python import base64 import os import re # noqa -import sys import tempfile import socket from datetime import timedelta - -if "pytest" in sys.modules: - IS_TESTING_MODE = True - from unittest import mock - - with mock.patch('__main__.__builtins__.dir', return_value=[]): - import ldap -else: - IS_TESTING_MODE = False - import ldap +# python-ldap +import ldap DEBUG = True diff --git a/awx/settings/development.py b/awx/settings/development.py index ad739158b3..3966cc7dbc 100644 --- a/awx/settings/development.py +++ b/awx/settings/development.py @@ -9,7 +9,6 @@ import socket import copy import sys import traceback -import uuid # Centos-7 doesn't include the svg mime type # /usr/lib64/python/mimetypes.py @@ -65,22 +64,6 @@ INSTALL_UUID = '00000000-0000-0000-0000-000000000000' BASE_VENV_PATH = "/var/lib/awx/venv/" AWX_VENV_PATH = os.path.join(BASE_VENV_PATH, "awx") -# Use SQLite for unit tests instead of PostgreSQL. If the lines below are -# commented out, Django will create the test_awx-dev database in PostgreSQL to -# run unit tests. -if "pytest" in sys.modules: - CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-{}'.format(str(uuid.uuid4()))}} - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'awx.sqlite3'), # noqa - 'TEST': { - # Test database cannot be :memory: for inventory tests. - 'NAME': os.path.join(BASE_DIR, 'awx_test.sqlite3') # noqa - }, - } - } - CLUSTER_HOST_ID = socket.gethostname() AWX_CALLBACK_PROFILE = True @@ -132,6 +115,6 @@ except ImportError: # because conf.d files will define DATABASES and this should modify that from .application_name import set_application_name -set_application_name(DATABASES, CLUSTER_HOST_ID) +set_application_name(DATABASES, CLUSTER_HOST_ID) # NOQA del set_application_name diff --git a/awx_collection/test/awx/test_bulk.py b/awx_collection/test/awx/test_bulk.py index 6ba97e9006..e0315732db 100644 --- a/awx_collection/test/awx/test_bulk.py +++ b/awx_collection/test/awx/test_bulk.py @@ -10,7 +10,7 @@ from awx.main.models import WorkflowJob @pytest.mark.django_db def test_bulk_job_launch(run_module, admin_user, job_template): jobs = [dict(unified_job_template=job_template.id)] - run_module( + result = run_module( 'bulk_job_launch', { 'name': "foo-bulk-job", @@ -21,6 +21,8 @@ def test_bulk_job_launch(run_module, admin_user, job_template): }, admin_user, ) + assert not result.get('failed', False), result.get('msg', result) + assert result.get('changed'), result bulk_job = WorkflowJob.objects.get(name="foo-bulk-job") assert bulk_job.extra_vars == '{"animal": "owl"}' @@ -30,7 +32,7 @@ def test_bulk_job_launch(run_module, admin_user, job_template): @pytest.mark.django_db def test_bulk_host_create(run_module, admin_user, inventory): hosts = [dict(name="127.0.0.1"), dict(name="foo.dns.org")] - run_module( + result = run_module( 'bulk_host_create', { 'inventory': inventory.name, @@ -38,6 +40,8 @@ def test_bulk_host_create(run_module, admin_user, inventory): }, admin_user, ) + assert not result.get('failed', False), result.get('msg', result) + assert result.get('changed'), result resp_hosts = inventory.hosts.all().values_list('name', flat=True) for h in hosts: assert h['name'] in resp_hosts diff --git a/pytest.ini b/pytest.ini index d4ffd2db9b..8a1e82820d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,5 +1,5 @@ [pytest] -DJANGO_SETTINGS_MODULE = awx.settings.development +DJANGO_SETTINGS_MODULE = awx.main.tests.settings_for_test python_paths = /var/lib/awx/venv/tower/lib/python3.8/site-packages site_dirs = /var/lib/awx/venv/tower/lib/python3.8/site-packages python_files = *.py |