Skip to content

Commit

Permalink
Stop using mox in compute/test_server_actions.py
Browse files Browse the repository at this point in the history
Partially-Implements: blueprint remove-mox-ocata

Change-Id: I2e03624905e7d5325aed228c6710db1f1b7120a1
gaborantal authored and Dao Cong Tien committed Jan 4, 2017
1 parent d768bfa commit 612134c
Showing 1 changed file with 136 additions and 140 deletions.
276 changes: 136 additions & 140 deletions nova/tests/unit/api/openstack/compute/test_server_actions.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
# under the License.

import mock
from mox3 import mox
from oslo_utils import uuidutils
import webob

from nova.api.openstack.compute import extension_info
@@ -79,7 +79,7 @@ def setUp(self):
self.flags(group='glance', api_servers=['http://localhost:9292'])
self.stub_out('nova.db.instance_get_by_uuid',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE,
host='fake_host'))
host='fake_host'))
self.stub_out('nova.db.instance_update_and_get_original',
instance_update_and_get_original)

@@ -103,56 +103,42 @@ def _get_controller(self):
def _set_fake_extension(self):
pass

def _rebuild(self, context, image_ref, value=None):
if value is not None:
compute_api.API.rebuild(context, mox.IgnoreArg(), image_ref,
mox.IgnoreArg(), preserve_ephemeral=value)
else:
compute_api.API.rebuild(context, mox.IgnoreArg(), image_ref,
mox.IgnoreArg())

def _stub_instance_get(self, context, uuid=None):
self.mox.StubOutWithMock(compute_api.API, 'get')
if uuid is None:
uuid = uuids.fake
instance = fake_instance.fake_db_instance(
id=1, uuid=uuid, vm_state=vm_states.ACTIVE, task_state=None,
project_id=context.project_id,
user_id=context.user_id)
instance = objects.Instance._from_db_object(
self.context, objects.Instance(), instance)

self.compute_api.get(self.context, uuid,
expected_attrs=['flavor', 'pci_devices',
'numa_topology']
).AndReturn(instance)
return instance

def _test_locked_instance(self, action, method=None, body_map=None,
compute_api_args_map=None):
if body_map is None:
body_map = {}
if compute_api_args_map is None:
compute_api_args_map = {}

instance = self._stub_instance_get(self.req.environ['nova.context'])
args, kwargs = compute_api_args_map.get(action, ((), {}))

getattr(compute_api.API, method)(self.context, instance,
*args, **kwargs).AndRaise(
exception.InstanceIsLocked(instance_uuid=instance['uuid']))

self.mox.ReplayAll()
uuid = uuidutils.generate_uuid()
context = self.req.environ['nova.context']
instance = fake_instance.fake_db_instance(
id=1, uuid=uuid, vm_state=vm_states.ACTIVE, task_state=None,
project_id=context.project_id,
user_id=context.user_id)
instance = objects.Instance._from_db_object(
self.context, objects.Instance(), instance)

controller_function = 'self.controller.' + action
self.assertRaises(webob.exc.HTTPConflict,
eval(controller_function),
self.req, instance['uuid'],
body=body_map.get(action))
# Do these here instead of tearDown because this method is called
# more than once for the same test case
self.mox.VerifyAll()
self.mox.UnsetStubs()
with test.nested(
mock.patch.object(compute_api.API, 'get',
return_value=instance),
mock.patch.object(compute_api.API, method,
side_effect=exception.InstanceIsLocked(
instance_uuid=instance['uuid'])),
) as (mock_get, mock_method):

controller_function = 'self.controller.' + action
self.assertRaises(webob.exc.HTTPConflict,
eval(controller_function),
self.req, instance['uuid'],
body=body_map.get(action))

mock_get.assert_called_once_with(self.context, uuid,
expected_attrs=['flavor', 'pci_devices', 'numa_topology'])
mock_method.assert_called_once_with(self.context, instance,
*args, **kwargs)

def test_actions_with_locked_instance(self):
actions = ['_action_resize', '_action_confirm_resize',
@@ -179,7 +165,6 @@ def test_actions_with_locked_instance(self):

for action in actions:
method = method_translations.get(action)
self.mox.StubOutWithMock(compute_api.API, method)
self._test_locked_instance(action, method=method,
body_map=body_map,
compute_api_args_map=args_map)
@@ -227,7 +212,7 @@ def fake_reboot(*args, **kwargs):
state='fake_state', method='fake_method',
instance_uuid='fake')

self.stubs.Set(compute_api.API, 'reboot', fake_reboot)
self.stub_out('nova.compute.api.API.reboot', fake_reboot)

self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_reboot,
@@ -280,13 +265,15 @@ def _test_rebuild_preserve_ephemeral(self, value=None):
if value is not None:
body['rebuild']['preserve_ephemeral'] = value

self.mox.StubOutWithMock(compute_api.API, 'rebuild')
with mock.patch.object(compute_api.API, 'rebuild') as mock_rebuild:
self.controller._action_rebuild(self.req, FAKE_UUID, body=body)

self._rebuild(self.context, self._image_href, value)

self.mox.ReplayAll()

self.controller._action_rebuild(self.req, FAKE_UUID, body=body)
if value is not None:
mock_rebuild.assert_called_once_with(self.context, mock.ANY,
self._image_href, mock.ANY, preserve_ephemeral=value)
else:
mock_rebuild.assert_called_once_with(self.context, mock.ANY,
self._image_href, mock.ANY)

def test_rebuild_preserve_ephemeral_true(self):
self._test_rebuild_preserve_ephemeral(True)
@@ -325,8 +312,8 @@ def rebuild(self2, context, instance, image_href, *args, **kwargs):
info['image_href_in_call'] = image_href

self.stub_out('nova.db.instance_get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE))
self.stubs.Set(compute_api.API, 'rebuild', rebuild)
fakes.fake_instance_get(vm_state=vm_states.ACTIVE))
self.stub_out('nova.compute.api.API.rebuild', rebuild)

# proper local hrefs must start with 'http://localhost/v2/'
body = {
@@ -386,7 +373,7 @@ def fake_rebuild(*args, **kwargs):
state='fake_state', method='fake_method',
instance_uuid='fake')

self.stubs.Set(compute_api.API, 'rebuild', fake_rebuild)
self.stub_out('nova.compute.api.API.rebuild', fake_rebuild)

self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_rebuild,
@@ -538,8 +525,8 @@ def wrap_get(*args, **kwargs):
def fake_save(context, **kwargs):
data['changes'].update(data['instance'].obj_get_changes())

self.stubs.Set(compute_api.API, 'get', wrap_get)
self.stubs.Set(objects.Instance, 'save', fake_save)
self.stub_out('nova.compute.api.API.get', wrap_get)
self.stub_out('nova.objects.Instance.save', fake_save)

self.controller._action_rebuild(self.req, FAKE_UUID, body=body)

@@ -568,7 +555,8 @@ def return_image_meta(*args, **kwargs):

return image_meta

self.stubs.Set(fake._FakeImageService, 'show', return_image_meta)
self.stub_out('nova.tests.unit.image.fake._FakeImageService.show',
return_image_meta)
body = {
"rebuild": {
"imageRef": "155d900f-4e14-4e4c-a73d-069cbf4541e6",
@@ -610,9 +598,10 @@ def return_image_meta(*args, **kwargs):

return image_meta

self.stubs.Set(fake._FakeImageService, 'show', return_image_meta)
self.stubs.Set(compute_api.API, 'get', wrap_get)
self.stubs.Set(objects.Instance, 'save', fake_save)
self.stub_out('nova.tests.unit.image.fake._FakeImageService.show',
return_image_meta)
self.stub_out('nova.compute.api.API.get', wrap_get)
self.stub_out('nova.objects.Instance.save', fake_save)
body = {
"rebuild": {
"imageRef": "155d900f-4e14-4e4c-a73d-069cbf4541e6",
@@ -646,7 +635,7 @@ def test_resize_server(self):
def resize_mock(*args):
self.resize_called = True

self.stubs.Set(compute_api.API, 'resize', resize_mock)
self.stub_out('nova.compute.api.API.resize', resize_mock)

self.controller._action_resize(self.req, FAKE_UUID, body=body)

@@ -683,7 +672,7 @@ def test_resize_server_invalid_flavor_ref(self):
def test_resize_with_server_not_found(self):
body = dict(resize=dict(flavorRef="http://localhost/3"))

self.stubs.Set(compute_api.API, 'get', return_server_not_found)
self.stub_out('nova.compute.api.API.get', return_server_not_found)

self.assertRaises(webob.exc.HTTPNotFound,
self.controller._action_resize,
@@ -712,7 +701,7 @@ def _fake_resize(obj, context, instance, flavor_id):
self.resize_called += 1
raise next(raised)

self.stubs.Set(compute_api.API, 'resize', _fake_resize)
self.stub_out('nova.compute.api.API.resize', _fake_resize)

for call_no in range(len(exceptions)):
next_exception = next(expected)
@@ -753,7 +742,7 @@ def test_resize_with_too_many_instances(self):
def fake_resize(*args, **kwargs):
raise exception.TooManyInstances(message="TooManyInstance")

self.stubs.Set(compute_api.API, 'resize', fake_resize)
self.stub_out('nova.compute.api.API.resize', fake_resize)

self.assertRaises(webob.exc.HTTPForbidden,
self.controller._action_resize,
@@ -767,7 +756,7 @@ def fake_resize(*args, **kwargs):
state='fake_state', method='fake_method',
instance_uuid='fake')

self.stubs.Set(compute_api.API, 'resize', fake_resize)
self.stub_out('nova.compute.api.API.resize', fake_resize)

self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_resize,
@@ -811,7 +800,7 @@ def test_confirm_resize_server(self):
def cr_mock(*args):
self.confirm_resize_called = True

self.stubs.Set(compute_api.API, 'confirm_resize', cr_mock)
self.stub_out('nova.compute.api.API.confirm_resize', cr_mock)

self.controller._action_confirm_resize(self.req, FAKE_UUID, body=body)

@@ -824,9 +813,8 @@ def confirm_resize_mock(*args):
raise exception.MigrationNotFoundByStatus(instance_id=1,
status='finished')

self.stubs.Set(compute_api.API,
'confirm_resize',
confirm_resize_mock)
self.stub_out('nova.compute.api.API.confirm_resize',
confirm_resize_mock)

self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._action_confirm_resize,
@@ -840,8 +828,8 @@ def fake_confirm_resize(*args, **kwargs):
state='fake_state', method='fake_method',
instance_uuid='fake')

self.stubs.Set(compute_api.API, 'confirm_resize',
fake_confirm_resize)
self.stub_out('nova.compute.api.API.confirm_resize',
fake_confirm_resize)

self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_confirm_resize,
@@ -854,9 +842,8 @@ def revert_resize_mock(*args):
raise exception.MigrationNotFoundByStatus(instance_id=1,
status='finished')

self.stubs.Set(compute_api.API,
'revert_resize',
revert_resize_mock)
self.stub_out('nova.compute.api.API.revert_resize',
revert_resize_mock)

self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._action_revert_resize,
@@ -877,7 +864,7 @@ def test_revert_resize_server(self):
def revert_mock(*args):
self.revert_resize_called = True

self.stubs.Set(compute_api.API, 'revert_resize', revert_mock)
self.stub_out('nova.compute.api.API.revert_resize', revert_mock)

body = self.controller._action_revert_resize(self.req, FAKE_UUID,
body=body)
@@ -892,8 +879,8 @@ def fake_revert_resize(*args, **kwargs):
state='fake_state', method='fake_method',
instance_uuid='fake')

self.stubs.Set(compute_api.API, 'revert_resize',
fake_revert_resize)
self.stub_out('nova.compute.api.API.revert_resize',
fake_revert_resize)

self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_revert_resize,
@@ -970,51 +957,55 @@ def fake_block_device_mapping_get_all_by_instance(context, inst_id,
system_metadata=system_metadata)
self.stub_out('nova.db.instance_get_by_uuid', instance)

self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi,
'quiesce_instance')
self.controller.compute_api.compute_rpcapi.quiesce_instance(
mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
exception.InstanceQuiesceNotSupported(instance_id='fake',
reason='test'))

volume = dict(id=_fake_id('a'),
size=1,
host='fake',
display_description='fake')
snapshot = dict(id=_fake_id('d'))
self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
volume_api = self.controller.compute_api.volume_api
volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

self.mox.ReplayAll()

response = self.controller._action_create_image(self.req, FAKE_UUID,
body=body)
snapshot = dict(id=_fake_id('d'))

location = response.headers['Location']
image_id = location.replace(self.image_url or
with test.nested(
mock.patch.object(self.controller.compute_api.compute_rpcapi,
'quiesce_instance',
side_effect=exception.InstanceQuiesceNotSupported(
instance_id='fake', reason='test')),
mock.patch.object(self.controller.compute_api.volume_api, 'get',
return_value=volume),
mock.patch.object(self.controller.compute_api.volume_api,
'create_snapshot_force',
return_value=snapshot),
) as (mock_quiesce, mock_vol_get, mock_vol_create):

response = self.controller._action_create_image(self.req,
FAKE_UUID, body=body)

location = response.headers['Location']
image_id = location.replace(self.image_url or
glance.generate_image_url(''), '')
image = image_service.show(None, image_id)

self.assertEqual(image['name'], 'snapshot_of_volume_backed')
properties = image['properties']
self.assertEqual(properties['kernel_id'], _fake_id('b'))
self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
self.assertEqual(properties['root_device_name'], '/dev/vda')
self.assertTrue(properties['bdm_v2'])
bdms = properties['block_device_mapping']
self.assertEqual(len(bdms), 1)
self.assertEqual(bdms[0]['boot_index'], 0)
self.assertEqual(bdms[0]['source_type'], 'snapshot')
self.assertEqual(bdms[0]['destination_type'], 'volume')
self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
self.assertEqual('/dev/vda', bdms[0]['device_name'])
for fld in ('connection_info', 'id', 'instance_uuid'):
self.assertNotIn(fld, bdms[0])
for k in extra_properties.keys():
self.assertEqual(properties[k], extra_properties[k])
image = image_service.show(None, image_id)

self.assertEqual(image['name'], 'snapshot_of_volume_backed')
properties = image['properties']
self.assertEqual(properties['kernel_id'], _fake_id('b'))
self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
self.assertEqual(properties['root_device_name'], '/dev/vda')
self.assertTrue(properties['bdm_v2'])
bdms = properties['block_device_mapping']
self.assertEqual(len(bdms), 1)
self.assertEqual(bdms[0]['boot_index'], 0)
self.assertEqual(bdms[0]['source_type'], 'snapshot')
self.assertEqual(bdms[0]['destination_type'], 'volume')
self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
self.assertEqual('/dev/vda', bdms[0]['device_name'])
for fld in ('connection_info', 'id', 'instance_uuid'):
self.assertNotIn(fld, bdms[0])
for k in extra_properties.keys():
self.assertEqual(properties[k], extra_properties[k])

mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
mock.ANY, mock.ANY)

def test_create_volume_backed_image_no_metadata(self):
self._do_test_create_volume_backed_image({})
@@ -1059,37 +1050,42 @@ def fake_block_device_mapping_get_all_by_instance(context, inst_id,
'image_test_key2': 'test_value2'})
self.stub_out('nova.db.instance_get_by_uuid', instance)

self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi,
'quiesce_instance')
self.controller.compute_api.compute_rpcapi.quiesce_instance(
mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
exception.InstanceQuiesceNotSupported(instance_id='fake',
reason='test'))

volume = dict(id=_fake_id('a'),
size=1,
host='fake',
display_description='fake')
snapshot = dict(id=_fake_id('d'))
self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
volume_api = self.controller.compute_api.volume_api
volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

self.mox.ReplayAll()
response = self.controller._action_create_image(self.req, FAKE_UUID,
body=body)
location = response.headers['Location']
image_id = location.replace(self.image_base_url, '')
image = image_service.show(None, image_id)
snapshot = dict(id=_fake_id('d'))

properties = image['properties']
self.assertEqual(properties['test_key1'], 'test_value1')
self.assertEqual(properties['test_key2'], 'test_value2')
if extra_metadata:
for key, val in extra_metadata.items():
self.assertEqual(properties[key], val)
with test.nested(
mock.patch.object(self.controller.compute_api.compute_rpcapi,
'quiesce_instance',
side_effect=exception.InstanceQuiesceNotSupported(
instance_id='fake', reason='test')),
mock.patch.object(self.controller.compute_api.volume_api, 'get',
return_value=volume),
mock.patch.object(self.controller.compute_api.volume_api,
'create_snapshot_force',
return_value=snapshot),
) as (mock_quiesce, mock_vol_get, mock_vol_create):

response = self.controller._action_create_image(self.req,
FAKE_UUID, body=body)
location = response.headers['Location']
image_id = location.replace(self.image_base_url, '')
image = image_service.show(None, image_id)

properties = image['properties']
self.assertEqual(properties['test_key1'], 'test_value1')
self.assertEqual(properties['test_key2'], 'test_value2')
if extra_metadata:
for key, val in extra_metadata.items():
self.assertEqual(properties[key], val)

mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
mock.ANY, mock.ANY)

def test_create_vol_backed_img_with_meta_from_vol_without_extra_meta(self):
self._test_create_volume_backed_image_with_metadata_from_volume()
@@ -1178,7 +1174,7 @@ def snapshot(*args, **kwargs):
raise exception.InstanceInvalidState(attr='fake_attr',
state='fake_state', method='fake_method',
instance_uuid='fake')
self.stubs.Set(compute_api.API, 'snapshot', snapshot)
self.stub_out('nova.compute.api.API.snapshot', snapshot)

body = {
"createImage": {

0 comments on commit 612134c

Please sign in to comment.