From 35e1c19fc26d50b16cc5d5115678383d94f7cb69 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Fri, 3 Apr 2015 12:00:56 -0400 Subject: [PATCH] do not run tests if mongodb connect fails --- awx/main/tests/base.py | 15 ++++++++++----- .../tests/commands/run_fact_cache_receiver.py | 7 ++++--- awx/main/tests/dbtransform.py | 4 ++-- awx/main/tests/models/fact.py | 6 +++--- awx/settings/__init__.py | 12 +++++++++--- awx/settings/local_settings.py.example | 2 ++ 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/awx/main/tests/base.py b/awx/main/tests/base.py index 69fd0e41e2..6cbbb068da 100644 --- a/awx/main/tests/base.py +++ b/awx/main/tests/base.py @@ -26,7 +26,7 @@ from django.test.client import Client from django.test.utils import override_settings # MongoEngine -from mongoengine.connection import get_db +from mongoengine.connection import get_db, ConnectionError # AWX from awx.main.models import * # noqa @@ -43,6 +43,15 @@ TEST_PLAYBOOK = '''- hosts: mygroup command: test 1 = 1 ''' +class MongoDBRequired(django.test.TestCase): + def setUp(self): + # Drop mongo database + try: + self.db = get_db() + self.db.connection.drop_database(settings.MONGO_DB) + except ConnectionError as e: + self.skipTest('MongoDB connection failed') + class QueueTestMixin(object): def start_queue(self): self.start_redis() @@ -88,10 +97,6 @@ class BaseTestMixin(QueueTestMixin): def setUp(self): super(BaseTestMixin, self).setUp() - # Drop mongo database - self.db = get_db() - self.db.connection.drop_database(settings.MONGO_DB) - self.object_ctr = 0 # Save sys.path before tests. self._sys_path = [x for x in sys.path] diff --git a/awx/main/tests/commands/run_fact_cache_receiver.py b/awx/main/tests/commands/run_fact_cache_receiver.py index c55782d015..99f0e33a8a 100644 --- a/awx/main/tests/commands/run_fact_cache_receiver.py +++ b/awx/main/tests/commands/run_fact_cache_receiver.py @@ -11,7 +11,7 @@ from copy import deepcopy from mock import Mock, MagicMock # AWX -from awx.main.tests.base import BaseTest +from awx.main.tests.base import BaseTest, MongoDBRequired from awx.main.tests.commands.base import BaseCommandMixin from awx.main.management.commands.run_fact_cache_receiver import FactCacheReceiver, _MODULES from awx.main.models.fact import * @@ -98,7 +98,7 @@ def copy_only_module(data, module): return data -class RunFactCacheReceiverFunctionalTest(BaseCommandMixin, BaseTest): +class RunFactCacheReceiverFunctionalTest(BaseCommandMixin, BaseTest, MongoDBRequired): @unittest.skip('''\ TODO: run_fact_cache_receiver enters a while True loop that never exists. \ This differs from most other commands that we test for. More logic and work \ @@ -108,7 +108,8 @@ in terms of increase coverage and confidence.''') result, stdout, stderr = self.run_command('run_fact_cache_receiver') self.assertEqual(result, None) -class RunFactCacheReceiverUnitTest(BaseTest): +class RunFactCacheReceiverUnitTest(BaseTest, MongoDBRequired): + # TODO: Check that timestamp and other attributes are as expected def check_process_fact_message_module(self, data, module): fact_found = None diff --git a/awx/main/tests/dbtransform.py b/awx/main/tests/dbtransform.py index 47eb7d75dd..164e177e13 100644 --- a/awx/main/tests/dbtransform.py +++ b/awx/main/tests/dbtransform.py @@ -10,12 +10,12 @@ from mongoengine import connect from django.conf import settings # AWX -from awx.main.tests.base import BaseTest +from awx.main.tests.base import BaseTest, MongoDBRequired from awx.main.models.fact import * __all__ = ['DBTransformTest'] -class DBTransformTest(BaseTest): +class DBTransformTest(BaseTest, MongoDBRequired): def setUp(self): super(DBTransformTest, self).setUp() diff --git a/awx/main/tests/models/fact.py b/awx/main/tests/models/fact.py index 7414f7456b..1f400080cb 100644 --- a/awx/main/tests/models/fact.py +++ b/awx/main/tests/models/fact.py @@ -8,7 +8,7 @@ from datetime import datetime # AWX from awx.main.models.fact import * -from awx.main.tests.base import BaseTest +from awx.main.tests.base import BaseTest, MongoDBRequired __all__ = ['FactHostTest', 'FactTest'] @@ -49,7 +49,7 @@ TEST_FACT_DATA = { # Strip off microseconds because mongo has less precision TEST_FACT_DATA['add_fact_data']['timestamp'] = TEST_FACT_DATA['add_fact_data']['timestamp'].replace(microsecond=0) -class FactHostTest(BaseTest): +class FactHostTest(BaseTest, MongoDBRequired): def test_create_host(self): host = FactHost(hostname=TEST_FACT_DATA['hostname']) host.save() @@ -59,7 +59,7 @@ class FactHostTest(BaseTest): self.assertEqual(TEST_FACT_DATA['hostname'], host.hostname, "Gotten record hostname does not match expected hostname") -class FactTest(BaseTest): +class FactTest(BaseTest, MongoDBRequired): def setUp(self): super(FactTest, self).setUp() TEST_FACT_DATA['add_fact_data']['host'] = FactHost(hostname=TEST_FACT_DATA['hostname']).save() diff --git a/awx/settings/__init__.py b/awx/settings/__init__.py index 73f421a6e1..988ff1d52e 100644 --- a/awx/settings/__init__.py +++ b/awx/settings/__init__.py @@ -3,8 +3,14 @@ from django.conf import settings from mongoengine import connect -from mongoengine.connection import get_db +from mongoengine.connection import get_db, ConnectionError from awx.main.dbtransform import register_key_transform +import logging -connect(settings.MONGO_DB) -register_key_transform(get_db()) \ No newline at end of file +logger = logging.getLogger('awx.settings.__init__') + +try: + connect(settings.MONGO_DB) + register_key_transform(get_db()) +except ConnectionError: + logger.warn('Failed to establish connect to MongDB "%s"' % (settings.MONGO_DB)) diff --git a/awx/settings/local_settings.py.example b/awx/settings/local_settings.py.example index db4bd8eba9..50a95e1728 100644 --- a/awx/settings/local_settings.py.example +++ b/awx/settings/local_settings.py.example @@ -42,6 +42,8 @@ if len(sys.argv) >= 2 and sys.argv[1] == 'test': 'TEST_NAME': os.path.join(BASE_DIR, 'awx_test.sqlite3'), } } + + MONGO_DB = 'system_tracking_test' # Celery AMQP configuration. BROKER_URL = 'redis://localhost/'