Merge "Update compute API.get() stubs in test_server_actions"

This commit is contained in:
Zuul 2018-11-13 22:07:39 +00:00 committed by Gerrit Code Review
commit 5adfb64c6c
1 changed files with 71 additions and 89 deletions

View File

@ -36,24 +36,6 @@ from nova.tests.unit.image import fake
CONF = nova.conf.CONF CONF = nova.conf.CONF
FAKE_UUID = fakes.FAKE_UUID FAKE_UUID = fakes.FAKE_UUID
INSTANCE_IDS = {FAKE_UUID: 1}
def return_server_not_found(*arg, **kwarg):
raise exception.InstanceNotFound(instance_id=FAKE_UUID)
def instance_update_and_get_original(context, instance_uuid, values,
columns_to_join=None,
):
inst = fakes.stub_instance(INSTANCE_IDS[instance_uuid], host='fake_host')
inst = dict(inst, **values)
return (inst, inst)
def instance_update(context, instance_uuid, kwargs):
inst = fakes.stub_instance(INSTANCE_IDS[instance_uuid], host='fake_host')
return inst
class MockSetAdminPassword(object): class MockSetAdminPassword(object):
@ -78,13 +60,11 @@ class ServerActionsControllerTestV21(test.TestCase):
def setUp(self): def setUp(self):
super(ServerActionsControllerTestV21, self).setUp() super(ServerActionsControllerTestV21, self).setUp()
self.flags(group='glance', api_servers=['http://localhost:9292']) self.flags(group='glance', api_servers=['http://localhost:9292'])
self.stub_out('nova.db.api.instance_get_by_uuid', self.stub_out('nova.compute.api.API.get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE, fakes.fake_compute_get(vm_state=vm_states.ACTIVE,
host='fake_host')) host='fake_host'))
self.stub_out('nova.db.api.instance_update_and_get_original', self.stub_out('nova.objects.Instance.save', lambda *a, **kw: None)
instance_update_and_get_original)
fakes.stub_out_nw_api(self)
fakes.stub_out_compute_api_snapshot(self) fakes.stub_out_compute_api_snapshot(self)
fake.stub_out_image_service(self) fake.stub_out_image_service(self)
self.flags(enable_instance_password=True, group='api') self.flags(enable_instance_password=True, group='api')
@ -92,7 +72,14 @@ class ServerActionsControllerTestV21(test.TestCase):
self.controller = self._get_controller() self.controller = self._get_controller()
self.compute_api = self.controller.compute_api self.compute_api = self.controller.compute_api
self.req = fakes.HTTPRequest.blank('') # We don't care about anything getting as far as hitting the compute
# RPC API so we just mock it out here.
mock_rpcapi = mock.patch.object(self.compute_api, 'compute_rpcapi')
mock_rpcapi.start()
self.addCleanup(mock_rpcapi.stop)
# The project_id here matches what is used by default in
# fake_compute_get which need to match for policy checks.
self.req = fakes.HTTPRequest.blank('', project_id='fake_project')
self.context = self.req.environ['nova.context'] self.context = self.req.environ['nova.context']
self.image_api = image.API() self.image_api = image.API()
@ -100,9 +87,6 @@ class ServerActionsControllerTestV21(test.TestCase):
def _get_controller(self): def _get_controller(self):
return self.servers.ServersController() return self.servers.ServersController()
def _set_fake_extension(self):
pass
def _test_locked_instance(self, action, method=None, body_map=None, def _test_locked_instance(self, action, method=None, body_map=None,
compute_api_args_map=None): compute_api_args_map=None):
if body_map is None: if body_map is None:
@ -197,13 +181,13 @@ class ServerActionsControllerTestV21(test.TestCase):
self.req, FAKE_UUID, body=body) self.req, FAKE_UUID, body=body)
def test_reboot_not_found(self): def test_reboot_not_found(self):
self.stub_out('nova.db.api.instance_get_by_uuid',
return_server_not_found)
body = dict(reboot=dict(type="HARD")) body = dict(reboot=dict(type="HARD"))
self.assertRaises(webob.exc.HTTPNotFound, with mock.patch('nova.compute.api.API.get',
self.controller._action_reboot, side_effect=exception.InstanceNotFound(
self.req, uuids.fake, body=body) instance_id=uuids.fake)):
self.assertRaises(webob.exc.HTTPNotFound,
self.controller._action_reboot,
self.req, uuids.fake, body=body)
def test_reboot_raises_conflict_on_invalid_state(self): def test_reboot_raises_conflict_on_invalid_state(self):
body = dict(reboot=dict(type="HARD")) body = dict(reboot=dict(type="HARD"))
@ -221,42 +205,43 @@ class ServerActionsControllerTestV21(test.TestCase):
def test_reboot_soft_with_soft_in_progress_raises_conflict(self): def test_reboot_soft_with_soft_in_progress_raises_conflict(self):
body = dict(reboot=dict(type="SOFT")) body = dict(reboot=dict(type="SOFT"))
self.stub_out('nova.db.api.instance_get_by_uuid', self.stub_out('nova.compute.api.API.get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE, fakes.fake_compute_get(vm_state=vm_states.ACTIVE,
task_state=task_states.REBOOTING)) task_state=task_states.REBOOTING))
self.assertRaises(webob.exc.HTTPConflict, self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_reboot, self.controller._action_reboot,
self.req, FAKE_UUID, body=body) self.req, FAKE_UUID, body=body)
def test_reboot_hard_with_soft_in_progress_does_not_raise(self): def test_reboot_hard_with_soft_in_progress_does_not_raise(self):
body = dict(reboot=dict(type="HARD")) body = dict(reboot=dict(type="HARD"))
self.stub_out('nova.db.api.instance_get_by_uuid', self.stub_out('nova.compute.api.API.get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE, fakes.fake_compute_get(vm_state=vm_states.ACTIVE,
task_state=task_states.REBOOTING)) task_state=task_states.REBOOTING))
self.controller._action_reboot(self.req, FAKE_UUID, body=body) self.controller._action_reboot(self.req, FAKE_UUID, body=body)
def test_reboot_hard_with_hard_in_progress(self): def test_reboot_hard_with_hard_in_progress(self):
body = dict(reboot=dict(type="HARD")) body = dict(reboot=dict(type="HARD"))
self.stub_out('nova.db.api.instance_get_by_uuid', self.stub_out('nova.compute.api.API.get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE, fakes.fake_compute_get(
task_state=task_states.REBOOTING_HARD)) vm_state=vm_states.ACTIVE,
task_state=task_states.REBOOTING_HARD))
self.controller._action_reboot(self.req, FAKE_UUID, body=body) self.controller._action_reboot(self.req, FAKE_UUID, body=body)
def test_reboot_soft_with_hard_in_progress_raises_conflict(self): def test_reboot_soft_with_hard_in_progress_raises_conflict(self):
body = dict(reboot=dict(type="SOFT")) body = dict(reboot=dict(type="SOFT"))
self.stub_out('nova.db.api.instance_get_by_uuid', self.stub_out('nova.compute.api.API.get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE, fakes.fake_compute_get(
task_state=task_states.REBOOTING_HARD)) vm_state=vm_states.ACTIVE,
task_state=task_states.REBOOTING_HARD))
self.assertRaises(webob.exc.HTTPConflict, self.assertRaises(webob.exc.HTTPConflict,
self.controller._action_reboot, self.controller._action_reboot,
self.req, FAKE_UUID, body=body) self.req, FAKE_UUID, body=body)
def _test_rebuild_preserve_ephemeral(self, value=None): def _test_rebuild_preserve_ephemeral(self, value=None):
self._set_fake_extension() return_server = fakes.fake_compute_get(image_ref='2',
return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE,
vm_state=vm_states.ACTIVE, host='fake_host')
host='fake_host') self.stub_out('nova.compute.api.API.get', return_server)
self.stub_out('nova.db.api.instance_get_by_uuid', return_server)
body = { body = {
"rebuild": { "rebuild": {
@ -286,9 +271,9 @@ class ServerActionsControllerTestV21(test.TestCase):
self._test_rebuild_preserve_ephemeral() self._test_rebuild_preserve_ephemeral()
def test_rebuild_accepted_minimum(self): def test_rebuild_accepted_minimum(self):
return_server = fakes.fake_instance_get(image_ref='2', return_server = fakes.fake_compute_get(image_ref='2',
vm_state=vm_states.ACTIVE, host='fake_host') vm_state=vm_states.ACTIVE, host='fake_host')
self.stub_out('nova.db.api.instance_get_by_uuid', return_server) self.stub_out('nova.compute.api.API.get', return_server)
self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID
body = { body = {
@ -312,8 +297,6 @@ class ServerActionsControllerTestV21(test.TestCase):
def rebuild(self2, context, instance, image_href, *args, **kwargs): def rebuild(self2, context, instance, image_href, *args, **kwargs):
info['image_href_in_call'] = image_href info['image_href_in_call'] = image_href
self.stub_out('nova.db.api.instance_get',
fakes.fake_instance_get(vm_state=vm_states.ACTIVE))
self.stub_out('nova.compute.api.API.rebuild', rebuild) self.stub_out('nova.compute.api.API.rebuild', rebuild)
# proper local hrefs must start with 'http://localhost/v2/' # proper local hrefs must start with 'http://localhost/v2/'
@ -343,9 +326,9 @@ class ServerActionsControllerTestV21(test.TestCase):
# is missing from response. See lp bug 921814 # is missing from response. See lp bug 921814
self.flags(enable_instance_password=False, group='api') self.flags(enable_instance_password=False, group='api')
return_server = fakes.fake_instance_get(image_ref='2', return_server = fakes.fake_compute_get(image_ref='2',
vm_state=vm_states.ACTIVE, host='fake_host') vm_state=vm_states.ACTIVE, host='fake_host')
self.stub_out('nova.db.api.instance_get_by_uuid', return_server) self.stub_out('nova.compute.api.API.get', return_server)
self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID
body = { body = {
@ -383,9 +366,9 @@ class ServerActionsControllerTestV21(test.TestCase):
def test_rebuild_accepted_with_metadata(self): def test_rebuild_accepted_with_metadata(self):
metadata = {'new': 'metadata'} metadata = {'new': 'metadata'}
return_server = fakes.fake_instance_get(metadata=metadata, return_server = fakes.fake_compute_get(metadata=metadata,
vm_state=vm_states.ACTIVE, host='fake_host') vm_state=vm_states.ACTIVE, host='fake_host')
self.stub_out('nova.db.api.instance_get_by_uuid', return_server) self.stub_out('nova.compute.api.API.get', return_server)
body = { body = {
"rebuild": { "rebuild": {
@ -437,9 +420,9 @@ class ServerActionsControllerTestV21(test.TestCase):
self.req, FAKE_UUID, body=body) self.req, FAKE_UUID, body=body)
def test_rebuild_admin_pass(self): def test_rebuild_admin_pass(self):
return_server = fakes.fake_instance_get(image_ref='2', return_server = fakes.fake_compute_get(image_ref='2',
vm_state=vm_states.ACTIVE, host='fake_host') vm_state=vm_states.ACTIVE, host='fake_host')
self.stub_out('nova.db.api.instance_get_by_uuid', return_server) self.stub_out('nova.compute.api.API.get', return_server)
body = { body = {
"rebuild": { "rebuild": {
@ -459,9 +442,9 @@ class ServerActionsControllerTestV21(test.TestCase):
# is missing from response. See lp bug 921814 # is missing from response. See lp bug 921814
self.flags(enable_instance_password=False, group='api') self.flags(enable_instance_password=False, group='api')
return_server = fakes.fake_instance_get(image_ref='2', return_server = fakes.fake_compute_get(image_ref='2',
vm_state=vm_states.ACTIVE, host='fake_host') vm_state=vm_states.ACTIVE, host='fake_host')
self.stub_out('nova.db.api.instance_get_by_uuid', return_server) self.stub_out('nova.compute.api.API.get', return_server)
body = { body = {
"rebuild": { "rebuild": {
@ -477,20 +460,17 @@ class ServerActionsControllerTestV21(test.TestCase):
self.assertNotIn('adminPass', body['server']) self.assertNotIn('adminPass', body['server'])
def test_rebuild_server_not_found(self): def test_rebuild_server_not_found(self):
def server_not_found(self, instance_id,
columns_to_join=None, use_slave=False):
raise exception.InstanceNotFound(instance_id=instance_id)
self.stub_out('nova.db.api.instance_get_by_uuid', server_not_found)
body = { body = {
"rebuild": { "rebuild": {
"imageRef": self._image_href, "imageRef": self._image_href,
}, },
} }
with mock.patch('nova.compute.api.API.get',
self.assertRaises(webob.exc.HTTPNotFound, side_effect=exception.InstanceNotFound(
self.controller._action_rebuild, instance_id=FAKE_UUID)):
self.req, FAKE_UUID, body=body) self.assertRaises(webob.exc.HTTPNotFound,
self.controller._action_rebuild,
self.req, FAKE_UUID, body=body)
def test_rebuild_with_bad_image(self): def test_rebuild_with_bad_image(self):
body = { body = {
@ -672,12 +652,12 @@ class ServerActionsControllerTestV21(test.TestCase):
def test_resize_with_server_not_found(self): def test_resize_with_server_not_found(self):
body = dict(resize=dict(flavorRef="http://localhost/3")) body = dict(resize=dict(flavorRef="http://localhost/3"))
with mock.patch('nova.compute.api.API.get',
self.stub_out('nova.compute.api.API.get', return_server_not_found) side_effect=exception.InstanceNotFound(
instance_id=FAKE_UUID)):
self.assertRaises(webob.exc.HTTPNotFound, self.assertRaises(webob.exc.HTTPNotFound,
self.controller._action_resize, self.controller._action_resize,
self.req, FAKE_UUID, body=body) self.req, FAKE_UUID, body=body)
def test_resize_with_image_exceptions(self): def test_resize_with_image_exceptions(self):
body = dict(resize=dict(flavorRef="http://localhost/3")) body = dict(resize=dict(flavorRef="http://localhost/3"))
@ -852,10 +832,12 @@ class ServerActionsControllerTestV21(test.TestCase):
def test_revert_resize_server_not_found(self): def test_revert_resize_server_not_found(self):
body = dict(revertResize=None) body = dict(revertResize=None)
with mock.patch('nova.compute.api.API.get',
self.assertRaises(webob. exc.HTTPNotFound, side_effect=exception.InstanceNotFound(
self.controller._action_revert_resize, instance_id='bad_server_id')):
self.req, "bad_server_id", body=body) self.assertRaises(webob. exc.HTTPNotFound,
self.controller._action_revert_resize,
self.req, "bad_server_id", body=body)
def test_revert_resize_server(self): def test_revert_resize_server(self):
body = dict(revertResize=None) body = dict(revertResize=None)
@ -968,11 +950,11 @@ class ServerActionsControllerTestV21(test.TestCase):
image_root_device_name='/dev/vda', image_root_device_name='/dev/vda',
image_block_device_mapping=str(bdm), image_block_device_mapping=str(bdm),
image_container_format='ami') image_container_format='ami')
instance = fakes.fake_instance_get(image_ref=uuids.fake, instance = fakes.fake_compute_get(image_ref=uuids.fake,
vm_state=vm_states.ACTIVE, vm_state=vm_states.ACTIVE,
root_device_name='/dev/vda', root_device_name='/dev/vda',
system_metadata=system_metadata) system_metadata=system_metadata)
self.stub_out('nova.db.api.instance_get_by_uuid', instance) self.stub_out('nova.compute.api.API.get', instance)
volume = dict(id=_fake_id('a'), volume = dict(id=_fake_id('a'),
size=1, size=1,
@ -1074,13 +1056,13 @@ class ServerActionsControllerTestV21(test.TestCase):
self.stub_out('nova.db.api.block_device_mapping_get_all_by_instance', self.stub_out('nova.db.api.block_device_mapping_get_all_by_instance',
fake_block_device_mapping_get_all_by_instance) fake_block_device_mapping_get_all_by_instance)
instance = fakes.fake_instance_get( instance = fakes.fake_compute_get(
image_ref='', image_ref='',
vm_state=vm_states.ACTIVE, vm_state=vm_states.ACTIVE,
root_device_name='/dev/vda', root_device_name='/dev/vda',
system_metadata={'image_test_key1': 'test_value1', system_metadata={'image_test_key1': 'test_value1',
'image_test_key2': 'test_value2'}) 'image_test_key2': 'test_value2'})
self.stub_out('nova.db.api.instance_get_by_uuid', instance) self.stub_out('nova.compute.api.API.get', instance)
volume = dict(id=_fake_id('a'), volume = dict(id=_fake_id('a'),
size=1, size=1,