Merge "nova-manage: modify image properties in request_spec"

This commit is contained in:
Zuul 2024-09-20 07:11:40 +00:00 committed by Gerrit Code Review
commit 9c36174539
3 changed files with 28 additions and 4 deletions

View File

@ -3343,9 +3343,10 @@ class ImagePropertyCommands:
# Return the dict so we can update the instance system_metadata # Return the dict so we can update the instance system_metadata
return image_properties return image_properties
def _update_image_properties(self, instance, image_properties): def _update_image_properties(self, ctxt, instance, image_properties):
"""Update instance image properties """Update instance image properties
:param ctxt: nova.context.RequestContext
:param instance: The instance to update :param instance: The instance to update
:param image_properties: List of image properties and values to update :param image_properties: List of image properties and values to update
""" """
@ -3369,8 +3370,13 @@ class ImagePropertyCommands:
for image_property, value in image_properties.items(): for image_property, value in image_properties.items():
instance.system_metadata[f'image_{image_property}'] = value instance.system_metadata[f'image_{image_property}'] = value
request_spec = objects.RequestSpec.get_by_instance_uuid(
ctxt, instance.uuid)
request_spec.image = instance.image_meta
# Save and return 0 # Save and return 0
instance.save() instance.save()
request_spec.save()
return 0 return 0
@action_description(_( @action_description(_(
@ -3405,7 +3411,7 @@ class ImagePropertyCommands:
instance = objects.Instance.get_by_uuid( instance = objects.Instance.get_by_uuid(
cctxt, instance_uuid, expected_attrs=['system_metadata']) cctxt, instance_uuid, expected_attrs=['system_metadata'])
return self._update_image_properties( return self._update_image_properties(
instance, image_properties) ctxt, instance, image_properties)
except ValueError as e: except ValueError as e:
print(str(e)) print(str(e))
return 6 return 6

View File

@ -4217,6 +4217,8 @@ class ImagePropertyCommandsTestCase(test.NoDBTestCase):
image_property='hw_disk_bus') image_property='hw_disk_bus')
self.assertEqual(1, ret, 'return code') self.assertEqual(1, ret, 'return code')
@mock.patch('nova.objects.RequestSpec.save')
@mock.patch('nova.objects.RequestSpec.get_by_instance_uuid')
@mock.patch('nova.objects.Instance.get_by_uuid') @mock.patch('nova.objects.Instance.get_by_uuid')
@mock.patch('nova.context.target_cell') @mock.patch('nova.context.target_cell')
@mock.patch('nova.objects.Instance.save') @mock.patch('nova.objects.Instance.save')
@ -4225,7 +4227,8 @@ class ImagePropertyCommandsTestCase(test.NoDBTestCase):
@mock.patch('nova.context.get_admin_context', @mock.patch('nova.context.get_admin_context',
new=mock.Mock(return_value=mock.sentinel.ctxt)) new=mock.Mock(return_value=mock.sentinel.ctxt))
def test_set_image_properties( def test_set_image_properties(
self, mock_instance_save, mock_target_cell, mock_get_instance self, mock_instance_save, mock_target_cell, mock_get_instance,
mock_get_request_spec, mock_request_spec_save
): ):
mock_target_cell.return_value.__enter__.return_value = \ mock_target_cell.return_value.__enter__.return_value = \
mock.sentinel.cctxt mock.sentinel.cctxt
@ -4234,9 +4237,11 @@ class ImagePropertyCommandsTestCase(test.NoDBTestCase):
vm_state=obj_fields.InstanceState.STOPPED, vm_state=obj_fields.InstanceState.STOPPED,
system_metadata={ system_metadata={
'image_hw_disk_bus': 'virtio', 'image_hw_disk_bus': 'virtio',
} },
image_ref=''
) )
mock_get_instance.return_value = instance mock_get_instance.return_value = instance
mock_get_request_spec.return_value = objects.RequestSpec()
ret = self.commands.set( ret = self.commands.set(
instance_uuid=uuidsentinel.instance, instance_uuid=uuidsentinel.instance,
image_properties=['hw_cdrom_bus=sata'] image_properties=['hw_cdrom_bus=sata']
@ -4253,7 +4258,12 @@ class ImagePropertyCommandsTestCase(test.NoDBTestCase):
instance.system_metadata.get('image_hw_disk_bus'), instance.system_metadata.get('image_hw_disk_bus'),
'image_hw_disk_bus' 'image_hw_disk_bus'
) )
image_props = mock_get_request_spec.return_value.image.properties
self.assertEqual('sata', image_props.get('hw_cdrom_bus'))
self.assertEqual('virtio', image_props.get('hw_disk_bus'))
mock_instance_save.assert_called_once() mock_instance_save.assert_called_once()
mock_request_spec_save.assert_called_once()
@mock.patch('nova.objects.Instance.get_by_uuid') @mock.patch('nova.objects.Instance.get_by_uuid')
@mock.patch('nova.objects.InstanceMapping.get_by_instance_uuid', @mock.patch('nova.objects.InstanceMapping.get_by_instance_uuid',

View File

@ -0,0 +1,8 @@
---
fixes:
- |
Before the `Bug 2078999 <https://bugs.launchpad.net/nova/+bug/2078999>`_ was fixed,
the ``nova-manage image_property set`` command would update the image properties
embedded in the instance but would not update the ones in the request specs. This
led to an unexpected rollback of the image properties that were updated by the
command after an instance migration.