Make Evacuate API use Instance objects.
Replace current instance dicts and conversions to Instance Objects. Add services arg to create_fake_instance_obj. Change-Id: I7ddf11ce17df254a8253461e17b5c6519cb7d6fe Related to blueprint compute-manager-objects-juno
This commit is contained in:
parent
c24fb76f80
commit
3020ed9502
|
@ -72,8 +72,8 @@ class Controller(wsgi.Controller):
|
|||
raise exc.HTTPNotFound(explanation=msg)
|
||||
|
||||
try:
|
||||
instance = self.compute_api.get(context, id)
|
||||
if instance['host'] == host:
|
||||
instance = self.compute_api.get(context, id, want_objects=True)
|
||||
if instance.host == host:
|
||||
msg = _("The target host can't be the same one.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
self.compute_api.evacuate(context, instance, host,
|
||||
|
|
|
@ -77,8 +77,9 @@ class EvacuateController(wsgi.Controller):
|
|||
msg = _("Compute host %s not found.") % host
|
||||
raise exc.HTTPNotFound(explanation=msg)
|
||||
|
||||
instance = common.get_instance(self.compute_api, context, id)
|
||||
if instance['host'] == host:
|
||||
instance = common.get_instance(self.compute_api, context, id,
|
||||
want_objects=True)
|
||||
if instance.host == host:
|
||||
msg = _("The target host can't be the same one.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
|
|
|
@ -3025,7 +3025,7 @@ class API(base.Base):
|
|||
raising an exception.
|
||||
"""
|
||||
LOG.debug('vm evacuation scheduled')
|
||||
inst_host = instance['host']
|
||||
inst_host = instance.host
|
||||
service = service_obj.Service.get_by_compute_host(context, inst_host)
|
||||
if self.servicegroup_api.service_is_up(service):
|
||||
msg = (_('Instance compute service state on %s '
|
||||
|
@ -3033,18 +3033,12 @@ class API(base.Base):
|
|||
LOG.error(msg)
|
||||
raise exception.ComputeServiceInUse(host=inst_host)
|
||||
|
||||
instance = self.update(context, instance, expected_task_state=[None],
|
||||
task_state=task_states.REBUILDING)
|
||||
|
||||
instance.task_state = task_states.REBUILDING
|
||||
instance.save(expected_task_state=[None])
|
||||
self._record_action_start(context, instance, instance_actions.EVACUATE)
|
||||
|
||||
# NOTE(danms): Transitional until evacuate supports objects
|
||||
inst_obj = objects.Instance._from_db_object(
|
||||
context, objects.Instance(), instance,
|
||||
expected_attrs=['metadata', 'system_metadata', 'info_cache'])
|
||||
|
||||
return self.compute_rpcapi.rebuild_instance(context,
|
||||
instance=inst_obj,
|
||||
instance=instance,
|
||||
new_pass=admin_password,
|
||||
injected_files=None,
|
||||
image_ref=None,
|
||||
|
|
|
@ -24,6 +24,8 @@ from nova import exception
|
|||
from nova.openstack.common import jsonutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests import fake_instance
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('password_length', 'nova.utils')
|
||||
|
@ -33,17 +35,14 @@ def fake_compute_api(*args, **kwargs):
|
|||
return True
|
||||
|
||||
|
||||
def fake_compute_api_get(self, context, instance_id):
|
||||
def fake_compute_api_get(self, context, instance_id, want_objects=False):
|
||||
# BAD_UUID is something that does not exist
|
||||
if instance_id == 'BAD_UUID':
|
||||
raise exception.InstanceNotFound(instance_id=instance_id)
|
||||
else:
|
||||
return {
|
||||
'id': 1,
|
||||
'uuid': instance_id,
|
||||
'vm_state': vm_states.ACTIVE,
|
||||
'task_state': None, 'host': 'host1'
|
||||
}
|
||||
return fake_instance.fake_instance_obj(context, id=1, uuid=instance_id,
|
||||
task_state=None, host='host1',
|
||||
vm_state=vm_states.ACTIVE)
|
||||
|
||||
|
||||
def fake_service_get_by_compute_host(self, context, host):
|
||||
|
|
|
@ -24,6 +24,7 @@ from nova import exception
|
|||
from nova.openstack.common import jsonutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests import fake_instance
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('password_length', 'nova.utils')
|
||||
|
@ -33,14 +34,11 @@ def fake_compute_api(*args, **kwargs):
|
|||
return True
|
||||
|
||||
|
||||
def fake_compute_api_get(self, context, instance_id, expected_attrs=None,
|
||||
want_objects=False):
|
||||
return {
|
||||
'id': 1,
|
||||
'uuid': instance_id,
|
||||
'vm_state': vm_states.ACTIVE,
|
||||
'task_state': None, 'host': 'host1'
|
||||
}
|
||||
def fake_compute_api_get(self, context, instance_id, want_objects=False,
|
||||
**kwargs):
|
||||
return fake_instance.fake_instance_obj(context, id=1, uuid=instance_id,
|
||||
vm_state=vm_states.ACTIVE,
|
||||
task_state=None, host='host1')
|
||||
|
||||
|
||||
def fake_service_get_by_compute_host(self, context, host):
|
||||
|
|
|
@ -310,8 +310,10 @@ class BaseTestCase(test.TestCase):
|
|||
self.context, objects.Instance(), db_inst,
|
||||
expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS)
|
||||
|
||||
def _create_fake_instance_obj(self, params=None, type_name='m1.tiny'):
|
||||
db_inst = self._create_fake_instance(params, type_name=type_name)
|
||||
def _create_fake_instance_obj(self, params=None, type_name='m1.tiny',
|
||||
services=False):
|
||||
db_inst = self._create_fake_instance(params, type_name=type_name,
|
||||
services=services)
|
||||
return self._objectify(db_inst)
|
||||
|
||||
def _create_instance_type(self, params=None):
|
||||
|
@ -9112,20 +9114,16 @@ class ComputeAPITestCase(BaseTestCase):
|
|||
instance.refresh()
|
||||
self.assertEqual(instance['task_state'], task_states.MIGRATING)
|
||||
|
||||
def _check_evacuate(self, instance_params=None):
|
||||
instance = jsonutils.to_primitive(self._create_fake_instance(
|
||||
instance_params, services=True))
|
||||
instance_uuid = instance['uuid']
|
||||
instance = db.instance_get_by_uuid(self.context, instance_uuid)
|
||||
self.assertIsNone(instance['task_state'])
|
||||
def test_evacuate(self):
|
||||
instance = self._create_fake_instance_obj(services=True)
|
||||
self.assertIsNone(instance.task_state)
|
||||
|
||||
def fake_service_is_up(*args, **kwargs):
|
||||
return False
|
||||
|
||||
def fake_rebuild_instance(*args, **kwargs):
|
||||
self.assertIn('info_cache', kwargs['instance'])
|
||||
db.instance_update(self.context, instance_uuid,
|
||||
{'host': kwargs['host']})
|
||||
instance.host = kwargs['host']
|
||||
instance.save()
|
||||
|
||||
self.stubs.Set(self.compute_api.servicegroup_api, 'service_is_up',
|
||||
fake_service_is_up)
|
||||
|
@ -9137,17 +9135,10 @@ class ComputeAPITestCase(BaseTestCase):
|
|||
on_shared_storage=True,
|
||||
admin_password=None)
|
||||
|
||||
instance = db.instance_get_by_uuid(self.context, instance_uuid)
|
||||
self.assertEqual(instance['task_state'], task_states.REBUILDING)
|
||||
self.assertEqual(instance['host'], 'fake_dest_host')
|
||||
|
||||
db.instance_destroy(self.context, instance['uuid'])
|
||||
|
||||
def test_evacuate(self):
|
||||
self._check_evacuate()
|
||||
|
||||
def test_error_evacuate(self):
|
||||
self._check_evacuate({'vm_state': vm_states.ERROR})
|
||||
instance.refresh()
|
||||
self.assertEqual(instance.task_state, task_states.REBUILDING)
|
||||
self.assertEqual(instance.host, 'fake_dest_host')
|
||||
instance.destroy()
|
||||
|
||||
def test_fail_evacuate_from_non_existing_host(self):
|
||||
inst = {}
|
||||
|
@ -9168,26 +9159,18 @@ class ComputeAPITestCase(BaseTestCase):
|
|||
inst['ephemeral_gb'] = 0
|
||||
inst['architecture'] = 'x86_64'
|
||||
inst['os_type'] = 'Linux'
|
||||
instance = self._create_fake_instance_obj(inst)
|
||||
|
||||
instance = jsonutils.to_primitive(db.instance_create(self.context,
|
||||
inst))
|
||||
instance_uuid = instance['uuid']
|
||||
instance = db.instance_get_by_uuid(self.context, instance_uuid)
|
||||
self.assertIsNone(instance['task_state'])
|
||||
|
||||
self.assertIsNone(instance.task_state)
|
||||
self.assertRaises(exception.ComputeHostNotFound,
|
||||
self.compute_api.evacuate, self.context.elevated(), instance,
|
||||
host='fake_dest_host', on_shared_storage=True,
|
||||
admin_password=None)
|
||||
|
||||
db.instance_destroy(self.context, instance['uuid'])
|
||||
instance.destroy()
|
||||
|
||||
def test_fail_evacuate_from_running_host(self):
|
||||
instance = jsonutils.to_primitive(self._create_fake_instance(
|
||||
services=True))
|
||||
instance_uuid = instance['uuid']
|
||||
instance = db.instance_get_by_uuid(self.context, instance_uuid)
|
||||
self.assertIsNone(instance['task_state'])
|
||||
instance = self._create_fake_instance_obj(services=True)
|
||||
self.assertIsNone(instance.task_state)
|
||||
|
||||
def fake_service_is_up(*args, **kwargs):
|
||||
return True
|
||||
|
@ -9199,33 +9182,21 @@ class ComputeAPITestCase(BaseTestCase):
|
|||
self.compute_api.evacuate, self.context.elevated(), instance,
|
||||
host='fake_dest_host', on_shared_storage=True,
|
||||
admin_password=None)
|
||||
|
||||
db.instance_destroy(self.context, instance['uuid'])
|
||||
instance.destroy()
|
||||
|
||||
def test_fail_evacuate_instance_in_wrong_state(self):
|
||||
instances = [
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.BUILDING})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.PAUSED})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.SUSPENDED})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.RESCUED})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.RESIZED})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.SOFT_DELETED})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'vm_state': vm_states.DELETED}))
|
||||
]
|
||||
states = [vm_states.BUILDING, vm_states.PAUSED, vm_states.SUSPENDED,
|
||||
vm_states.RESCUED, vm_states.RESIZED, vm_states.SOFT_DELETED,
|
||||
vm_states.DELETED]
|
||||
instances = [self._create_fake_instance_obj({'vm_state': state})
|
||||
for state in states]
|
||||
|
||||
for instance in instances:
|
||||
self.assertRaises(exception.InstanceInvalidState,
|
||||
self.compute_api.evacuate, self.context, instance,
|
||||
host='fake_dest_host', on_shared_storage=True,
|
||||
admin_password=None)
|
||||
db.instance_destroy(self.context, instance['uuid'])
|
||||
instance.destroy()
|
||||
|
||||
def test_get_migrations(self):
|
||||
migration = test_migration.fake_db_migration(uuid="1234")
|
||||
|
|
Loading…
Reference in New Issue