1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
summary: Check that `snap run` can actually run hooks
details: |
Verifies the `snap run` can run snap hooks. Also check
`snap run` cannot call invalid hooks.
environment:
# Ensure that running purely from the deb (without re-exec) works
# correctly
SNAP_REEXEC/reexec0: 0
SNAP_REEXEC/reexec1: 1
ENVDUMP: /var/snap/basic-hooks/current/hooks-env
prepare: |
if [ "$SNAP_REEXEC" = "0" ] && tests.info is-snapd-from-archive; then
tests.exec skip-test "No need to test when the snapd pkg is from the repository and reexec is disabled" && exit 0
fi
"$TESTSTOOLS"/snaps-state install-local basic-hooks
SNAP_MOUNT_DIR="$(os.paths snap-mount-dir)"
if [ "$SNAP_REEXEC" = "1" ] && [ "$SNAP_MOUNT_DIR" != "/snap" ] && [ ! -L /snap ]; then
# reexec expects to find the snapd snap under /snap, enable it to do so
# on distros using /var/lib/snapd/snap
ln -sf "$SNAP_MOUNT_DIR" /snap
tests.cleanup defer rm -f /snap
fi
execute: |
tests.exec is-skipped && exit 0
# Note that `snap run` doesn't exit non-zero if the hook is missing, so we
# check the output instead.
echo "Test that snap run can call valid hooks"
if ! output="$(snap run --hook=configure basic-hooks)"; then
echo "Failed to run configure hook"
exit 1
fi
expected_output="configure hook"
if [ "$output" != "$expected_output" ]; then
echo "Expected configure output to be '$expected_output', but it was '$output'"
exit 1
fi
echo "Test that snap run cannot call invalid hooks"
if output="$(snap run --hook=invalid-hook basic-hooks)"; then
echo "Expected snap run to fail upon missing hook, but it was '$output'"
exit 1
fi
expected_output=""
if [ "$output" != "$expected_output" ]; then
echo "Expected invalid-hook output to be '$expected_output', but it was '$output'"
exit 1
fi
snap set basic-hooks command=dump-env
echo "Test that environment variables were interpolated"
MATCH "^TEST_COMMON=/var/snap/basic-hooks/common$" < "$ENVDUMP"
MATCH "^TEST_DATA=/var/snap/basic-hooks/.*$" < "$ENVDUMP"
MATCH "^TEST_SNAP=/snap/basic-hooks/.*$" < "$ENVDUMP"
MATCH "^TEST_EXTRA=extra-stuff$" < "$ENVDUMP"
|