Skip to content

Commit

Permalink
tests: properly build snapd snap (#14141)
Browse files Browse the repository at this point in the history
* tests: properly build snapd snap

Now we build also the test version of snapd snap in `snap-builds`
workflow job. We copy this into the spread tests. And we use that
snap, which we only instrument instead of copying the snapd deb build.

If the snap is not available, then we build it in spread. On CI, this
happens on arm since the workflow does not build it. It will also
happen when triggering test manually.

* tests: couple of small improvements to test syntax, move WORK_DIR into script scope, use PWD instead of dot notation

---------

Co-authored-by: Philip Meulengracht <[email protected]>
  • Loading branch information
valentindavid and Meulengracht authored Jul 3, 2024
1 parent 26e8fb4 commit b1dc3c6
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 195 deletions.
50 changes: 47 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@ concurrency:

jobs:
snap-builds:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
matrix:
toolchain:
- default
- FIPS
version:
# test version is a build of snapd with test keys and should
# only be installed by test runners. The pristine versions
# are the build that should be installed by human users.
- pristine
- test
# only build the snap for pull requests, it's not needed on release branches
# or on master since we have launchpad build recipes which do this already
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Select Go toolchain
run: |
Expand All @@ -48,12 +56,25 @@ jobs:
exit 1
;;
esac
case "${{ matrix.version }}" in
pristine)
rm -f test-build
;;
test)
touch test-build
;;
esac
- name: Build snapd snap
uses: snapcore/action-build@v1
with:
snapcraft-channel: 8.x/stable

- name: Build test snapd snap
uses: snapcore/action-build@v1
with:
snapcraft-channel: 8.x/stable

- name: Check built artifact
run: |
unsquashfs snapd*.snap meta/snap.yaml usr/lib/snapd/
Expand All @@ -71,7 +92,7 @@ jobs:
- name: Uploading snapd snap artifact
uses: actions/upload-artifact@v3
with:
name: snap-files-${{ matrix.toolchain }}
name: snap-files-${{ matrix.toolchain }}-${{ matrix.version }}
path: "*.snap"

cache-build-deps:
Expand Down Expand Up @@ -592,7 +613,7 @@ jobs:
verbose: true

spread:
needs: [unit-tests]
needs: [unit-tests, snap-builds]
# have spread jobs run on master on PRs only, but on both PRs and pushes to
# release branches
if: ${{ github.event_name != 'push' || github.ref != 'refs/heads/master' }}
Expand Down Expand Up @@ -793,6 +814,19 @@ jobs:
# The log-filter tool is used to filter the spread logs to be stored
echo FILTER_PARAMS="-o spread_$CHANGE_ID.filtered.log -e Debug -e WARNING: -f Failed=NO_LINES -f Error=NO_LINES" >> $GITHUB_ENV
- name: Download built snap
uses: actions/download-artifact@v3
with:
name: snap-files-default-test
pattern: snapd_1337.*.snap
path: "${{ github.workspace }}/built-snap"

- name: Rename imported snap
run: |
for snap in built-snap/snapd_1337.*.snap; do
mv "${snap}" "${snap}.keep"
done
- name: Run spread tests
if: "!contains(github.event.pull_request.labels.*.name, 'Skip spread') && ( !startsWith(matrix.group, 'nested-') || contains(github.event.pull_request.labels.*.name, 'Run nested') )"
env:
Expand All @@ -807,6 +841,16 @@ jobs:
export NESTED_ENABLE_KVM=true
fi
case "${{ matrix.systems }}" in
*-arm-*)
TESTS_USE_PREBUILT_SNAPD_SNAP=false
;;
*)
TESTS_USE_PREBUILT_SNAPD_SNAP=true
;;
esac
export TESTS_USE_PREBUILT_SNAPD_SNAP
if [[ "${{ matrix.systems }}" =~ amazon-linux-2023 ]]; then
# Amazon Linux 2023 has no xdelta, however we cannot disable
# xdelta on a per-target basis as it's used in the repack section
Expand Down
3 changes: 3 additions & 0 deletions build-aux/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ parts:
echo "-- appending FIPS tag to version $VERSION"
VERSION="$VERSION-fips"
fi
if [ -f test-build ]; then
VERSION="1337.${VERSION}"
fi
craftctl set version="$VERSION"
./get-deps.sh --skip-unused-check
Expand Down
28 changes: 20 additions & 8 deletions spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ environment:
# a global setting for LXD channel to use in the tests
# TODO: Consider reverting to latest/candidate after Snapcraft compatibility with LXD 5.21 extended version string "5.21 LTS" is fixed
LXD_SNAP_CHANNEL: "latest/candidate"
UBUNTU_IMAGE_SNAP_CHANNEL: "2/stable"
OLD_UBUNTU_IMAGE_SNAP_CHANNEL: "2/stable"
UBUNTU_IMAGE_SNAP_CHANNEL: "latest/edge"
SNAPCRAFT_SNAP_CHANNEL: "latest/candidate"
# controls whether ubuntu-image is built using the current snapd tree as a
# dependency or the one listed in its go.mod
Expand Down Expand Up @@ -107,6 +108,8 @@ environment:
NESTED_REPACK_GADGET_SNAP: '$(HOST: echo "${NESTED_REPACK_GADGET_SNAP:-true}")'
NESTED_REPACK_BASE_SNAP: '$(HOST: echo "${NESTED_REPACK_BASE_SNAP:-true}")'
NESTED_FORCE_MS_KEYS: '$(HOST: echo "${NESTED_FORCE_MS_KEYS:-false}")'
# Whether we should use snapd snap ./built-snap/ directory
TESTS_USE_PREBUILT_SNAPD_SNAP: '$(HOST: echo "${TESTS_USE_PREBUILT_SNAPD_SNAP:-false}")'

backends:
google:
Expand Down Expand Up @@ -241,15 +244,16 @@ backends:
location: snapd-spread/us-central1-a
plan: t2a-standard-1
halt-timeout: 2h
kill-timeout: 60m
systems:
- ubuntu-20.04-arm-64:
image: ubuntu-os-cloud/ubuntu-2004-lts-arm64
workers: 8
storage: 12G
storage: 15G
- ubuntu-22.04-arm-64:
image: ubuntu-os-cloud/ubuntu-2204-lts-arm64
workers: 8
storage: 12G
storage: 15G
- ubuntu-core-22-arm-64:
image: ubuntu-22.04-arm-64
workers: 6
Expand Down Expand Up @@ -1182,10 +1186,12 @@ suites:
. "$TESTSLIB"/image.sh
distro_update_package_db
distro_install_package snapd qemu-kvm qemu-utils genisoimage sshpass cloud-image-utils ovmf kpartx xz-utils mtools ca-certificates xdelta3
if os.query is-xenial || os.query is-arm; then
if os.query is-xenial; then
# the new ubuntu-image expects mkfs to support -d option, which was not
# supported yet by the version of mkfs that shipped with Ubuntu 16.04
# also ubuntu-image binary is not prebuilt for arm instances
snap install ubuntu-image --channel="$OLD_UBUNTU_IMAGE_SNAP_CHANNEL" --classic
elif os.query is-arm; then
snap install ubuntu-image --channel="$UBUNTU_IMAGE_SNAP_CHANNEL" --classic
else
get_ubuntu_image
Expand All @@ -1207,7 +1213,7 @@ suites:
tests.nested prepare
if os.query is-xenial && ! command -v ubuntu-image >/dev/null; then
# This is needed because the snap in removed during on restore-each
snap install ubuntu-image --channel="$UBUNTU_IMAGE_SNAP_CHANNEL" --classic
snap install ubuntu-image --channel="$OLD_UBUNTU_IMAGE_SNAP_CHANNEL" --classic
fi
restore-each: |
tests.nested vm remove
Expand Down Expand Up @@ -1239,10 +1245,12 @@ suites:
. "$TESTSLIB"/image.sh
distro_update_package_db
distro_install_package snapd qemu-kvm qemu-utils genisoimage sshpass cloud-image-utils ovmf kpartx xz-utils mtools ca-certificates xdelta3
if os.query is-xenial || os.query is-arm; then
if os.query is-xenial; then
# the new ubuntu-image expects mkfs to support -d option, which was not
# supported yet by the version of mkfs that shipped with Ubuntu 16.04
# also ubuntu-image binary is not prebuilt for arm instances
snap install ubuntu-image --channel="$OLD_UBUNTU_IMAGE_SNAP_CHANNEL" --classic
elif os.query is-arm; then
snap install ubuntu-image --channel="$UBUNTU_IMAGE_SNAP_CHANNEL" --classic
else
get_ubuntu_image
Expand Down Expand Up @@ -1300,10 +1308,12 @@ suites:
. "$TESTSLIB"/image.sh
distro_update_package_db
distro_install_package snapd qemu-kvm qemu-utils genisoimage sshpass cloud-image-utils ovmf kpartx xz-utils mtools ca-certificates xdelta3
if os.query is-xenial || os.query is-arm; then
if os.query is-xenial; then
# the new ubuntu-image expects mkfs to support -d option, which was not
# supported yet by the version of mkfs that shipped with Ubuntu 16.04
# also ubuntu-image binary is not prebuilt for arm instances
snap install ubuntu-image --channel="$OLD_UBUNTU_IMAGE_SNAP_CHANNEL" --classic
elif os.query is-arm; then
snap install ubuntu-image --channel="$UBUNTU_IMAGE_SNAP_CHANNEL" --classic
else
get_ubuntu_image
Expand Down Expand Up @@ -1363,10 +1373,12 @@ suites:
. "$TESTSLIB"/image.sh
distro_update_package_db
distro_install_package snapd qemu-kvm qemu-utils genisoimage sshpass cloud-image-utils ovmf kpartx xz-utils mtools ca-certificates xdelta3
if os.query is-xenial || os.query is-arm; then
if os.query is-xenial; then
# the new ubuntu-image expects mkfs to support -d option, which was not
# supported yet by the version of mkfs that shipped with Ubuntu 16.04
# also ubuntu-image binary is not prebuilt for arm instances
snap install ubuntu-image --channel="$OLD_UBUNTU_IMAGE_SNAP_CHANNEL" --classic
elif os.query is-arm; then
snap install ubuntu-image --channel="$UBUNTU_IMAGE_SNAP_CHANNEL" --classic
else
get_ubuntu_image
Expand Down
42 changes: 26 additions & 16 deletions tests/lib/nested.sh
Original file line number Diff line number Diff line change
Expand Up @@ -595,23 +595,33 @@ nested_prepare_snapd() {
echo "Repacking snapd snap"
local snap_name output_name snap_id
if nested_is_core_16_system; then
snap_name="core"
output_name="core-from-snapd-deb.snap"
snap_id="99T7MUlRhtI3U0QFgl5mXXESAiSwt776"
if [ ! -f "$NESTED_ASSETS_DIR/core-from-snapd-deb.snap" ]; then
"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap core "$NESTED_ASSETS_DIR"
cp "$NESTED_ASSETS_DIR/core-from-snapd-deb.snap" "$(nested_get_extra_snaps_path)/core-from-snapd-deb.snap"
fi
# sign the snapd snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
"$TESTSTOOLS"/store-state make-snap-installable --noack "$NESTED_FAKESTORE_BLOB_DIR" "$(nested_get_extra_snaps_path)/core-from-snapd-deb.snap" "99T7MUlRhtI3U0QFgl5mXXESAiSwt776"
fi
else
snap_name="snapd"
output_name="snapd-from-deb.snap"
snap_id="PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4"
fi

if [ ! -f "$NESTED_ASSETS_DIR/$output_name" ]; then
"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap "$snap_name" "$NESTED_ASSETS_DIR"
fi
cp "$NESTED_ASSETS_DIR/$output_name" "$(nested_get_extra_snaps_path)/$output_name"

# sign the snapd snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
"$TESTSTOOLS"/store-state make-snap-installable --noack "$NESTED_FAKESTORE_BLOB_DIR" "$(nested_get_extra_snaps_path)/$output_name" "$snap_id"
for f in "${NESTED_ASSETS_DIR}"/snapd_*.snap; do
snap_name="$(basename "${f}")"
break
done
if [ ! -f "${NESTED_ASSETS_DIR}/${snap_name}" ]; then
# shellcheck source=tests/lib/prepare.sh
. "$TESTSLIB"/prepare.sh
build_snapd_snap "$NESTED_ASSETS_DIR"
for f in "${NESTED_ASSETS_DIR}"/snapd_*.snap; do
snap_name="$(basename "${f}")"
break
done
cp "${NESTED_ASSETS_DIR}/${snap_name}" "$(nested_get_extra_snaps_path)/"
fi
# sign the snapd snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
"$TESTSTOOLS"/store-state make-snap-installable --noack "$NESTED_FAKESTORE_BLOB_DIR" "$(nested_get_extra_snaps_path)/${snap_name}" "PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4"
fi
fi
fi
}
Expand Down
Loading

0 comments on commit b1dc3c6

Please sign in to comment.