Skip to content

Commit

Permalink
mkcw: mkcw_check_image use bats run_with_log
Browse files Browse the repository at this point in the history
Add `run_with_log` to mkcw tests.

Add `sleep 1` during cleanup between attempting `luksClose`
and unmounting the filesystem mounted on the device /dev/mapper/"$uuid".
Without this somehow we end up in a state where mount is still being
used by the kernel because when we do `lsof /dev/mapper/"$uuid"` it
shows nothing but `dmsetup info -c $uuid` shows the device is still
under use. Adding `sleep 1` in between somehow fixes this.

Also this problem with `cryptsetup` is pretty common for reference
one thread which I found https://lore.kernel.org/all/[email protected]/T/

Signed-off-by: flouthoc <[email protected]>
  • Loading branch information
flouthoc committed Feb 17, 2025
1 parent 15712b5 commit 391ba38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
43 changes: 43 additions & 0 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,49 @@ function rm() {
run_unshared rm "$@"
}

#################
# run_with_log # Logs command before running it
#################
#
function run_with_log() {
local expected_rc=0
local retry=1
local cmd="$*"
case "$1" in
[0-9]) expected_rc=$1; shift;;
[1-9][0-9]) expected_rc=$1; shift;;
[12][0-9][0-9]) expected_rc=$1; shift;;
'?') expected_rc= ; shift;; # ignore exit code
--retry) retry=3; shift;; # retry with sleep of 1 sec
esac
while [ $retry -gt 0 ]; do
retry=$(( retry - 1 ))
echo "$_LOG_PROMPT $cmd"
run "$@"
echo "$output"
if [ "$status" -ne 0 ]; then
echo -n "[ rc=$status ";
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
echo -n "(expected) ";
else
echo -n "(** EXPECTED $expected_rc **) ";
fi
fi
echo "]"
fi
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
return
elif [ $retry -gt 0 ]; then
echo "[ RETRYING ]" >&2
sleep 1
else
die "exit code is $status; expected $expected_rc"
fi
fi
done
}

#################
# run_buildah # Invoke buildah, with timeout, using BATS 'run'
Expand Down
16 changes: 11 additions & 5 deletions tests/mkcw.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function mkcw_check_image() {

# Decrypt, mount, and take a look around.
uuid=$(cryptsetup luksUUID "$mountpoint"/disk.img)
cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
run_with_log cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
mkdir -p "$TEST_SCRATCH_DIR"/mount
mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
run_with_log mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
# Should have a not-empty config file with parts of an image's config.
test -s "$TEST_SCRATCH_DIR"/mount/.krun_config.json
# Should have a /tmp directory, at least.
Expand All @@ -42,9 +42,15 @@ function mkcw_check_image() {
fi

# Clean up.
umount "$TEST_SCRATCH_DIR"/mount
cryptsetup luksClose "$uuid"
buildah umount "$ctrID"
run_with_log umount -f "$TEST_SCRATCH_DIR"/mount
# `Retry` if `luksClose` fails with defaults of `run_with_log` because
# when unmounting the filesystem mounted on the device /dev/mapper/"$uuid"
# without `retry` somehow we end up in a state where mount is still being
# used by the kernel because when we do `lsof /dev/mapper/"$uuid"` it
# shows nothing but `dmsetup info -c $uuid` shows the device is still
# under use. Adding `--retry` in between somehow fixes this.
run_with_log --retry cryptsetup luksClose "$uuid"
run_buildah umount "$ctrID"
}

@test "mkcw-convert" {
Expand Down

0 comments on commit 391ba38

Please sign in to comment.