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
|
summary: Check that `snap set` runs configure hook.
details: |
The `snap set` command modifies configuration options for snaps. As part of
this process, configure hooks defined in the snap are run. This test checks
the following scenarios related to `snap set`:
1. If a snap has no configure hook defined, `snap set` fails.
2. If a configure hook errors, `snap set` fails.
3. Configuration options set via `snap set` can be retrieved by the
resulting configure hook.
4. Configuration options can be set to `null`, which removes the option.
5. Configuration options can be unset, which removes the option.
6. Setting an invalid key results in an error.
7. If a snap has a configure hook which results in an error, installation
of the snap fails.
8. If `snap set` is called multiple times, the order in which these calls
are handled is deterministic, with the most recent value set for a given
option taking precedence.
prepare: |
echo "Build basic test package (without hooks)"
"$TESTSTOOLS"/snaps-state install-local basic
echo "Build failing hooks package"
snap pack failing-config-hooks
echo "Build package with hook to run snapctl set"
"$TESTSTOOLS"/snaps-state install-local snapctl-hooks
execute: |
echo "Test that snap set fails without configure hook"
if snap set basic foo=bar; then
echo "Expected snap set to fail without a configure hook"
exit 1
fi
echo "Test that snap set fails when configure hook fails"
if snap set snapctl-hooks command=test-exit-one; then
echo "Expected snap set to fail when configure hook fails"
exit 1
fi
echo "Test that the set value can be retrieved by the hook"
if ! snap set snapctl-hooks command=test-snapctl-get-foo foo=bar; then
echo "Expected hook to be able to retrieve set value"
exit 1
fi
echo "Precondition check before we unset the value with null"
if ! obtained=$(snap get snapctl-hooks foo); then
echo "Expected snap get to be able to retrieve set value"
exit 1
fi
[[ "$obtained" == "bar" ]]
echo "Test that the set value can be null and it removes the option"
if ! snap set snapctl-hooks command=test-snapctl-foo-null foo=null; then
echo "Expected hook to be able to retrieve set value"
exit 1
fi
snap get snapctl-hooks foo 2>&1 | MATCH 'snap "snapctl-hooks" has no "foo" configuration option'
echo "Set foo back"
snap set snapctl-hooks command=test-snapctl-get-foo foo=bar
if ! obtained=$(snap get snapctl-hooks foo); then
echo "Expected snap get to be able to retrieve set value"
exit 1
fi
test "$obtained" = "bar"
echo "Test that the foo value can be unset"
snap set snapctl-hooks command=test-snapctl-foo-null foo!
snap get snapctl-hooks foo 2>&1 | MATCH 'snap "snapctl-hooks" has no "foo" configuration option'
echo "Test that an invalid key results in an error"
if obtained=$(snap set snapctl-hooks invalid_key=value 2>&1); then
echo "Expected usage of an invalid key to result in an error"
exit 1
fi
[[ "$obtained" == *"invalid option name"* ]]
echo "Install should fail altogether as it has a broken hook"
if obtained=$(snap install --dangerous failing-config-hooks_1.0_all.snap 2>&1); then
echo "Expected install of snap with broken configure hook to fail"
exit 1
fi
[[ "$obtained" == *"error from within configure hook"* ]]
echo "Test that the 'snap set' order is deterministic"
for _ in $(seq 50); do
snap set snapctl-hooks command=noop one!
snap set snapctl-hooks one.two=2 one='{"three":3}'
snap get snapctl-hooks -l one.two | MATCH "one.two[ ]*2"
snap get snapctl-hooks -l one.three | MATCH "one.three[ ]*3"
done
echo "Test that 'snap set' correctly handles empty strings"
snap set snapctl-hooks '' 2>&1 | MATCH 'invalid configuration: "" \(want key=value\)'
snap set snapctl-hooks '!' 2>&1 | MATCH 'configuration keys cannot be empty \(use key! to unset a key\)'
snap set snapctl-hooks '=value' 2>&1 | MATCH 'configuration keys cannot be empty'
|