Skip to content

Commit

Permalink
tests/main: add test for snap run inhibition flows
Browse files Browse the repository at this point in the history
* test/main/snap-run-inhibition-flow: remove text fallback check
	Text fallback is inconsistent across systems due to the terminal
	checks in snapd. It is hard to mock a real terminal in all systems
	while redirecting output to a file for testing.

Signed-off-by: Zeyad Gouda <[email protected]>
  • Loading branch information
ZeyadYasser committed Apr 11, 2024
1 parent fe16c73 commit 9f15777
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/main/snap-run-inhibition-flow/api-client/bin/api-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python3

import argparse
import http.client
import sys
import socket

class UnixSocketHTTPConnection(http.client.HTTPConnection):
def __init__(self, socket_path):
super().__init__('localhost')
self._socket_path = socket_path

def connect(self):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self._socket_path)
self.sock = s


def main(argv):
parser = argparse.ArgumentParser('Call the snapd REST API')
parser.add_argument('--socket', default='/run/snapd.socket',
help='The socket path to connect to')
parser.add_argument('--method', default='GET',
help='The HTTP method to use')
parser.add_argument('path', metavar='PATH',
help='The HTTP path to request')
parser.add_argument('body', metavar='BODY', default=None, nargs='?',
help='The HTTP request body')
args = parser.parse_args(argv[1:])

conn = UnixSocketHTTPConnection(args.socket)
conn.request(args.method, args.path, args.body)

response = conn.getresponse()
body = response.read()
print(body.decode('UTF-8'))
return response.status >= 300

if __name__ == '__main__':
sys.exit(main(sys.argv))
8 changes: 8 additions & 0 deletions tests/main/snap-run-inhibition-flow/api-client/meta/snap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: api-client
version: 1
base: core18
apps:
api-client:
command: bin/api-client.py
plugs:
- snap-refresh-observe
56 changes: 56 additions & 0 deletions tests/main/snap-run-inhibition-flow/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
summary: Check that snap run notifies the user about run inhibition due to refreshes.

details: |
This test exercises the inhibition flow triggered when snap run is
inhibited from running due to an onging refresh. When snap run is inhibited
it record a snap-run-inhibit notice which should be parsed by another
client (e.g. snapd-desktop-integration snap).
TODO: Add a check for the text fallback
If snap run detects that no snap has the marker interface connected and
we are running in a terminal then snap run falls back to showing a text
notification.
environment:
SNAPD_INHIBIT_DIR: "/var/lib/snapd/inhibit"

prepare: |
snap install --edge jq
echo "Install snap with marker snap-refresh-observe interface connected"
"$TESTSTOOLS"/snaps-state install-local api-client
snap connect api-client:snap-refresh-observe
# Make sure inhibit dir exists
mkdir -p $SNAPD_INHIBIT_DIR
# Mock test-snapd-tools snap as inhibited due to refresh
snap install test-snapd-tools
SNAP_MOUNT_DIR="$(os.paths snap-mount-dir)"
REVNO="$(readlink "$SNAP_MOUNT_DIR"/test-snapd-tools/current)"
echo -n "refresh" > $SNAPD_INHIBIT_DIR/test-snapd-tools.lock
echo -n '{"previous":"'"${REVNO}"'"}' > $SNAPD_INHIBIT_DIR/test-snapd-tools.refresh
restore: |
rm -f $SNAPD_INHIBIT_DIR/test-snapd-tools.lock
rm -f $SNAPD_INHIBIT_DIR/test-snapd-tools.refresh
snap remove --purge test-snapd-tools
snap remove --purge api-client
snap remove --purge jq
execute: |
echo "Try running inhibited snap"
touch output
test-snapd-tools.echo hi > output 2>&1 &
echo "Command is waiting due to inhibition, no output"
NOMATCH "hi" < output
# Notice is recorded for inhibition
api-client --socket /run/snapd-snap.socket "/v2/notices?types=snap-run-inhibit&keys=test-snapd-tools" | jq '.result[0].occurrences' | MATCH '^1$'
echo "Mark snap as no longer inhibited"
echo -n "" > $SNAPD_INHIBIT_DIR/test-snapd-tools.lock
echo "snap is no longer inhibited, command should run now"
sleep 1
MATCH "hi" < output

0 comments on commit 9f15777

Please sign in to comment.