From 7172083cf2c06e40869e8364f92d5da7ea4014ce Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Fri, 4 Oct 2024 22:24:10 -0300 Subject: [PATCH] updated failing tests ude to new re-execution mechanism --- spread.yaml | 2 + tests/lib/prepare.sh | 23 ++++++----- tests/main/preseed-lxd/task.yaml | 44 ++++++++++++++++----- tests/main/snap-confine-tmp-mount/task.yaml | 3 +- tests/main/snapd-reexec/task.yaml | 38 +++++++++++++----- tests/main/user-session-env/task.yaml | 2 +- 6 files changed, 78 insertions(+), 34 deletions(-) diff --git a/spread.yaml b/spread.yaml index b04ebe7b8af..f8e3294ae75 100644 --- a/spread.yaml +++ b/spread.yaml @@ -86,6 +86,8 @@ environment: 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}")' + # Directory where the built snapd snaps and other assets are stored + SNAPD_WORK_DIR: '$(HOST: echo "${SPREAD_SNAPD_WORK_DIR:-/tmp/work-dir}")' # Directory where the nested images and test assets are stored NESTED_WORK_DIR: '$(HOST: echo "${NESTED_WORK_DIR:-/tmp/work-dir}")' diff --git a/tests/lib/prepare.sh b/tests/lib/prepare.sh index 4a578381493..55c34c2418e 100755 --- a/tests/lib/prepare.sh +++ b/tests/lib/prepare.sh @@ -9,8 +9,6 @@ set -eux # shellcheck source=tests/lib/state.sh . "$TESTSLIB/state.sh" -: "${WORK_DIR:=/tmp/work-dir}" - disable_kernel_rate_limiting() { # kernel rate limiting hinders debugging security policy so turn it off echo "Turning off kernel rate-limiting" @@ -420,15 +418,16 @@ prepare_classic() { # This also prevents snapd from automatically installing snapd snap as # prerequisite for installing any non-base snap introduced in PR#14173. if snap list snapd ; then - snap info snapd - echo "Error: not expecting snapd snap to be installed" - exit 1 + snap info snapd + echo "Error: not expecting snapd snap to be installed" + exit 1 else - build_dir="$WORK_DIR/snapd_snap_for_classic" - rm -rf "$build_dir" - mkdir -p "$build_dir" - build_snapd_snap "$build_dir" - snap install --dangerous "$build_dir/"snapd_*.snap + build_dir="$SNAPD_WORK_DIR/snapd_snap_for_classic" + rm -rf "$build_dir" + mkdir -p "$build_dir" + build_snapd_snap "$build_dir" + snap install --dangerous "$build_dir/"snapd_*.snap + snap wait system seed.loaded fi snap list snapd @@ -549,7 +548,7 @@ build_snapd_snap() { local snapd_snap_cache TARGET="${1}" - snapd_snap_cache="$WORK_DIR/snapd_snap" + snapd_snap_cache="$SNAPD_WORK_DIR/snapd_snap" mkdir -p "${snapd_snap_cache}" for snap in "${snapd_snap_cache}"/snapd_*.snap; do if ! [ -f "${snap}" ]; then @@ -592,7 +591,7 @@ build_snapd_snap_with_run_mode_firstboot_tweaks() { TARGET="${1}" - snapd_snap_cache="$WORK_DIR/snapd_snap_with_tweaks" + snapd_snap_cache="$SNAPD_WORK_DIR/snapd_snap_with_tweaks" mkdir -p "${snapd_snap_cache}" for snap in "${snapd_snap_cache}"/snapd_*.snap; do if [ -f "${snap}" ]; then diff --git a/tests/main/preseed-lxd/task.yaml b/tests/main/preseed-lxd/task.yaml index 699ab671049..6a60c8e180a 100644 --- a/tests/main/preseed-lxd/task.yaml +++ b/tests/main/preseed-lxd/task.yaml @@ -106,21 +106,45 @@ execute: | lxc config device add my-ubuntu mounted-ubuntu-image disk source="$IMAGE_MOUNTPOINT" path="$IMAGE_MOUNTPOINT" lxc restart my-ubuntu - echo "Make latest snap-preseed command available in the lxd container" + echo "Prepare container to run snap-preseed command" lxc exec my-ubuntu -- mkdir -p "$GOHOME" - SNAP_PRESEED=/usr/lib/snapd/snap-preseed - if tests.info is-snapd-pkg-repo; then - MOUNT_DIR="$(os.paths snap-mount-dir)" - SNAP_PRESEED="$MOUNT_DIR/snapd/current$SNAP_PRESEED" - fi - lxc file push "$SNAP_PRESEED" "my-ubuntu/$GOHOME/" lxc file push cloudimg.img "my-ubuntu/$GOHOME/" lxc file push preseed-prepare.sh "my-ubuntu/$GOHOME/" - - echo "Running preseeding in the lxd container" # mount /dev, /proc, /sys under mounted ubuntu image in the container. lxc exec my-ubuntu -- "$GOHOME"/preseed-prepare.sh "$IMAGE_MOUNTPOINT" - lxc exec my-ubuntu -- "$GOHOME"/snap-preseed "$IMAGE_MOUNTPOINT" + + echo "Make latest snap-preseed command available in the lxd container" + echo "Running preseeding in the lxd container" + SNAP_PRESEED=/usr/lib/snapd/snap-preseed + if tests.info is-snapd-pkg-repo; then + # We use the snap preseed from the current snapd snap + # The current snapd snap is installed in the lxd container to make + # sure all the dependencies are in place, otherwise it fails to access to + # /snap/snapd/current/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 + + CURRENT_SNAPD_SNAP="$(ls $SNAPD_WORK_DIR/snapd_snap/snapd_*.snap)" + CURRENT_SNAPD_SNAP_NAME="$(basename $CURRENT_SNAPD_SNAP)" + if [ "$(wc -w <<< $CURRENT_SNAPD_SNAP)" != 1 ]; then + echo "It is expected just 1 snapd snap in $SNAPD_WORK_DIR/snapd_snap" + exit 1 + fi + lxc file push "$CURRENT_SNAPD_SNAP" "my-ubuntu/$GOHOME/" + for iter in $(seq 30); do + echo "checking snapd ready in container: iter $iter" + if lxc exec my-ubuntu -- sh -c 'snap changes' | MATCH "Done.*Initialize system state"; then + break + fi + sleep 1 + done + lxc exec my-ubuntu -- snap install --dangerous "$GOHOME/$CURRENT_SNAPD_SNAP_NAME" + + MOUNT_DIR="$(os.paths snap-mount-dir)" + SNAP_PRESEED="$MOUNT_DIR/snapd/current$SNAP_PRESEED" + lxc exec my-ubuntu -- "$SNAP_PRESEED" "$IMAGE_MOUNTPOINT" + else + lxc file push "$SNAP_PRESEED" "my-ubuntu/$GOHOME/" + lxc exec my-ubuntu -- "$GOHOME"/snap-preseed "$IMAGE_MOUNTPOINT" + fi echo "Precondition check of mount units" # preseeded image is mounted on the host, so we can directly inspect diff --git a/tests/main/snap-confine-tmp-mount/task.yaml b/tests/main/snap-confine-tmp-mount/task.yaml index bc5fbd12dbf..32468d90504 100644 --- a/tests/main/snap-confine-tmp-mount/task.yaml +++ b/tests/main/snap-confine-tmp-mount/task.yaml @@ -24,9 +24,10 @@ debug: | cat /tmp/snap-confine-stderr.log || true execute: | + MOUNT_DIR="$(os.paths snap-mount-dir)" SNAP_CONFINE=$(os.paths libexec-dir)/snapd/snap-confine if tests.info is-snapd-pkg-repo; then - SNAP_CONFINE="/snap/snapd/current$SNAP_CONFINE" + SNAP_CONFINE="$MOUNT_DIR/snapd/current$SNAP_CONFINE" fi # on Ubuntu Core we need to use the correct path to ensure it is diff --git a/tests/main/snapd-reexec/task.yaml b/tests/main/snapd-reexec/task.yaml index c09a1627a2a..8c7f9d6b726 100644 --- a/tests/main/snapd-reexec/task.yaml +++ b/tests/main/snapd-reexec/task.yaml @@ -77,7 +77,19 @@ execute: | fi echo "Ensure we re-exec by default" - /usr/bin/env SNAPD_DEBUG=1 snap list 2>&1 | MATCH "DEBUG: restarting into \"$SNAPD_MOUNT_DIR/current/usr/bin/snap\"" + IS_REEXEC=true + if [ "$SNAPD_SRC" = core ] && tests.info is-snapd-pkg-repo; then + # In this scenario it is possible that the core snap version is older + # than the distribution package because the core snap comes from latest/edge + # which is not updated regularly. + # TODO: Compare manually the snap and deb versions and determine which string needs to be in MATCH + if /usr/bin/env SNAPD_DEBUG=1 snap list 2>&1 | MATCH "snap \(at .*\) is older \(.*\) than distribution package"; then + IS_REEXEC=false + fi + fi + if [ "$IS_REEXEC" = true ]; then + /usr/bin/env SNAPD_DEBUG=1 snap list 2>&1 | MATCH "DEBUG: restarting into \"$SNAPD_MOUNT_DIR/current/usr/bin/snap\"" + fi echo "Ensure that we do not re-exec into older versions" systemctl stop snapd.service snapd.socket @@ -118,11 +130,13 @@ execute: | systemctl daemon-reload systemctl start snapd.service snapd.socket - echo "Ensure SNAP_REEXEC=0 is honored for snap" - mount --bind /tmp/broken-snapd "$SNAPD_MOUNT_DIR/current/usr/bin/snap" - snap list|MATCH "from the $SNAPD_SRC snap" + if [ "$IS_REEXEC" = true ]; then + echo "Ensure SNAP_REEXEC=0 is honored for snap" + mount --bind /tmp/broken-snapd "$SNAPD_MOUNT_DIR/current/usr/bin/snap" + snap list | MATCH "from the $SNAPD_SRC snap" + umount "$SNAPD_MOUNT_DIR/current/usr/bin/snap" + fi SNAP_REEXEC=0 SNAPD_DEBUG=1 snap list 2>&1 |MATCH "DEBUG: re-exec disabled by user" - umount "$SNAPD_MOUNT_DIR/current/usr/bin/snap" echo "Ensure a snapd source refresh restarts snapd" prev_src=$(snap list | awk "/^$SNAPD_SRC / {print(\$3)}") @@ -135,8 +149,12 @@ execute: | echo "Test broken $now_src and $prev_src are the same" exit 1 fi - SNAPD_PATH=$(readlink -f "/proc/$(pidof snapd)/exe") - if [ "$SNAPD_PATH" != "/snap/$SNAPD_SRC/${now_src}/usr/lib/snapd/snapd" ]; then - echo "unexpected $SNAPD_PATH for $now_src snap (previous $prev_src)" - exit 1 - fi + + # This check is valid only if snap command is re-executing to the snap + if [ "$IS_REEXEC" = true ]; then + SNAPD_PATH=$(readlink -f "/proc/$(pidof snapd)/exe") + if [ "$SNAPD_PATH" != "/snap/$SNAPD_SRC/${now_src}/usr/lib/snapd/snapd" ]; then + echo "unexpected $SNAPD_PATH for $now_src snap (previous $prev_src)" + exit 1 + fi + fi diff --git a/tests/main/user-session-env/task.yaml b/tests/main/user-session-env/task.yaml index 7d1cc328f4d..54062d12501 100644 --- a/tests/main/user-session-env/task.yaml +++ b/tests/main/user-session-env/task.yaml @@ -12,7 +12,7 @@ systems: - -amazon-linux-2-* # no fish package for AMZN2 - -amazon-linux-2023-* # no fish package for AL2023 - -centos-9-* # no fish package - + - -debian-11-* # not supposed to work as snapd is very old in the repository environment: TEST_ZSH_USER: test-zsh