From 52e4a2e3046172c3ac5821646c04920085801d08 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Charaoui?= <jerome@riseup.net>
Date: Sun, 12 Feb 2023 12:51:25 -0500
Subject: [PATCH] improve robustness of sys tests

---
 test/sys.bats | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/test/sys.bats b/test/sys.bats
index 7e1b67f..24f424c 100644
--- a/test/sys.bats
+++ b/test/sys.bats
@@ -25,6 +25,8 @@ EOF
     # Setup LUKS
     parted -s /dev/sdd mklabel msdos mkpart p 1MiB 50% mkpart p 50% 100%
     partprobe
+    test -b /dev/sdd1
+    test -b /dev/sdd2
     cryptsetup -q --type luks1 luksFormat /dev/sdd1 <<< 123test
     cryptsetup -q --type luks2 luksFormat /dev/sdd2 <<< 123test
     cryptsetup -q --type luks2 luksFormat /dev/sde <<< 123test
@@ -37,12 +39,11 @@ finish_sys() {
     lvremove -f vgtest/lvtest
     vgremove vgtest
     pvremove /dev/sdc
-    # cleanup luks data and partition tables
-    dd if=/dev/zero of=/dev/sdc bs=512 count=1 conv=notrunc
-    dd if=/dev/zero of=/dev/sdd1 bs=512 count=2048 conv=notrunc
-    dd if=/dev/zero of=/dev/sdd2 bs=512 count=2048 conv=notrunc
-    dd if=/dev/zero of=/dev/sdd bs=512 count=1 conv=notrunc
-    dd if=/dev/zero of=/dev/sde bs=512 count=2048 conv=notrunc
+    # cleanup luks headers and partition tables
+    for d in /dev/sdc /dev/sdd1 /dev/sdd2 /dev/sdd /dev/sde; do
+        cryptsetup isLuks $dev && cryptsetup luksErase $dev <<< YES
+        test -b $dev && dd if=/dev/zero of=$dev bs=512 count=2048 conv=notrunc
+    done
     partprobe
 }
 
-- 
GitLab