Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: new approach to run tests in ubuntu and debian using snapd deb from the repo #14496

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bb15877
tests: new approach to run tests in ubuntu and debian using snapd deb…
sergiocazzolato Sep 12, 2024
a2637fd
Squashed 'tests/lib/external/snapd-testing-tools/' changes from 1c8ef…
sergiocazzolato Sep 12, 2024
63b2c2f
Merge commit 'a2637fda70a9d9f9fdc3d299712da9090735223f' into tests-up…
sergiocazzolato Sep 12, 2024
d1f0f31
download snapd snap-confine and ubuntu-core-launcher
sergiocazzolato Sep 12, 2024
6d5d458
skip tests that trust on deb pkg testkeys
sergiocazzolato Sep 16, 2024
ab0b03f
fix shellcheck
sergiocazzolato Sep 17, 2024
3c76431
revert kill timeout
sergiocazzolato Sep 17, 2024
7d78737
fix tests using snapd.tool and store-state test
sergiocazzolato Sep 18, 2024
87cbe02
new tests.info tool
sergiocazzolato Sep 19, 2024
1338735
skip tests which are testing with re-exec:0 and snap is comming from …
sergiocazzolato Sep 19, 2024
b7d5efb
update the systems where snapd comes from the repo
sergiocazzolato Sep 19, 2024
d9aa6db
skip another test when re-exec:0
sergiocazzolato Sep 20, 2024
3f2209b
Merge branch 'master' into tests-update-snapd-package
sergiocazzolato Sep 20, 2024
5e50960
Merge commit 'b0f4d8daff730868b7a60aebf75aac53c24b92c7' into tests-up…
sergiocazzolato Sep 20, 2024
b0f4d8d
Squashed 'tests/lib/external/snapd-testing-tools/' changes from 33c1d…
sergiocazzolato Sep 20, 2024
6cb8cb6
Adjust failing test to new re-exec scenario
sergiocazzolato Sep 23, 2024
9ddfe30
fix other 2 tests using correct paths
sergiocazzolato Sep 23, 2024
06f3a15
Squashed 'tests/lib/external/snapd-testing-tools/' changes from 91213…
sergiocazzolato Sep 23, 2024
c589280
Merge commit '06f3a157d82cfa92350c7d9a0634690d05874764' into tests-up…
sergiocazzolato Sep 23, 2024
73548a7
Merge branch 'master' into tests-update-snapd-package
sergiocazzolato Sep 23, 2024
744c89a
some tests fixes
sergiocazzolato Sep 23, 2024
2e32c65
New set of test fixes
sergiocazzolato Sep 24, 2024
514dbb8
update tests info command to check re-execution
sergiocazzolato Sep 24, 2024
ec96de4
fix uc16 which preparing
sergiocazzolato Sep 24, 2024
e048240
check SPREAD_SYSTEM is not core in tests.info
sergiocazzolato Sep 24, 2024
cb9b585
use .skip in tests to determine if a phase has to be skipped
sergiocazzolato Sep 25, 2024
463424e
skip updating core snap when the snap pkg is not built from local
sergiocazzolato Sep 26, 2024
5758688
save installed core snap when snap deb is used from the repo
sergiocazzolato Sep 26, 2024
cd751ac
copy also .orig core snap
sergiocazzolato Sep 27, 2024
8e5bfcb
change how we copy the core snap
sergiocazzolato Sep 27, 2024
d7f37b9
change the snapname used for core snap
sergiocazzolato Sep 27, 2024
1624ebf
skip saving the core snap
sergiocazzolato Sep 27, 2024
5e1e520
Fix error using snapcraft for testing in arm
sergiocazzolato Sep 27, 2024
37487c1
make sure fips tests dont run with reexec=0 and snapd.deb from the repo
sergiocazzolato Sep 27, 2024
c0ccc68
simplify how fips tests are skipped
sergiocazzolato Sep 27, 2024
b14c80d
fix indentation on test
sergiocazzolato Sep 27, 2024
cde70a7
Make sure we save core snap in TESTSTMP/core_snap
sergiocazzolato Sep 27, 2024
b50e533
use tests.exec tool to skip tests
sergiocazzolato Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ environment:
SNAPD_NO_MEMORY_LIMIT: '$(HOST: echo "${SPREAD_SNAPD_NO_MEMORY_LIMIT:-}")'

SNAPD_PUBLISHED_VERSION: '$(HOST: echo "$SPREAD_SNAPD_PUBLISHED_VERSION")'
sergiocazzolato marked this conversation as resolved.
Show resolved Hide resolved
# Use the snapd package from the repository when possible
SNAPD_DEB_FROM_REPO: '$(HOST: echo "${SPREAD_SNAPD_DEB_FROM_REPO:-true}")'
# Build and use snapd from current branch
BUILD_SNAPD_FROM_CURRENT: '$(HOST: echo "${SPREAD_BUILD_SNAPD_FROM_CURRENT:-true}")'

Expand Down Expand Up @@ -1430,6 +1432,10 @@ suites:
"$TESTSLIB"/prepare-restore.sh --prepare-suite
prepare-each: |
"$TESTSLIB"/prepare-restore.sh --prepare-suite-each
# When snapd.deb is from the repo, we just need to test when re-exec is enabled
if [ "$SNAP_REEXEC" = "0" ] && tests.info is-snapd-pkg-repo; then
tests.exec skip-test "When snapd.deb is from the repo, we just need to test when re-exec is enabled" && exit 0
fi
restore-each: |
"$TESTSLIB"/prepare-restore.sh --restore-suite-each
restore: |
Expand Down
1 change: 1 addition & 0 deletions tests/bin/tests.exec
1 change: 1 addition & 0 deletions tests/bin/tests.info
7 changes: 6 additions & 1 deletion tests/lib/prepare-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,12 @@ prepare_project() {
# go mod runs as root and will leave strange permissions
chown test:test -R "$SPREAD_PATH"

if [ "$BUILD_SNAPD_FROM_CURRENT" = true ]; then
# We are testing snapd snap on top of snapd from the archive
# of the tested distribution. Download snapd and snap-confine
# as they exist in the archive for further use.
if tests.info is-snapd-pkg-repo; then
sergiocazzolato marked this conversation as resolved.
Show resolved Hide resolved
( cd "${GOHOME}" && tests.pkgs download snapd snap-confine)
elif [ "$BUILD_SNAPD_FROM_CURRENT" = true ]; then
case "$SPREAD_SYSTEM" in
ubuntu-*|debian-*)
build_deb
Expand Down
37 changes: 32 additions & 5 deletions tests/lib/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ setup_experimental_features() {
fi
}

save_installed_core_snap() {
local target_dir="${1-}"

SNAP_MOUNT_DIR="$(os.paths snap-mount-dir)"
LIBEXEC_DIR="$(os.paths libexec-dir)"
core="$(readlink -f "$SNAP_MOUNT_DIR/core/current")"
snap="$(mount | grep " $core" | head -n 1 | awk '{print $1}')"
snap_name="$(basename "$snap")"

# make a copy for later use
if [ -n "$target_dir" ]; then
mkdir -p "$target_dir"

cp -av "$snap" "${target_dir}/${snap_name}"
cp "$snap" "${target_dir}/${snap_name}.orig"
fi
}


# update_core_snap_for_classic_reexec modifies the core snap for snapd re-exec
# by injecting binaries from the installed snapd deb built from our modified code.
# $1: directory where updated core snap should be copied (optional)
Expand Down Expand Up @@ -450,10 +469,17 @@ prepare_classic() {

snap list | grep core

systemctl stop snapd.{service,socket}
# repack and also make a side copy of the core snap
update_core_snap_for_classic_reexec "$TESTSTMP/core_snap"
systemctl start snapd.{service,socket}
# With reexec, and on classic, the snapd snap is preferred over the core snap for reexecution target,
# so to be as close as possible to the actual real life scenarios, we only update the snapd snap.
# The tests alreday ensure that snapd snap is installed.
if tests.info is-snapd-pkg-repo; then
save_installed_core_snap "$TESTSTMP/core_snap"
else
systemctl stop snapd.{service,socket}
# repack and also make a side copy of the core snap
update_core_snap_for_classic_reexec "$TESTSTMP/core_snap"
systemctl start snapd.{service,socket}
fi

prepare_reexec_override
prepare_memory_limit_override
Expand Down Expand Up @@ -547,6 +573,7 @@ build_snapd_snap() {
;;
esac
[ -d "${TARGET}" ] || mkdir -p "${TARGET}"
touch "${PROJECT_PATH}"/test-build
sergiocazzolato marked this conversation as resolved.
Show resolved Hide resolved
chmod -R go+r "${PROJECT_PATH}/tests"
# TODO: run_snapcraft does not currently guarantee or check the required version for building snapd
run_snapcraft --use-lxd --verbosity quiet --output="snapd_from_snapcraft.snap"
Expand Down Expand Up @@ -1192,7 +1219,7 @@ setup_reflash_magic() {
# FIXME: install would be better but we don't have dpkg on
# the image
# unpack our freshly build snapd into the new snapd snap
dpkg-deb -x "$SPREAD_PATH"/../snapd_*.deb "$UNPACK_DIR"
dpkg-deb -x "$GOHOME"/snapd_*.deb "$UNPACK_DIR"
# Debian packages don't carry permissions correctly and we use
# post-inst hooks to fix that on classic systems. Here, as a special
# case, fix the void directory we just unpacked.
Expand Down
11 changes: 9 additions & 2 deletions tests/lib/tools/snapd.tool
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ main() {
esac
done

LIBEXEC_DIR="$(os.paths libexec-dir)"
exec "$LIBEXEC_DIR/snapd/$tool" "$@"
# Use the tool from the snapd snap when re-exec is enabled
# Otherwise use the tool from the distro installation
if tests.info is-reexec-in-use; then
MOUNT_DIR="$(os.paths snap-mount-dir)"
exec "$MOUNT_DIR"/snapd/current/usr/lib/snapd/"$tool" "$@"
else
LIBEXEC_DIR="$(os.paths libexec-dir)"
exec "$LIBEXEC_DIR/snapd/$tool" "$@"
fi
}

main "$@"
58 changes: 58 additions & 0 deletions tests/lib/tools/tests.exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -eu

show_help() {
echo "usage: tests.exec skip-test [MSG]"
echo "usage: tests.exec is-skipped"
echo
echo "Supported commands:"
echo " skip-test: indicates the test has to be skipped and saves the raeson"
echo " is-skipped: check if the test has to be skipped and prints the raeson"
}

skip_test() {
local raeson="${1:-}"
echo "$raeson" > tests.exec
}

is_skipped() {
if [ -f tests.exec ]; then
echo "skip raeson: $(cat tests.exec)"
return 0
fi
return 1
}

main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

local subcommand="$1"
local action=
while [ $# -gt 0 ]; do
case "$subcommand" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done

if [ -z "$(declare -f "$action")" ]; then
echo "tests.exec: no such command: $subcommand"
show_help
exit 1
fi

"$action" "$@"
}

main "$@"
60 changes: 60 additions & 0 deletions tests/lib/tools/tests.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -eu

zyga marked this conversation as resolved.
Show resolved Hide resolved
show_help() {
echo "usage: tests.info <CMD>"
echo
echo "Supported commands:"
echo " is-snapd-pkg-repo: indicates when the snapd package is from the repository"
echo " is-reexec-enabled: indicates re-execution has been explicitly enabled through the env"
echo " is-reexec-in-use: indicates re-execution to the snapd snap is being effectively performed"
}

is_snapd_pkg_repo() {
[ "$SNAPD_DEB_FROM_REPO" = true ] && \
[ -n "$(command -v apt)" ] && \
[[ "$SPREAD_SYSTEM" != ubuntu-core-* ]] && \
not os.query is-trusty
}

is_reexec_enabled() {
snap debug execution snap | grep -q "is-reexec-enabled: true"
}

is_reexec_in_use() {
snap debug execution snap | grep -q "is-reexecd: true"
sergiocazzolato marked this conversation as resolved.
Show resolved Hide resolved
}

main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

local subcommand="$1"
local action=
while [ $# -gt 0 ]; do
case "$subcommand" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done

if [ -z "$(declare -f "$action")" ]; then
echo "tests.info: no such command: $subcommand"
show_help
exit 1
fi

"$action" "$@"
}

main "$@"
17 changes: 6 additions & 11 deletions tests/main/classic-custom-device-reg/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ environment:
SEED_DIR: /var/lib/snapd/seed

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
# In this scenario, the keys from the snapd pkg are used
if [ "$TRUST_TEST_KEYS" = "false" ] || tests.info is-snapd-pkg-repo; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking if this is really the case. Snapd classic will re-exec into snapd snap that may be built with test keys. Does it really fail?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some scenarios where we don't have snapd snap installed, so it uses the keys from the deb.
I'll add a comment there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this sounds good. If We identify all the cases that don't have snapd snap installed then we can work on fixing them. I assume that the point of the test was not to test that specific behavior and that we can fix them over time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, my idea is to create a following pr to cleanup some scenarios and fix other tests

tests.exec skip-test "This test needs test keys to be trusted" && exit 0
fi

snap pack "$TESTSLIB/snaps/classic-gadget"
Expand Down Expand Up @@ -52,20 +52,15 @@ prepare: |
systemd-run --unit fakedevicesvc fakedevicesvc localhost:11029

restore: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

systemctl stop snapd.service snapd.socket fakedevicesvc

rm -rf "$SEED_DIR"
systemctl start snapd.socket snapd.service

execute: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

#shellcheck source=tests/lib/core-config.sh
. "$TESTSLIB"/core-config.sh
Expand Down
16 changes: 5 additions & 11 deletions tests/main/classic-firstboot/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ environment:
SEED_DIR: /var/lib/snapd/seed

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
# In this scenario, the keys from the snapd pkg are used
if [ "$TRUST_TEST_KEYS" = "false" ] || tests.info is-snapd-pkg-repo; then
tests.exec skip-test "This test needs test keys to be trusted" && exit 0
fi

snap pack "$TESTSLIB/snaps/basic"
Expand Down Expand Up @@ -51,19 +51,13 @@ prepare: |
cp ./test-snapd-service_1.0_all.snap "$SEED_DIR/snaps/test-snapd-service.snap"

restore: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

rm -rf "$SEED_DIR"
systemctl start snapd.socket snapd.service

execute: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

echo "Start the daemon with an empty state, this will make it import "
echo "assertions from the $SEED_DIR/assertions subdirectory and "
Expand Down
17 changes: 6 additions & 11 deletions tests/main/classic-prepare-image-no-core/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ environment:
SEED_DIR: /var/lib/snapd/seed

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
# In this scenario, the keys from the snapd pkg are used
if [ "$TRUST_TEST_KEYS" = "false" ] || tests.info is-snapd-pkg-repo; then
tests.exec skip-test "This test needs test keys to be trusted" && exit 0
fi

"$TESTSTOOLS"/store-state setup-fake-store "$STORE_DIR"
Expand All @@ -49,10 +49,8 @@ prepare: |
systemd-run --unit fakedevicesvc fakedevicesvc localhost:11029

restore: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

systemctl stop snapd.service snapd.socket fakedevicesvc

rm -rf "$SEED_DIR"
Expand All @@ -61,10 +59,7 @@ restore: |
rm -rf "$ROOT"

execute: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

#shellcheck source=tests/lib/core-config.sh
. "$TESTSLIB"/core-config.sh
Expand Down
17 changes: 6 additions & 11 deletions tests/main/classic-prepare-image/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ environment:
SEED_DIR: /var/lib/snapd/seed

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
# In this scenario, the keys from the snapd pkg are used
if [ "$TRUST_TEST_KEYS" = "false" ] || tests.info is-snapd-pkg-repo; then
tests.exec skip-test "This test needs test keys to be trusted" && exit 0
fi

"$TESTSTOOLS"/store-state setup-fake-store "$STORE_DIR"
Expand All @@ -48,10 +48,8 @@ prepare: |
systemd-run --unit fakedevicesvc fakedevicesvc localhost:11029

restore: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

systemctl stop snapd.service snapd.socket fakedevicesvc

rm -rf "$SEED_DIR"
Expand All @@ -60,10 +58,7 @@ restore: |
rm -rf "$ROOT"

execute: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
tests.exec is-skipped && exit 0

#shellcheck source=tests/lib/core-config.sh
. "$TESTSLIB"/core-config.sh
Expand Down
Loading
Loading