Skip to content

Commit

Permalink
Merge pull request canonical#8986 from sergiocazzolato/tests-add-snap…
Browse files Browse the repository at this point in the history
…s-exec

tests: new snaps-state command - part1
  • Loading branch information
sergiocazzolato authored Sep 27, 2020
2 parents f567084 + 5bd0667 commit abac2b9
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tests/lib/snaps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

make_snap() {
local SNAP_NAME="$1"
local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}"
if [ $# -gt 1 ]; then
SNAP_DIR="$2"
local SNAP_DIR="${2:-$TESTSLIB/snaps/${SNAP_NAME}}"
local SNAP_VERSION="${3:-1.0}"

local META_FILE META_NAME SNAP_FILE
META_FILE="$SNAP_DIR/meta/snap.yaml"
if [ ! -f "$META_FILE" ]; then
echo "snap.yaml file not found for $SNAP_NAME snap"
return 1
fi
local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_all.snap"
META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')"
SNAP_FILE="${SNAP_DIR}/${META_NAME}_${SNAP_VERSION}_all.snap"
# assigned in a separate step to avoid hiding a failure
if [ ! -f "$SNAP_FILE" ]; then
snap pack "$SNAP_DIR" "$SNAP_DIR" >/dev/null || return 1
snap pack "$SNAP_DIR" "$SNAP_DIR" >/dev/null
fi
# echo the snap name
if [ -f "$SNAP_FILE" ]; then
echo "$SNAP_FILE"
else
find "$SNAP_DIR" -name '*.snap' | head -n1
find "$SNAP_DIR" -name "${META_NAME}_*.snap"| head -n1
fi
}

Expand Down
93 changes: 93 additions & 0 deletions tests/lib/tools/snaps-state
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash -e

show_help() {
echo "usage: pack-local <snap-name>"
echo " install-local <snap-name> [OPTIONS]"
echo " install-local-as <snap-name> <dest-name> [OPTIONS]"
echo ""
echo "Available options:"
echo " --devmode --jailmode --classic"
echo ""
echo "Pack and install commands save the packed snap for future uses,"
echo "which is reused on the following calls."
echo "The paths for locating the sources of the snaps to either pack or"
echo "install are the local path and then 'tests/lib/snaps/'"
}

pack_local() {
local SNAP_NAME="$1"
local SNAP_DIR="${2:-$TESTSLIB/snaps/${SNAP_NAME}}"
local SNAP_VERSION="${3:-1.0}"

local META_FILE META_NAME SNAP_FILE
META_FILE="$SNAP_DIR/meta/snap.yaml"
if [ ! -f "$META_FILE" ]; then
echo "snap.yaml file not found for $SNAP_NAME snap"
return 1
fi
META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')"
SNAP_FILE="${SNAP_DIR}/${META_NAME}_${SNAP_VERSION}_all.snap"
# assigned in a separate step to avoid hiding a failure
if [ ! -f "$SNAP_FILE" ]; then
snap pack "$SNAP_DIR" "$SNAP_DIR" >/dev/null
fi
# echo the snap name
if [ -f "$SNAP_FILE" ]; then
echo "$SNAP_FILE"
else
find "$SNAP_DIR" -name "${META_NAME}_*.snap"| head -n1
fi
}

install_local() {
local SNAP_NAME="$1"
local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}"
shift

if [ -d "$SNAP_NAME" ]; then
SNAP_DIR="$PWD/$SNAP_NAME"
fi
SNAP_FILE=$(pack_local "$SNAP_NAME" "$SNAP_DIR")

snap install --dangerous "$@" "$SNAP_FILE"
}

install_local_as() {
local snap="$1"
local name="$2"
shift 2
install_local "$snap" --name "$name" "$@"
}

main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

local subcommand="$1"
local action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done

if [ -z "$(declare -f "$action")" ]; then
echo "snaps-state: no such command: $subcommand"
show_help
exit 1
fi

"$action" "$@"
}

main "$@"
76 changes: 76 additions & 0 deletions tests/main/snaps-state/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
summary: smoke test for the snaps-state tool

prepare: |
snap set system experimental.parallel-instances=true
restore: |
snap set system experimental.parallel-instances=null
execute: |
SNAP_NAME=test-snapd-tools
SNAP_CLASSIC=test-snapd-classic-confinement
SNAP_DEVMODE=test-snapd-devmode
SNAP_JAILMODE=test-devmode-cgroup
# Check help
"$TESTSTOOLS"/snaps-state | MATCH "usage: pack-local <snap-name>"
"$TESTSTOOLS"/snaps-state -h | MATCH "usage: pack-local <snap-name>"
"$TESTSTOOLS"/snaps-state --help | MATCH "usage: pack-local <snap-name>"
# Pack a local snap by using the pack-local subcommand
snap_path=$("$TESTSTOOLS"/snaps-state pack-local "$SNAP_NAME")
snap install --dangerous "${snap_path}"
test-snapd-tools.echo test123 | MATCH "test123"
snap remove "$SNAP_NAME"
# Check the local snap file is already created
test -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap"
rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap"
# Try to pack a local snap which does not exist
"$TESTSTOOLS"/snaps-state pack-local SNAP_NO_EXIST 2>&1 | MATCH "snap.yaml file not found for SNAP_NO_EXIST snap"
# Make and install a snap by using the install-local subcommand
snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_NAME")
test-snapd-tools.echo test123 | MATCH "test123"
snap remove "$SNAP_NAME"
# Check the local snap file is already created
test -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap"
# Make and install a snap when snap file is already created
snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_NAME")
test-snapd-tools.echo test123 | MATCH "test123"
snap remove "$SNAP_NAME"
# Check the local snap file is already created
test -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap"
rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap"
# Make and install a snap by using the install-local-as subcommand
snap_path=$("$TESTSTOOLS"/snaps-state install-local-as "$SNAP_NAME" "$SNAP_NAME"_test)
test-snapd-tools_test.echo test123 | MATCH "test123"
snap remove "$SNAP_NAME"_test
rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_test_1.0_all.snap"
# Make and install a snap by using the install-local subcommand with --devmode
snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_DEVMODE" --devmode)
snap list "$SNAP_DEVMODE"
snap remove "$SNAP_DEVMODE"
rm -f "$TESTSLIB/snaps/${SNAP_DEVMODE}/${SNAP_DEVMODE}_1.0_all.snap"
# Make and install a snap by using the install-local subcommand with --classic
if snap debug sandbox-features --required=confinement-options:classic; then
snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_CLASSIC" --classic)
snap list "$SNAP_CLASSIC" | MATCH 'classic$'
snap remove "$SNAP_CLASSIC"
rm -f "$TESTSLIB/snaps/${SNAP_CLASSIC}/${SNAP_CLASSIC}_1.0_all.snap"
fi
# Make and install a snap by using the install-local subcommand with --jailmode
if [ "$(snap debug confinement)" = strict ] ; then
snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_JAILMODE" --jailmode)
snap list "$SNAP_JAILMODE" | MATCH 'jailmode$'
snap remove "$SNAP_JAILMODE"
rm -f "$TESTSLIB/snaps/${SNAP_JAILMODE}/${SNAP_JAILMODE}_1.0_all.snap"
fi

0 comments on commit abac2b9

Please sign in to comment.