From 4669c44af983ee635553ac9e84909c7945aed322 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 23 Jun 2015 11:21:00 -0400 Subject: [PATCH] robust check for HA environment setup --- awx/main/ha.py | 31 ++++++++++++++++++++++++++----- awx/main/tests/ha.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/awx/main/ha.py b/awx/main/ha.py index 7bf45e806c..9bc14d7434 100644 --- a/awx/main/ha.py +++ b/awx/main/ha.py @@ -1,11 +1,16 @@ # Copyright (c) 2015 Ansible, Inc. # All Rights Reserved. +# Python +import os +from IPy import IP + +# Django from django.conf import settings +# AWX from awx.main.models import Instance - def is_ha_environment(): """Return True if this is an HA environment, and False otherwise. @@ -16,8 +21,24 @@ 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') and not host.startswith('/'): - return True - # We are not in an HA environment. - return False + # Is host special case 'localhost' ? + if host is 'localhost': + return False + + # Check if host is an absolute file (i.e. named socket) + if os.path.isabs(host): + return False + + # Is host a LOCAL or REMOTE ip address ? + try: + if IP(host).iptype() is 'LOOPBACK': + return False + else: + return True + except ValueError: + pass + + # host may be a domain name like postgres.mycompany.com + # Assume HA Environment + return True diff --git a/awx/main/tests/ha.py b/awx/main/tests/ha.py index 968909a0d7..17e0b66c46 100644 --- a/awx/main/tests/ha.py +++ b/awx/main/tests/ha.py @@ -13,15 +13,38 @@ from awx.main.ha import * # noqa __all__ = ['HAUnitTest',] TEST_LOCALHOST = { - 'HOST': 'localhost' + 'default': { + 'HOST': 'localhost' + } } TEST_127_0_0_1 = { - 'HOST': '127.0.0.1' + 'default': { + 'HOST': '127.0.0.1' + } } TEST_FILE = { - 'HOST': '/i/might/be/a/file', + 'default': { + 'HOST': '/i/might/be/a/file' + } +} + +TEST_DOMAIN = { + 'default': { + 'HOST': 'postgres.mycompany.com' + } +} + +TEST_REMOTE_IP = { + 'default': { + 'HOST': '8.8.8.8' + } +} + +TEST_EMPTY = { + 'default': { + } } class HAUnitTest(SimpleTestCase): @@ -44,3 +67,18 @@ class HAUnitTest(SimpleTestCase): @mock.patch.dict('django.conf.settings.DATABASES', TEST_FILE) def test_db_file_socket(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', TEST_DOMAIN) + def test_db_domain(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', TEST_REMOTE_IP) + def test_db_remote_ip(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', TEST_EMPTY) + def test_db_empty(self, ignore): + self.assertFalse(is_ha_environment())