Skip to content

Commit

Permalink
test(spread): add support for spread tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tigarmo committed Jan 9, 2024
1 parent 86c6382 commit 3363260
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "tools/external"]
path = tools/external
url = https://github.com/snapcore/snapd-testing-tools.git
branch = main
88 changes: 88 additions & 0 deletions spread.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
project: snapcraft-rocks

# note: the `path` is inside /root because the docker snap has issues
# mounting directories that are outside the user's home
path: /root/snapcraft-rocks
environment:
PROJECT_PATH: /root/snapcraft-rocks
SNAPD_TESTING_TOOLS: $PROJECT_PATH/tools/external/tools
PATH: /snap/bin:$PATH:$SNAPD_TESTING_TOOLS:$PROJECT_PATH/tools/spread
# This variable is used to set the base in multi-base projects
SNAPCRAFT_BASE: core22

include:
- tests/
- tools/

backends:
google:
key: '$(HOST: echo "$SPREAD_GOOGLE_KEY")'
location: snapd-spread/us-east1-b
halt-timeout: 2h
systems:
- ubuntu-20.04-64:
workers: 2
storage: 40G
- ubuntu-22.04-64:
workers: 2
storage: 40G
- fedora-37-64:
workers: 2
storage: 40G

prepare: |
# if the 'tools' directory inside the submodule does not exist, then assume the submodule is empty
if [[ ! -d "$SNAPD_TESTING_TOOLS" ]]; then
echo "Cannot run spread because submodule 'snapd-testing-tools' is empty. Fetch with 'git submodule update --init' and rerun spread."
exit 1
fi
if os.query is-ubuntu; then
tempfile="$(mktemp)"
if ! apt-get update > "$tempfile" 2>&1; then
cat "$tempfile"
exit 1
fi
fi
tests.pkgs install snapd
snap wait system seed.loaded
# The /snap directory does not exist in some environments
[ ! -d /snap ] && ln -s /var/lib/snapd/snap /snap
# Hold snap refreshes for 24h.
snap set system refresh.hold="$(date --date=tomorrow +%Y-%m-%dT%H:%M:%S%:z)"
if ! snap watch --last=auto-refresh?; then
journalctl -xe
fi
if ! snap watch --last=install?; then
journalctl -xe
fi
if [ "$SPREAD_SYSTEM" = "fedora-37-64" ]; then
# Latest docker snap needs a more recent version of snapd than Fedora ships
# https://github.com/canonical/rockcraft/pull/277
snap install docker --channel=core18/stable
else
snap install docker
fi
# make sure docker is working
retry -n 10 --wait 2 sh -c 'docker run --rm hello-world'
# install rockcraft just to use its bundled skopeo
snap install --classic --stable rockcraft
load_snapcraft_rock
restore-each: |
# Cleanup after each task.
rm -f *.snap
suites:
tests/spread/general/:
summary: general tests that should work on all bases and Snapcraft versions
16 changes: 16 additions & 0 deletions tests/spread/general/hello/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: hello-world
version: "1.0"
summary: test
description: hello-world
grade: devel
confinement: strict
base: SNAPCRAFT_BASE

parts:
hello-world:
plugin: nil
stage-packages: ["hello"]

apps:
hello-world:
command: usr/bin/hello
9 changes: 9 additions & 0 deletions tests/spread/general/hello/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
summary: build & run a basic "hello world" snap

execute: |
# pack the snap
run_snapcraft pack
# install & run it
snap install --dangerous hello-world_1.0_amd64.snap
hello-world -g "hello-from-snap" | MATCH "hello-from-snap"
1 change: 1 addition & 0 deletions tools/external
Submodule external added at 5121bf
13 changes: 13 additions & 0 deletions tools/spread/load_snapcraft_rock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# load_snapcraft_rock: helper to load the Snapcraft rock into docker.
#
# The rock is always loaded as "snapcraft-rock", regardless of supported base.

if stat ${PROJECT_PATH}/tests/*.rock 2>/dev/null; then
rockname=$(ls ${PROJECT_PATH}/tests/*.rock)
/snap/rockcraft/current/bin/skopeo --insecure-policy copy oci-archive:${rockname} docker-daemon:snapcraft-rock:latest
else
echo "Expected a rock to exist in ${PROJECT_PATH}/tests/"
exit 1
fi
7 changes: 7 additions & 0 deletions tools/spread/run_snapcraft
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# run_snapcraft: helper to run Snapcraft inside a container.

# replace the SNAPCRAFT_BASE token with the correct base for our Snapcraft image
sed --in-place=.bak "s/SNAPCRAFT_BASE/$SNAPCRAFT_BASE/" snap/snapcraft.yaml
docker run --rm -v `pwd`:/project snapcraft-rock:latest "$@"

0 comments on commit 3363260

Please sign in to comment.