Skip to content

Commit

Permalink
test: separate detection of overlay support from every stacker run.
Browse files Browse the repository at this point in the history
In unpriv test mode, we were running 'stacker testsuite-check-overlay'
every time we ran stacker.

That was causing me confusion and also is just wasteful, as the ability
to use overlay does not change within the course of a test run.

This introduces 'suite_setup' which ideally would be used by the bats
'suite_setup' functionality.  However, the Ubuntu 22.04 verison of bats
does not *have* that functionalty.

When run with '-j', this code will run more than once, but will worst
case only run once per test, rather than once per invocation of
'stacker' (some tests invoke stacker many times).

Also, adjust output of run_stacker function to be more useful
than just "Debug mode: "

Signed-off-by: Scott Moser <smoser@brickies.net>
Signed-off-by: Serge E. Hallyn <serge@hallyn.com>
  • Loading branch information
smoser authored and hallyn committed Jan 8, 2024
1 parent 68bae2b commit c1c854a
Showing 1 changed file with 93 additions and 21 deletions.
114 changes: 93 additions & 21 deletions test/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ if [ "$(id -u)" != "0" ]; then
fi

function skip_if_no_unpriv_overlay {
run sudo -u $SUDO_USER "${ROOT_DIR}/stacker" --debug internal-go testsuite-check-overlay
echo $output
[ "$status" -eq 50 ] && skip "need newer kernel for unpriv overlay"
[ "$status" -eq 0 ]
case "$UNPRIV_OVERLAY_SUPPORT" in
true) return 0;;
false) skip "need newer kernel for unpriv overlay";;
"") echo "UNPRIV_OVERLAY_SUPPORT was not set; stacker_setup must be called";;
*) echo "UNPRIV_OVERLAY_SUPPORT set to unexpected value '$UNPRIV_OVERLAY_SUPPORT'";;
esac
exit 1
}

function run_stacker {
echo "Debug mode: $NO_DEBUG"
# add --debug unless NO_DEBUG=1
if [ "$NO_DEBUG" != "1" ]; then
set -- --debug "$@"
fi

if [ "$PRIVILEGE_LEVEL" = "priv" ]; then
if [[ -n "$NO_DEBUG" && "$NO_DEBUG" = 1 ]]; then
run "${ROOT_DIR}/stacker" "$@"
else
run "${ROOT_DIR}/stacker" --debug "$@"
fi
set -- run "${ROOT_DIR}/stacker" "$@"
else
skip_if_no_unpriv_overlay
if [[ -n "$NO_DEBUG" && "$NO_DEBUG" = 1 ]]; then
run sudo -u $SUDO_USER "${ROOT_DIR}/stacker" "$@"
else
run sudo -u $SUDO_USER "${ROOT_DIR}/stacker" --debug "$@"
fi
set -- run sudo -u "$SUDO_USER" "${ROOT_DIR}/stacker" "$@"
fi
echo "execute stacker as uid=$(id -u):" "$@"
"$@"
}

function image_copy {
Expand Down Expand Up @@ -61,16 +62,87 @@ function sha() {
echo $(sha256sum $1 | cut -f1 -d" ")
}

function stacker_setup() {
export TEST_TMPDIR=$(tmpd $BATS_TEST_NAME)
cd $TEST_TMPDIR
# with a newer bats version, this would be the suite_setup function.
# with Ubuntu 22.04 version (1.21) we have to abuse BATS_RUN_TMPDIR
# and an atomically written state file. The setup could be run more
# than once due to TOCTOU race but the result should be the same each
# time and if the file is sourced it should be consistent.
function suite_setup() {
local statef="${BATS_RUN_TMPDIR}/stacker_suite_setup.state"

if [ "$PRIVILEGE_LEVEL" = "priv" ]; then
if source "$statef" >/dev/null 2>&1; then
echo "[$$/${BATS_TEST_NAME}] used existing $statef"
return
fi

"${ROOT_DIR}/stacker" unpriv-setup
chown -R $SUDO_USER:$SUDO_USER .
echo "[$$/${BATS_TEST_NAME}] doing suite_setup"
local startd="$PWD" workd="" setupd=""
workd=$(tmpd suite-setup)
cd "$workd"
give_user_ownership "$workd"

if [ "$PRIVILEGE_LEVEL" = "unpriv" ]; then
if [ -z "$UNPRIV_SETUP" ]; then
setupd="$workd/unprivsetup-d"
mkdir "$setupd"
give_user_ownership "$setupd"
cd "$setupd"
run "${ROOT_DIR}/stacker" unpriv-setup
if [ $status -ne 0 ]; then
echo "$output"
echo "Failed unpriv-setup"
exit 1
fi
UNPRIV_SETUP="done"
cd "$workd"
rm -Rf "$setupd"
fi

if [ -z "$UNPRIV_OVERLAY_SUPPORT" ]; then
setupd="$workd/unpriv-check-overlay-d"
mkdir "$setupd"
give_user_ownership "$setupd"
cd "$setupd"
run sudo -u "$SUDO_USER" "${ROOT_DIR}/stacker" \
--debug internal-go testsuite-check-overlay
case "$status" in
50) UNPRIV_OVERLAY_SUPPORT=false;;
0) UNPRIV_OVERLAY_SUPPORT=true;;
*) echo "testsuite-check-overlay exited with unexpected value $status"
echo "$output"
exit 1;;
esac
cd "$workd"
rm -Rf "$setupd"
fi
fi

printf "%s\n%s\n" \
"UNPRIV_OVERLAY_SUPPORT=${UNPRIV_OVERLAY_SUPPORT}" \
"UNPRIV_SETUP=${UNPRIV_SETUP}" \
> "$statef.$$"

mv "$statef.$$" "$statef"
cd "$startd"
}

function stacker_setup() {
suite_setup

export TEST_TMPDIR=$(tmpd $BATS_TEST_NAME)
cd "$TEST_TMPDIR"
give_user_ownership .
}

function give_user_ownership() {
if [ "$PRIVILEGE_LEVEL" = "priv" ]; then
return
fi
if [ -z "$SUDO_UID" ]; then
echo "PRIVILEGE_LEVEL=$PRIVILEGE_LEVEL but empty SUDO_USER"
exit 1
fi
chown -R "$SUDO_USER:$SUDO_USER" "$@"
}

function cleanup() {
Expand Down

0 comments on commit c1c854a

Please sign in to comment.