def _test_locked_instance(self, action, method=None, body=None, compute_api_args_map=None): if method is None: method = action.replace('_', '') compute_api_args_map = compute_api_args_map or {} instance = self._stub_instance_get() args, kwargs = compute_api_args_map.get(action, ((), {})) getattr(self.compute_api, method)(self.context, instance, *args, **kwargs).AndRaise( exception.InstanceIsLocked(instance_uuid=instance.uuid)) self.mox.ReplayAll() controller_function = getattr(self.controller, action) self.assertRaises(webob.exc.HTTPConflict, controller_function, self.req, instance.uuid, body=body) # 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()
def fake_swap_volume_for_locked_server(self, context, instance, old_volume, new_volume): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_detach_volume_from_locked_server(self, context, instance, volume): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_attach_volume_to_locked_server(self, context, instance, volume_id, device=None): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_start_stop_invalid_state(self, context, instance): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
class DeferredDeleteExtensionTestV21(test.NoDBTestCase): ext_ver = dd_v21.DeferredDeleteController def setUp(self): super(DeferredDeleteExtensionTestV21, self).setUp() self.fake_input_dict = {} self.fake_uuid = 'fake_uuid' self.fake_context = context.RequestContext('fake', 'fake') self.fake_req = FakeRequest(self.fake_context) self.extension = self.ext_ver() def test_force_delete(self): self.mox.StubOutWithMock(compute_api.API, 'get') self.mox.StubOutWithMock(compute_api.API, 'force_delete') fake_instance = 'fake_instance' compute_api.API.get(self.fake_context, self.fake_uuid, expected_attrs=None, want_objects=True).AndReturn(fake_instance) compute_api.API.force_delete(self.fake_context, fake_instance) self.mox.ReplayAll() res = self.extension._force_delete(self.fake_req, self.fake_uuid, self.fake_input_dict) # NOTE: on v2.1, http status code is set as wsgi_code of API # method instead of status_int in a response object. if isinstance(self.extension, dd_v21.DeferredDeleteController): status_int = self.extension._force_delete.wsgi_code else: status_int = res.status_int self.assertEqual(202, status_int) def test_force_delete_instance_not_found(self): self.mox.StubOutWithMock(compute_api.API, 'get') compute_api.API.get( self.fake_context, self.fake_uuid, expected_attrs=None, want_objects=True).AndRaise( exception.InstanceNotFound(instance_id='instance-0000')) self.mox.ReplayAll() self.assertRaises(webob.exc.HTTPNotFound, self.extension._force_delete, self.fake_req, self.fake_uuid, self.fake_input_dict) @mock.patch.object(compute_api.API, 'get') @mock.patch.object( compute_api.API, 'force_delete', side_effect=exception.InstanceIsLocked(instance_uuid='fake_uuid')) def test_force_delete_instance_locked(self, mock_force_delete, mock_get): req = fakes.HTTPRequest.blank('/v2/fake/servers/fake_uuid/action') ex = self.assertRaises(webob.exc.HTTPConflict, self.extension._force_delete, req, 'fake_uuid', '') self.assertIn('Instance fake_uuid is locked', ex.explanation) def test_restore(self): self.mox.StubOutWithMock(compute_api.API, 'get') self.mox.StubOutWithMock(compute_api.API, 'restore') fake_instance = 'fake_instance' compute_api.API.get(self.fake_context, self.fake_uuid, expected_attrs=None, want_objects=True).AndReturn(fake_instance) compute_api.API.restore(self.fake_context, fake_instance) self.mox.ReplayAll() res = self.extension._restore(self.fake_req, self.fake_uuid, self.fake_input_dict) # NOTE: on v2.1, http status code is set as wsgi_code of API # method instead of status_int in a response object. if isinstance(self.extension, dd_v21.DeferredDeleteController): status_int = self.extension._restore.wsgi_code else: status_int = res.status_int self.assertEqual(202, status_int) def test_restore_instance_not_found(self): self.mox.StubOutWithMock(compute_api.API, 'get') compute_api.API.get( self.fake_context, self.fake_uuid, expected_attrs=None, want_objects=True).AndRaise( exception.InstanceNotFound(instance_id='instance-0000')) self.mox.ReplayAll() self.assertRaises(webob.exc.HTTPNotFound, self.extension._restore, self.fake_req, self.fake_uuid, self.fake_input_dict) def test_restore_raises_conflict_on_invalid_state(self): self.mox.StubOutWithMock(compute_api.API, 'get') self.mox.StubOutWithMock(compute_api.API, 'restore') fake_instance = 'fake_instance' exc = exception.InstanceInvalidState(attr='fake_attr', state='fake_state', method='fake_method', instance_uuid='fake') compute_api.API.get(self.fake_context, self.fake_uuid, expected_attrs=None, want_objects=True).AndReturn(fake_instance) compute_api.API.restore(self.fake_context, fake_instance).AndRaise(exc) self.mox.ReplayAll() self.assertRaises(webob.exc.HTTPConflict, self.extension._restore, self.fake_req, self.fake_uuid, self.fake_input_dict)
def fake_attach_interface_to_locked_server(self, context, instance, network_id, port_id, requested_ip): raise exception.InstanceIsLocked(instance_uuid=FAKE_UUID1)
def fake_start_stop_locked_server(self, context, instance): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_detach_interface_from_locked_server(self, context, instance, port_id): raise exception.InstanceIsLocked(instance_uuid=FAKE_UUID1)
class BadStateServerMetaDataTestV21(test.TestCase): def setUp(self): super(BadStateServerMetaDataTestV21, self).setUp() fakes.stub_out_key_pair_funcs(self.stubs) self.stubs.Set(patron.db, 'instance_metadata_get', return_server_metadata) self.stubs.Set(compute_rpcapi.ComputeAPI, 'change_instance_metadata', fake_change_instance_metadata) self.stubs.Set(patron.db, 'instance_get', self._return_server_in_build) self.stubs.Set(patron.db, 'instance_get_by_uuid', self._return_server_in_build_by_uuid) self.stubs.Set(patron.db, 'instance_metadata_delete', delete_server_metadata) self._set_up_resources() def _set_up_resources(self): self.controller = server_metadata_v21.ServerMetadataController() self.uuid = str(uuid.uuid4()) self.url = '/fake/servers/%s/metadata' % self.uuid def _get_request(self, param_url=''): return fakes.HTTPRequestV3.blank(self.url + param_url) def test_invalid_state_on_delete(self): req = self._get_request('/key2') req.method = 'DELETE' self.assertRaises(webob.exc.HTTPConflict, self.controller.delete, req, self.uuid, 'key2') def test_invalid_state_on_update_metadata(self): self.stubs.Set(patron.db, 'instance_metadata_update', return_create_instance_metadata) req = self._get_request() req.method = 'POST' req.content_type = 'application/json' expected = { 'metadata': { 'key1': 'updatedvalue', 'key29': 'newkey', } } req.body = jsonutils.dumps(expected) self.assertRaises(webob.exc.HTTPConflict, self.controller.update_all, req, self.uuid, body=expected) def _return_server_in_build(self, context, server_id, columns_to_join=None): return fake_instance.fake_db_instance( **{ 'id': server_id, 'uuid': '0cc3346e-9fef-4445-abe6-5d2b2690ec64', 'name': 'fake', 'locked': False, 'vm_state': vm_states.BUILDING }) def _return_server_in_build_by_uuid(self, context, server_uuid, columns_to_join=None, use_slave=False): return fake_instance.fake_db_instance( **{ 'id': 1, 'uuid': '0cc3346e-9fef-4445-abe6-5d2b2690ec64', 'name': 'fake', 'locked': False, 'vm_state': vm_states.BUILDING }) @mock.patch.object(patron.compute.api.API, 'update_instance_metadata', side_effect=exception.InstanceIsLocked(instance_uuid=0)) def test_instance_lock_update_metadata(self, mock_update): req = self._get_request() req.method = 'POST' req.content_type = 'application/json' expected = { 'metadata': { 'keydummy': 'newkey', } } req.body = jsonutils.dumps(expected) self.assertRaises(webob.exc.HTTPConflict, self.controller.update_all, req, self.uuid, body=expected)
def fake_rescue_from_locked_server(self, context, instance, rescue_password=None, rescue_image_ref=None): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_unrescue_from_locked_server(self, context, instance): raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
def fake_actions_to_locked_server(self, context, instance, *args, **kwargs): raise exc.InstanceIsLocked(instance_uuid=instance['uuid'])