Skip to content

Commit 5bbbc26

Browse files
committed
cdh: add drop guard for close_device in tests
Similar to the drop guard for cleaning up the LUKS header file, add a drop guard for close_device, ensuring the device will be closed even in case of a panic. Observed /dev/mapper across local test runs to ensure nothing is left behind. Signed-off-by: Manuel Huber <manuelh@nvidia.com>
1 parent 8276eb0 commit 5bbbc26

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

  • confidential-data-hub/hub/src/storage/drivers

confidential-data-hub/hub/src/storage/drivers/luks2.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ mod tests {
196196
}
197197
}
198198

199+
/// Closes the dm-crypt device on drop so tests don't leave mapper devices behind.
200+
struct CloseDeviceOnDrop(String);
201+
impl Drop for CloseDeviceOnDrop {
202+
fn drop(&mut self) {
203+
let _ = Luks2Formatter::default().close_device(&self.0);
204+
}
205+
}
206+
199207
#[test]
200208
#[serial]
201209
fn encrypt_open_device_no_integrity() {
@@ -216,8 +224,7 @@ mod tests {
216224
luks2_formatter
217225
.open_device(path, None, NAME, passphrase)
218226
.unwrap();
219-
220-
luks2_formatter.close_device(NAME).unwrap();
227+
let _device_guard = CloseDeviceOnDrop(NAME.to_string());
221228
}
222229

223230
#[test]
@@ -240,8 +247,7 @@ mod tests {
240247
luks2_formatter
241248
.open_device(path, None, NAME, passphrase)
242249
.unwrap();
243-
244-
luks2_formatter.close_device(NAME).unwrap();
250+
let _device_guard = CloseDeviceOnDrop(NAME.to_string());
245251
}
246252

247253
#[test]
@@ -266,8 +272,7 @@ mod tests {
266272
luks2_formatter
267273
.open_device(path, Some(&header_path), NAME, passphrase)
268274
.unwrap();
269-
270-
luks2_formatter.close_device(NAME).unwrap();
275+
let _device_guard = CloseDeviceOnDrop(NAME.to_string());
271276
}
272277

273278
#[test]
@@ -292,8 +297,7 @@ mod tests {
292297
luks2_formatter
293298
.open_device(path, Some(&header_path), NAME, passphrase)
294299
.unwrap();
295-
296-
luks2_formatter.close_device(NAME).unwrap();
300+
let _device_guard = CloseDeviceOnDrop(NAME.to_string());
297301
}
298302

299303
#[test]

0 commit comments

Comments
 (0)