Simplifies test.py
Removes unnecessary setup in test.py Change-Id: I61ecf0b38e863219f67111e3ebd3e613ac0bc5a9
This commit is contained in:
@@ -26,20 +26,14 @@ import os
|
||||
import eventlet
|
||||
eventlet.monkey_patch(os=False)
|
||||
|
||||
import copy
|
||||
import inspect
|
||||
import mock
|
||||
|
||||
import fixtures
|
||||
from nova import objects
|
||||
from nova.objects import base as objects_base
|
||||
from nova.tests import fixtures as nova_fixtures
|
||||
from nova.tests.unit import conf_fixture
|
||||
from nova.tests.unit import policy_fixture
|
||||
from nova import utils
|
||||
from oslo_concurrency import lockutils
|
||||
from oslo_config import cfg
|
||||
from oslo_config import fixture as config_fixture
|
||||
from oslo_log.fixture import logging_error as log_fixture
|
||||
from oslo_log import log as logging
|
||||
from oslotest import moxstubout
|
||||
@@ -47,16 +41,9 @@ import testtools
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('enabled', 'nova.api.openstack', group='osapi_v3')
|
||||
|
||||
logging.register_options(CONF)
|
||||
CONF.set_override('use_stderr', False)
|
||||
logging.setup(CONF, 'nova')
|
||||
|
||||
# NOTE(comstud): Make sure we have all of the objects loaded. We do this
|
||||
# at module import time, because we may be using mock decorators in our
|
||||
# tests that run at import time.
|
||||
objects.register_all()
|
||||
|
||||
_TRUE_VALUES = ('True', 'true', '1', 'yes')
|
||||
|
||||
@@ -84,20 +71,18 @@ def _patch_mock_to_raise_for_invalid_assert_calls():
|
||||
_patch_mock_to_raise_for_invalid_assert_calls()
|
||||
|
||||
|
||||
class TestCase(testtools.TestCase):
|
||||
class NoDBTestCase(testtools.TestCase):
|
||||
"""Test case base class for all unit tests.
|
||||
|
||||
Due to the slowness of DB access, please consider deriving from
|
||||
`NoDBTestCase` first.
|
||||
"""
|
||||
USES_DB = True
|
||||
REQUIRES_LOCKING = False
|
||||
|
||||
TIMEOUT_SCALING_FACTOR = 1
|
||||
|
||||
def setUp(self):
|
||||
"""Run before each test method to initialize test environment."""
|
||||
super(TestCase, self).setUp()
|
||||
super(NoDBTestCase, self).setUp()
|
||||
self.useFixture(nova_fixtures.Timeout(
|
||||
os.environ.get('OS_TEST_TIMEOUT', 0),
|
||||
self.TIMEOUT_SCALING_FACTOR))
|
||||
@@ -107,62 +92,21 @@ class TestCase(testtools.TestCase):
|
||||
self.useFixture(log_fixture.get_logging_handle_error_fixture())
|
||||
|
||||
self.useFixture(nova_fixtures.OutputStreamCapture())
|
||||
|
||||
self.useFixture(nova_fixtures.StandardLogging())
|
||||
|
||||
# NOTE(sdague): because of the way we were using the lock
|
||||
# wrapper we eneded up with a lot of tests that started
|
||||
# relying on global external locking being set up for them. We
|
||||
# consider all of these to be *bugs*. Tests should not require
|
||||
# global external locking, or if they do, they should
|
||||
# explicitly set it up themselves.
|
||||
#
|
||||
# The following REQUIRES_LOCKING class parameter is provided
|
||||
# as a bridge to get us there. No new tests should be added
|
||||
# that require it, and existing classes and tests should be
|
||||
# fixed to not need it.
|
||||
if self.REQUIRES_LOCKING:
|
||||
lock_path = self.useFixture(fixtures.TempDir()).path
|
||||
self.fixture = self.useFixture(
|
||||
config_fixture.Config(lockutils.CONF))
|
||||
self.fixture.config(lock_path=lock_path,
|
||||
group='oslo_concurrency')
|
||||
|
||||
self.useFixture(conf_fixture.ConfFixture(CONF))
|
||||
self.useFixture(nova_fixtures.RPCFixture('nova.test'))
|
||||
|
||||
if self.USES_DB:
|
||||
self.useFixture(nova_fixtures.Database())
|
||||
|
||||
# NOTE(blk-u): WarningsFixture must be after the Database fixture
|
||||
# because sqlalchemy-migrate messes with the warnings filters.
|
||||
self.useFixture(nova_fixtures.WarningsFixture())
|
||||
|
||||
# NOTE(danms): Make sure to reset us back to non-remote objects
|
||||
# for each test to avoid interactions. Also, backup the object
|
||||
# registry.
|
||||
objects_base.NovaObject.indirection_api = None
|
||||
self._base_test_obj_backup = copy.copy(
|
||||
objects_base.NovaObjectRegistry._registry._obj_classes)
|
||||
self.addCleanup(self._restore_obj_registry)
|
||||
|
||||
# NOTE(mnaser): All calls to utils.is_neutron() are cached in
|
||||
# nova.utils._IS_NEUTRON. We set it to None to avoid any
|
||||
# caching of that value.
|
||||
utils._IS_NEUTRON = None
|
||||
|
||||
mox_fixture = self.useFixture(moxstubout.MoxStubout())
|
||||
self.mox = mox_fixture.mox
|
||||
self.stubs = mox_fixture.stubs
|
||||
self.addCleanup(self._clear_attrs)
|
||||
self.useFixture(fixtures.EnvironmentVariable('http_proxy'))
|
||||
self.policy = self.useFixture(policy_fixture.PolicyFixture())
|
||||
|
||||
self.useFixture(nova_fixtures.PoisonFunctions())
|
||||
|
||||
def _restore_obj_registry(self):
|
||||
objects_base.NovaObject._obj_classes = self._base_test_obj_backup
|
||||
|
||||
def _clear_attrs(self):
|
||||
# Delete attributes that don't start with _ so they don't pin
|
||||
# memory around unnecessarily for the duration of the test
|
||||
@@ -205,11 +149,3 @@ class TestCase(testtools.TestCase):
|
||||
self.assertEqual(baseargs, implargs,
|
||||
"%s args don't match base class %s" %
|
||||
(name, baseclass))
|
||||
|
||||
|
||||
class NoDBTestCase(TestCase):
|
||||
"""`NoDBTestCase` differs from TestCase in that DB access is not supported.
|
||||
This makes tests run significantly faster. If possible, all new tests
|
||||
should derive from this class.
|
||||
"""
|
||||
USES_DB = False
|
||||
|
||||
Reference in New Issue
Block a user