diff options
-rw-r--r-- | nova/test.py | 32 | ||||
-rw-r--r-- | nova/tests/scheduler/fakes.py | 10 | ||||
-rw-r--r-- | nova/tests/scheduler/test_filter_scheduler.py | 33 | ||||
-rw-r--r-- | nova/tests/scheduler/test_host_filters.py | 7 | ||||
-rw-r--r-- | nova/tests/scheduler/test_host_manager.py | 6 | ||||
-rw-r--r-- | nova/tests/scheduler/test_rpcapi.py | 2 | ||||
-rw-r--r-- | nova/tests/scheduler/test_scheduler.py | 16 | ||||
-rw-r--r-- | nova/tests/scheduler/test_scheduler_options.py | 2 | ||||
-rw-r--r-- | nova/tests/scheduler/test_weights.py | 4 |
9 files changed, 80 insertions, 32 deletions
diff --git a/nova/test.py b/nova/test.py index f0dba7c7c..d7502b4ea 100644 --- a/nova/test.py +++ b/nova/test.py @@ -189,7 +189,12 @@ class TestingException(Exception): class TestCase(testtools.TestCase): - """Test case base class for all unit tests.""" + """Test case base class for all unit tests. + + Due to the slowness of DB access, please consider deriving from + `NoDBTestCase` first. + """ + USES_DB = True def setUp(self): """Run before each test method to initialize test environment.""" @@ -217,13 +222,15 @@ class TestCase(testtools.TestCase): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.useFixture(conf_fixture.ConfFixture(CONF)) - global _DB_CACHE - if not _DB_CACHE: - _DB_CACHE = Database(session, migration, - sql_connection=CONF.sql_connection, - sqlite_db=CONF.sqlite_db, - sqlite_clean_db=CONF.sqlite_clean_db) - self.useFixture(_DB_CACHE) + if self.USES_DB: + global _DB_CACHE + if not _DB_CACHE: + _DB_CACHE = Database(session, migration, + sql_connection=CONF.sql_connection, + sqlite_db=CONF.sqlite_db, + sqlite_clean_db=CONF.sqlite_clean_db) + + self.useFixture(_DB_CACHE) mox_fixture = self.useFixture(MoxStubout()) self.mox = mox_fixture.mox @@ -274,3 +281,12 @@ class TimeOverride(fixtures.Fixture): super(TimeOverride, self).setUp() timeutils.set_time_override() self.addCleanup(timeutils.clear_time_override) + + +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 diff --git a/nova/tests/scheduler/fakes.py b/nova/tests/scheduler/fakes.py index c9157d11c..3dfc7c452 100644 --- a/nova/tests/scheduler/fakes.py +++ b/nova/tests/scheduler/fakes.py @@ -18,7 +18,6 @@ Fakes For Scheduler tests. import mox -from nova.compute import flavors from nova.compute import vm_states from nova import db from nova.scheduler import filter_scheduler @@ -107,14 +106,14 @@ class FakeHostState(host_manager.HostState): class FakeInstance(object): - def __init__(self, context=None, params=None, type_name='m1.tiny'): + def __init__(self, context=None, params=None): """Create a test instance. Returns uuid.""" self.context = context - i = self._create_fake_instance(params, type_name=type_name) + i = self._create_fake_instance(params=params) self.uuid = i['uuid'] - def _create_fake_instance(self, params=None, type_name='m1.tiny'): + def _create_fake_instance(self, params=None): """Create a test instance.""" if not params: params = {} @@ -125,8 +124,7 @@ class FakeInstance(object): inst['reservation_id'] = 'r-fakeres' inst['user_id'] = 'fake' inst['project_id'] = 'fake' - type_id = flavors.get_instance_type_by_name(type_name)['id'] - inst['instance_type_id'] = type_id + inst['instance_type_id'] = 2 inst['ami_launch_index'] = 0 inst.update(params) return db.instance_create(self.context, inst) diff --git a/nova/tests/scheduler/test_filter_scheduler.py b/nova/tests/scheduler/test_filter_scheduler.py index 80680dda8..919187fd8 100644 --- a/nova/tests/scheduler/test_filter_scheduler.py +++ b/nova/tests/scheduler/test_filter_scheduler.py @@ -18,7 +18,6 @@ Tests For Filter Scheduler. import mox -from nova.compute import flavors from nova.compute import rpcapi as compute_rpcapi from nova.compute import utils as compute_utils from nova.compute import vm_states @@ -78,6 +77,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): compute_utils.add_instance_fault_from_exc(fake_context, mox.IsA(conductor_api.LocalAPI), new_ref, mox.IsA(exception.NoValidHost), mox.IgnoreArg()) + + self.mox.StubOutWithMock(db, 'compute_node_get_all') + db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) + self.mox.ReplayAll() sched.schedule_run_instance( fake_context, request_spec, None, None, None, None, {}) @@ -240,6 +243,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): request_spec = dict(instance_properties=instance_properties) filter_properties = {} + self.mox.StubOutWithMock(db, 'compute_node_get_all') + db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) + self.mox.ReplayAll() + sched._schedule(self.context, request_spec, filter_properties=filter_properties) @@ -255,6 +262,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): request_spec = dict(instance_properties=instance_properties) filter_properties = {} + self.mox.StubOutWithMock(db, 'compute_node_get_all') + db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) + self.mox.ReplayAll() + sched._schedule(self.context, request_spec, filter_properties=filter_properties) @@ -272,6 +283,10 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): retry = dict(num_attempts=1) filter_properties = dict(retry=retry) + self.mox.StubOutWithMock(db, 'compute_node_get_all') + db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) + self.mox.ReplayAll() + sched._schedule(self.context, request_spec, filter_properties=filter_properties) @@ -335,10 +350,22 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): sched = fakes.FakeFilterScheduler() image = 'image' - instance = db.instance_create(self.context, {}) + instance = {'disable_terminate': False, + 'uuid': 'fakeuuid', + 'deleted': 0, 'info_cache': {}, + 'created_at': None, + 'system_metadata': [], 'shutdown_terminate': False, + 'id': 1, 'security_groups': [], 'metadata': []} instance_properties = {'project_id': 'fake', 'os_type': 'Linux'} - instance_type = flavors.get_instance_type_by_name("m1.tiny") + instance_type = { + 'memory_mb': 1024, 'root_gb': 40, 'deleted_at': None, + 'name': u'm1.medium', 'deleted': 0, 'created_at': None, + 'ephemeral_gb': 0, 'updated_at': None, 'disabled': False, + 'vcpus': 2, 'extra_specs': {}, 'swap': 0, + 'rxtx_factor': 1.0, 'is_public': True, 'flavorid': u'3', + 'vcpu_weight': None, 'id': 1} + request_spec = {'instance_properties': instance_properties, 'instance_type': instance_type} retry = {'hosts': [], 'num_attempts': 1} diff --git a/nova/tests/scheduler/test_host_filters.py b/nova/tests/scheduler/test_host_filters.py index 7d7c12339..b09e23f1d 100644 --- a/nova/tests/scheduler/test_host_filters.py +++ b/nova/tests/scheduler/test_host_filters.py @@ -44,7 +44,7 @@ class TestBogusFilter(object): pass -class ExtraSpecsOpsTestCase(test.TestCase): +class ExtraSpecsOpsTestCase(test.NoDBTestCase): def _do_extra_specs_ops_test(self, value, req, matches): assertion = self.assertTrue if matches else self.assertFalse assertion(extra_specs_ops.match(value, req)) @@ -230,8 +230,11 @@ class ExtraSpecsOpsTestCase(test.TestCase): matches=False) -class HostFiltersTestCase(test.TestCase): +class HostFiltersTestCase(test.NoDBTestCase): """Test case for host filters.""" + # FIXME(sirp): These tests still require DB access until we can separate + # the testing of the DB API code from the host-filter code. + USES_DB = True def fake_oat_request(self, *args, **kwargs): """Stubs out the response from OAT service.""" diff --git a/nova/tests/scheduler/test_host_manager.py b/nova/tests/scheduler/test_host_manager.py index c03c66c4e..8db62bfea 100644 --- a/nova/tests/scheduler/test_host_manager.py +++ b/nova/tests/scheduler/test_host_manager.py @@ -37,7 +37,7 @@ class FakeFilterClass2(filters.BaseHostFilter): pass -class HostManagerTestCase(test.TestCase): +class HostManagerTestCase(test.NoDBTestCase): """Test case for HostManager class.""" def setUp(self): @@ -358,7 +358,7 @@ class HostManagerTestCase(test.TestCase): 8388608) -class HostManagerChangedNodesTestCase(test.TestCase): +class HostManagerChangedNodesTestCase(test.NoDBTestCase): """Test case for HostManager class.""" def setUp(self): @@ -416,7 +416,7 @@ class HostManagerChangedNodesTestCase(test.TestCase): self.assertEqual(len(host_states_map), 0) -class HostStateTestCase(test.TestCase): +class HostStateTestCase(test.NoDBTestCase): """Test case for HostState class.""" # update_from_compute_node() and consume_from_instance() are tested diff --git a/nova/tests/scheduler/test_rpcapi.py b/nova/tests/scheduler/test_rpcapi.py index 44e6a91b8..cecc55f20 100644 --- a/nova/tests/scheduler/test_rpcapi.py +++ b/nova/tests/scheduler/test_rpcapi.py @@ -28,7 +28,7 @@ from nova import test CONF = cfg.CONF -class SchedulerRpcAPITestCase(test.TestCase): +class SchedulerRpcAPITestCase(test.NoDBTestCase): def _test_scheduler_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = scheduler_rpcapi.SchedulerAPI() diff --git a/nova/tests/scheduler/test_scheduler.py b/nova/tests/scheduler/test_scheduler.py index f4f607647..7742962bd 100644 --- a/nova/tests/scheduler/test_scheduler.py +++ b/nova/tests/scheduler/test_scheduler.py @@ -48,7 +48,7 @@ from nova.tests.scheduler import fakes from nova import utils -class SchedulerManagerTestCase(test.TestCase): +class SchedulerManagerTestCase(test.NoDBTestCase): """Test case for scheduler manager.""" manager_cls = manager.SchedulerManager @@ -373,7 +373,7 @@ class SchedulerManagerTestCase(test.TestCase): self.context, None, request) -class SchedulerTestCase(test.TestCase): +class SchedulerTestCase(test.NoDBTestCase): """Test case for base scheduler driver class.""" # So we can subclass this test and re-use tests if we need. @@ -435,9 +435,13 @@ class SchedulerTestCase(test.TestCase): self.assertEqual(result, ['host2']) def _live_migration_instance(self): - inst_type = flavors.get_instance_type(1) - # NOTE(danms): we have _got_ to stop doing this! - inst_type['memory_mb'] = 1024 + inst_type = {'memory_mb': 1024, 'root_gb': 40, 'deleted_at': None, + 'name': u'm1.medium', 'deleted': 0, 'created_at': None, + 'ephemeral_gb': 0, 'updated_at': None, 'disabled': False, + 'vcpus': 2, 'extra_specs': {}, 'swap': 0, + 'rxtx_factor': 1.0, 'is_public': True, 'flavorid': u'3', + 'vcpu_weight': None, 'id': 1} + sys_meta = utils.dict_to_metadata( flavors.save_instance_type_info({}, inst_type)) return {'id': 31337, @@ -952,7 +956,7 @@ class SchedulerDriverBaseTestCase(SchedulerTestCase): fake_request_spec, {}, {}, {}, None) -class SchedulerDriverModuleTestCase(test.TestCase): +class SchedulerDriverModuleTestCase(test.NoDBTestCase): """Test case for scheduler driver module methods.""" def setUp(self): diff --git a/nova/tests/scheduler/test_scheduler_options.py b/nova/tests/scheduler/test_scheduler_options.py index df54cde5f..6ab245ef9 100644 --- a/nova/tests/scheduler/test_scheduler_options.py +++ b/nova/tests/scheduler/test_scheduler_options.py @@ -50,7 +50,7 @@ class FakeSchedulerOptions(scheduler_options.SchedulerOptions): return self._time_now -class SchedulerOptionsTestCase(test.TestCase): +class SchedulerOptionsTestCase(test.NoDBTestCase): def test_get_configuration_first_time_no_flag(self): last_checked = None now = datetime.datetime(2012, 1, 1, 1, 1, 1) diff --git a/nova/tests/scheduler/test_weights.py b/nova/tests/scheduler/test_weights.py index aa0f0c15f..7495edacc 100644 --- a/nova/tests/scheduler/test_weights.py +++ b/nova/tests/scheduler/test_weights.py @@ -23,7 +23,7 @@ from nova.tests import matchers from nova.tests.scheduler import fakes -class TestWeighedHost(test.TestCase): +class TestWeighedHost(test.NoDBTestCase): def test_dict_conversion(self): host_state = fakes.FakeHostState('somehost', None, {}) host = weights.WeighedHost(host_state, 'someweight') @@ -38,7 +38,7 @@ class TestWeighedHost(test.TestCase): self.assertIn('RAMWeigher', class_names) -class RamWeigherTestCase(test.TestCase): +class RamWeigherTestCase(test.NoDBTestCase): def setUp(self): super(RamWeigherTestCase, self).setUp() self.host_manager = fakes.FakeHostManager() |