Remove mox in tests/unit/api/openstack/compute

Replace mox with mock or stub_out in the following files
in nova/tests/unit/api/openstack/compute directory.

* admin_only_action_common.py
* test_admin_actions.py
* test_lock_server.py
* test_migrate_server.py
* test_pause_server.py
* test_suspend_server.py

Change-Id: I83b473e9ba557545b5c186f979e068e442de2424
Implements: blueprint mox-removal
This commit is contained in:
Takashi NATSUME
2018-05-07 17:46:06 +09:00
parent e2b0b469be
commit 399554a518
6 changed files with 180 additions and 208 deletions
@@ -43,13 +43,9 @@ class MigrateServerTestsV21(admin_only_action_common.CommonTests):
super(MigrateServerTestsV21, self).setUp()
self.controller = getattr(self.migrate_server, self.controller_name)()
self.compute_api = self.controller.compute_api
def _fake_controller(*args, **kwargs):
return self.controller
self.stubs.Set(self.migrate_server, self.controller_name,
_fake_controller)
self.mox.StubOutWithMock(self.compute_api, 'get')
self.stub_out('nova.api.openstack.compute.migrate_server.'
'MigrateServerController',
lambda *a, **kw: self.controller)
def _get_migration_body(self, **kwargs):
return {'os-migrateLive': self._get_params(**kwargs)}
@@ -120,16 +116,18 @@ class MigrateServerTestsV21(admin_only_action_common.CommonTests):
args_map=args_map, method_translations=method_translations)
def _test_migrate_exception(self, exc_info, expected_result):
self.mox.StubOutWithMock(self.compute_api, 'resize')
instance = self._stub_instance_get()
self.compute_api.resize(
self.context, instance,
host_name=self.host_name).AndRaise(exc_info)
self.mox.ReplayAll()
self.assertRaises(expected_result,
self.controller._migrate,
self.req, instance['uuid'], body={'migrate': None})
with mock.patch.object(self.compute_api, 'resize',
side_effect=exc_info) as mock_resize:
self.assertRaises(expected_result,
self.controller._migrate,
self.req, instance['uuid'],
body={'migrate': None})
mock_resize.assert_called_once_with(
self.context, instance, host_name=self.host_name)
self.mock_get.assert_called_once_with(self.context, instance.uuid,
expected_attrs=None)
def test_migrate_too_many_instances(self):
exc_info = exception.TooManyInstances(overs='', req='', used=0,
@@ -137,17 +135,21 @@ class MigrateServerTestsV21(admin_only_action_common.CommonTests):
self._test_migrate_exception(exc_info, webob.exc.HTTPForbidden)
def _test_migrate_live_succeeded(self, param):
self.mox.StubOutWithMock(self.compute_api, 'live_migrate')
instance = self._stub_instance_get()
self.compute_api.live_migrate(self.context, instance, False,
self.disk_over_commit, 'hostname',
self.force, self.async)
self.mox.ReplayAll()
live_migrate_method = self.controller._migrate_live
live_migrate_method(self.req, instance.uuid,
body={'os-migrateLive': param})
self.assertEqual(202, live_migrate_method.wsgi_code)
with mock.patch.object(self.compute_api,
'live_migrate') as mock_live_migrate:
live_migrate_method(self.req, instance.uuid,
body={'os-migrateLive': param})
self.assertEqual(202, live_migrate_method.wsgi_code)
mock_live_migrate.assert_called_once_with(
self.context, instance, False, self.disk_over_commit,
'hostname', self.force, self.async)
self.mock_get.assert_called_once_with(self.context, instance.uuid,
expected_attrs=None)
def test_migrate_live_enabled(self):
param = self._get_params(host='hostname')
@@ -209,21 +211,22 @@ class MigrateServerTestsV21(admin_only_action_common.CommonTests):
uuid=None,
expected_exc=webob.exc.HTTPBadRequest,
check_response=True):
self.mox.StubOutWithMock(self.compute_api, 'live_migrate')
instance = self._stub_instance_get(uuid=uuid)
self.compute_api.live_migrate(self.context, instance, False,
self.disk_over_commit,
'hostname', self.force, self.async
).AndRaise(fake_exc)
self.mox.ReplayAll()
body = self._get_migration_body(host='hostname')
ex = self.assertRaises(expected_exc,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
if check_response:
self.assertIn(six.text_type(fake_exc), ex.explanation)
with mock.patch.object(
self.compute_api, 'live_migrate',
side_effect=fake_exc) as mock_live_migrate:
ex = self.assertRaises(expected_exc,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
if check_response:
self.assertIn(six.text_type(fake_exc), ex.explanation)
mock_live_migrate.assert_called_once_with(
self.context, instance, False, self.disk_over_commit,
'hostname', self.force, self.async)
self.mock_get.assert_called_once_with(self.context, instance.uuid,
expected_attrs=None)
def test_migrate_live_compute_service_unavailable(self):
self._test_migrate_live_failed_with_exception(
@@ -428,39 +431,41 @@ class MigrateServerTestsV234(MigrateServerTestsV230):
pass
def test_migrate_live_compute_host_not_found(self):
body = {'os-migrateLive':
{'host': 'hostname', 'block_migration': 'auto'}}
exc = exception.ComputeHostNotFound(
reason="Compute host %(host)s could not be found.",
host='hostname')
self.mox.StubOutWithMock(self.compute_api, 'live_migrate')
instance = self._stub_instance_get()
self.compute_api.live_migrate(self.context, instance, None,
self.disk_over_commit, 'hostname',
self.force, self.async).AndRaise(exc)
self.mox.ReplayAll()
body = {'os-migrateLive':
{'host': 'hostname', 'block_migration': 'auto'}}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
with mock.patch.object(self.compute_api, 'live_migrate',
side_effect=exc) as mock_live_migrate:
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
mock_live_migrate.assert_called_once_with(
self.context, instance, None, self.disk_over_commit,
'hostname', self.force, self.async)
self.mock_get.assert_called_once_with(self.context, instance.uuid,
expected_attrs=None)
def test_migrate_live_unexpected_error(self):
exc = exception.InvalidHypervisorType(
reason="The supplied hypervisor type of is invalid.")
self.mox.StubOutWithMock(self.compute_api, 'live_migrate')
instance = self._stub_instance_get()
self.compute_api.live_migrate(self.context, instance, None,
self.disk_over_commit, 'hostname',
self.force, self.async).AndRaise(exc)
self.mox.ReplayAll()
body = {'os-migrateLive':
{'host': 'hostname', 'block_migration': 'auto'}}
exc = exception.InvalidHypervisorType(
reason="The supplied hypervisor type of is invalid.")
instance = self._stub_instance_get()
self.assertRaises(webob.exc.HTTPInternalServerError,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
with mock.patch.object(self.compute_api, 'live_migrate',
side_effect=exc) as mock_live_migrate:
self.assertRaises(webob.exc.HTTPInternalServerError,
self.controller._migrate_live,
self.req, instance.uuid, body=body)
mock_live_migrate.assert_called_once_with(
self.context, instance, None, self.disk_over_commit,
'hostname', self.force, self.async)
self.mock_get.assert_called_once_with(self.context, instance.uuid,
expected_attrs=None)
class MigrateServerTestsV256(MigrateServerTestsV234):