Merge pull request #1766 from chrismeyersfsu/i_like_parallel_tests_and_I_can_not_lie

parallelize test running
This commit is contained in:
Chris Meyers
2018-05-16 15:13:20 -04:00
committed by GitHub
11 changed files with 51 additions and 14 deletions

View File

@@ -379,7 +379,7 @@ test:
@if [ "$(VENV_BASE)" ]; then \ @if [ "$(VENV_BASE)" ]; then \
. $(VENV_BASE)/awx/bin/activate; \ . $(VENV_BASE)/awx/bin/activate; \
fi; \ fi; \
py.test $(TEST_DIRS) py.test -n auto $(TEST_DIRS)
test_combined: test_ansible test test_combined: test_ansible test

View File

@@ -77,7 +77,7 @@ class InstanceManager(models.Manager):
def me(self): def me(self):
"""Return the currently active instance.""" """Return the currently active instance."""
# If we are running unit tests, return a stub record. # If we are running unit tests, return a stub record.
if settings.IS_TESTING(sys.argv): if settings.IS_TESTING(sys.argv) or hasattr(sys, '_called_from_test'):
return self.model(id=1, return self.model(id=1,
hostname='localhost', hostname='localhost',
uuid='00000000-0000-0000-0000-000000000000') uuid='00000000-0000-0000-0000-000000000000')

View File

@@ -15,6 +15,16 @@ from awx.main.tests.factories import (
) )
def pytest_configure(config):
import sys
sys._called_from_test = True
def pytest_unconfigure(config):
import sys
del sys._called_from_test
@pytest.fixture @pytest.fixture
def mock_access(): def mock_access():
@contextmanager @contextmanager

View File

@@ -102,21 +102,21 @@ class TestOAuth2Application:
assert access.can_delete(app) is can_access assert access.can_delete(app) is can_access
def test_superuser_can_always_create(self, admin, org_admin, org_member, alice): def test_superuser_can_always_create(self, admin, org_admin, org_member, alice, organization):
access = OAuth2ApplicationAccess(admin) access = OAuth2ApplicationAccess(admin)
for user in [admin, org_admin, org_member, alice]: for user in [admin, org_admin, org_member, alice]:
assert access.can_add({ assert access.can_add({
'name': 'test app', 'user': user.pk, 'client_type': 'confidential', 'name': 'test app', 'user': user.pk, 'client_type': 'confidential',
'authorization_grant_type': 'password', 'organization': 1 'authorization_grant_type': 'password', 'organization': organization.id
}) })
def test_normal_user_cannot_create(self, admin, org_admin, org_member, alice): def test_normal_user_cannot_create(self, admin, org_admin, org_member, alice, organization):
for access_user in [org_member, alice]: for access_user in [org_member, alice]:
access = OAuth2ApplicationAccess(access_user) access = OAuth2ApplicationAccess(access_user)
for user in [admin, org_admin, org_member, alice]: for user in [admin, org_admin, org_member, alice]:
assert not access.can_add({ assert not access.can_add({
'name': 'test app', 'user': user.pk, 'client_type': 'confidential', 'name': 'test app', 'user': user.pk, 'client_type': 'confidential',
'authorization_grant_type': 'password', 'organization': 1 'authorization_grant_type': 'password', 'organization': organization.id
}) })

View File

@@ -1,8 +1,9 @@
import tempfile import tempfile
import json import json
import yaml import yaml
import pytest import pytest
from itertools import count
from awx.main.utils.encryption import encrypt_value from awx.main.utils.encryption import encrypt_value
from awx.main.tasks import RunJob from awx.main.tasks import RunJob
from awx.main.models import ( from awx.main.models import (
@@ -16,6 +17,15 @@ from awx.main.utils.safe_yaml import SafeLoader
ENCRYPTED_SECRET = encrypt_value('secret') ENCRYPTED_SECRET = encrypt_value('secret')
class DistinctParametrize(object):
def __init__(self):
self._gen = count(0)
def __call__(self, value):
return str(next(self._gen))
@pytest.mark.survey @pytest.mark.survey
class SurveyVariableValidation: class SurveyVariableValidation:
@@ -243,7 +253,7 @@ def test_optional_survey_question_defaults(
('password', 'foo', 5, {'extra_vars': {'x': ''}}, {'x': ''}), ('password', 'foo', 5, {'extra_vars': {'x': ''}}, {'x': ''}),
('password', ENCRYPTED_SECRET, 5, {'extra_vars': {'x': '$encrypted$'}}, {}), ('password', ENCRYPTED_SECRET, 5, {'extra_vars': {'x': '$encrypted$'}}, {}),
('password', ENCRYPTED_SECRET, 10, {'extra_vars': {'x': '$encrypted$'}}, {'x': ENCRYPTED_SECRET}), ('password', ENCRYPTED_SECRET, 10, {'extra_vars': {'x': '$encrypted$'}}, {'x': ENCRYPTED_SECRET}),
]) ], ids=DistinctParametrize())
def test_survey_encryption_defaults(survey_spec_factory, question_type, default, maxlen, kwargs, expected): def test_survey_encryption_defaults(survey_spec_factory, question_type, default, maxlen, kwargs, expected):
spec = survey_spec_factory([ spec = survey_spec_factory([
{ {

View File

@@ -158,7 +158,7 @@ def test_jt_existing_values_are_nonsensitive(job_template_with_ids, user_unit):
"""Assure that permission checks are not required if submitted data is """Assure that permission checks are not required if submitted data is
identical to what the job template already has.""" identical to what the job template already has."""
data = model_to_dict(job_template_with_ids) data = model_to_dict(job_template_with_ids, exclude=['unifiedjobtemplate_ptr'])
access = JobTemplateAccess(user_unit) access = JobTemplateAccess(user_unit)
assert access.changes_are_non_sensitive(job_template_with_ids, data) assert access.changes_are_non_sensitive(job_template_with_ids, data)

View File

@@ -2155,7 +2155,7 @@ def test_aquire_lock_open_fail_logged(logging_getLogger, os_open):
ProjectUpdate = tasks.RunProjectUpdate() ProjectUpdate = tasks.RunProjectUpdate()
with pytest.raises(OSError, errno=3, strerror='dummy message'): with pytest.raises(OSError, message='dummy message'):
ProjectUpdate.acquire_lock(instance) ProjectUpdate.acquire_lock(instance)
assert logger.err.called_with("I/O error({0}) while trying to open lock file [{1}]: {2}".format(3, 'this_file_does_not_exist', 'dummy message')) assert logger.err.called_with("I/O error({0}) while trying to open lock file [{1}]: {2}".format(3, 'this_file_does_not_exist', 'dummy message'))
@@ -2181,7 +2181,7 @@ def test_aquire_lock_acquisition_fail_logged(fcntl_flock, logging_getLogger, os_
ProjectUpdate = tasks.RunProjectUpdate() ProjectUpdate = tasks.RunProjectUpdate()
with pytest.raises(IOError, errno=3, strerror='dummy message'): with pytest.raises(IOError, message='dummy message'):
ProjectUpdate.acquire_lock(instance) ProjectUpdate.acquire_lock(instance)
os_close.assert_called_with(3) os_close.assert_called_with(3)
assert logger.err.called_with("I/O error({0}) while trying to aquire lock on file [{1}]: {2}".format(3, 'this_file_does_not_exist', 'dummy message')) assert logger.err.called_with("I/O error({0}) while trying to aquire lock on file [{1}]: {2}".format(3, 'this_file_does_not_exist', 'dummy message'))

View File

@@ -0,0 +1,9 @@
import pytest
from mock import PropertyMock
@pytest.fixture(autouse=True)
def _disable_database_settings(mocker):
m = mocker.patch('awx.conf.settings.SettingsWrapper.all_supported_settings', new_callable=PropertyMock)
m.return_value = []

View File

@@ -4,7 +4,6 @@
import os import os
import re # noqa import re # noqa
import sys import sys
import ldap
import djcelery import djcelery
import six import six
from datetime import timedelta from datetime import timedelta
@@ -39,6 +38,13 @@ def IS_TESTING(argv=None):
return is_testing(argv) return is_testing(argv)
if "pytest" in sys.modules:
import mock
with mock.patch('__main__.__builtins__.dir', return_value=[]):
import ldap
else:
import ldap
DEBUG = True DEBUG = True
SQL_DEBUG = DEBUG SQL_DEBUG = DEBUG

View File

@@ -13,6 +13,7 @@
############################################################################### ###############################################################################
import os import os
import urllib import urllib
import sys
def patch_broken_pipe_error(): def patch_broken_pipe_error():
@@ -66,7 +67,7 @@ DATABASES = {
# Use SQLite for unit tests instead of PostgreSQL. If the lines below are # 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 # commented out, Django will create the test_awx-dev database in PostgreSQL to
# run unit tests. # run unit tests.
if is_testing(sys.argv): if "pytest" in sys.modules:
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',

View File

@@ -6,12 +6,13 @@ unittest2
pep8 pep8
flake8 flake8
pyflakes pyflakes
pytest==2.9.2 pytest==3.5.1
pytest-cov pytest-cov
pytest-django pytest-django
pytest-pythonpath pytest-pythonpath
pytest-mock pytest-mock
pytest-timeout pytest-timeout
pytest-xdist
logutils logutils
flower flower
uwsgitop uwsgitop