Unit tests now start their own Redis.

This commit is contained in:
Luke Sneeringer 2014-11-14 11:57:26 -06:00
parent 825d996316
commit 3d51b25762
7 changed files with 55 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import sys
import tempfile
import time
from multiprocessing import Process
from subprocess import Popen
# PyYAML
import yaml
@ -445,6 +446,7 @@ class BaseTestMixin(object):
self.assertTrue(set(obj.keys()) <= set(fields), msg)
def start_taskmanager(self, command_port):
self.start_redis()
self.taskmanager_process = Process(target=run_taskmanager,
args=(command_port,))
self.taskmanager_process.start()
@ -452,8 +454,10 @@ class BaseTestMixin(object):
def terminate_taskmanager(self):
if hasattr(self, 'taskmanager_process'):
self.taskmanager_process.terminate()
self.stop_redis()
def start_queue(self):
self.start_redis()
receiver = CallbackReceiver()
self.queue_process = Process(target=receiver.run_subscriber,
args=(False,))
@ -462,9 +466,21 @@ class BaseTestMixin(object):
def terminate_queue(self):
if hasattr(self, 'queue_process'):
self.queue_process.terminate()
self.stop_redis()
def start_redis(self):
if not getattr(self, 'redis_process', None):
self.redis_process = Popen('redis-server --port 16379 > /dev/null',
shell=True, executable='/bin/bash')
def stop_redis(self):
if getattr(self, 'redis_process', None):
self.redis_process.kill()
self.redis_process = None
@override_settings(SYSTEM_UUID='00000000-0000-0000-0000-000000000000')
@override_settings(SYSTEM_UUID='00000000-0000-0000-0000-000000000000',
BROKER_URL='redis://localhost:16379/')
class BaseTest(BaseTestMixin, django.test.TestCase):
'''
Base class for unit tests.

View File

@ -194,9 +194,14 @@ class CleanupDeletedTest(BaseCommandMixin, BaseTest):
'''
def setUp(self):
self.start_redis()
super(CleanupDeletedTest, self).setUp()
self.create_test_inventories()
def tearDown(self):
super(CleanupDeletedTest, self).tearDown()
self.stop_redis()
def get_model_counts(self):
def get_models(m):
if not m._meta.abstract:
@ -415,10 +420,15 @@ class InventoryImportTest(BaseCommandMixin, BaseLiveServerTest):
def setUp(self):
super(InventoryImportTest, self).setUp()
self.start_redis()
self.create_test_inventories()
self.create_test_ini()
self.create_test_license_file()
def tearDown(self):
super(InventoryImportTest, self).tearDown()
self.stop_redis()
def create_test_ini(self, inv_dir=None, ini_content=None):
ini_content = ini_content or TEST_INVENTORY_INI
handle, self.ini_path = tempfile.mkstemp(suffix='.txt', dir=inv_dir)

View File

@ -26,7 +26,7 @@ TEST_SIMPLE_INVENTORY_SCRIPT = "#!/usr/bin/env python\nimport json\nprint json.d
class InventoryTest(BaseTest):
def setUp(self):
self.start_redis()
super(InventoryTest, self).setUp()
self.setup_users()
self.organizations = self.make_organizations(self.super_django_user, 3)
@ -47,6 +47,10 @@ class InventoryTest(BaseTest):
permission_type = 'read'
)
def tearDown(self):
super(InventoryTest, self).tearDown()
self.stop_redis()
def test_get_inventory_list(self):
url = reverse('api:inventory_list')
qs = Inventory.objects.filter(active=True).distinct()

View File

@ -198,6 +198,14 @@ TEST_SURVEY_REQUIREMENTS = '''
class BaseJobTestMixin(BaseTestMixin):
''''''
def setUp(self):
self.start_redis()
super(BaseJobTestMixin, self).setUp()
def tearDown(self):
self.stop_redis()
super(BaseJobTestMixin, self).tearDown()
def _create_inventory(self, name, organization, created_by,
groups_hosts_dict):
'''Helper method for creating inventory with groups and hosts.'''

View File

@ -16,6 +16,7 @@ from awx.main.task_engine import *
class LicenseTests(BaseTest):
def setUp(self):
self.start_redis()
super(LicenseTests, self).setUp()
self.setup_users()
u = self.super_django_user
@ -34,6 +35,10 @@ class LicenseTests(BaseTest):
host = Host.objects.create(name='a11', inventory=inventory, created_by=u)
host = Host.objects.create(name='a12', inventory=inventory, created_by=u)
def tearDown(self):
super(LicenseTests, self).tearDown()
self.stop_redis()
def test_license_writer(self):
writer = TaskEngager(

View File

@ -57,6 +57,7 @@ class ScheduleTest(BaseTest):
def setUp(self):
super(ScheduleTest, self).setUp()
self.start_redis()
self.setup_users()
self.organizations = self.make_organizations(self.super_django_user, 2)
self.organizations[0].admins.add(self.normal_django_user)
@ -99,6 +100,10 @@ class ScheduleTest(BaseTest):
self.without_valid_source_inventory_group = self.without_valid_source_inventory.groups.create(name='not valid source')
self.without_valid_source_inventory_source = self.without_valid_source_inventory_group.inventory_source
def tearDown(self):
super(ScheduleTest, self).tearDown()
self.stop_redis()
def test_schedules_list(self):
url = reverse('api:schedule_list')
enabled_schedules = Schedule.objects.filter(enabled=True).distinct()

View File

@ -68,6 +68,7 @@ class InventoryScriptTest(BaseScriptTest):
def setUp(self):
super(InventoryScriptTest, self).setUp()
self.start_redis()
self.setup_users()
self.organizations = self.make_organizations(self.super_django_user, 2)
self.projects = self.make_projects(self.normal_django_user, 2)
@ -120,6 +121,10 @@ class InventoryScriptTest(BaseScriptTest):
group.hosts.add(local)
self.groups.extend(groups)
def tearDown(self):
super(InventoryScriptTest, self).tearDown()
self.stop_redis()
def run_inventory_script(self, *args, **options):
rest_api_url = self.live_server_url
parts = urlparse.urlsplit(rest_api_url)