Use SpawnIsSynchronousFixture in most unit tests
SpawnIsSynchronousFixture is introduced in 90fb490a
this patch try to use the fixture and remove unnessary mocks
in compute layer test cases.
Change-Id: Idaedb9558b0aec047f0583c0593331a634cc59a2
This commit is contained in:
@@ -44,6 +44,7 @@ from nova import objects
|
||||
from nova.objects import block_device as block_device_obj
|
||||
from nova.objects import migrate_data as migrate_data_obj
|
||||
from nova import test
|
||||
from nova.tests import fixtures
|
||||
from nova.tests.unit.compute import fake_resource_tracker
|
||||
from nova.tests.unit import fake_block_device
|
||||
from nova.tests.unit import fake_instance
|
||||
@@ -71,6 +72,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
self.context = context.RequestContext('fake', 'fake')
|
||||
fake_server_actions.stub_out_action_events(self.stubs)
|
||||
|
||||
self.useFixture(fixtures.SpawnIsSynchronousFixture())
|
||||
|
||||
@mock.patch.object(manager.ComputeManager, '_get_power_state')
|
||||
@mock.patch.object(manager.ComputeManager, '_sync_instance_power_state')
|
||||
@mock.patch.object(objects.Instance, 'get_by_uuid')
|
||||
@@ -349,11 +352,9 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
self.assertEqual(final_result, res)
|
||||
self.assertEqual(1, sleep.call_count)
|
||||
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_do_build_and_run_instance')
|
||||
def _test_max_concurrent_builds(self, mock_dbari, mock_spawn):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def _test_max_concurrent_builds(self, mock_dbari):
|
||||
|
||||
with mock.patch.object(self.compute,
|
||||
'_build_semaphore') as mock_sem:
|
||||
@@ -2951,6 +2952,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self.compute.driver, self.node)
|
||||
self.compute._resource_tracker_dict[self.node] = fake_rt
|
||||
|
||||
self.useFixture(fixtures.SpawnIsSynchronousFixture())
|
||||
|
||||
def _do_build_instance_update(self, reschedule_update=False):
|
||||
self.mox.StubOutWithMock(self.instance, 'save')
|
||||
self.instance.save(
|
||||
@@ -2999,10 +3002,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
'build_instance', result, mock.ANY, mock.ANY, f=None)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_build_and_run_instance_called_with_proper_args(self, mock_spawn,
|
||||
mock_hooks):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def test_build_and_run_instance_called_with_proper_args(self, mock_hooks):
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self._do_build_instance_update()
|
||||
self.compute._build_and_run_instance(self.context, self.instance,
|
||||
@@ -3029,11 +3029,9 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
# node, NetworkRequest object can load from three items tuple.
|
||||
@mock.patch('nova.objects.Instance.save')
|
||||
@mock.patch('nova.compute.manager.ComputeManager._build_and_run_instance')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_build_and_run_instance_with_icehouse_requested_network(
|
||||
self, mock_spawn, mock_build_and_run, mock_save):
|
||||
self, mock_build_and_run, mock_save):
|
||||
fake_server_actions.stub_out_action_events(self.stubs)
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
mock_save.return_value = self.instance
|
||||
self.compute.build_and_run_instance(self.context, self.instance,
|
||||
self.image, request_spec={},
|
||||
@@ -3053,18 +3051,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self.assertEqual('fake_port_id', requested_network.port_id)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_build_abort_exception(self, mock_spawn, mock_hooks):
|
||||
def fake_spawn(f, *args, **kwargs):
|
||||
# NOTE(danms): Simulate the detached nature of spawn so that
|
||||
# we confirm that the inner task has the fault logic
|
||||
try:
|
||||
return f(*args, **kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
mock_spawn.side_effect = fake_spawn
|
||||
|
||||
def test_build_abort_exception(self, mock_hooks):
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(self.compute, '_cleanup_allocated_networks')
|
||||
self.mox.StubOutWithMock(self.compute, '_cleanup_volumes')
|
||||
@@ -3107,9 +3094,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
build_results.FAILED)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_rescheduled_exception(self, mock_spawn, mock_hooks):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def test_rescheduled_exception(self, mock_hooks):
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(self.compute, '_set_instance_obj_error_state')
|
||||
self.mox.StubOutWithMock(self.compute.compute_task_api,
|
||||
@@ -3228,9 +3213,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self.security_groups, self.block_device_mapping)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_rescheduled_exception_without_retry(self, mock_spawn, mock_hooks):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def test_rescheduled_exception_without_retry(self, mock_hooks):
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(compute_utils, 'add_instance_fault_from_exc')
|
||||
self.mox.StubOutWithMock(self.compute, '_set_instance_obj_error_state')
|
||||
@@ -3270,10 +3253,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
build_results.FAILED)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_rescheduled_exception_do_not_deallocate_network(self, mock_spawn,
|
||||
def test_rescheduled_exception_do_not_deallocate_network(self,
|
||||
mock_hooks):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(self.compute.driver,
|
||||
'deallocate_networks_on_reschedule')
|
||||
@@ -3317,10 +3298,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
build_results.RESCHEDULED)
|
||||
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_rescheduled_exception_deallocate_network(self, mock_spawn,
|
||||
def test_rescheduled_exception_deallocate_network(self,
|
||||
mock_hooks):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(self.compute.driver,
|
||||
'deallocate_networks_on_reschedule')
|
||||
@@ -3361,7 +3340,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self._assert_build_instance_hook_called(mock_hooks,
|
||||
build_results.RESCHEDULED)
|
||||
|
||||
def _test_build_and_run_exceptions(self, exc, set_error=False,
|
||||
@mock.patch('nova.hooks._HOOKS')
|
||||
def _test_build_and_run_exceptions(self, exc, mock_hooks, set_error=False,
|
||||
cleanup_volumes=False, nil_out_host_and_node=False):
|
||||
self.mox.StubOutWithMock(self.compute, '_build_and_run_instance')
|
||||
self.mox.StubOutWithMock(self.compute, '_cleanup_allocated_networks')
|
||||
@@ -3395,15 +3375,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self._instance_action_events()
|
||||
self.mox.ReplayAll()
|
||||
|
||||
with test.nested(
|
||||
mock.patch('nova.utils.spawn_n'),
|
||||
mock.patch('nova.hooks._HOOKS')
|
||||
) as (
|
||||
mock_spawn,
|
||||
mock_hooks
|
||||
):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
self.compute.build_and_run_instance(self.context, self.instance,
|
||||
self.compute.build_and_run_instance(self.context, self.instance,
|
||||
self.image, request_spec={},
|
||||
filter_properties=self.filter_properties,
|
||||
injected_files=self.injected_files,
|
||||
@@ -3412,8 +3384,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
security_groups=self.security_groups,
|
||||
block_device_mapping=self.block_device_mapping, node=self.node,
|
||||
limits=self.limits)
|
||||
self._assert_build_instance_hook_called(mock_hooks,
|
||||
build_results.FAILED)
|
||||
self._assert_build_instance_hook_called(mock_hooks,
|
||||
build_results.FAILED)
|
||||
|
||||
def test_build_and_run_notfound_exception(self):
|
||||
self._test_build_and_run_exceptions(exception.InstanceNotFound(
|
||||
@@ -3589,9 +3561,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
|
||||
self.instance, self.block_device_mapping,
|
||||
self.requested_networks, try_deallocate_networks=True)
|
||||
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_reschedule_on_resources_unavailable(self, mock_spawn):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def test_reschedule_on_resources_unavailable(self):
|
||||
reason = 'resource unavailable'
|
||||
exc = exception.ComputeResourcesUnavailable(reason=reason)
|
||||
|
||||
@@ -4053,6 +4023,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
|
||||
new_instance_type_id=7)
|
||||
self.migration.status = 'migrating'
|
||||
fake_server_actions.stub_out_action_events(self.stubs)
|
||||
self.useFixture(fixtures.SpawnIsSynchronousFixture())
|
||||
|
||||
@mock.patch.object(objects.Migration, 'save')
|
||||
@mock.patch.object(objects.Migration, 'obj_as_admin')
|
||||
@@ -4219,11 +4190,9 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
|
||||
self.assertTrue(self.compute._consoles_enabled())
|
||||
self.flags(enabled=False, group=console)
|
||||
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_do_live_migration')
|
||||
def _test_max_concurrent_live(self, mock_lm, mock_spawn):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def _test_max_concurrent_live(self, mock_lm):
|
||||
|
||||
@mock.patch('nova.objects.Migration.save')
|
||||
def _do_it(mock_mig_save):
|
||||
|
||||
@@ -45,6 +45,7 @@ from nova import rpc
|
||||
from nova.scheduler import client as scheduler_client
|
||||
from nova.scheduler import utils as scheduler_utils
|
||||
from nova import test
|
||||
from nova.tests import fixtures
|
||||
from nova.tests.unit import cast_as_call
|
||||
from nova.tests.unit.compute import test_compute
|
||||
from nova.tests.unit import fake_instance
|
||||
@@ -292,6 +293,8 @@ class _BaseTaskTestCase(object):
|
||||
self.stubs.Set(rpc.RequestContextSerializer, 'deserialize_context',
|
||||
fake_deserialize_context)
|
||||
|
||||
self.useFixture(fixtures.SpawnIsSynchronousFixture())
|
||||
|
||||
def _prepare_rebuild_args(self, update_args=None):
|
||||
# Args that don't get passed in to the method but do get passed to RPC
|
||||
migration = update_args and update_args.pop('migration', None)
|
||||
@@ -395,9 +398,7 @@ class _BaseTaskTestCase(object):
|
||||
self._test_cold_migrate(clean_shutdown=False)
|
||||
|
||||
@mock.patch('nova.objects.Instance.refresh')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_build_instances(self, mock_spawn, mock_refresh):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
def test_build_instances(self, mock_refresh):
|
||||
instance_type = flavors.get_default_flavor()
|
||||
instances = [objects.Instance(context=self.context,
|
||||
id=i,
|
||||
@@ -526,12 +527,7 @@ class _BaseTaskTestCase(object):
|
||||
|
||||
@mock.patch.object(scheduler_utils, 'set_vm_state_and_notify')
|
||||
@mock.patch.object(scheduler_utils, 'populate_retry')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def _test(spawn_mock, populate_retry, set_vm_state_and_notify):
|
||||
# NOTE(gibi): LocalComputeTaskAPI use eventlet spawn that
|
||||
# makes mocking hard so use direct call instead.
|
||||
spawn_mock.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
|
||||
def _test(populate_retry, set_vm_state_and_notify):
|
||||
# build_instances() is a cast, we need to wait for it to
|
||||
# complete
|
||||
self.useFixture(cast_as_call.CastAsCall(self.stubs))
|
||||
@@ -560,23 +556,18 @@ class _BaseTaskTestCase(object):
|
||||
|
||||
_test()
|
||||
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
@mock.patch.object(scheduler_utils, 'build_request_spec')
|
||||
@mock.patch.object(scheduler_utils, 'setup_instance_group')
|
||||
@mock.patch.object(conductor_manager.ComputeTaskManager,
|
||||
'_set_vm_state_and_notify')
|
||||
def test_build_instances_scheduler_group_failure(self, state_mock,
|
||||
sig_mock, bs_mock,
|
||||
spawn_mock):
|
||||
sig_mock, bs_mock):
|
||||
instances = [fake_instance.fake_instance_obj(self.context)
|
||||
for i in range(2)]
|
||||
image = {'fake-data': 'should_pass_silently'}
|
||||
spec = {'fake': 'specs',
|
||||
'instance_properties': instances[0]}
|
||||
|
||||
# NOTE(gibi): LocalComputeTaskAPI use eventlet spawn that makes mocking
|
||||
# hard so use direct call instead.
|
||||
spawn_mock.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
bs_mock.return_value = spec
|
||||
exception = exc.UnsupportedPolicyException(reason='fake-reason')
|
||||
sig_mock.side_effect = exception
|
||||
@@ -877,7 +868,6 @@ class _BaseTaskTestCase(object):
|
||||
select_dest_mock.assert_called_once_with(self.context, fake_spec)
|
||||
self.assertFalse(rebuild_mock.called)
|
||||
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
@mock.patch.object(conductor_manager.compute_rpcapi.ComputeAPI,
|
||||
'rebuild_instance')
|
||||
@mock.patch.object(scheduler_utils, 'setup_instance_group')
|
||||
@@ -891,17 +881,12 @@ class _BaseTaskTestCase(object):
|
||||
bs_mock,
|
||||
select_dest_mock,
|
||||
sig_mock,
|
||||
rebuild_mock,
|
||||
spawn_mock):
|
||||
rebuild_mock):
|
||||
inst_obj = self._create_fake_instance_obj()
|
||||
rebuild_args, _ = self._prepare_rebuild_args({'host': None})
|
||||
request_spec = {}
|
||||
bs_mock.return_value = request_spec
|
||||
|
||||
# NOTE(gibi): LocalComputeTaskAPI use eventlet spawn that makes mocking
|
||||
# hard so use direct call instead.
|
||||
spawn_mock.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
|
||||
exception = exc.UnsupportedPolicyException(reason='')
|
||||
sig_mock.side_effect = exception
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ from nova.pci import stats as pci_stats
|
||||
from nova.scheduler import filters
|
||||
from nova.scheduler import host_manager
|
||||
from nova import test
|
||||
from nova.tests import fixtures
|
||||
from nova.tests.unit import fake_instance
|
||||
from nova.tests.unit import matchers
|
||||
from nova.tests.unit.scheduler import fakes
|
||||
@@ -71,16 +72,16 @@ class HostManagerTestCase(test.NoDBTestCase):
|
||||
self.fake_hosts += [host_manager.HostState('fake_multihost',
|
||||
'fake-node%s' % x) for x in range(1, 5)]
|
||||
|
||||
self.useFixture(fixtures.SpawnIsSynchronousFixture())
|
||||
|
||||
def test_load_filters(self):
|
||||
filters = self.host_manager._load_filters()
|
||||
self.assertEqual(filters, ['FakeFilterClass1'])
|
||||
|
||||
@mock.patch.object(nova.objects.InstanceList, 'get_by_filters')
|
||||
@mock.patch.object(nova.objects.ComputeNodeList, 'get_all')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_init_instance_info_batches(self, mock_spawn, mock_get_all,
|
||||
def test_init_instance_info_batches(self, mock_get_all,
|
||||
mock_get_by_filters):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
cn_list = objects.ComputeNodeList()
|
||||
for num in range(22):
|
||||
host_name = 'host_%s' % num
|
||||
@@ -91,10 +92,8 @@ class HostManagerTestCase(test.NoDBTestCase):
|
||||
|
||||
@mock.patch.object(nova.objects.InstanceList, 'get_by_filters')
|
||||
@mock.patch.object(nova.objects.ComputeNodeList, 'get_all')
|
||||
@mock.patch('nova.utils.spawn_n')
|
||||
def test_init_instance_info(self, mock_spawn, mock_get_all,
|
||||
def test_init_instance_info(self, mock_get_all,
|
||||
mock_get_by_filters):
|
||||
mock_spawn.side_effect = lambda f, *a, **k: f(*a, **k)
|
||||
cn1 = objects.ComputeNode(host='host1')
|
||||
cn2 = objects.ComputeNode(host='host2')
|
||||
inst1 = objects.Instance(host='host1', uuid='uuid1')
|
||||
|
||||
Reference in New Issue
Block a user