Generate large test license for cloud inventory import unit tests.

This commit is contained in:
Chris Church
2014-06-05 16:20:17 -04:00
parent d2d7ac3d1c
commit 3a5db149e9
3 changed files with 38 additions and 39 deletions

View File

@@ -9,6 +9,7 @@ import json
import os import os
import random import random
import shutil import shutil
import sys
import tempfile import tempfile
import time import time
from multiprocessing import Process from multiprocessing import Process
@@ -28,6 +29,7 @@ from awx.main.backend import LDAPSettings
from awx.main.management.commands.run_callback_receiver import run_subscriber from awx.main.management.commands.run_callback_receiver import run_subscriber
from awx.main.management.commands.run_task_system import run_taskmanager from awx.main.management.commands.run_task_system import run_taskmanager
from awx.main.utils import get_ansible_version from awx.main.utils import get_ansible_version
from awx.main.task_engine import TaskEngager as LicenseWriter
class BaseTestMixin(object): class BaseTestMixin(object):
@@ -38,6 +40,13 @@ class BaseTestMixin(object):
def setUp(self): def setUp(self):
super(BaseTestMixin, self).setUp() super(BaseTestMixin, self).setUp()
self.object_ctr = 0 self.object_ctr = 0
# Save sys.path before tests.
self._sys_path = [x for x in sys.path]
# Save os.environ before tests.
self._environ = dict(os.environ.items())
# Capture current directory to change back after each test.
self._cwd = os.getcwd()
# Capture list of temp files/directories created during tests.
self._temp_paths = [] self._temp_paths = []
self._current_auth = None self._current_auth = None
self._user_passwords = {} self._user_passwords = {}
@@ -46,8 +55,6 @@ class BaseTestMixin(object):
# Wrap settings so we can redefine them within each test. # Wrap settings so we can redefine them within each test.
self._wrapped = settings._wrapped self._wrapped = settings._wrapped
settings._wrapped = UserSettingsHolder(settings._wrapped) settings._wrapped = UserSettingsHolder(settings._wrapped)
# Capture current directory, change back after each test.
self._cwd = os.getcwd()
# Set all AUTH_LDAP_* settings to defaults to avoid using LDAP for # Set all AUTH_LDAP_* settings to defaults to avoid using LDAP for
# tests unless expicitly configured. # tests unless expicitly configured.
for name, value in LDAPSettings.defaults.items(): for name, value in LDAPSettings.defaults.items():
@@ -84,6 +91,18 @@ class BaseTestMixin(object):
def tearDown(self): def tearDown(self):
super(BaseTestMixin, self).tearDown() super(BaseTestMixin, self).tearDown()
# Restore sys.path after tests.
sys.path = self._sys_path
# Restore os.environ after tests.
for k,v in self._environ.items():
if os.environ.get(k, None) != v:
os.environ[k] = v
for k,v in os.environ.items():
if k not in self._environ.keys():
del os.environ[k]
# Restore current directory after each test.
os.chdir(self._cwd)
# Cleanup temp files/directories created during tests.
for project_dir in self._temp_paths: for project_dir in self._temp_paths:
if os.path.exists(project_dir): if os.path.exists(project_dir):
if os.path.isdir(project_dir): if os.path.isdir(project_dir):
@@ -92,8 +111,20 @@ class BaseTestMixin(object):
os.remove(project_dir) os.remove(project_dir)
# Restore previous settings after each test. # Restore previous settings after each test.
settings._wrapped = self._wrapped settings._wrapped = self._wrapped
# Restore current directory after each test.
os.chdir(self._cwd) def create_test_license_file(self, instance_count=10000):
writer = LicenseWriter(
company_name='AWX',
contact_name='AWX Admin',
contact_email='awx@example.com',
license_date=int(time.time() + 3600),
instance_count=instance_count,
)
handle, license_path = tempfile.mkstemp(suffix='.json')
os.close(handle)
writer.write_file(license_path)
self._temp_paths.append(license_path)
os.environ['AWX_LICENSE_FILE'] = license_path
def assertElapsedLessThan(self, seconds): def assertElapsedLessThan(self, seconds):
elapsed = time.time() - self._start_time elapsed = time.time() - self._start_time

View File

@@ -21,7 +21,6 @@ from django.utils.timezone import now
from django.test.utils import override_settings from django.test.utils import override_settings
# AWX # AWX
from awx.main.task_engine import TaskEngager as LicenseWriter
from awx.main.models import * from awx.main.models import *
from awx.main.tests.base import BaseTest, BaseLiveServerTest from awx.main.tests.base import BaseTest, BaseLiveServerTest
@@ -97,25 +96,6 @@ class BaseCommandMixin(object):
Base class for tests that run management commands. Base class for tests that run management commands.
''' '''
def setUp(self):
super(BaseCommandMixin, self).setUp()
self._sys_path = [x for x in sys.path]
self._environ = dict(os.environ.items())
self._temp_files = []
def tearDown(self):
super(BaseCommandMixin, self).tearDown()
sys.path = self._sys_path
for k,v in self._environ.items():
if os.environ.get(k, None) != v:
os.environ[k] = v
for k,v in os.environ.items():
if k not in self._environ.keys():
del os.environ[k]
for tf in self._temp_files:
if os.path.exists(tf):
os.remove(tf)
def create_test_inventories(self): def create_test_inventories(self):
self.setup_users() self.setup_users()
self.organizations = self.make_organizations(self.super_django_user, 2) self.organizations = self.make_organizations(self.super_django_user, 2)
@@ -439,27 +419,13 @@ class InventoryImportTest(BaseCommandMixin, BaseLiveServerTest):
self.create_test_ini() self.create_test_ini()
self.create_test_license_file() self.create_test_license_file()
def create_test_license_file(self):
writer = LicenseWriter(
company_name='AWX',
contact_name='AWX Admin',
contact_email='awx@example.com',
license_date=int(time.time() + 3600),
instance_count=10000,
)
handle, license_path = tempfile.mkstemp(suffix='.json')
os.close(handle)
writer.write_file(license_path)
self._temp_files.append(license_path)
os.environ['AWX_LICENSE_FILE'] = license_path
def create_test_ini(self, inv_dir=None, ini_content=None): def create_test_ini(self, inv_dir=None, ini_content=None):
ini_content = ini_content or TEST_INVENTORY_INI ini_content = ini_content or TEST_INVENTORY_INI
handle, self.ini_path = tempfile.mkstemp(suffix='.txt', dir=inv_dir) handle, self.ini_path = tempfile.mkstemp(suffix='.txt', dir=inv_dir)
ini_file = os.fdopen(handle, 'w') ini_file = os.fdopen(handle, 'w')
ini_file.write(ini_content) ini_file.write(ini_content)
ini_file.close() ini_file.close()
self._temp_files.append(self.ini_path) self._temp_paths.append(self.ini_path)
def create_test_dir(self, hostnames=None): def create_test_dir(self, hostnames=None):
hostnames = hostnames or [] hostnames = hostnames or []

View File

@@ -1373,6 +1373,7 @@ class InventoryUpdatesTest(BaseTransactionTest):
source_regions = getattr(settings, 'TEST_AWS_REGIONS', 'all') source_regions = getattr(settings, 'TEST_AWS_REGIONS', 'all')
if not all([source_username, source_password]): if not all([source_username, source_password]):
self.skipTest('no test ec2 credentials defined!') self.skipTest('no test ec2 credentials defined!')
self.create_test_license_file()
credential = Credential.objects.create(kind='aws', credential = Credential.objects.create(kind='aws',
user=self.super_django_user, user=self.super_django_user,
username=source_username, username=source_username,
@@ -1419,6 +1420,7 @@ class InventoryUpdatesTest(BaseTransactionTest):
source_regions = getattr(settings, 'TEST_RACKSPACE_REGIONS', '') source_regions = getattr(settings, 'TEST_RACKSPACE_REGIONS', '')
if not all([source_username, source_password]): if not all([source_username, source_password]):
self.skipTest('no test rackspace credentials defined!') self.skipTest('no test rackspace credentials defined!')
self.create_test_license_file()
credential = Credential.objects.create(kind='rax', credential = Credential.objects.create(kind='rax',
user=self.super_django_user, user=self.super_django_user,
username=source_username, username=source_username,