From c9de74ac2a618f1f85cffaaf5210f07b8329cc05 Mon Sep 17 00:00:00 2001 From: Takashi NATSUME Date: Tue, 31 May 2016 19:42:12 +0900 Subject: [PATCH] Stop using mox stubs in nova/tests/unit/cells The mox stubs have been replaced in the following files. * nova/tests/unit/cells/test_cells_filters.py * nova/tests/unit/cells/test_cells_rpcapi.py * nova/tests/unit/cells/test_cells_scheduler.py * nova/tests/unit/cells/test_cells_state_manager.py * nova/tests/unit/cells/test_cells_utils.py Change-Id: I27b22e7b1ae22527c4f99371a417679afbe07048 Implements: blueprint remove-mox-ocata --- nova/tests/unit/cells/test_cells_filters.py | 6 +- nova/tests/unit/cells/test_cells_rpcapi.py | 15 +- nova/tests/unit/cells/test_cells_scheduler.py | 150 ++++++++++-------- .../unit/cells/test_cells_state_manager.py | 22 ++- nova/tests/unit/cells/test_cells_utils.py | 4 +- 5 files changed, 104 insertions(+), 93 deletions(-) diff --git a/nova/tests/unit/cells/test_cells_filters.py b/nova/tests/unit/cells/test_cells_filters.py index de78b2a978..b6c697ddec 100644 --- a/nova/tests/unit/cells/test_cells_filters.py +++ b/nova/tests/unit/cells/test_cells_filters.py @@ -152,13 +152,13 @@ class TestTargetCellFilter(_FilterTestClass): def test_target_cell_specified_not_me(self): info = {} - def _fake_build_instances(ctxt, cell, sched_kwargs): + def _fake_build_instances(self, ctxt, cell, sched_kwargs): info['ctxt'] = ctxt info['cell'] = cell info['sched_kwargs'] = sched_kwargs - self.stubs.Set(self.msg_runner, 'build_instances', - _fake_build_instances) + self.stub_out('nova.cells.messaging.MessageRunner.build_instances', + _fake_build_instances) cells = [1, 2, 3] target_cell = 'fake!cell!path' current_cell = 'not!the!same' diff --git a/nova/tests/unit/cells/test_cells_rpcapi.py b/nova/tests/unit/cells/test_cells_rpcapi.py index a2dd666681..dafe6bfce9 100644 --- a/nova/tests/unit/cells/test_cells_rpcapi.py +++ b/nova/tests/unit/cells/test_cells_rpcapi.py @@ -44,23 +44,26 @@ class CellsAPITestCase(test.NoDBTestCase): orig_prepare = self.cells_rpcapi.client.prepare - def fake_rpc_prepare(**kwargs): + def fake_rpc_prepare(self_rpcclient, **kwargs): if 'version' in kwargs: call_info['version'] = kwargs.pop('version') return self.cells_rpcapi.client - def fake_csv(version): + def fake_csv(self, version): return orig_prepare(version).can_send_version() - def fake_rpc_method(ctxt, method, **kwargs): + def fake_rpc_method(self, ctxt, method, **kwargs): call_info['context'] = ctxt call_info['method'] = method call_info['args'] = kwargs return result - self.stubs.Set(self.cells_rpcapi.client, 'prepare', fake_rpc_prepare) - self.stubs.Set(self.cells_rpcapi.client, 'can_send_version', fake_csv) - self.stubs.Set(self.cells_rpcapi.client, rpc_method, fake_rpc_method) + self.stub_out('oslo_messaging.rpc.client.RPCClient.prepare', + fake_rpc_prepare) + self.stub_out('oslo_messaging.rpc.client.RPCClient.can_send_version', + fake_csv) + self.stub_out('oslo_messaging.rpc.client.RPCClient.%s' % rpc_method, + fake_rpc_method) return call_info diff --git a/nova/tests/unit/cells/test_cells_scheduler.py b/nova/tests/unit/cells/test_cells_scheduler.py index 204393de15..0f2996c1f1 100644 --- a/nova/tests/unit/cells/test_cells_scheduler.py +++ b/nova/tests/unit/cells/test_cells_scheduler.py @@ -16,7 +16,6 @@ Tests For CellsScheduler """ import copy -import time import mock from oslo_utils import uuidutils @@ -30,7 +29,6 @@ from nova import context from nova import db from nova import exception from nova import objects -from nova.scheduler import utils as scheduler_utils from nova import test from nova.tests.unit.cells import fakes from nova.tests.unit import fake_block_device @@ -121,11 +119,12 @@ class CellsSchedulerTestCase(test.TestCase): anon=True)) ]) - def _fake_instance_update_at_top(_ctxt, instance): + def _fake_instance_update_at_top(self, _ctxt, instance): call_info['uuids'].append(instance['uuid']) - self.stubs.Set(self.msg_runner, 'instance_update_at_top', - _fake_instance_update_at_top) + self.stub_out('nova.cells.messaging.MessageRunner.' + 'instance_update_at_top', + _fake_instance_update_at_top) self.scheduler._create_instances_here(self.ctxt, instance_uuids, instance_props, inst_type, image, @@ -197,7 +196,8 @@ class CellsSchedulerTestCase(test.TestCase): orig_fn = self.msg_runner.build_instances - def msg_runner_build_instances(ctxt, target_cell, build_inst_kwargs): + def msg_runner_build_instances(self_mr, ctxt, target_cell, + build_inst_kwargs): # This gets called twice. Once for our running it # in this cell.. and then it'll get called when the # child cell is picked. So, first time.. just run it @@ -215,10 +215,10 @@ class CellsSchedulerTestCase(test.TestCase): 'image': image} return request_spec - self.stubs.Set(self.msg_runner, 'build_instances', - msg_runner_build_instances) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) + self.stub_out('nova.cells.messaging.MessageRunner.build_instances', + msg_runner_build_instances) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) self.msg_runner.build_instances(self.ctxt, self.my_cell_state, self.build_inst_kwargs) @@ -230,6 +230,8 @@ class CellsSchedulerTestCase(test.TestCase): self.assertIn(call_info['target_cell'], child_cells) def test_build_instances_selects_current_cell(self): + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + group='cells') # Make sure there's no child cells so that we will be # selected self.state_manager.child_cells = {} @@ -237,7 +239,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info = {} build_inst_kwargs = copy.deepcopy(self.build_inst_kwargs) - def fake_create_instances_here(ctxt, instance_uuids, + def fake_create_instances_here(self_cs, ctxt, instance_uuids, instance_properties, instance_type, image, security_groups, block_device_mapping): call_info['ctxt'] = ctxt @@ -249,7 +251,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info['block_device_mapping'] = block_device_mapping return self.instances - def fake_rpc_build_instances(ctxt, **build_inst_kwargs): + def fake_rpc_build_instances(self, ctxt, **build_inst_kwargs): call_info['build_inst_kwargs'] = build_inst_kwargs def fake_build_request_spec(ctxt, image, instances): @@ -258,12 +260,13 @@ class CellsSchedulerTestCase(test.TestCase): 'image': image} return request_spec - self.stubs.Set(self.scheduler, '_create_instances_here', - fake_create_instances_here) - self.stubs.Set(self.scheduler.compute_task_api, - 'build_instances', fake_rpc_build_instances) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) + self.stub_out('nova.cells.scheduler.CellsScheduler.' + '_create_instances_here', + fake_create_instances_here) + self.stub_out('nova.conductor.api.ComputeTaskAPI.' + 'build_instances', fake_rpc_build_instances) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) self.msg_runner.build_instances(self.ctxt, self.my_cell_state, build_inst_kwargs) @@ -285,11 +288,12 @@ class CellsSchedulerTestCase(test.TestCase): self.assertEqual(self.instance_uuids, call_info['instance_uuids']) def test_build_instances_retries_when_no_cells_avail(self): - self.flags(scheduler_retries=7, group='cells') + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + scheduler_retries=7, group='cells') call_info = {'num_tries': 0, 'errored_uuids': []} - def fake_grab_target_cells(filter_properties): + def fake_grab_target_cells(self, filter_properties): call_info['num_tries'] += 1 raise exception.NoCellsAvailable() @@ -306,12 +310,12 @@ class CellsSchedulerTestCase(test.TestCase): 'image': image} return request_spec - self.stubs.Set(self.scheduler, '_grab_target_cells', - fake_grab_target_cells) - self.stubs.Set(time, 'sleep', fake_sleep) - self.stubs.Set(objects.Instance, 'save', fake_instance_save) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) + self.stub_out('nova.cells.scheduler.CellsScheduler._grab_target_cells', + fake_grab_target_cells) + self.stub_out('time.sleep', fake_sleep) + self.stub_out('nova.objects.Instance.save', fake_instance_save) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) self.msg_runner.build_instances(self.ctxt, self.my_cell_state, self.build_inst_kwargs) @@ -320,7 +324,8 @@ class CellsSchedulerTestCase(test.TestCase): self.assertEqual(self.instance_uuids, call_info['errored_uuids']) def test_schedule_method_on_random_exception(self): - self.flags(scheduler_retries=7, group='cells') + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + scheduler_retries=7, group='cells') instances = [objects.Instance(uuid=uuid) for uuid in self.instance_uuids] @@ -333,7 +338,7 @@ class CellsSchedulerTestCase(test.TestCase): 'errored_uuids1': [], 'errored_uuids2': []} - def fake_grab_target_cells(filter_properties): + def fake_grab_target_cells(self, filter_properties): call_info['num_tries'] += 1 raise test.TestingException() @@ -341,7 +346,7 @@ class CellsSchedulerTestCase(test.TestCase): self.assertEqual(vm_states.ERROR, inst.vm_state) call_info['errored_uuids1'].append(inst.uuid) - def fake_instance_update_at_top(ctxt, instance): + def fake_instance_update_at_top(self_mr, ctxt, instance): self.assertEqual(vm_states.ERROR, instance['vm_state']) call_info['errored_uuids2'].append(instance['uuid']) @@ -351,13 +356,14 @@ class CellsSchedulerTestCase(test.TestCase): 'image': image} return request_spec - self.stubs.Set(self.scheduler, '_grab_target_cells', - fake_grab_target_cells) - self.stubs.Set(objects.Instance, 'save', fake_instance_save) - self.stubs.Set(self.msg_runner, 'instance_update_at_top', - fake_instance_update_at_top) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) + self.stub_out('nova.cells.scheduler.CellsScheduler._grab_target_cells', + fake_grab_target_cells) + self.stub_out('nova.objects.Instance.save', fake_instance_save) + self.stub_out('nova.cells.messaging.MessageRunner.' + 'instance_update_at_top', + fake_instance_update_at_top) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) self.msg_runner.build_instances(self.ctxt, self.my_cell_state, method_kwargs) @@ -379,7 +385,8 @@ class CellsSchedulerTestCase(test.TestCase): our_path = 'nova.tests.unit.cells.test_cells_scheduler' cls_names = [our_path + '.' + 'FakeFilterClass1', our_path + '.' + 'FakeFilterClass2'] - self.flags(scheduler_filter_classes=cls_names, group='cells') + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + scheduler_filter_classes=cls_names, group='cells') self._init_cells_scheduler() # Make sure there's no child cells so that we will be @@ -388,7 +395,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info = {} - def fake_create_instances_here(ctxt, instance_uuids, + def fake_create_instances_here(self_cs, ctxt, instance_uuids, instance_properties, instance_type, image, security_groups, block_device_mapping): call_info['ctxt'] = ctxt @@ -399,10 +406,10 @@ class CellsSchedulerTestCase(test.TestCase): call_info['security_groups'] = security_groups call_info['block_device_mapping'] = block_device_mapping - def fake_rpc_build_instances(ctxt, **host_sched_kwargs): + def fake_rpc_build_instances(self, ctxt, **host_sched_kwargs): call_info['host_sched_kwargs'] = host_sched_kwargs - def fake_get_filtered_objs(filters, cells, filt_properties): + def fake_get_filtered_objs(self, filters, cells, filt_properties): call_info['filt_objects'] = filters call_info['filt_cells'] = cells call_info['filt_props'] = filt_properties @@ -416,15 +423,16 @@ class CellsSchedulerTestCase(test.TestCase): 'instance_type': 'fake_type'} return request_spec - self.stubs.Set(self.scheduler, '_create_instances_here', - fake_create_instances_here) - self.stubs.Set(self.scheduler.compute_task_api, - 'build_instances', fake_rpc_build_instances) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) - filter_handler = self.scheduler.filter_handler - self.stubs.Set(filter_handler, 'get_filtered_objects', - fake_get_filtered_objs) + self.stub_out('nova.cells.scheduler.CellsScheduler.' + '_create_instances_here', + fake_create_instances_here) + self.stub_out('nova.conductor.api.ComputeTaskAPI.' + 'build_instances', fake_rpc_build_instances) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) + self.stub_out('nova.cells.filters.CellFilterHandler.' + 'get_filtered_objects', + fake_get_filtered_objs) host_sched_kwargs = {'image': 'fake_image', 'instances': self.instances, @@ -461,7 +469,8 @@ class CellsSchedulerTestCase(test.TestCase): our_path = 'nova.tests.unit.cells.test_cells_scheduler' cls_names = [our_path + '.' + 'FakeFilterClass1', our_path + '.' + 'FakeFilterClass2'] - self.flags(scheduler_filter_classes=cls_names, group='cells') + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + scheduler_filter_classes=cls_names, group='cells') self._init_cells_scheduler() # Make sure there's no child cells so that we will be @@ -470,7 +479,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info = {'scheduled': False} - def fake_create_instances_here(ctxt, request_spec): + def fake_create_instances_here(self, ctxt, request_spec): # Should not be called call_info['scheduled'] = True @@ -479,11 +488,12 @@ class CellsSchedulerTestCase(test.TestCase): # filter did it. return None - self.stubs.Set(self.scheduler, '_create_instances_here', - fake_create_instances_here) - filter_handler = self.scheduler.filter_handler - self.stubs.Set(filter_handler, 'get_filtered_objects', - fake_get_filtered_objs) + self.stub_out('nova.cells.scheduler.CellsScheduler.' + '_create_instances_here', + fake_create_instances_here) + self.stub_out('nova.cells.filters.CellFilterHandler.' + 'get_filtered_objects', + fake_get_filtered_objs) self.msg_runner.build_instances(self.ctxt, self.my_cell_state, {}) @@ -494,7 +504,8 @@ class CellsSchedulerTestCase(test.TestCase): our_path = 'nova.tests.unit.cells.test_cells_scheduler' cls_names = [our_path + '.' + 'FakeWeightClass1', our_path + '.' + 'FakeWeightClass2'] - self.flags(scheduler_weight_classes=cls_names, group='cells') + self.flags(scheduler='nova.cells.scheduler.CellsScheduler', + scheduler_weight_classes=cls_names, group='cells') self._init_cells_scheduler() # Make sure there's no child cells so that we will be @@ -503,7 +514,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info = {} - def fake_create_instances_here(ctxt, instance_uuids, + def fake_create_instances_here(self_cs, ctxt, instance_uuids, instance_properties, instance_type, image, security_groups, block_device_mapping): call_info['ctxt'] = ctxt @@ -514,10 +525,10 @@ class CellsSchedulerTestCase(test.TestCase): call_info['security_groups'] = security_groups call_info['block_device_mapping'] = block_device_mapping - def fake_rpc_build_instances(ctxt, **host_sched_kwargs): + def fake_rpc_build_instances(self, ctxt, **host_sched_kwargs): call_info['host_sched_kwargs'] = host_sched_kwargs - def fake_get_weighed_objs(weighers, cells, filt_properties): + def fake_get_weighed_objs(self, weighers, cells, filt_properties): call_info['weighers'] = weighers call_info['weight_cells'] = cells call_info['weight_props'] = filt_properties @@ -531,15 +542,16 @@ class CellsSchedulerTestCase(test.TestCase): 'instance_type': 'fake_type'} return request_spec - self.stubs.Set(self.scheduler, '_create_instances_here', - fake_create_instances_here) - self.stubs.Set(scheduler_utils, 'build_request_spec', - fake_build_request_spec) - self.stubs.Set(self.scheduler.compute_task_api, - 'build_instances', fake_rpc_build_instances) - weight_handler = self.scheduler.weight_handler - self.stubs.Set(weight_handler, 'get_weighed_objects', - fake_get_weighed_objs) + self.stub_out('nova.cells.scheduler.CellsScheduler.' + '_create_instances_here', + fake_create_instances_here) + self.stub_out('nova.scheduler.utils.build_request_spec', + fake_build_request_spec) + self.stub_out('nova.conductor.api.ComputeTaskAPI.' + 'build_instances', fake_rpc_build_instances) + self.stub_out('nova.cells.weights.CellWeightHandler.' + 'get_weighed_objects', + fake_get_weighed_objs) host_sched_kwargs = {'image': 'fake_image', 'instances': self.instances, diff --git a/nova/tests/unit/cells/test_cells_state_manager.py b/nova/tests/unit/cells/test_cells_state_manager.py index 74dbd16396..d39f6f0c74 100644 --- a/nova/tests/unit/cells/test_cells_state_manager.py +++ b/nova/tests/unit/cells/test_cells_state_manager.py @@ -126,10 +126,10 @@ class TestCellsStateManager(test.NoDBTestCase): def setUp(self): super(TestCellsStateManager, self).setUp() - self.stubs.Set(objects.ComputeNodeList, 'get_all', - _fake_compute_node_get_all) - self.stubs.Set(objects.ServiceList, 'get_by_binary', - _fake_service_get_all_by_binary) + self.stub_out('nova.objects.ComputeNodeList.get_all', + _fake_compute_node_get_all) + self.stub_out('nova.objects.ServiceList.get_by_binary', + _fake_service_get_all_by_binary) self.stub_out('nova.db.flavor_get_all', _fake_instance_type_all) self.stub_out('nova.db.cell_get_all', _fake_cell_get_all) @@ -233,8 +233,8 @@ class TestCellsStateManagerNToOne(TestCellsStateManager): def setUp(self): super(TestCellsStateManagerNToOne, self).setUp() - self.stubs.Set(objects.ComputeNodeList, 'get_all', - _fake_compute_node_n_to_one_get_all) + self.stub_out('nova.objects.ComputeNodeList.get_all', + _fake_compute_node_n_to_one_get_all) def test_capacity_part_reserve(self): # utilize half the cell's free capacity @@ -315,19 +315,15 @@ class TestCellsGetCapacity(TestCellsStateManager): other_cell = models.Cell(name="other_cell_name") cell.capacities = self.capacities other_cell.capacities = self.capacities - self.stubs.Set(self.state_manager, 'child_cells', - {"cell_name": cell, - "other_cell_name": other_cell}) + self.state_manager.child_cells = {"cell_name": cell, + "other_cell_name": other_cell} + self.state_manager.my_cell_state.capacities = self.capacities def test_get_cell_capacity_for_all_cells(self): - self.stubs.Set(self.state_manager.my_cell_state, 'capacities', - self.capacities) capacities = self.state_manager.get_capacities() self.assertEqual({"ram_free": 3702}, capacities) def test_get_cell_capacity_for_the_parent_cell(self): - self.stubs.Set(self.state_manager.my_cell_state, 'capacities', - self.capacities) capacities = self.state_manager.\ get_capacities(self.state_manager.my_cell_state.name) self.assertEqual({"ram_free": 3702}, capacities) diff --git a/nova/tests/unit/cells/test_cells_utils.py b/nova/tests/unit/cells/test_cells_utils.py index 9a05896697..a6c815b30f 100644 --- a/nova/tests/unit/cells/test_cells_utils.py +++ b/nova/tests/unit/cells/test_cells_utils.py @@ -52,9 +52,9 @@ class CellsUtilsTestCase(test.NoDBTestCase): instances = [fake_instance.fake_db_instance() for i in range(3)] return instances - self.stubs.Set(objects.InstanceList, 'get_by_filters', + self.stub_out('nova.objects.InstanceList.get_by_filters', instance_get_all_by_filters) - self.stubs.Set(random, 'shuffle', random_shuffle) + self.stub_out('random.shuffle', random_shuffle) instances = cells_utils.get_instances_to_sync(fake_context) self.assertTrue(inspect.isgenerator(instances))