Skip to content

Commit

Permalink
tests: unify the way the snapd/core and kernel are repacked in nested…
Browse files Browse the repository at this point in the history
… helper (#12026)

* unify the way the snapd/core and kernel are repacked in nested helper

The idea is to simplify the code used to create images by creating
functions to prepare the snaps used to build the source images for the
nested tests.

This initial change includes snapd and kernel preparation.

* Fix missing then

* fix shellcheck

* Fix wrong variable name in prepare_snapd

* fix issues found in logs

* keep a copy of the snaps in $NESTED_ASSETS_DIR

* Update image_id for some tests

* Use the task name to identify the image when it is not set in the task

* fix shellcheck

* fix task name using basename
  • Loading branch information
sergiocazzolato committed Aug 12, 2022
1 parent aa40a4b commit 8d23538
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 67 deletions.
2 changes: 2 additions & 0 deletions spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,8 @@ suites:
NESTED_ENABLE_TPM: '$(HOST: echo "${NESTED_ENABLE_TPM:-}")'
# Enable secure boot in the nested vm in case it is supported
NESTED_ENABLE_SECURE_BOOT: '$(HOST: echo "${NESTED_ENABLE_SECURE_BOOT:-}")'
# Generic imane name which has to be overwritten in the test
NESTED_IMAGE_ID: "unset"
manual: true
warn-timeout: 10m
kill-timeout: 60m
Expand Down
149 changes: 85 additions & 64 deletions tests/lib/nested.sh
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,22 @@ nested_get_image_name() {
VERSION="18"
fi

# Use task name to build the image in case the NESTED_IMAGE_ID is unset
# This scenario is valid on manual tests when it is required to set the NESTED_IMAGE_ID
if [ "$NAME" = "unset" ]; then
NAME="$(basename "$SPREAD_TASK")"
if [ -n "$SPREAD_VARIANT" ]; then
NAME="${NAME}_${SPREAD_VARIANT}"
fi
fi

if [ "$NESTED_BUILD_SNAPD_FROM_CURRENT" = "true" ]; then
SOURCE="custom"
fi
if [ "$(nested_get_extra_snaps | wc -l)" != "0" ]; then
SOURCE="custom"
fi
echo "ubuntu-${TYPE}-${VERSION}-${SOURCE}-${NAME}-${NESTED_DISK_LOGICAL_BLOCK_SIZE}.img"
echo "ubuntu-${TYPE}-${VERSION}-${SOURCE}-${NAME}.img"
}

nested_is_generic_image() {
Expand Down Expand Up @@ -598,6 +607,67 @@ nested_ensure_ubuntu_save() {
fi
}

prepare_snapd() {
if [ "$NESTED_BUILD_SNAPD_FROM_CURRENT" = "true" ]; then
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"
else
snap_name="snapd"
output_name="snapd-from-deb.snap"
snap_id="PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4"
fi

"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap "$snap_name" "$NESTED_ASSETS_DIR"
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
make_snap_installable_with_id --noack "$NESTED_FAKESTORE_BLOB_DIR" "$(nested_get_extra_snaps_path)/$output_name" "$snap_id"
fi
fi
}

prepare_kernel() {
# allow repacking the kernel
if [ "$NESTED_REPACK_KERNEL_SNAP" = "true" ]; then
echo "Repacking kernel snap"
local kernel_snap output_name snap_id
output_name="pc-kernel.snap"
snap_id="pYVQrBcKmBa0mZ4CCN7ExT6jH8rY1hza"

if nested_is_core_16_system || nested_is_core_18_system; then
kernel_snap=pc-kernel-new.snap
repack_kernel_snap "$kernel_snap"
elif nested_is_core_20_system || nested_is_core_22_system; then
snap download --basename=pc-kernel --channel="$VERSION/edge" pc-kernel

# set the unix bump time if the NESTED_* var is set,
# otherwise leave it empty
local epochBumpTime
epochBumpTime=${NESTED_CORE20_INITRAMFS_EPOCH_TIMESTAMP:-}
if [ -n "$epochBumpTime" ]; then
epochBumpTime="--epoch-bump-time=$epochBumpTime"
fi

uc20_build_initramfs_kernel_snap "$PWD/pc-kernel.snap" "$NESTED_ASSETS_DIR" "$epochBumpTime"
rm -f "$PWD/pc-kernel.snap"

# Prepare the pc kernel snap
kernel_snap=$(ls "$NESTED_ASSETS_DIR"/pc-kernel_*.snap)
chmod 0600 "$kernel_snap"
fi
cp "$kernel_snap" "$(nested_get_extra_snaps_path)/$output_name"
# sign the pc-kernel snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
make_snap_installable_with_id --noack "$NESTED_FAKESTORE_BLOB_DIR" "$(nested_get_extra_snaps_path)/$output_name" "$snap_id"
fi
fi
}

nested_create_core_vm() {
# shellcheck source=tests/lib/prepare.sh
. "$TESTSLIB"/prepare.sh
Expand All @@ -606,7 +676,6 @@ nested_create_core_vm() {

local IMAGE_NAME
IMAGE_NAME="$(nested_get_image_name core)"

mkdir -p "$NESTED_IMAGES_DIR"

if [ -f "$NESTED_IMAGES_DIR/$IMAGE_NAME.pristine" ]; then
Expand All @@ -627,35 +696,16 @@ nested_create_core_vm() {
# ubuntu-image on 16.04 needs to be installed from a snap
UBUNTU_IMAGE=/snap/bin/ubuntu-image
fi
local EXTRA_FUNDAMENTAL=""
local EXTRA_SNAPS=""
for mysnap in $(nested_get_extra_snaps); do
EXTRA_SNAPS="$EXTRA_SNAPS --snap $mysnap"
done

local EXTRA_FUNDAMENTAL=""
if [ "$NESTED_BUILD_SNAPD_FROM_CURRENT" = "true" ]; then
if nested_is_core_16_system; then
echo "Repacking core snap"
"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap core "$NESTED_ASSETS_DIR"
EXTRA_FUNDAMENTAL="$EXTRA_FUNDAMENTAL --snap $NESTED_ASSETS_DIR/core-from-snapd-deb.snap"

# allow repacking the kernel
if [ "$NESTED_REPACK_KERNEL_SNAP" = "true" ]; then
KERNEL_SNAP=new-kernel.snap
repack_kernel_snap "$KERNEL_SNAP"
EXTRA_FUNDAMENTAL="$EXTRA_FUNDAMENTAL --snap $KERNEL_SNAP"
fi
prepare_snapd
prepare_kernel

elif nested_is_core_18_system; then
echo "Repacking snapd snap"
"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap snapd "$NESTED_ASSETS_DIR"
EXTRA_FUNDAMENTAL="$EXTRA_FUNDAMENTAL --snap $NESTED_ASSETS_DIR/snapd-from-deb.snap"

# allow repacking the kernel
if [ "$NESTED_REPACK_KERNEL_SNAP" = "true" ]; then
KERNEL_SNAP=new-kernel.snap
repack_kernel_snap "$KERNEL_SNAP"
EXTRA_FUNDAMENTAL="$EXTRA_FUNDAMENTAL --snap $KERNEL_SNAP"
fi
prepare_snapd
prepare_kernel

# allow tests to provide their own core18 snap
local CORE18_SNAP
Expand All @@ -677,32 +727,8 @@ nested_create_core_vm() {

elif nested_is_core_20_system || nested_is_core_22_system; then
VERSION="$(nested_get_version)"
if [ "$NESTED_REPACK_KERNEL_SNAP" = "true" ]; then
echo "Repacking kernel snap"
snap download --basename=pc-kernel --channel="$VERSION/edge" pc-kernel

# set the unix bump time if the NESTED_* var is set,
# otherwise leave it empty
local epochBumpTime
epochBumpTime=${NESTED_CORE20_INITRAMFS_EPOCH_TIMESTAMP:-}
if [ -n "$epochBumpTime" ]; then
epochBumpTime="--epoch-bump-time=$epochBumpTime"
fi

uc20_build_initramfs_kernel_snap "$PWD/pc-kernel.snap" "$NESTED_ASSETS_DIR" "$epochBumpTime"
rm -f "$PWD/pc-kernel.snap"

# Prepare the pc kernel snap
KERNEL_SNAP=$(ls "$NESTED_ASSETS_DIR"/pc-kernel_*.snap)

chmod 0600 "$KERNEL_SNAP"
EXTRA_FUNDAMENTAL="--snap $KERNEL_SNAP"

# sign the pc-kernel snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
make_snap_installable_with_id --noack "$NESTED_FAKESTORE_BLOB_DIR" "$KERNEL_SNAP" "pYVQrBcKmBa0mZ4CCN7ExT6jH8rY1hza"
fi
fi
prepare_snapd
prepare_kernel

# Prepare the pc gadget snap (unless provided by extra-snaps)
local GADGET_SNAP
Expand Down Expand Up @@ -760,16 +786,6 @@ EOF
make_snap_installable_with_id --noack --extra-decl-json "$NESTED_FAKESTORE_SNAP_DECL_PC_GADGET" "$NESTED_FAKESTORE_BLOB_DIR" "$GADGET_SNAP" "UqFziVZDHLSyO3TqSWgNBoAdHbLI4dAH"
fi

echo "Repacking snapd snap"
snap download --channel="latest/edge" snapd
"$TESTSTOOLS"/snaps-state repack_snapd_deb_into_snap snapd
EXTRA_FUNDAMENTAL="$EXTRA_FUNDAMENTAL --snap $PWD/snapd-from-deb.snap"

# sign the snapd snap with fakestore if requested
if [ "$NESTED_SIGN_SNAPS_FAKESTORE" = "true" ]; then
make_snap_installable_with_id --noack "$NESTED_FAKESTORE_BLOB_DIR" "$PWD/snapd-from-deb.snap" "PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4"
fi

if [ "$NESTED_REPACK_BASE_SNAP" = "true" ]; then
snap download --channel="$CORE_CHANNEL" --basename="core$VERSION" "core$VERSION"
repack_core_snap_with_tweaks "core$VERSION.snap" "new-core$VERSION.snap"
Expand All @@ -795,6 +811,11 @@ EOF
local NESTED_MODEL
NESTED_MODEL="$(nested_get_model)"

local EXTRA_SNAPS=""
for mysnap in $(nested_get_extra_snaps); do
EXTRA_SNAPS="$EXTRA_SNAPS --snap $mysnap"
done

# only set SNAPPY_FORCE_SAS_URL because we don't need it defined
# anywhere else but here, where snap prepare-image as called by
# ubuntu-image will look for assertions for the snaps we provide
Expand Down Expand Up @@ -951,7 +972,7 @@ nested_add_file_to_vm() {
ubuntuSeedDev="${devloop}p2"

# Wait for the partition to show up
retry -n 2 --wait test -b "${ubuntuSeedDev}" || true
retry -n 2 --wait 1 test -b "${ubuntuSeedDev}" || true

# losetup does not set the right block size on LOOP_CONFIGURE
# but with LOOP_SET_BLOCK_SIZE later. So the block size might have
Expand Down
2 changes: 1 addition & 1 deletion tests/nested/manual/core20-remodel/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ systems: [ubuntu-20.04-64, ubuntu-22.04-64]

environment:
NESTED_CUSTOM_MODEL: $TESTSLIB/assertions/valid-for-testing-pc-{VERSION}.model
NESTED_IMAGE_ID: uc20-remodel-testing
NESTED_IMAGE_ID: uc20-remodel
NESTED_ENABLE_TPM: true
NESTED_ENABLE_SECURE_BOOT: true
NESTED_BUILD_SNAPD_FROM_CURRENT: true
Expand Down
2 changes: 1 addition & 1 deletion tests/nested/manual/remodel-cross-store/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ systems: [ubuntu-18.04-64, ubuntu-20.04-64]

environment:
NESTED_CUSTOM_AUTO_IMPORT_ASSERTION: $TESTSLIB/assertions/test-snapd-remodel-auto-import.assert
NESTED_IMAGE_ID: uc20-remodel-testing-cross-store
NESTED_IMAGE_ID: remodel-cross-store
NESTED_ENABLE_TPM: false
NESTED_ENABLE_SECURE_BOOT: false
NESTED_USE_CLOUD_INIT: false
Expand Down
2 changes: 1 addition & 1 deletion tests/nested/manual/remodel-simple/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ systems: [ubuntu-18.04-64, ubuntu-20.04-64]

environment:
NESTED_CUSTOM_AUTO_IMPORT_ASSERTION: $TESTSLIB/assertions/test-snapd-remodel-auto-import.assert
NESTED_IMAGE_ID: uc20-remodel-testing
NESTED_IMAGE_ID: remodel-simple
NESTED_ENABLE_TPM: false
NESTED_ENABLE_SECURE_BOOT: false
NESTED_USE_CLOUD_INIT: false
Expand Down

0 comments on commit 8d23538

Please sign in to comment.