@@ -9049,6 +9049,19 @@ def test_disconnect_volume_luks(self, mock_get_volume_encryptor):
9049
9049
uuids.volume_id)
9050
9050
mock_encryptor.detach_volume.assert_not_called()
9051
9051
9052
+ # assert that no attempt to remove the secret is made when
9053
+ # destroy_secrets=False
9054
+ drvr._host.find_secret.reset_mock()
9055
+ drvr._host.delete_secret.reset_mock()
9056
+ drvr._disconnect_volume(
9057
+ self.context,
9058
+ connection_info,
9059
+ instance,
9060
+ encryption=encryption,
9061
+ destroy_secrets=False
9062
+ )
9063
+ drvr._host.delete_secret.assert_not_called()
9064
+
9052
9065
# assert that the encryptor is used if no secret is found
9053
9066
drvr._host.find_secret.reset_mock()
9054
9067
drvr._host.delete_secret.reset_mock()
@@ -10112,6 +10125,36 @@ def test_detach_encryptor_native_luks_device_path_secret_missing(
10112
10125
mock_find_secret.assert_called_once_with('volume', uuids.volume_id)
10113
10126
mock_get_encryptor.assert_not_called()
10114
10127
10128
+ @mock.patch('nova.virt.libvirt.host.Host.delete_secret')
10129
+ @mock.patch('nova.virt.libvirt.host.Host.find_secret', new=mock.Mock())
10130
+ def test_detach_encryptor_skip_secret_removal(self, mock_delete_secret):
10131
+ drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
10132
+ drvr._detach_encryptor(
10133
+ self.context,
10134
+ {
10135
+ 'data': {
10136
+ 'volume_id': uuids.volume_id
10137
+ }
10138
+ },
10139
+ None,
10140
+ destroy_secrets=False
10141
+ )
10142
+ # Assert that no attempt is made to delete the volume secert
10143
+ mock_delete_secret.assert_not_called()
10144
+
10145
+ drvr._detach_encryptor(
10146
+ self.context,
10147
+ {
10148
+ 'data': {
10149
+ 'volume_id': uuids.volume_id
10150
+ }
10151
+ },
10152
+ None,
10153
+ destroy_secrets=True
10154
+ )
10155
+ # Assert that volume secert is deleted
10156
+ mock_delete_secret.assert_called_once_with('volume', uuids.volume_id)
10157
+
10115
10158
def test_allow_native_luksv1(self):
10116
10159
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
10117
10160
self.assertFalse(drvr._allow_native_luksv1({}))
@@ -15752,7 +15795,8 @@ def destroy_side_effect(*args, **kwargs):
15752
15795
mock_domain_destroy.assert_called_once_with()
15753
15796
mock_teardown_container.assert_called_once_with(instance)
15754
15797
mock_cleanup.assert_called_once_with(self.context, instance,
15755
- network_info, None, False)
15798
+ network_info, None, False,
15799
+ destroy_secrets=True)
15756
15800
15757
15801
@mock.patch.object(libvirt_driver.LibvirtDriver, 'cleanup')
15758
15802
@mock.patch.object(libvirt_driver.LibvirtDriver, '_teardown_container')
@@ -15772,7 +15816,8 @@ def test_destroy_lxc_calls_teardown_container_when_no_domain(self,
15772
15816
mock.call(instance)])
15773
15817
mock_teardown_container.assert_called_once_with(instance)
15774
15818
mock_cleanup.assert_called_once_with(self.context, instance,
15775
- network_info, None, False)
15819
+ network_info, None, False,
15820
+ destroy_secrets=True)
15776
15821
15777
15822
@mock.patch.object(host.Host, 'get_guest')
15778
15823
def test_reboot_different_ids(self, mock_get):
@@ -15993,7 +16038,8 @@ def test_hard_reboot(self, mock_get_mdev, mock_destroy, mock_get_disk_info,
15993
16038
mock_get_mdev.assert_called_once_with(instance)
15994
16039
mock_destroy.assert_called_once_with(self.context, instance,
15995
16040
network_info, destroy_disks=False,
15996
- block_device_info=block_device_info)
16041
+ block_device_info=block_device_info,
16042
+ destroy_secrets=False)
15997
16043
15998
16044
mock_get_guest_xml.assert_called_once_with(self.context, instance,
15999
16045
network_info, mock.ANY, mock.ANY,
@@ -19278,6 +19324,59 @@ def test_cleanup_instance_dir_with_rbd_workaround(self,
19278
19324
self.assertTrue(instance.cleaned)
19279
19325
save.assert_called_once_with()
19280
19326
19327
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._disconnect_volume')
19328
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._undefine_domain',
19329
+ new=mock.Mock())
19330
+ @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vpmems',
19331
+ new=mock.Mock(return_value=None))
19332
+ def test_cleanup_destroy_secrets(self, mock_disconnect_volume):
19333
+ block_device_info = {
19334
+ 'block_device_mapping': [
19335
+ {
19336
+ 'connection_info': mock.sentinel.connection_info
19337
+ }
19338
+ ]
19339
+ }
19340
+ instance = objects.Instance(self.context, **self.test_instance)
19341
+ drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI())
19342
+
19343
+ # Pass destroy_vifs=False and destroy_disks=False as we only care about
19344
+ # asserting the behaviour of destroy_secrets in this test.
19345
+ drvr.cleanup(
19346
+ self.context,
19347
+ instance,
19348
+ network_info={},
19349
+ block_device_info=block_device_info,
19350
+ destroy_vifs=False,
19351
+ destroy_disks=False,
19352
+ destroy_secrets=False
19353
+ )
19354
+ drvr.cleanup(
19355
+ self.context,
19356
+ instance,
19357
+ network_info={},
19358
+ block_device_info=block_device_info,
19359
+ destroy_vifs=False,
19360
+ destroy_disks=False,
19361
+ )
19362
+
19363
+ # Assert that disconnect_volume is called with destroy_secrets=False
19364
+ # and destroy_secrets=True by default
19365
+ mock_disconnect_volume.assert_has_calls([
19366
+ mock.call(
19367
+ self.context,
19368
+ mock.sentinel.connection_info,
19369
+ instance,
19370
+ destroy_secrets=False
19371
+ ),
19372
+ mock.call(
19373
+ self.context,
19374
+ mock.sentinel.connection_info,
19375
+ instance,
19376
+ destroy_secrets=True
19377
+ )
19378
+ ])
19379
+
19281
19380
@mock.patch.object(libvirt_driver.LibvirtDriver, '_get_volume_encryption')
19282
19381
@mock.patch.object(libvirt_driver.LibvirtDriver, '_allow_native_luksv1')
19283
19382
def test_swap_volume_native_luks_blocked(self, mock_allow_native_luksv1,
0 commit comments