Skip to content

Commit 5633be2

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Avoid unbound instance_uuid var during delete" into stable/wallaby
2 parents 5e8f34c + fc20dc2 commit 5633be2

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

nova/compute/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,12 @@ def _delete(self, context, instance, delete_type, cb, **instance_attrs):
22312231
# Normal delete should be attempted.
22322232
may_have_ports_or_volumes = compute_utils.may_have_ports_or_volumes(
22332233
instance)
2234+
2235+
# Save a copy of the instance UUID early, in case
2236+
# _lookup_instance returns instance = None, to pass to
2237+
# _local_delete_cleanup if needed.
2238+
instance_uuid = instance.uuid
2239+
22342240
if not instance.host and not may_have_ports_or_volumes:
22352241
try:
22362242
if self._delete_while_booting(context, instance):
@@ -2244,10 +2250,6 @@ def _delete(self, context, instance, delete_type, cb, **instance_attrs):
22442250
# full Instance or None if not found. If not found then it's
22452251
# acceptable to skip the rest of the delete processing.
22462252

2247-
# Save a copy of the instance UUID early, in case
2248-
# _lookup_instance returns instance = None, to pass to
2249-
# _local_delete_cleanup if needed.
2250-
instance_uuid = instance.uuid
22512253
cell, instance = self._lookup_instance(context, instance.uuid)
22522254
if cell and instance:
22532255
try:

nova/tests/unit/compute/test_api.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,34 @@ def test_delete_instance_from_cell0(self, destroy_mock, notify_mock):
16201620
self.compute_api.notifier, self.context, instance)
16211621
destroy_mock.assert_called_once_with()
16221622

1623+
def test_delete_instance_while_booting_host_changes_lookup_fails(self):
1624+
"""Tests the case where the instance become scheduled while being
1625+
destroyed but then the final lookup fails.
1626+
"""
1627+
instance = self._create_instance_obj({'host': None})
1628+
1629+
with test.nested(
1630+
mock.patch.object(
1631+
self.compute_api, '_delete_while_booting',
1632+
side_effect=exception.ObjectActionError(
1633+
action="delete", reason="reason")),
1634+
mock.patch.object(
1635+
self.compute_api, '_lookup_instance',
1636+
return_value=(None, None)),
1637+
mock.patch.object(self.compute_api, '_local_delete_cleanup')
1638+
) as (
1639+
_delete_while_booting, _lookup_instance, _local_delete_cleanup
1640+
):
1641+
self.compute_api._delete(
1642+
self.context, instance, 'delete', mock.NonCallableMock())
1643+
1644+
_delete_while_booting.assert_called_once_with(
1645+
self.context, instance)
1646+
_lookup_instance.assert_called_once_with(
1647+
self.context, instance.uuid)
1648+
_local_delete_cleanup.assert_called_once_with(
1649+
self.context, instance.uuid)
1650+
16231651
@mock.patch.object(context, 'target_cell')
16241652
@mock.patch.object(objects.InstanceMapping, 'get_by_instance_uuid',
16251653
side_effect=exception.InstanceMappingNotFound(

0 commit comments

Comments
 (0)