1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
summary: Check that `snap get` works as expected
details: |
Snaps expose configuration options that can be viewed and changed.
The commands for viewing and changing these configuration options
are snap get, snap set and snap unset.
This test verifies that using the `snap get` command one can obtain
the snap configuration set with `snap set` command. Is it also checked
the json output format and error scenarios.
prepare: |
echo "Build basic test package (without hooks)"
snap pack "$TESTSLIB"/snaps/basic
snap install --dangerous basic_1.0_all.snap
echo "Build package with hook to run snapctl set"
snap pack "$TESTSLIB"/snaps/snapctl-hooks
snap install --dangerous snapctl-hooks_1.0_all.snap
execute: |
echo "Test that snap get fails on a snap without any hooks"
if output=$(snap get basic foo); then
echo "snap get unexpectedly worked with output '$output'"
exit 1
fi
echo "Test that getting root document without any configuration produces an error with list format"
if output=$(snap get snapctl-hooks 2>&1); then
echo "snap get didn't fail as expected"
exit 1
fi
expected="error: snap \"snapctl-hooks\" has no configuration"
if [ "$output" != "$expected" ]; then
echo "Expected '$expected' error, but it was '$output'"
exit 1
fi
echo "Test that getting root document without any configuration works for json output"
snap get snapctl-hooks -d | MATCH "^{}$"
echo "Test that values set via snapctl can be obtained via snap get"
if ! snap set snapctl-hooks command=test-snapctl-set-foo; then
echo "snap set unexpectedly failed"
exit 1
fi
if ! output=$(snap get snapctl-hooks command); then
echo "snap get unexpectedly failed"
exit 1
fi
expected="test-snapctl-set-foo"
if [ "$output" != "$expected" ]; then
echo "Expected 'command' to be '$expected', but it was '$output'"
exit 1
fi
if ! output=$(snap get snapctl-hooks foo); then
echo "snap get unexpectedly failed"
exit 1
fi
expected="bar"
if [ "$output" != "$expected" ]; then
echo "Expected 'foo' to be '$expected', but it was '$output'"
exit 1
fi
echo "Test that keys of json documents can be obtained via snap get"
if ! snap set snapctl-hooks command=test-snapctl-set-bar-doc; then
echo "snap set unexpectedly failed"
exit 1
fi
snap get snapctl-hooks bar 2>&1 | MATCH -z "WARNING"
snap get snapctl-hooks -l bar 2>&1 | MATCH -z '^Key.*Value.*bar.a.*{\.\.\.}.*bar.b.*3'
snap get snapctl-hooks -d bar | MATCH -z "^{.*\"bar\": {.*\"a\": {.*\"aa\": 1,.*\"ab\": 2.*},.*\"b\": 3.*}.*}"
snap get snapctl-hooks bar.a.aa | MATCH "^1$"
snap get snapctl-hooks bar.a.ab | MATCH "^2$"
echo "Test that root document can be obtained via snap get"
snap get snapctl-hooks -l 2>&1 | MATCH -z '^Key.*Value.*bar.*{\.\.\.}.*command.*test-snapctl-set-bar-doc.*foo.*bar'
snap get snapctl-hooks -d | MATCH -z "^{.*\"bar\": {.*\"a\": {.*\"aa\": 1,.*\"ab\": 2.*},.*\"b\": 3.*}.*,.*\"command\": \"test-snapctl-set-bar-doc\",.*\"foo\": \"bar\".*}"
echo "Test number formats"
if ! snap set snapctl-hooks command=test-get-int intnumber=1234567890 intnumber2="{\"a\":9876543210}"; then
echo "snap set unexpectedly failed"
exit 1
fi
if ! output=$(snap get snapctl-hooks intnumber); then
echo "snap get unexpectedly failed"
fi
expected="1234567890"
if [ "$output" != "$expected" ]; then
echo "Expected 'intnumber' to be '$expected', but it was '$output'"
exit 1
fi
echo "Test nested values"
snap set snapctl-hooks command=test-get-nested root.key1="a" root.key2="b"
snap set snapctl-hooks command=test-get-nested root.key1="a"
if ! output=$(snap get snapctl-hooks intnumber2); then
echo "snap get unexpectedly failed"
fi
echo "$output" | MATCH ".*\"a\": 9876543210.*"
echo "Ensure config value has correct format"
gojq ".data[\"config\"][\"snapctl-hooks\"].intnumber" /var/lib/snapd/state.json | MATCH "1234567890"
echo "Test unsetting of root.key2 with exclamation mark via snapctl"
# precondition check
snap get snapctl-hooks root.key2 | MATCH "b"
# note, unsetting happens in the configure hook in response to "test-unset" value
snap set snapctl-hooks command=test-unset root.key1="c"
if ! output=$(snap get snapctl-hooks root.key1); then
echo "snap get unexpectedly failed"
fi
snap get snapctl-hooks root.key2 2>&1 | MATCH 'snap "snapctl-hooks" has no "root.key2" configuration option'
echo "Test unsetting of root.key2 with via snapctl unset"
# init and precondition check
snap set snapctl-hooks command=noop root.key2="b"
snap get snapctl-hooks root.key2 | MATCH "b"
# note, unsetting happens in the configure hook in response to "test-unset-with-unset" value
snap set snapctl-hooks command=test-unset-with-unset
snap get snapctl-hooks root.key2 2>&1 | MATCH 'snap "snapctl-hooks" has no "root.key2" configuration option'
echo "Test that config values are not available once snap is removed"
snap remove --purge snapctl-hooks
if output=$(snap get snapctl-hooks foo); then
echo "Expected snap get to fail, but got '$output'"
exit 1
fi
|