@@ -9084,6 +9084,19 @@ def test_disconnect_volume_luks(self, mock_get_volume_encryptor):
9084
9084
uuids.volume_id)
9085
9085
mock_encryptor.detach_volume.assert_not_called()
9086
9086
9087
+ # assert that no attempt to remove the secret is made when
9088
+ # destroy_secrets=False
9089
+ drvr._host.find_secret.reset_mock()
9090
+ drvr._host.delete_secret.reset_mock()
9091
+ drvr._disconnect_volume(
9092
+ self.context,
9093
+ connection_info,
9094
+ instance,
9095
+ encryption=encryption,
9096
+ destroy_secrets=False
9097
+ )
9098
+ drvr._host.delete_secret.assert_not_called()
9099
+
9087
9100
# assert that the encryptor is used if no secret is found
9088
9101
drvr._host.find_secret.reset_mock()
9089
9102
drvr._host.delete_secret.reset_mock()
@@ -10147,6 +10160,36 @@ def test_detach_encryptor_native_luks_device_path_secret_missing(
10147
10160
mock_find_secret.assert_called_once_with('volume', uuids.volume_id)
10148
10161
mock_get_encryptor.assert_not_called()
10149
10162
10163
+ @mock.patch('nova.virt.libvirt.host.Host.delete_secret')
10164
+ @mock.patch('nova.virt.libvirt.host.Host.find_secret', new=mock.Mock())
10165
+ def test_detach_encryptor_skip_secret_removal(self, mock_delete_secret):
10166
+ drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
10167
+ drvr._detach_encryptor(
10168
+ self.context,
10169
+ {
10170
+ 'data': {
10171
+ 'volume_id': uuids.volume_id
10172
+ }
10173
+ },
10174
+ None,
10175
+ destroy_secrets=False
10176
+ )
10177
+ # Assert that no attempt is made to delete the volume secert
10178
+ mock_delete_secret.assert_not_called()
10179
+
10180
+ drvr._detach_encryptor(
10181
+ self.context,
10182
+ {
10183
+ 'data': {
10184
+ 'volume_id': uuids.volume_id
10185
+ }
10186
+ },
10187
+ None,
10188
+ destroy_secrets=True
10189
+ )
10190
+ # Assert that volume secert is deleted
10191
+ mock_delete_secret.assert_called_once_with('volume', uuids.volume_id)
10192
+
10150
10193
def test_allow_native_luksv1(self):
10151
10194
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
10152
10195
self.assertFalse(drvr._allow_native_luksv1({}))
@@ -15793,7 +15836,8 @@ def destroy_side_effect(*args, **kwargs):
15793
15836
mock_domain_destroy.assert_called_once_with()
15794
15837
mock_teardown_container.assert_called_once_with(instance)
15795
15838
mock_cleanup.assert_called_once_with(self.context, instance,
15796
- network_info, None, False)
15839
+ network_info, None, False,
15840
+ destroy_secrets=True)
15797
15841
15798
15842
@mock.patch.object(libvirt_driver.LibvirtDriver, 'cleanup')
15799
15843
@mock.patch.object(libvirt_driver.LibvirtDriver, '_teardown_container')
@@ -15813,7 +15857,8 @@ def test_destroy_lxc_calls_teardown_container_when_no_domain(self,
15813
15857
mock.call(instance)])
15814
15858
mock_teardown_container.assert_called_once_with(instance)
15815
15859
mock_cleanup.assert_called_once_with(self.context, instance,
15816
- network_info, None, False)
15860
+ network_info, None, False,
15861
+ destroy_secrets=True)
15817
15862
15818
15863
@mock.patch.object(host.Host, 'get_guest')
15819
15864
def test_reboot_different_ids(self, mock_get):
@@ -16034,7 +16079,8 @@ def test_hard_reboot(self, mock_get_mdev, mock_destroy, mock_get_disk_info,
16034
16079
mock_get_mdev.assert_called_once_with(instance)
16035
16080
mock_destroy.assert_called_once_with(self.context, instance,
16036
16081
network_info, destroy_disks=False,
16037
- block_device_info=block_device_info)
16082
+ block_device_info=block_device_info,
16083
+ destroy_secrets=False)
16038
16084
16039
16085
mock_get_guest_xml.assert_called_once_with(self.context, instance,
16040
16086
network_info, mock.ANY, mock.ANY,
@@ -19321,6 +19367,59 @@ def test_cleanup_instance_dir_with_rbd_workaround(self,
19321
19367
self.assertTrue(instance.cleaned)
19322
19368
save.assert_called_once_with()
19323
19369
19370
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._disconnect_volume')
19371
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._undefine_domain',
19372
+ new=mock.Mock())
19373
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vpmems',
19374
+ new=mock.Mock(return_value=None))
19375
+ def test_cleanup_destroy_secrets(self, mock_disconnect_volume):
19376
+ block_device_info = {
19377
+ 'block_device_mapping': [
19378
+ {
19379
+ 'connection_info': mock.sentinel.connection_info
19380
+ }
19381
+ ]
19382
+ }
19383
+ instance = objects.Instance(self.context, **self.test_instance)
19384
+ drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI())
19385
+
19386
+ # Pass destroy_vifs=False and destroy_disks=False as we only care about
19387
+ # asserting the behaviour of destroy_secrets in this test.
19388
+ drvr.cleanup(
19389
+ self.context,
19390
+ instance,
19391
+ network_info={},
19392
+ block_device_info=block_device_info,
19393
+ destroy_vifs=False,
19394
+ destroy_disks=False,
19395
+ destroy_secrets=False
19396
+ )
19397
+ drvr.cleanup(
19398
+ self.context,
19399
+ instance,
19400
+ network_info={},
19401
+ block_device_info=block_device_info,
19402
+ destroy_vifs=False,
19403
+ destroy_disks=False,
19404
+ )
19405
+
19406
+ # Assert that disconnect_volume is called with destroy_secrets=False
19407
+ # and destroy_secrets=True by default
19408
+ mock_disconnect_volume.assert_has_calls([
19409
+ mock.call(
19410
+ self.context,
19411
+ mock.sentinel.connection_info,
19412
+ instance,
19413
+ destroy_secrets=False
19414
+ ),
19415
+ mock.call(
19416
+ self.context,
19417
+ mock.sentinel.connection_info,
19418
+ instance,
19419
+ destroy_secrets=True
19420
+ )
19421
+ ])
19422
+
19324
19423
@mock.patch.object(libvirt_driver.LibvirtDriver, '_get_volume_encryption')
19325
19424
@mock.patch.object(libvirt_driver.LibvirtDriver, '_allow_native_luksv1')
19326
19425
def test_swap_volume_native_luks_blocked(self, mock_allow_native_luksv1,
0 commit comments