Skip to content

Commit

Permalink
tests: replaced curl with snap debug api
Browse files Browse the repository at this point in the history
  • Loading branch information
maykathm committed Oct 3, 2024
1 parent 19c3f09 commit 7ed9b34
Show file tree
Hide file tree
Showing 24 changed files with 50 additions and 101 deletions.
8 changes: 3 additions & 5 deletions tests/core/snapd-maintenance-msg/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ details: |
systems: [ubuntu-core-20-64]

prepare: |
# devmode as the snap does not have snapd-control
snap install test-snapd-curl --devmode --edge
snap install jq
# make sure that the snapd daemon gives us time for comms before
Expand All @@ -17,7 +15,7 @@ prepare: |
systemctl restart snapd
restore: |
snap remove test-snapd-curl jq
snap remove jq
# remove SNAPD_SHUTDOWN_DELAY from /etc/environment again
#shellcheck disable=SC2005
Expand All @@ -33,15 +31,15 @@ execute: |
# closed so we need to catch it in that timeframe.
echo "Testing maintenance message for daemon restarts"
snap install --dangerous "$SNAPD_SNAP" &
retry -n 20 --wait 0.5 sh -c 'test-snapd-curl.curl -sS --unix-socket /run/snapd.socket http://localhost/v2/changes?select=all | jq ".maintenance" | MATCH "daemon is restarting"'
retry -n 20 --wait 0.5 sh -c 'snap debug api '/v2/changes?select=all' | jq ".maintenance" | MATCH "daemon is restarting"'
wait
echo "Restoring the snapd snap"
snap revert snapd
echo "Testing maintenance message for system reboots"
snap refresh core20 --channel=stable --amend &
retry -n 20 --wait 0.5 sh -c 'test-snapd-curl.curl -sS --unix-socket /run/snapd.socket http://localhost/v2/changes?select=all | jq ".maintenance" | MATCH "system is restarting"'
retry -n 20 --wait 0.5 sh -c 'snap debug api '/v2/changes?select=all' | jq ".maintenance" | MATCH "system is restarting"'
wait
REBOOT
Expand Down
12 changes: 5 additions & 7 deletions tests/core/uc20-recovery/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ execute: |
echo "In run mode"
snap install --edge jq
# devmode as the snap does not have snapd-control
snap install test-snapd-curl --devmode --edge
MATCH 'snapd_recovery_mode=run' < /proc/cmdline
# verify we are in run mode via the API
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/system-info > system-info
snap debug api '/v2/system-info' > system-info
jq -r '.result["system-mode"]' < system-info | MATCH 'run'
echo "Obtain available systems"
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/systems > systems.json
snap debug api '/v2/systems' > systems.json
# TODO:UC20: there is only one system for now
jq .result.systems[0].current < systems.json | MATCH 'true'
label="$(jq -r .result.systems[0].label < systems.json)"
Expand All @@ -67,7 +65,7 @@ execute: |
# host data should be accessible
test -e /host/ubuntu-data/systems.json.run
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/systems > systems.json
snap debug api '/v2/systems' > systems.json
jq -r .result.systems[0].actions[].mode < systems.json | sort | tr '\n' ' ' | MATCH 'install run'
label="$(cat /host/ubuntu-data/systems.label)"
Expand All @@ -80,7 +78,7 @@ execute: |
transition_to_run_mode "$label"
elif [ "$SPREAD_REBOOT" == "2" ]; then
echo "In run mode again"
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/system-info > system-info
snap debug api '/v2/system-info' > system-info
jq -r '.result["system-mode"]' < system-info | MATCH 'run'
# now go back to recover mode so we can test that a simple reboot
Expand Down Expand Up @@ -108,6 +106,6 @@ execute: |
REBOOT
elif [ "$SPREAD_REBOOT" == "4" ]; then
echo "In run mode again again"
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/system-info > system-info
snap debug api '/v2/system-info' > system-info
jq -r '.result["system-mode"]' < system-info | MATCH 'run'
fi
3 changes: 1 addition & 2 deletions tests/lib/uc20-recovery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ prepare_recover_mode() {

# we're running in an ephemeral system and thus have to re-install snaps
snap install --edge jq
snap install test-snapd-curl --devmode --edge

MATCH 'snapd_recovery_mode=recover' < /proc/cmdline
# verify we are in recovery mode via the API
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/system-info > system-info
snap debug api /v2/system-info > system-info
jq -r '.result["system-mode"]' < system-info | MATCH 'recover'
}
6 changes: 2 additions & 4 deletions tests/main/api-get-systems-label/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ systems:

execute: |
snap install --edge jq
# devmode as the snap does not have snapd-control
snap install test-snapd-curl --devmode --edge
echo "Find what systems are available"
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/systems > systems
snap debug api /v2/systems > systems
current_label=$(jq -r '.result.systems[0]["label"]' < systems)
echo "Get details for a specific system"
test-snapd-curl.curl -s --unix-socket /run/snapd.socket http://localhost/v2/systems/"$current_label" > current-system
snap debug api /v2/systems/"$current_label" > current-system
echo "Ensure the result contains a model assertion"
jq -r '.result.model.type' < current-system | MATCH model
jq -r '.result.model.series' < current-system | MATCH 16
Expand Down
17 changes: 7 additions & 10 deletions tests/main/apparmor-prompting-flag-restart/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ systems:

prepare: |
snap install jq
if ! command -v curl; then
snap install --devmode --edge test-snapd-curl
snap alias test-snapd-curl.curl curl
fi
# prerequisite for having a prompts handler service
snap set system experimental.user-daemons=true
"$TESTSTOOLS"/snaps-state install-local test-snapd-prompt-handler
Expand Down Expand Up @@ -97,9 +93,9 @@ execute: |
echo "Check that snap CLI reports prompting flag set correctly"
snap get system experimental.apparmor-prompting | MATCH "$1"
echo "Check that /v2/snaps/system/conf reports prompting flag set correctly"
curl -sS --unix-socket /run/snapd.socket http://localhost/v2/snaps/system/conf | jq -r '.result.experimental."apparmor-prompting"' | MATCH "$1"
snap debug api /v2/snaps/system/conf | jq -r '.result.experimental."apparmor-prompting"' | MATCH "$1"
echo "Check that /v2/system-info reports prompting correctly"
curl -sS --unix-socket /run/snapd.socket http://localhost/v2/system-info | jq -r '.result.features."apparmor-prompting".enabled' | MATCH "$1"
snap debug api /v2/system-info | jq -r '.result.features."apparmor-prompting".enabled' | MATCH "$1"
}
echo "Precondition check that snapd is active"
Expand Down Expand Up @@ -143,16 +139,17 @@ execute: |
check_prompting_setting "false"
echo "Enable prompting via API request"
curl -sS --unix-socket /run/snapd.socket http://localhost/v2/snaps/system/conf -X PUT -d '{"experimental.apparmor-prompting": true}' | jq -r '.status' | MATCH "Accepted" || reset_start_limit
echo "Check that snapd restarted after prompting set to true via curl"
echo '{"experimental.apparmor-prompting": true}' | snap debug api -X PUT -H 'Content-Type: application/json' /v2/snaps/system/conf | jq -r '.status' | MATCH "Accepted" || reset_start_limit
echo "Check that snapd restarted after prompting set to true via snap debug"
check_snapd_restarted
check_prompting_setting "true"
echo "Disable prompting via API request"
curl -sS --unix-socket /run/snapd.socket http://localhost/v2/snaps/system/conf -X PUT -d '{"experimental.apparmor-prompting": false}' | jq -r '.status' | MATCH "Accepted" || reset_start_limit
echo '{"experimental.apparmor-prompting": false}' | snap debug api -X PUT -H 'Content-Type: application/json' /v2/snaps/system/conf | jq -r '.status' | MATCH "Accepted" || reset_start_limit
echo "Check that snapd restarted after prompting set to false via curl"
echo "Check that snapd restarted after prompting set to false via snap debug"
check_snapd_restarted
check_prompting_setting "false"
Expand Down
10 changes: 3 additions & 7 deletions tests/main/appstream-id/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ details: |
Verify the AppStream ID is included in the search API response and also validate
that the AppStream ID is included in the installed snaps and apps API response.
# ubuntu-core: no curl
# ubuntu-14.04: curl too old, missing --unix-socket
systems: [-ubuntu-14.04-*, -ubuntu-core*]

prepare: |
snap install --edge jq
Expand All @@ -16,21 +12,21 @@ debug: |
execute: |
echo "Verify that search results contain common-ids"
curl -s --unix-socket /run/snapd.socket --max-time 5 'http://localhost/v2/find?name=test-snapd-appstreamid' > response
timeout 5 snap debug api /v2/find?name=test-snapd-appstreamid > response
# shellcheck disable=SC2002
cat response | jq -r ' .result[0]["common-ids"] | sort | join (",")' | \
MATCH 'io.snapcraft.test-snapd-appstreamid.bar,io.snapcraft.test-snapd-appstreamid.foo'
snap install --edge test-snapd-appstreamid
echo "Verify that installed snap info contains common-ids"
curl -s --unix-socket /run/snapd.socket --max-time 5 'http://localhost/v2/snaps/test-snapd-appstreamid' > response
timeout 5 snap debug api /v2/snaps/test-snapd-appstreamid > response
# shellcheck disable=SC2002
cat response | jq -r ' .result["common-ids"] | sort | join(",")' | \
MATCH 'io.snapcraft.test-snapd-appstreamid.bar,io.snapcraft.test-snapd-appstreamid.foo'
echo "Verify that apps have their common-id set"
curl -s --unix-socket /run/snapd.socket --max-time 5 'http://localhost/v2/apps?names=test-snapd-appstreamid' > response
timeout 5 snap debug api /v2/apps?names=test-snapd-appstreamid > response
# shellcheck disable=SC2002
cat response | jq -r ' .result | sort_by(.name) | [.[]."common-id"] | join(",")' | \
MATCH 'io.snapcraft.test-snapd-appstreamid.bar,,io.snapcraft.test-snapd-appstreamid.foo'
3 changes: 1 addition & 2 deletions tests/main/aux-info/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ systems: [ubuntu-18.04-64, ubuntu-2*, ubuntu-core-*, fedora-*]
prepare: |
snap install snap-store
snap install jq
snap install --devmode --edge test-snapd-curl
execute: |
snap_id=$(snap info snap-store | grep snap-id | awk '{ print $2 }')
Expand All @@ -21,6 +20,6 @@ execute: |
media_length=$(jq '. | length' < media.json)
test "${media_length}" -gt 0
test-snapd-curl.curl -s --unix-socket /run/snapd.socket --max-time 5 'http://localhost/v2/snaps/snap-store' | jq --sort-keys .result.media > snapd-media.json
timeout 5 snap debug api /v2/snaps/snap-store | jq --sort-keys .result.media > snapd-media.json
diff media.json snapd-media.json
12 changes: 4 additions & 8 deletions tests/main/debug-pprof/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ details: |
This test checks that the pprof profiles can be accessed through
the http://localhost/v2/debug/pprof/ API
# ubuntu-core: no curl
# ubuntu-14.04: curl does not have --unix-socket option
systems: [-ubuntu-core-*, -ubuntu-14.04-*]

execute: |
# endpoints are accessible only for the root user
su -c 'curl -s --unix-socket /run/snapd.socket http://localhost/v2/debug/pprof/cmdline' test > unauthorized
MATCH '"kind":"login-required"' < unauthorized
su -c 'snap debug api /v2/debug/pprof/cmdline' test > unauthorized
MATCH "login-required" <<<$(jq '.result.kind' unauthorized)
# one of pprof endpoints exposes a cmdline of the process
curl -s --unix-socket /run/snapd.socket -o reported-cmdline http://localhost/v2/debug/pprof/cmdline
snap debug api /v2/debug/pprof/cmdline > reported-cmdline
# should be the same as in /proc/<pid>/cmdline
mainpid=$(systemctl show -p MainPID snapd.service | cut -f2 -d=)
tr -d '\0' < "/proc/$mainpid/cmdline" > real-cmdline
diff -up <(od -c real-cmdline) <(od -c reported-cmdline)
# try to fetch a heap profile
curl -s --unix-socket /run/snapd.socket -o heap http://localhost/v2/debug/pprof/heap
snap debug api /v2/debug/pprof/heap > heap
# go tool pprof fails if profile is corrupted
go tool pprof -raw ./heap
5 changes: 1 addition & 4 deletions tests/main/generic-unregister/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ details: |
the unregister to be performed immediately and after snapd is
restarted.
# ubuntu-14.04: curl does not have --unix-socket option
systems: [-ubuntu-core-*, -ubuntu-14.04-*]

environment:
UNTIL_REBOOT/rereg: false
UNTIL_REBOOT/until_reboot: true
Expand Down Expand Up @@ -44,7 +41,7 @@ execute: |
keyfile=(/var/lib/snapd/device/private-keys-v1/*)
test -f "${keyfile[0]}"
curl --data '{"action":"forget","no-registration-until-reboot":'${UNTIL_REBOOT}'}' --unix-socket /run/snapd.socket http://localhost/v2/model/serial
echo "{\"action\":\"forget\",\"no-registration-until-reboot\":${UNTIL_REBOOT}}" | snap debug api -X POST -H 'Content-Type: application/json' /v2/model/serial
snap model --serial 2>&1|MATCH "error: device not registered yet"
not test -e "${keyfile[0]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ execute: |
echo "Check snap can access rules via /v2/interfaces/requests/rules"
api-client --socket /run/snapd-snap.socket "/v2/interfaces/requests/rules" | jq '."status-code"' | MATCH '^'"$EXPECTED_HTTP_CODE"'$'
# XXX: creating rules requires polkit authentication, so for now, use curl
# XXX: should use `snap debug api` instead of curl, but should really just use api-client
# echo "Check snap can create rule via /v2/interfaces/requests/rules"
# api-client --socket /run/snapd-snap.socket --method=POST '{"action":"add","rule":{"snap":"api-client","interface":"home","constraints":{"path-pattern":"/path/to/file","permissions":["read","write","execute"]},"outcome":"allow","lifespan":"forever"}}' "/v2/interfaces/requests/rules" > result.json
apt install -y curl
curl -sS --unix-socket /run/snapd.socket -d '{"action":"add","rule":{"snap":"api-client","interface":"home","constraints":{"path-pattern":"/path/to/file","permissions":["read","write","execute"]},"outcome":"allow","lifespan":"forever"}}' "http://localhost/v2/interfaces/requests/rules" | tee result.json
echo '{"action":"add","rule":{"snap":"api-client","interface":"home","constraints":{"path-pattern":"/path/to/file","permissions":["read","write","execute"]},"outcome":"allow","lifespan":"forever"}}' | snap debug api -X POST -H 'Content-Type: application/json' "/v2/interfaces/requests/rules" | tee result.json
jq '."status-code"' < result.json | MATCH '^'"$EXPECTED_HTTP_CODE"'$'
RULE_ID=$(jq '."result"."id"' < result.json | tr -d '"')
echo "Check snap can view a single rule via /v2/interfaces/requests/rules/<ID>"
Expand Down
7 changes: 3 additions & 4 deletions tests/main/snap-quota-memory/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ systems:

prepare: |
snap install go-example-webserver jq remarshal hello-world
snap install test-snapd-curl --edge --devmode
execute: |
echo "Create a group with a snap in it"
Expand Down Expand Up @@ -81,7 +80,7 @@ execute: |
exit 1
fi
snapdSaysMemUsage="$(sudo snap run test-snapd-curl.curl --unix-socket /run/snapd.socket http://localhost/v2/quotas/group-one | jq -r '.result.current.memory')"
snapdSaysMemUsage="$(sudo snap debug api /v2/quotas/group-one | jq -r '.result.current.memory')"
kernelSaysMemUsage="$(cat "$cgroupMemFile")"
pyCmd="import math; print(math.ceil(abs($snapdSaysMemUsage - $kernelSaysMemUsage) / $snapdSaysMemUsage * 100))"
Expand Down Expand Up @@ -176,7 +175,7 @@ execute: |
# in reporting it's memory usage on old systemd versions
snap set-quota group-five --memory=10MB --parent=group-four hello-world
snapdSaysMemUsage="$(sudo snap run test-snapd-curl.curl --unix-socket /run/snapd.socket http://localhost/v2/quotas/group-five | jq -r '.result.current.memory')"
snapdSaysMemUsage="$(sudo snap debug api /v2/quotas/group-five | jq -r '.result.current.memory')"
# both 0 and up to 12KiB values are expected here, 0 is for older systemd/kernels
# where an empty cgroup has exactly 0, but on newer systems there is some
# minimum amount of accounting memory for an empty cgroup, which is observed
Expand All @@ -190,7 +189,7 @@ execute: |
exit 1
esac
snapdSaysMemUsage="$(sudo snap run test-snapd-curl.curl --unix-socket /run/snapd.socket http://localhost/v2/quotas/group-four | jq -r '.result.current.memory')"
snapdSaysMemUsage="$(sudo snap debug api /v2/quotas/group-four | jq -r '.result.current.memory')"
case "$snapdSaysMemUsage" in
null|0|4096|8192|12288)
# expected
Expand Down
1 change: 0 additions & 1 deletion tests/main/snap-quota-thread/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ systems:

prepare: |
snap install test-snapd-stressd --edge --devmode
snap install test-snapd-curl --edge --devmode
restore: |
echo "Stop the service"
Expand Down
12 changes: 1 addition & 11 deletions tests/main/snapctl/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ details: |
prepare: |
snap pack "$TESTSLIB"/snaps/snapctl-hooks
snap install --dangerous snapctl-hooks_1.0_all.snap
# ensure curl is available (needed for e.g. core18)
if ! command -v curl; then
snap install --devmode --edge test-snapd-curl
snap alias test-snapd-curl.curl curl
fi
restore: |
if snap list test-snapd-curl; then
snap remove --purge test-snapd-curl
fi
execute: |
echo "Verify that snapctl -h runs without a context"
Expand Down Expand Up @@ -47,7 +37,7 @@ execute: |
echo "Verify that the snapd API is only available via the snapd socket"
if ! printf 'GET /v2/snaps HTTP/1.0\r\n\r\n' | nc -U -w 1 /run/snapd.socket | grep '200 OK'; then
echo "Expected snapd API to be available on the snapd socket"
echo "Got: $(curl -s --unix-socket /run/snapd.socket http:/v2/snaps)"
echo "Got: $(snap debug api /v2/snaps)"
exit 1
fi
Expand Down
9 changes: 4 additions & 5 deletions tests/main/snapshot-exclusions-dynamic/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ environment:

prepare: |
"$TESTSTOOLS"/snaps-state install-local test-snap
snap install --devmode --edge test-snapd-curl
snap install jq
debug: |
Expand Down Expand Up @@ -48,11 +47,11 @@ execute: |
# and is valid
snap check-snapshot "$SET_ID"
# and the snapshot metadata does not include an options entry
test-snapd-curl.curl -s --unix-socket /run/snapd.socket --max-time 5 http://localhost/v2/snapshots?set="$SET_ID" | NOMATCH '"options":'
timeout 5 snap debug api /v2/snapshots?set="$SET_ID" | NOMATCH '"options":'
# Create snapshot that will apply dynamic exclusions, and grab the set ID
#shellcheck disable=SC2016
RESPONSE=$( test-snapd-curl.curl -s --unix-socket /run/snapd.socket --max-time 5 http://localhost/v2/snaps -X POST -H 'Content-Type: application/json' -d '{"action": "snapshot", "snaps": ["test-snap"], "snapshot-options": {"test-snap":{"exclude":["$SNAP_DATA/dynamic-exclude.txt", "$SNAP_COMMON/dynamic-exclude.txt", "$SNAP_USER_COMMON/dynamic-exclude.txt", "$SNAP_USER_DATA/dynamic-exclude.txt"]}}}' )
RESPONSE=$( echo '{"action": "snapshot", "snaps": ["test-snap"], "snapshot-options": {"test-snap":{"exclude":["$SNAP_DATA/dynamic-exclude.txt", "$SNAP_COMMON/dynamic-exclude.txt", "$SNAP_USER_COMMON/dynamic-exclude.txt", "$SNAP_USER_DATA/dynamic-exclude.txt"]}}}' | timeout 5 snap debug api -X POST -H 'Content-Type: application/json' /v2/snaps )
SET_ID=$( echo "$RESPONSE" | jq '.result."set-id"' )
echo "$SET_ID" | MATCH "^[0-9]+$"
CHANGE=$( echo "$RESPONSE" | jq ".change" | grep -o "[0-9]*" )
Expand All @@ -65,7 +64,7 @@ execute: |
# and is valid
snap check-snapshot "$SET_ID"
# and the snapshot metadata includes options
test-snapd-curl.curl -s --unix-socket /run/snapd.socket --max-time 5 http://localhost/v2/snapshots?set="$SET_ID" | MATCH '"options":'
timeout 5 snap debug api /v2/snapshots?set="$SET_ID" | MATCH '"options":'
# Check that shapshot is exportable
snap export-snapshot "$SET_ID" "${SET_ID}_export.snapshot"
Expand All @@ -79,7 +78,7 @@ execute: |
"\$SNAP_USER_COMMON/dynamic-exclude.txt"
"\$SNAP_USER_DATA/dynamic-exclude.txt"
EOF
test-snapd-curl.curl -s --unix-socket /run/snapd.socket --max-time 5 http://localhost/v2/snapshots?set="$SET_ID" | jq .result[0].snapshots[0].options.exclude[] > actual_options_entry
timeout 5 snap debug api /v2/snapshots?set="$SET_ID" | jq .result[0].snapshots[0].options.exclude[] > actual_options_entry
diff -u expected_options_entry actual_options_entry
# Remove the canaries to test restore
Expand Down
3 changes: 1 addition & 2 deletions tests/nested/core/core20-basic/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ execute: |
fi
# the remove API isn't exposed by snap recovery yet
remote.exec "sudo snap install --devmode --edge test-snapd-curl"
remote.exec "sudo test-snapd-curl.curl --unix-socket /run/snapd.socket -D- -d '{\"action\":\"remove\"}' http://localhost/v2/system-recovery-keys"
remote.exec "echo '{\"action\":\"remove\"}' | sudo snap debug api -X POST -H 'Content-Type: application/json' /v2/system-recovery-keys"
# keys were removed
remote.exec "test ! -f /var/lib/snapd/device/fde/recovery.key"
Expand Down
Loading

0 comments on commit 7ed9b34

Please sign in to comment.