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: fix for preseeding failures #8528

Merged
merged 13 commits into from Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 7 additions & 26 deletions tests/lib/preseed.sh
Expand Up @@ -3,8 +3,8 @@
# mount ubuntu cloud image through qemu-nbd and mount
# critical virtual filesystems (such as proc) under
# the root of mounted image.
# XXX: cannot be used in prepare: section of the tests
# as the test gets stuck around qemu-nbd on 20.04.
# The path of the image needs to be absolute as a systemd service
# gets created for qemu-nbd.
mount_ubuntu_image() {
local CLOUD_IMAGE=$1
local IMAGE_MOUNTPOINT=$2
Expand All @@ -13,9 +13,12 @@ mount_ubuntu_image() {
modprobe nbd
fi

qemu-nbd -c /dev/nbd0 "$CLOUD_IMAGE"
# Run qemu-ndb as a service, so that it does not interact with ssh
# stdin/stdout it would otherwise inherit from the spread session.
systemd-run --system --service-type=forking --unit=qemu-ndb-preseed.service "$(command -v qemu-nbd)" --fork -c /dev/nbd0 "$CLOUD_IMAGE"
Copy link
Member

Choose a reason for hiding this comment

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

nice fix!

# nbd0p1 may take a short while to become available
retry-tool -n 5 --wait 1 mount /dev/nbd0p1 "$IMAGE_MOUNTPOINT"
retry-tool -n 5 --wait 1 test -e /dev/nbd0p1
mount /dev/nbd0p1 "$IMAGE_MOUNTPOINT"
mount -t proc /proc "$IMAGE_MOUNTPOINT/proc"
mount -t sysfs sysfs "$IMAGE_MOUNTPOINT/sys"
mount -t devtmpfs udev "$IMAGE_MOUNTPOINT/dev"
Expand All @@ -35,25 +38,3 @@ umount_ubuntu_image() {
# reporting it's still in use.
retry-tool -n 5 --wait 1 qemu-nbd -d /dev/nbd0
}

# XXX inject new snapd into the core image in seed/snaps of the cloud image
# and make core unasserted.
# this will go away once snapd on the core is new enough to support
# pre-seeding.
setup_preseeding() {
local IMAGE_MOUNTPOINT=$1
local CORE_IMAGE

# TODO: on 20.04 there is no core_*.snap anymore, just snapd
CORE_IMAGE=$(find "$IMAGE_MOUNTPOINT/var/lib/snapd/seed/snaps/" -name "core_*.snap")
unsquashfs "$CORE_IMAGE"
cp /usr/lib/snapd/snapd squashfs-root/usr/lib/snapd/snapd
# XXX to satisfy version check; this will go away once preseeding
# is available in 2.44
echo "VERSION=2.44.0" > squashfs-root/usr/lib/snapd/info
rm "$CORE_IMAGE"
#shellcheck source=tests/lib/snaps.sh
. "$TESTSLIB"/snaps.sh
mksnap_fast squashfs-root "$CORE_IMAGE"
sed -i "$IMAGE_MOUNTPOINT/var/lib/snapd/seed/seed.yaml" -E -e 's/^(\s+)name: core/\1name: core\n\1unasserted: true/'
}
20 changes: 8 additions & 12 deletions tests/main/preseed-reset/task.yaml
Expand Up @@ -4,7 +4,7 @@ description: |
can be undone with --reset flag.

# TODO: reenable for ubuntu-20.04-*, that now uses the snapd snap
Copy link
Member

Choose a reason for hiding this comment

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

this TODO can be deleted, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let's remove this in the next pass.

systems: [ubuntu-19.10-*]
systems: [ubuntu-19.10-*, ubuntu-20.04-*]

environment:
IMAGE_MOUNTPOINT: /mnt/cloudimg
Expand All @@ -17,13 +17,17 @@ prepare: |
wget "$(get_image_url_for_nested_vm)" -O cloudimg.img
mkdir -p "$IMAGE_MOUNTPOINT"

#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
mount_ubuntu_image "$(pwd)/cloudimg.img" "$IMAGE_MOUNTPOINT"

restore: |
rm -f before-preseeding.txt
rm -f after-reset.txt

# any of the restore commands can fail depending on where execute part stopped,
# account for that with ||true.
umount_ubuntu_image "$IMAGE_MOUNTPOINT" || true
#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
umount_ubuntu_image "$IMAGE_MOUNTPOINT"

execute: |
find_files() {
Expand All @@ -36,11 +40,6 @@ execute: |
fi
}

#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
mount_ubuntu_image cloudimg.img "$IMAGE_MOUNTPOINT"
setup_preseeding "$IMAGE_MOUNTPOINT"

find_files > before-preseeding.txt

echo "Running pre-seeeding"
Expand Down Expand Up @@ -68,6 +67,3 @@ execute: |
echo "Running preseeeding again should succeed"
/usr/lib/snapd/snap-preseed "$IMAGE_MOUNTPOINT"

#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
umount_ubuntu_image "$IMAGE_MOUNTPOINT"
7 changes: 0 additions & 7 deletions tests/main/preseed-snapd-snap/seed.yaml.in

This file was deleted.

110 changes: 0 additions & 110 deletions tests/main/preseed-snapd-snap/task.yaml

This file was deleted.

95 changes: 53 additions & 42 deletions tests/main/preseed/task.yaml
Expand Up @@ -4,8 +4,7 @@ description: |
command works, up to the point where the image is ready to be booted.
The test assumes cloud image with a core and lxd snaps in its seeds/.

# TODO: reenable for ubuntu-20.04-*, that now uses the snapd snap
systems: [ubuntu-19.10-*]
systems: [ubuntu-19.10-*, ubuntu-20.04-*]

environment:
IMAGE_MOUNTPOINT: /mnt/cloudimg
Expand All @@ -18,22 +17,21 @@ prepare: |
wget "$(get_image_url_for_nested_vm)" -O cloudimg.img
mkdir -p "$IMAGE_MOUNTPOINT"

restore: |
# any of the restore commands can fail depending on where execute part stopped,
# account for that with ||true.
umount_ubuntu_image "$IMAGE_MOUNTPOINT" || true
#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
mount_ubuntu_image "$(pwd)/cloudimg.img" "$IMAGE_MOUNTPOINT"

execute: |
restore: |
#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
mount_ubuntu_image cloudimg.img "$IMAGE_MOUNTPOINT"
setup_preseeding "$IMAGE_MOUNTPOINT"
umount_ubuntu_image "$IMAGE_MOUNTPOINT" || true

execute: |
echo "Checking missing chroot path arg error"
/usr/lib/snapd/snap-preseed 2>&1 | MATCH "error: need chroot path as argument"

echo "Running pre-seeeding"
/usr/lib/snapd/snap-preseed "$IMAGE_MOUNTPOINT"
SNAPD_DEBUG=1 /usr/lib/snapd/snap-preseed "$IMAGE_MOUNTPOINT"

# sanity, core snap mounted by snap-preseed got unmounted
mount | not MATCH "snap-preseed"
Expand All @@ -42,72 +40,85 @@ execute: |

echo "Check that the tasks of preseeded snapd have expected statuses"
# Note, these checks match statuses, but not the order
MATCH "Done .+ prerequisites +Ensure prerequisites for \"core\" are available" < tasks.log
MATCH "Done .+ prepare-snap +Prepare snap \"/var/lib/snapd/seed/snaps/core_[0-9]+.snap" < tasks.log
MATCH "Done .+ mount-snap +Mount snap \"core\"" < tasks.log
MATCH "Done .+ copy-snap-data +Copy snap \"core\" data" < tasks.log
MATCH "Done .+ setup-profiles +Setup snap \"core\" \(unset\) security profiles" < tasks.log
MATCH "Done .+ link-snap +Make snap \"core\" \(unset\) available to the system" < tasks.log
MATCH "Done .+ auto-connect +Automatically connect eligible plugs and slots of snap \"core\"" < tasks.log
MATCH "Done .+ set-auto-aliases +Set automatic aliases for snap \"core\"" < tasks.log
MATCH "Done .+ setup-aliases +Setup snap \"core\" aliases" < tasks.log
# mark-preseeded task is where snap-preseed stopped, therefore it's in Doing.
MATCH "Doing .+ mark-preseeded +Mark system pre-seeded" < tasks.log

MATCH "Done .+ prerequisites +Ensure prerequisites for \"snapd\" are available" < tasks.log
MATCH "Done .+ prepare-snap +Prepare snap \"/var/lib/snapd/seed/snaps/snapd_[0-9]+.snap" < tasks.log
MATCH "Done .+ mount-snap +Mount snap \"snapd\" \([0-9]+\)" < tasks.log
MATCH "Done .+ copy-snap-data +Copy snap \"snapd\" data" < tasks.log
MATCH "Done .+ setup-profiles +Setup snap \"snapd\" \([0-9]+\) security profiles" < tasks.log
MATCH "Done .+ link-snap +Make snap \"snapd\" \([0-9]+\) available to the system" < tasks.log
MATCH "Done .+ auto-connect +Automatically connect eligible plugs and slots of snap \"snapd\"" < tasks.log
MATCH "Done .+ set-auto-aliases +Set automatic aliases for snap \"snapd\"" < tasks.log
MATCH "Done .+ setup-aliases +Setup snap \"snapd\" aliases" < tasks.log

MATCH "Done .+ prerequisites +Ensure prerequisites for \"core18\" are available" < tasks.log
MATCH "Done .+ prepare-snap +Prepare snap \"/var/lib/snapd/seed/snaps/core18_[0-9]+.snap" < tasks.log
MATCH "Done .+ mount-snap +Mount snap \"core18\" \([0-9]+\)" < tasks.log
MATCH "Done .+ copy-snap-data +Copy snap \"core18\" data" < tasks.log
MATCH "Done .+ setup-profiles +Setup snap \"core18\" \([0-9]+\) security profiles" < tasks.log
MATCH "Done .+ link-snap +Make snap \"core18\" \([0-9]+\) available to the system" < tasks.log
MATCH "Done .+ auto-connect +Automatically connect eligible plugs and slots of snap \"core18\"" < tasks.log
MATCH "Done .+ set-auto-aliases +Set automatic aliases for snap \"core18\"" < tasks.log
MATCH "Done .+ setup-aliases +Setup snap \"core18\" aliases" < tasks.log

MATCH "Done .+ prerequisites +Ensure prerequisites for \"lxd\" are available" < tasks.log
MATCH "Done .+ prepare-snap +Prepare snap \"/var/lib/snapd/seed/snaps/lxd_[0-9]+.snap\" \([0-9]+\)" < tasks.log
MATCH "Done .+ mount-snap +Mount snap \"lxd\" \([0-9]+\)" < tasks.log
MATCH "Done .+ copy-snap-data +Copy snap \"lxd\" data" < tasks.log
MATCH "Done .+ setup-profiles +Setup snap \"lxd\" \([0-9]+\) security profiles" < tasks.log
MATCH "Done .+ link-snap +Make snap \"lxd\" \([0-9]+\) available to the system" < tasks.log
MATCH "Done .+ auto-connect +Automatically connect eligible plugs and slots of snap \"lxd\"" < tasks.log
MATCH "Done .+ connect +Connect lxd:network to core:network" < tasks.log
MATCH "Done .+ connect +Connect lxd:lxd-support to core:lxd-support" < tasks.log
MATCH "Done .+ connect +Connect lxd:system-observe to core:system-observe" < tasks.log
MATCH "Done .+ connect +Connect lxd:network-bind to core:network-bind" < tasks.log
MATCH "Done .+ connect +Connect lxd:network to snapd:network" < tasks.log
MATCH "Done .+ connect +Connect lxd:lxd-support to snapd:lxd-support" < tasks.log
MATCH "Done .+ connect +Connect lxd:system-observe to snapd:system-observe" < tasks.log
MATCH "Done .+ connect +Connect lxd:network-bind to snapd:network-bind" < tasks.log
MATCH "Done .+ setup-profiles +Setup snap \"lxd\" \([0-9]+\) security profiles for auto-connections" < tasks.log
MATCH "Done .+ set-auto-aliases +Set automatic aliases for snap \"lxd\"" < tasks.log
MATCH "Done .+ setup-aliases +Setup snap \"lxd\" aliases" < tasks.log

echo "Checking that there were no other 'Done' tasks when preseeding"
[ "$(grep -c ' Done ' tasks.log)" = "23" ]

# mark-preseeded task is where snap-preseed stopped, therefore it's in Doing.
MATCH "Doing .+ mark-preseeded +Mark system pre-seeded" < tasks.log
[ "$(grep -c ' Done ' tasks.log)" = "32" ]

# everything below is pending execution on first boot
MATCH "Do .+ run-hook +Run install hook of \"lxd\" snap if present" < tasks.log
MATCH "Do .+ start-snap-services +Start snap \"lxd\" \([0-9]+\) services" < tasks.log
MATCH "Do .+ run-hook +Run install hook of \"core18\" snap if present" < tasks.log
MATCH "Do .+ start-snap-services +Start snap \"lxd\" \([0-9]+\) services" < tasks.log
MATCH "Do .+ run-hook +Run configure hook of \"lxd\" snap if present" < tasks.log
MATCH "Do .+ run-hook +Run install hook of \"core\" snap if present" < tasks.log
MATCH "Do .+ start-snap-services +Start snap \"core\" \(unset\) services" < tasks.log
MATCH "Do .+ run-hook +Run health check of \"lxd\" snap" < tasks.log
MATCH "Do .+ mark-seeded +Mark system seeded" < tasks.log
MATCH "Do .+ run-hook +Run install hook of \"snapd\" snap if present" < tasks.log
MATCH "Do .+ start-snap-services +Start snap \"snapd\" \([0-9]+\) services" < tasks.log
MATCH "Do .+ run-hook +Run configure hook of \"core\" snap if present" < tasks.log
MATCH "Do .+ start-snap-services +Start snap \"core18\" \([0-9]+\) services" < tasks.log
MATCH "Do .+ run-hook +Run health check of \"core18\" snap" < tasks.log
MATCH "Do .+ run-hook +Run install hook of \"lxd\" snap if present" < tasks.log

echo "Checking that apparmor and seccomp profiles have been created on the target image"
AA_PROFILES="$IMAGE_MOUNTPOINT"/var/lib/snapd/apparmor/profiles
SECCOMP_PROFILES="$IMAGE_MOUNTPOINT"/var/lib/snapd/seccomp/bpf
test -f "$AA_PROFILES"/snap-confine.core.*
test -f "$AA_PROFILES"/snap-update-ns.core

test -f "$AA_PROFILES"/snap-confine.snapd.*
test -f "$AA_PROFILES"/snap-update-ns.lxd

# the list of expected profiles isn't exhaustive, we're just checking some critical ones
for prof in snap.lxd.lxd snap.lxd.hook.install snap.lxd.hook.configure snap.lxd.daemon; do
test -f "$AA_PROFILES/$prof"
test -f "$SECCOMP_PROFILES/$prof.bin"
done

echo "Checking that mount units have been created on the target image"
echo "Checking that mount units have been created and enabled on the target image"
SYSTEMD_UNITS="$IMAGE_MOUNTPOINT"/etc/systemd
test -f "$SYSTEMD_UNITS"/system/snap-lxd-*.mount
test -f "$SYSTEMD_UNITS"/system/snap-core-*.mount
test -L "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap-lxd-*.mount
test -f "$SYSTEMD_UNITS"/system/snap-snapd-*.mount
test -f "$SYSTEMD_UNITS"/system/snap-core18-*.mount
test -L "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap-core18-*.mount
test -L "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap-snapd-*.mount

for unit in snap.lxd.daemon.service snap.lxd.daemon.unix.socket snap.lxd.activate.service; do
test -f "$SYSTEMD_UNITS/system/$unit"
done

echo "Checking enabled systemd mount units"
test -L "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap-lxd-*.mount
test -L "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap-core-*.mount
echo "LXD service shouldn't be enabled at this point"
test ! -e "$SYSTEMD_UNITS"/system/multi-user.target.wants/snap.lxd.activate.service

#shellcheck source=tests/lib/preseed.sh
. "$TESTSLIB/preseed.sh"
umount_ubuntu_image "$IMAGE_MOUNTPOINT"