Skip to content

Commit

Permalink
tests: test seeding optional snaps and components from snapd installe…
Browse files Browse the repository at this point in the history
…r api
  • Loading branch information
andrewphelpsj committed Aug 28, 2024
1 parent 28022b0 commit 5eba35a
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tests/lib/muinstaller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module github.com/snapcore/snapd/tests/lib/muinstaller

go 1.18

require github.com/snapcore/snapd v0.0.0-20240528091028-dc3b51bb4c9e
require github.com/snapcore/snapd v0.0.0-20240822132116-0bf8d83f4586

require (
github.com/canonical/go-efilib v0.3.1-0.20220815143333-7e5151412e93 // indirect
github.com/canonical/go-efilib v0.4.0 // indirect
github.com/canonical/go-sp800.108-kdf v0.0.0-20210314145419-a3359f2d21b9 // indirect
github.com/canonical/go-sp800.90a-drbg v0.0.0-20210314144037-6eeb1040d6c3 // indirect
github.com/canonical/go-tpm2 v0.0.0-20210827151749-f80ff5afff61 // indirect
Expand Down
9 changes: 5 additions & 4 deletions tests/lib/muinstaller/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/canonical/go-efilib v0.3.1-0.20220815143333-7e5151412e93 h1:F0bRDzPy/j2IX/iIWqCEA23S1nal+f7A+/vLyj6Ye+4=
github.com/canonical/go-efilib v0.3.1-0.20220815143333-7e5151412e93/go.mod h1:9b2PNAuPcZsB76x75/uwH99D8CyH/A2y4rq1/+bvplg=
github.com/canonical/go-efilib v0.4.0 h1:2ee5pvhIZ+g1EO4HxFE/owBgs5Up2g7dw1+Ls9/fiSs=
github.com/canonical/go-efilib v0.4.0/go.mod h1:9b2PNAuPcZsB76x75/uwH99D8CyH/A2y4rq1/+bvplg=
github.com/canonical/go-sp800.108-kdf v0.0.0-20210314145419-a3359f2d21b9 h1:USzKjrfWo/ESzozv2i3OMM7XDgxrZRvaHFrKkIKRtwU=
github.com/canonical/go-sp800.108-kdf v0.0.0-20210314145419-a3359f2d21b9/go.mod h1:Zrs3YjJr+w51u0R/dyLh/oWt/EcBVdLPCVFYC4daW5s=
github.com/canonical/go-sp800.90a-drbg v0.0.0-20210314144037-6eeb1040d6c3 h1:oe6fCvaEpkhyW3qAicT0TnGtyht/UrgvOwMcEgLb7Aw=
Expand Down Expand Up @@ -32,8 +32,8 @@ github.com/snapcore/go-gettext v0.0.0-20191107141714-82bbea49e785/go.mod h1:D3Ss
github.com/snapcore/secboot v0.0.0-20240411101434-f3ad7c92552a h1:yzzVi0yUosDYkjSQqGZNVtaVi+6yNFLiF0erKHlBbdo=
github.com/snapcore/secboot v0.0.0-20240411101434-f3ad7c92552a/go.mod h1:72paVOkm4sJugXt+v9ItmnjXgO921D8xqsbH2OekouY=
github.com/snapcore/snapd v0.0.0-20201005140838-501d14ac146e/go.mod h1:3xrn7QDDKymcE5VO2rgWEQ5ZAUGb9htfwlXnoel6Io8=
github.com/snapcore/snapd v0.0.0-20240528091028-dc3b51bb4c9e h1:y5tdsmeugpiJAhNeQnUW+BPlcSlOYf1vwoo53yzIh1s=
github.com/snapcore/snapd v0.0.0-20240528091028-dc3b51bb4c9e/go.mod h1:QNsQNTz6P2yqZ22QTU5Jt1FUIGGLlcUuNURvo2tWYOo=
github.com/snapcore/snapd v0.0.0-20240822132116-0bf8d83f4586 h1:ZdnBopkJlW0yERpbGMos1ZbFtf+W2MWMZ+RCy4B26rs=
github.com/snapcore/snapd v0.0.0-20240822132116-0bf8d83f4586/go.mod h1:RfaJNlcyEP2RfVuyAX/3x+CJPECCAlZn+36Yily9FzY=
go.etcd.io/bbolt v1.3.9 h1:8x7aARPEXiXbHmtUwAIv7eV2fQFHrLLavdiJ3uzJXoI=
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1 h1:A/5uWzF44DlIgdm/PQFwfMkW0JX+cIcQi/SwLAmZP5M=
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1/go.mod h1:SNgMg+EgDFwmvSmLRTNKC5fegJjB7v23qTQ0XLGUNHk=
Expand All @@ -54,6 +54,7 @@ golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0=
Expand Down
21 changes: 19 additions & 2 deletions tests/lib/muinstaller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -284,8 +285,9 @@ func postSystemsInstallFinish(cli *client.Client,

// Finish steps does the writing of assets
opts := &client.InstallSystemOptions{
Step: client.InstallStepFinish,
OnVolumes: vols,
Step: client.InstallStepFinish,
OnVolumes: vols,
OptionalInstall: maybeGetOptionalInstall(),
}
chgId, err := cli.InstallSystem(details.Label, opts)
if err != nil {
Expand All @@ -295,6 +297,21 @@ func postSystemsInstallFinish(cli *client.Client,
return waitChange(chgId)
}

func maybeGetOptionalInstall() *client.OptionalInstallRequest {
f, err := os.Open("./optional-install.json")
if err != nil {
return nil
}
defer f.Close()

var req client.OptionalInstallRequest
if err := json.NewDecoder(f).Decode(&req); err != nil {
return nil
}

return &req
}

// createAndMountFilesystems creates and mounts filesystems. It returns
// an slice with the paths where the filesystems have been mounted to.
func createAndMountFilesystems(bootDevice string, volumes map[string]*gadget.Volume, encryptedDevices map[string]string) ([]string, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
component: snap-with-comps+comp1
type: test
version: 1.0
summary: Component 1
description: First component for snap-with-comps
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: snap-with-comps
version: 1.0
summary: Snap with components
description: A snap that defines components
base: core22
components:
comp1:
type: test
apps:
test:
command: test
15 changes: 15 additions & 0 deletions tests/nested/manual/muinstaller-core/snap-with-comps/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh -e

if [ $# -ne 1 ]; then
echo "pass in a component name to check if it is installed"
exit 1
fi

if [ ! -f "/snap/${SNAP_NAME}/components/${SNAP_REVISION}/${1}/meta/component.yaml" ]; then
echo "component ${1} is not installed!"
exit 1
fi

comp_rev=$(basename "$(readlink -f "/snap/${SNAP_NAME}/components/${SNAP_REVISION}/${1}")")

echo "component ${1} is installed at revision ${comp_rev}"
68 changes: 68 additions & 0 deletions tests/nested/manual/muinstaller-core/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,34 @@ environment:
NESTED_ENABLE_TPM/plain: false
NESTED_ENABLE_SECURE_BOOT/plain: false

# default (unencrypted) case
NESTED_ENABLE_TPM: false
NESTED_ENABLE_SECURE_BOOT: false

# ensure we use our latest code
NESTED_BUILD_SNAPD_FROM_CURRENT: true
NESTED_REPACK_KERNEL_SNAP: true
NESTED_ENABLE_OVMF: true
# image
IMAGE_MOUNTPOINT: /mnt/cloudimg

# by default, we don't specify any optional installs. this means we should
# install all snaps and components
INSTALL_OPTIONAL_SNAP: false
INSTALL_OPTIONAL_COMPONENT: false
INSTALL_OPTIONAL_ALL: false
INSTALL_OPTIONAL_EXPECT_ALL: true

INSTALL_OPTIONAL_SNAP/install_optional_snap: true
INSTALL_OPTIONAL_EXPECT_ALL/install_optional_snap: false

INSTALL_OPTIONAL_SNAP/install_optional_snap_and_comp: true
INSTALL_OPTIONAL_COMPONENT/install_optional_snap_and_comp: true
INSTALL_OPTIONAL_EXPECT_ALL/install_optional_snap_and_comp: true

INSTALL_OPTIONAL_ALL/install_optional_all: true
INSTALL_OPTIONAL_EXPECT_ALL/install_optional_all: true

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
Expand Down Expand Up @@ -82,6 +103,9 @@ execute: |
fi
mv "${NESTED_ASSETS_DIR}"/pc-kernel_*.snap pc-kernel.snap
snap pack ./snap-with-comps --filename=snap-with-comps.snap
snap pack ./comp1 --filename=snap-with-comps+comp1.comp
# prepare a core seed
# TODO:
# - repacked snapd snap
Expand All @@ -92,6 +116,8 @@ execute: |
--channel=edge \
--snap ./pc-kernel.snap \
--snap ./pc.snap \
--snap ./snap-with-comps.snap \
--comp ./snap-with-comps+comp1.comp \
my.model \
./"$SEED_DIR"
Expand Down Expand Up @@ -178,6 +204,32 @@ execute: |
remote.exec "sudo snap install --classic --dangerous $(basename "$MUINSTALLER_SNAP")"
# Run installation
install_disk=$(remote.exec "readlink -f /dev/disk/by-id/virtio-target")
if [ "$INSTALL_OPTIONAL_ALL" = "true" ]; then
echo '{"all": true}' > optional-install.json
elif [ "$INSTALL_OPTIONAL_COMPONENT" = "true" ]; then
echo '{"snaps": ["snap-with-comps"], "components": {"snap-with-comps": ["comp1"]}}' > optional-install.json
elif [ "$INSTALL_OPTIONAL_SNAP" = "true" ]; then
echo '{"snaps": ["snap-with-comps"]}' > optional-install.json
fi
if [ -f optional-install.json ]; then
remote.push optional-install.json
fi
# make sure that the system reports the expected available optional snaps
remote.exec "sudo snap install --edge test-snapd-curl"
remote.exec "sudo curl -s --unix-socket /run/snapd.socket http://localhost/v2/systems/$LABEL" > available.json
cat <<EOF > expected-available.json
{
"snaps": ["snap-with-comps"],
"components": {
"snap-with-comps": ["comp1"];
}
}
EOF
diff -u <(jq --sort-keys . available.json) <(jq --sort-keys . expected-available.json)
remote.exec "sudo muinstaller $LABEL $install_disk"
remote.exec "sudo sync"
Expand Down Expand Up @@ -232,6 +284,22 @@ execute: |
remote.exec "test -f /run/mnt/ubuntu-seed/systems/${LABEL}/snaps/${sn}_${sn_version}.snap"
done
if [ "$INSTALL_OPTIONAL_SNAP" = "true" ] || [ "$INSTALL_OPTIONAL_EXPECT_ALL" = "true" ]; then
sn_version=$(remote.exec "snap list snap-with-comps" | awk 'NR != 1 { print $2 }')
# snap isn't asserted, but it is not in the model so it should end up here
remote.exec "test -f /run/mnt/ubuntu-seed/systems/${LABEL}/snaps/snap-with-comps_${sn_version}.snap"
if [ "$INSTALL_OPTIONAL_EXPECT_ALL" = "true" ]; then
# make sure our component is there too
remote.exec "snap run snap-with-comps.test comp1"
else
not remote.exec "snap run snap-with-comps.test comp1"
fi
else
not remote.exec "snap list snap-with-comps"
fi
# check for asserted snaps
for sn in snapd core"$version"; do
rev=$(remote.exec "snap list ${sn}" | awk 'NR != 1 { print $3 }')
Expand Down

0 comments on commit 5eba35a

Please sign in to comment.