From cdef11a1956374bdbd18870e7045d349d058a8aa Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Tue, 7 Jul 2020 09:36:13 -0300 Subject: [PATCH 01/15] New snaps-state command and test --- tests/lib/tools/snaps-state | 90 +++++++++++++++++++++ tests/lib/tools/suite/snaps-state/task.yaml | 67 +++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 tests/lib/tools/snaps-state create mode 100644 tests/lib/tools/suite/snaps-state/task.yaml diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state new file mode 100755 index 00000000000..7e49c0aedfc --- /dev/null +++ b/tests/lib/tools/snaps-state @@ -0,0 +1,90 @@ +#!/bin/bash + +show_help() { + echo "usage: make-snap [snap-name]" + echo "usage: install-local [snap-name]" + echo "usage: install-local-as [snap-name] [dest-name]" + echo "usage: install-local-devmode [snap-name]" + echo "usage: install-local-classic [snap-name]" + echo "usage: install-local-jailmode [snap-name]" +} + +make_snap() { + local SNAP_NAME="$1" + local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}" + if [ $# -gt 1 ]; then + SNAP_DIR="$2" + fi + local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_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 + fi + # echo the snap name + if [ -f "$SNAP_FILE" ]; then + echo "$SNAP_FILE" + else + find "$SNAP_DIR" -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=$(make_snap "$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" "$@" +} + +install_local_devmode() { + install_local "$1" --devmode +} + +install_local_classic() { + install_local "$1" --classic +} + +install_local_jailmode() { + install_local "$1" --jailmode +} + +if [ $# -eq 0 ]; then + show_help + exit 0 +fi + +subcommand=$1 +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 "Subcommand $subcommand not supported." + show_help + exit 1 +fi + +"$action" "$@" diff --git a/tests/lib/tools/suite/snaps-state/task.yaml b/tests/lib/tools/suite/snaps-state/task.yaml new file mode 100644 index 00000000000..b8ad7f80bdd --- /dev/null +++ b/tests/lib/tools/suite/snaps-state/task.yaml @@ -0,0 +1,67 @@ +summary: smoke test for the snaps-state tool +prepare: | + apt-get install -y shellcheck +restore: | + apt-get remove --purge -y shellcheck +execute: | + # Run shellcheck for to-one-line script + shellcheck "$TESTSTOOLS"/snaps-state + + SNAP_NAME=test-snapd-tools + + # Check help + "$TESTSTOOLS"/snaps-state | MATCH "usage: make-snap \[snap-name\]" + "$TESTSTOOLS"/snaps-state -h | MATCH "usage: make-snap \[snap-name\]" + "$TESTSTOOLS"/snaps-state --help | MATCH "usage: make-snap \[snap-name\]" + + # Make a snap by using the make-snap subcommand + snap_path=$("$TESTSTOOLS"/snaps-state make-snap "$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" + + # 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-devmode subcommand + snap_path=$("$TESTSTOOLS"/snaps-state install-local-devmode "$SNAP_NAME") + test-snapd-tools.echo test123 | MATCH "test123" + snap remove "$SNAP_NAME" + rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" + + # Make and install a snap by using the install-local-classic subcommand + snap_path=$("$TESTSTOOLS"/snaps-state install-local-classic "$SNAP_NAME") + test-snapd-tools.echo test123 | MATCH "test123" + snap list test-snapd-tools | MATCH "classic$" + snap remove "$SNAP_NAME" + rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" + + # Make and install a snap by using the install-local-jailmode subcommand + snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_NAME") + test-snapd-tools.echo test123 | MATCH "test123" + snap remove "$SNAP_NAME" + rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" From b7208fada4633209d5949bee01e576c13b85ece8 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Wed, 8 Jul 2020 19:23:51 -0300 Subject: [PATCH 02/15] Moving snaps-state test to main suite It is because the test needs the snap command to be executed. --- tests/{lib/tools/suite => main}/snaps-state/task.yaml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) rename tests/{lib/tools/suite => main}/snaps-state/task.yaml (93%) diff --git a/tests/lib/tools/suite/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml similarity index 93% rename from tests/lib/tools/suite/snaps-state/task.yaml rename to tests/main/snaps-state/task.yaml index b8ad7f80bdd..d986fbd9499 100644 --- a/tests/lib/tools/suite/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -1,12 +1,6 @@ summary: smoke test for the snaps-state tool -prepare: | - apt-get install -y shellcheck -restore: | - apt-get remove --purge -y shellcheck -execute: | - # Run shellcheck for to-one-line script - shellcheck "$TESTSTOOLS"/snaps-state +execute: | SNAP_NAME=test-snapd-tools # Check help From d7e13e30cb9ca48ab1b1155063c70225690fe422 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Wed, 8 Jul 2020 20:41:19 -0300 Subject: [PATCH 03/15] update snap names on test --- tests/main/snaps-state/task.yaml | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index d986fbd9499..87623166469 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -1,7 +1,15 @@ 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 # Check help "$TESTSTOOLS"/snaps-state | MATCH "usage: make-snap \[snap-name\]" @@ -42,20 +50,19 @@ execute: | rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_test_1.0_all.snap" # Make and install a snap by using the install-local-devmode subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-devmode "$SNAP_NAME") - test-snapd-tools.echo test123 | MATCH "test123" - snap remove "$SNAP_NAME" - rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" + snap_path=$("$TESTSTOOLS"/snaps-state install-local-devmode "$SNAP_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-classic subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-classic "$SNAP_NAME") - test-snapd-tools.echo test123 | MATCH "test123" - snap list test-snapd-tools | MATCH "classic$" - snap remove "$SNAP_NAME" - rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" + snap_path=$("$TESTSTOOLS"/snaps-state install-local-classic "$SNAP_CLASSIC") + snap list "$SNAP_CLASSIC" | MATCH "classic$" + snap remove "$SNAP_CLASSIC" + rm -f "$TESTSLIB/snaps/${SNAP_CLASSIC}/${SNAP_CLASSIC}_1.0_all.snap" # Make and install a snap by using the install-local-jailmode subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_NAME") - test-snapd-tools.echo test123 | MATCH "test123" - snap remove "$SNAP_NAME" - rm -f "$TESTSLIB/snaps/${SNAP_NAME}/${SNAP_NAME}_1.0_all.snap" + snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_DEVMODE") + snap list "$SNAP_DEVMODE" | MATCH "jailmode$" + snap remove "$SNAP_DEVMODE" + rm -f "$TESTSLIB/snaps/${SNAP_DEVMODE}/${SNAP_DEVMODE}_1.0_all.snap" From d5f40ddc70893983e67d83d0f80df5c22c72ab26 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Mon, 13 Jul 2020 17:30:46 -0300 Subject: [PATCH 04/15] Update snap used for jailmode --- tests/main/snaps-state/task.yaml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 87623166469..0c887da1d95 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -10,6 +10,7 @@ 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: make-snap \[snap-name\]" @@ -62,7 +63,7 @@ execute: | rm -f "$TESTSLIB/snaps/${SNAP_CLASSIC}/${SNAP_CLASSIC}_1.0_all.snap" # Make and install a snap by using the install-local-jailmode subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_DEVMODE") - snap list "$SNAP_DEVMODE" | MATCH "jailmode$" - snap remove "$SNAP_DEVMODE" - rm -f "$TESTSLIB/snaps/${SNAP_DEVMODE}/${SNAP_DEVMODE}_1.0_all.snap" + snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_JAILMODE") + snap list "$SNAP_JAILMODE" | MATCH "jailmode$" + snap remove "$SNAP_JAILMODE" + rm -f "$TESTSLIB/snaps/${SNAP_JAILMODE}/${SNAP_JAILMODE}_1.0_all.snap" From 2b3f7930e0f41e2b2c6253559e99118a1ec0148e Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Tue, 14 Jul 2020 13:37:37 -0300 Subject: [PATCH 05/15] New subcommands and test updated --- tests/lib/tools/snaps-state | 35 +++++++++++++------------------- tests/main/snaps-state/task.yaml | 22 ++++++++++---------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 7e49c0aedfc..5a973f78d10 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -1,15 +1,20 @@ -#!/bin/bash +#!/bin/bash -e show_help() { - echo "usage: make-snap [snap-name]" - echo "usage: install-local [snap-name]" - echo "usage: install-local-as [snap-name] [dest-name]" - echo "usage: install-local-devmode [snap-name]" - echo "usage: install-local-classic [snap-name]" - echo "usage: install-local-jailmode [snap-name]" + echo "usage: pack-local " + echo " install-local [OPTIONS]" + echo " install-local-as [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/'" } -make_snap() { +pack_local() { local SNAP_NAME="$1" local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}" if [ $# -gt 1 ]; then @@ -36,7 +41,7 @@ install_local() { if [ -d "$SNAP_NAME" ]; then SNAP_DIR="$PWD/$SNAP_NAME" fi - SNAP_FILE=$(make_snap "$SNAP_NAME" "$SNAP_DIR") + SNAP_FILE=$(pack_local "$SNAP_NAME" "$SNAP_DIR") snap install --dangerous "$@" "$SNAP_FILE" } @@ -48,18 +53,6 @@ install_local_as() { install_local "$snap" --name "$name" "$@" } -install_local_devmode() { - install_local "$1" --devmode -} - -install_local_classic() { - install_local "$1" --classic -} - -install_local_jailmode() { - install_local "$1" --jailmode -} - if [ $# -eq 0 ]; then show_help exit 0 diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 0c887da1d95..0f14ab670d3 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -13,12 +13,12 @@ execute: | SNAP_JAILMODE=test-devmode-cgroup # Check help - "$TESTSTOOLS"/snaps-state | MATCH "usage: make-snap \[snap-name\]" - "$TESTSTOOLS"/snaps-state -h | MATCH "usage: make-snap \[snap-name\]" - "$TESTSTOOLS"/snaps-state --help | MATCH "usage: make-snap \[snap-name\]" + "$TESTSTOOLS"/snaps-state | MATCH "usage: pack-local " + "$TESTSTOOLS"/snaps-state -h | MATCH "usage: pack-local " + "$TESTSTOOLS"/snaps-state --help | MATCH "usage: pack-local " - # Make a snap by using the make-snap subcommand - snap_path=$("$TESTSTOOLS"/snaps-state make-snap "$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" @@ -50,20 +50,20 @@ execute: | 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-devmode subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-devmode "$SNAP_DEVMODE") + # 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-classic subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-classic "$SNAP_CLASSIC") + # Make and install a snap by using the install-local subcommand with --classic + 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" - # Make and install a snap by using the install-local-jailmode subcommand - snap_path=$("$TESTSTOOLS"/snaps-state install-local-jailmode "$SNAP_JAILMODE") + # Make and install a snap by using the install-local subcommand with --jailmode + 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" From 92abf58f7cab0d1682c1f6ed79eb25c1311e9e7f Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Tue, 14 Jul 2020 17:39:57 -0300 Subject: [PATCH 06/15] New command and modification for test --- tests/lib/tools/snaps-state | 8 ++++++++ tests/main/snaps-state/task.yaml | 27 +++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 5a973f78d10..30d18e086e2 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -4,6 +4,7 @@ show_help() { echo "usage: pack-local " echo " install-local [OPTIONS]" echo " install-local-as [OPTIONS]" + echo " is-classic-confinement-supported" echo "" echo "Available options:" echo " --devmode --jailmode --classic" @@ -53,6 +54,13 @@ install_local_as() { install_local "$snap" --name "$name" "$@" } +is_classic_confinement_supported() { + if snap debug sandbox-features --required=confinement-options:classic; then + return 0 + fi + return 1 +} + if [ $# -eq 0 ]; then show_help exit 0 diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 0f14ab670d3..4a10e1c7bd7 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -56,14 +56,25 @@ execute: | snap remove "$SNAP_DEVMODE" rm -f "$TESTSLIB/snaps/${SNAP_DEVMODE}/${SNAP_DEVMODE}_1.0_all.snap" + # check the command is-classic-confinement-supported works + if [[ "$SPREAD_SYSTEM" == fedora-* ]]; then + not "$TESTSTOOLS"/snaps-state is-classic-confinement-supported + elif [[ "$SPREAD_SYSTEM" == ubuntu-1* ]]; then + "$TESTSTOOLS"/snaps-state is-classic-confinement-supported + fi + # Make and install a snap by using the install-local subcommand with --classic - 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" + if $("$TESTSTOOLS"/snaps-state is-classic-confinement-supported); 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 - 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" + 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 From 19f06d27626333633219161b4e97f393ff678d2b Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Fri, 17 Jul 2020 09:59:42 -0300 Subject: [PATCH 07/15] Fix shellcheck --- tests/main/snaps-state/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 4a10e1c7bd7..b27c5d56e0d 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -64,7 +64,7 @@ execute: | fi # Make and install a snap by using the install-local subcommand with --classic - if $("$TESTSTOOLS"/snaps-state is-classic-confinement-supported); then + if "$TESTSTOOLS"/snaps-state is-classic-confinement-supported; then snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_CLASSIC" --classic) snap list "$SNAP_CLASSIC" | MATCH "classic$" snap remove "$SNAP_CLASSIC" From 4c84a371aaa53bf3dce23deb27d14d2176178802 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Thu, 20 Aug 2020 16:08:38 -0300 Subject: [PATCH 08/15] Moving part of the code to to main function --- tests/lib/tools/snaps-state | 54 ++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 30d18e086e2..c4f8613259b 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -61,31 +61,35 @@ is_classic_confinement_supported() { return 1 } -if [ $# -eq 0 ]; then - show_help - exit 0 -fi +main(){ + if [ $# -eq 0 ]; then + show_help + exit 0 + fi + + subcommand=$1 + action= + while [ $# -gt 0 ]; do + case "$1" in + -h|--help) + show_help + exit 0 + ;; + *) + action=$(echo "$subcommand" | tr '-' '_') + shift + break + ;; + esac + done -subcommand=$1 -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 "Subcommand $subcommand not supported." + show_help + exit 1 + fi -if [ -z "$(declare -f "$action")" ]; then - echo "Subcommand $subcommand not supported." - show_help - exit 1 -fi + "$action" "$@" +} -"$action" "$@" +main "$@" From cd5cdab4aad24755cf51dbe3fc78bf6cb8188a1d Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Fri, 28 Aug 2020 17:24:41 -0300 Subject: [PATCH 09/15] Small fixes --- tests/lib/snaps.sh | 4 ++-- tests/lib/tools/snaps-state | 12 ++++++------ tests/main/snaps-state/task.yaml | 5 ++++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/lib/snaps.sh b/tests/lib/snaps.sh index 83c811f61e0..9197dd6bbd0 100644 --- a/tests/lib/snaps.sh +++ b/tests/lib/snaps.sh @@ -9,13 +9,13 @@ make_snap() { local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_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 "${SNAP_NAME}_*_all.snap" | head -n1 fi } diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index c4f8613259b..6aeaf429579 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -24,13 +24,13 @@ pack_local() { local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_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 "${SNAP_NAME}_*_all.snap" | head -n1 fi } @@ -61,14 +61,14 @@ is_classic_confinement_supported() { return 1 } -main(){ +main() { if [ $# -eq 0 ]; then show_help exit 0 fi - subcommand=$1 - action= + local subcommand="$1" + local action= while [ $# -gt 0 ]; do case "$1" in -h|--help) @@ -84,7 +84,7 @@ main(){ done if [ -z "$(declare -f "$action")" ]; then - echo "Subcommand $subcommand not supported." + echo "snaps-state: no such command: $subcommand" show_help exit 1 fi diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index b27c5d56e0d..20cb19f5cdd 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -27,6 +27,9 @@ execute: | 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 "error: cannot pack" + # 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" @@ -66,7 +69,7 @@ execute: | # Make and install a snap by using the install-local subcommand with --classic if "$TESTSTOOLS"/snaps-state is-classic-confinement-supported; then snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_CLASSIC" --classic) - snap list "$SNAP_CLASSIC" | MATCH "classic$" + snap list "$SNAP_CLASSIC" | MATCH classic$ snap remove "$SNAP_CLASSIC" rm -f "$TESTSLIB/snaps/${SNAP_CLASSIC}/${SNAP_CLASSIC}_1.0_all.snap" fi From efcc1c7dd8c2563f3ed79e6ca5cb35414af65e67 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Mon, 31 Aug 2020 09:51:31 -0300 Subject: [PATCH 10/15] revert change finding snapd due to errors on tests --- tests/lib/snaps.sh | 2 +- tests/lib/tools/snaps-state | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/snaps.sh b/tests/lib/snaps.sh index 9197dd6bbd0..2c4c4bba7ea 100644 --- a/tests/lib/snaps.sh +++ b/tests/lib/snaps.sh @@ -15,7 +15,7 @@ make_snap() { if [ -f "$SNAP_FILE" ]; then echo "$SNAP_FILE" else - find "$SNAP_DIR" -name "${SNAP_NAME}_*_all.snap" | head -n1 + find "$SNAP_DIR" -name '*.snap' | head -n1 fi } diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 6aeaf429579..621b9981ae9 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -30,7 +30,7 @@ pack_local() { if [ -f "$SNAP_FILE" ]; then echo "$SNAP_FILE" else - find "$SNAP_DIR" -name "${SNAP_NAME}_*_all.snap" | head -n1 + find "$SNAP_DIR" -name '*.snap' | head -n1 fi } From a1c40cfe529e7c2d8f2a8835dc6c7d93a148763f Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Mon, 21 Sep 2020 18:48:42 -0300 Subject: [PATCH 11/15] Support snap version in make snap funcion --- tests/lib/snaps.sh | 3 ++- tests/lib/tools/snaps-state | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/lib/snaps.sh b/tests/lib/snaps.sh index 2c4c4bba7ea..14a1001d723 100644 --- a/tests/lib/snaps.sh +++ b/tests/lib/snaps.sh @@ -2,11 +2,12 @@ make_snap() { local SNAP_NAME="$1" + local SNAP_VERSION="${2:-1.0}" local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}" if [ $# -gt 1 ]; then SNAP_DIR="$2" fi - local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_all.snap" + local SNAP_FILE="${SNAP_DIR}/${SNAP_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 diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 621b9981ae9..590672df681 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -17,11 +17,12 @@ show_help() { pack_local() { local SNAP_NAME="$1" + local SNAP_VERSION="${2:-1.0}" local SNAP_DIR="$TESTSLIB/snaps/${SNAP_NAME}" if [ $# -gt 1 ]; then SNAP_DIR="$2" fi - local SNAP_FILE="${SNAP_DIR}/${SNAP_NAME}_1.0_all.snap" + local SNAP_FILE="${SNAP_DIR}/${SNAP_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 From 866ad87d4f8bcef683447be429688365e7fb4285 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Thu, 24 Sep 2020 17:46:36 -0300 Subject: [PATCH 12/15] New make_snap which checks on snap.yaml the name --- tests/lib/snaps.sh | 16 ++++++++++------ tests/lib/tools/snaps-state | 24 ++++++++++-------------- tests/main/snaps-state/task.yaml | 4 ++-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/lib/snaps.sh b/tests/lib/snaps.sh index 14a1001d723..6a17c2bd4ec 100644 --- a/tests/lib/snaps.sh +++ b/tests/lib/snaps.sh @@ -2,12 +2,16 @@ make_snap() { local SNAP_NAME="$1" - local SNAP_VERSION="${2:-1.0}" - 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="$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}_${SNAP_VERSION}_all.snap" + local META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')" + local 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 @@ -16,7 +20,7 @@ make_snap() { 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 } diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index 590672df681..e704361b332 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -4,7 +4,6 @@ show_help() { echo "usage: pack-local " echo " install-local [OPTIONS]" echo " install-local-as [OPTIONS]" - echo " is-classic-confinement-supported" echo "" echo "Available options:" echo " --devmode --jailmode --classic" @@ -17,12 +16,16 @@ show_help() { pack_local() { local SNAP_NAME="$1" - local SNAP_VERSION="${2:-1.0}" - 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="$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}_${SNAP_VERSION}_all.snap" + local META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')" + local 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 @@ -31,7 +34,7 @@ pack_local() { 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 } @@ -55,13 +58,6 @@ install_local_as() { install_local "$snap" --name "$name" "$@" } -is_classic_confinement_supported() { - if snap debug sandbox-features --required=confinement-options:classic; then - return 0 - fi - return 1 -} - main() { if [ $# -eq 0 ]; then show_help diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 20cb19f5cdd..0482b61afb6 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -69,7 +69,7 @@ execute: | # Make and install a snap by using the install-local subcommand with --classic if "$TESTSTOOLS"/snaps-state is-classic-confinement-supported; then snap_path=$("$TESTSTOOLS"/snaps-state install-local "$SNAP_CLASSIC" --classic) - snap list "$SNAP_CLASSIC" | MATCH classic$ + snap list "$SNAP_CLASSIC" | MATCH 'classic$' snap remove "$SNAP_CLASSIC" rm -f "$TESTSLIB/snaps/${SNAP_CLASSIC}/${SNAP_CLASSIC}_1.0_all.snap" fi @@ -77,7 +77,7 @@ execute: | # 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 list "$SNAP_JAILMODE" | MATCH 'jailmode$' snap remove "$SNAP_JAILMODE" rm -f "$TESTSLIB/snaps/${SNAP_JAILMODE}/${SNAP_JAILMODE}_1.0_all.snap" fi From 2fa851c4e81f3ce9cb88910a2da6f67e7abe1753 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Thu, 24 Sep 2020 18:02:40 -0300 Subject: [PATCH 13/15] Update the test to adapt to new error message --- tests/main/snaps-state/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 0482b61afb6..69b7cfb2e96 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -28,7 +28,7 @@ execute: | 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 "error: cannot pack" + "$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") From 31074fb01d0f58c8d1abff596d767c6b5be03df1 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Thu, 24 Sep 2020 18:17:03 -0300 Subject: [PATCH 14/15] Remove check from test --- tests/main/snaps-state/task.yaml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/main/snaps-state/task.yaml b/tests/main/snaps-state/task.yaml index 69b7cfb2e96..c51a518fa2d 100644 --- a/tests/main/snaps-state/task.yaml +++ b/tests/main/snaps-state/task.yaml @@ -59,15 +59,8 @@ execute: | snap remove "$SNAP_DEVMODE" rm -f "$TESTSLIB/snaps/${SNAP_DEVMODE}/${SNAP_DEVMODE}_1.0_all.snap" - # check the command is-classic-confinement-supported works - if [[ "$SPREAD_SYSTEM" == fedora-* ]]; then - not "$TESTSTOOLS"/snaps-state is-classic-confinement-supported - elif [[ "$SPREAD_SYSTEM" == ubuntu-1* ]]; then - "$TESTSTOOLS"/snaps-state is-classic-confinement-supported - fi - # Make and install a snap by using the install-local subcommand with --classic - if "$TESTSTOOLS"/snaps-state is-classic-confinement-supported; then + 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" From 5bd06675eb4983ac9e04b57a8b87d85571e672c0 Mon Sep 17 00:00:00 2001 From: Sergio Cazzolato Date: Fri, 25 Sep 2020 09:20:12 -0300 Subject: [PATCH 15/15] fix shell check --- tests/lib/snaps.sh | 7 ++++--- tests/lib/tools/snaps-state | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/lib/snaps.sh b/tests/lib/snaps.sh index 6a17c2bd4ec..28370bf9d70 100644 --- a/tests/lib/snaps.sh +++ b/tests/lib/snaps.sh @@ -5,13 +5,14 @@ make_snap() { local SNAP_DIR="${2:-$TESTSLIB/snaps/${SNAP_NAME}}" local SNAP_VERSION="${3:-1.0}" - local META_FILE="$SNAP_DIR/meta/snap.yaml" + 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 META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')" - local SNAP_FILE="${SNAP_DIR}/${META_NAME}_${SNAP_VERSION}_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 diff --git a/tests/lib/tools/snaps-state b/tests/lib/tools/snaps-state index e704361b332..e3d913ed986 100755 --- a/tests/lib/tools/snaps-state +++ b/tests/lib/tools/snaps-state @@ -19,13 +19,14 @@ pack_local() { local SNAP_DIR="${2:-$TESTSLIB/snaps/${SNAP_NAME}}" local SNAP_VERSION="${3:-1.0}" - local META_FILE="$SNAP_DIR/meta/snap.yaml" + 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 META_NAME="$(grep '^name:' "$META_FILE" | awk '{ print $2 }' | tr -d ' ')" - local SNAP_FILE="${SNAP_DIR}/${META_NAME}_${SNAP_VERSION}_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