diff --git a/awx/main/ha.py b/awx/main/ha.py index 3d1294defd..71b96dd81b 100644 --- a/awx/main/ha.py +++ b/awx/main/ha.py @@ -16,7 +16,7 @@ def is_ha_environment(): # If the database is not local, then we are in an HA environment. host = settings.DATABASES['default'].get('HOST', 'localhost') - if host and host.lower() not in ('127.0.0.1', 'localhost'): + if host and host.lower() not in ('127.0.0.1', 'localhost') and not host.startswith('/'): return True # We are not in an HA environment. diff --git a/awx/main/tests/__init__.py b/awx/main/tests/__init__.py index de61f95cf1..5eb66d1ebd 100644 --- a/awx/main/tests/__init__.py +++ b/awx/main/tests/__init__.py @@ -18,3 +18,4 @@ from awx.main.tests.views import * # noqa from awx.main.tests.commands import * # noqa from awx.main.tests.fact import * # noqa from awx.main.tests.unified_jobs import * # noqa +from awx.main.tests.ha import * # noqa diff --git a/awx/main/tests/ha.py b/awx/main/tests/ha.py new file mode 100644 index 0000000000..68fe047424 --- /dev/null +++ b/awx/main/tests/ha.py @@ -0,0 +1,36 @@ +# Copyright (c) 2015 Ansible, Inc. + +# Python +import mock + +# Django +from django.test import SimpleTestCase +from django.conf import settings + +# AWX +from awx.main.models import * # noqa +from awx.main.ha import * # noqa +from awx.main.tests.base import BaseTest + +__all__ = ['HAUnitTest',] + +class HAUnitTest(SimpleTestCase): + + @mock.patch('awx.main.models.Instance.objects.count', return_value=2) + def test_multiple_instances(self, ignore): + self.assertTrue(is_ha_environment()) + + @mock.patch('awx.main.models.Instance.objects.count', return_value=1) + @mock.patch.dict('django.conf.settings.DATABASES', { 'HOST': 'localhost' }) + def test_db_localhost(self, ignore): + self.assertFalse(is_ha_environment()) + + @mock.patch('awx.main.models.Instance.objects.count', return_value=1) + @mock.patch.dict('django.conf.settings.DATABASES', { 'HOST': '127.0.0.1' }) + def test_db_127_0_0_1(self, ignore): + self.assertFalse(is_ha_environment()) + + @mock.patch('awx.main.models.Instance.objects.count', return_value=1) + @mock.patch.dict('django.conf.settings.DATABASES', { 'HOST': '/i/might/be/a/file' }) + def test_db_file_socket(self, ignore): + self.assertFalse(is_ha_environment())