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 15, 2025
1 parent 66ff8ff commit e1563d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Binary file modified internal/mkcw/embed/entrypoint_amd64.gz
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ function rm() {
run_unshared rm "$@"
}

#################
# run_with_log # Logs command before running it
#################
#
function run_with_log() {
local expected_rc=0
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
esac
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
else
die "exit code is $status; expected $expected_rc"
fi
fi
}

#################
# run_buildah # Invoke buildah, with timeout, using BATS 'run'
Expand Down
11 changes: 6 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,10 @@ 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
run_with_log sleep 1
run_with_log cryptsetup luksClose "$uuid"
run_buildah umount "$ctrID"
}

@test "mkcw-convert" {
Expand Down

0 comments on commit e1563d0

Please sign in to comment.