summaryrefslogtreecommitdiff
diff options
authorMichael Vogt <mvo@ubuntu.com>2023-06-19 19:02:19 +0200
committerMichael Vogt <mvo@ubuntu.com>2023-06-20 19:28:10 +0200
commit8866eae5e01f56a4d73b5f41c8a52cedaf85c801 (patch)
treedf7cae261fa98db6c887a540f35a14cb197101fc
parent6f485e152b56b269268dad43b1c624abc1482d6c (diff)
tests: fix api-changes-follow test
-rw-r--r--tests/main/api-changes-follow/api-client.py43
-rw-r--r--tests/main/api-changes-follow/task.yaml27
2 files changed, 66 insertions, 4 deletions
diff --git a/tests/main/api-changes-follow/api-client.py b/tests/main/api-changes-follow/api-client.py
new file mode 100644
index 0000000000..e7bdc00891
--- /dev/null
+++ b/tests/main/api-changes-follow/api-client.py
@@ -0,0 +1,43 @@
+#!/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()
+ while True:
+ body = response.readline()
+ print(body.decode('UTF-8'), end="", flush=True)
+ if response.status >= 300:
+ return
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
diff --git a/tests/main/api-changes-follow/task.yaml b/tests/main/api-changes-follow/task.yaml
index 1c5a06bed5..726cf5a0bb 100644
--- a/tests/main/api-changes-follow/task.yaml
+++ b/tests/main/api-changes-follow/task.yaml
@@ -1,18 +1,32 @@
summary: verify that the /v2/changes?follow=true works
+debug: |
+ cat follow.json || true
+
execute: |
+ if [ "$(command -v systemd-run)" = "" ]; then
+ echo "SKIP: no systemd-run"
+ exit 0
+ fi
snap install --edge jq
# devmode as the snap does not have snapd-control
snap install test-snapd-curl --devmode --edge
# follow the chanes
- # XXX: use systemd-run
- (test-snapd-curl.curl curl -s --unix-socket /run/snapd.socket http://localhost/v2/changes?follow=true > follow.json) &
+ HERE="$(pwd)"
+ # we cannot use curl here as it will not line-buffer
+ systemd-run --unit follow-changes.service \
+ /bin/sh -c "/usr/bin/python3 $HERE/api-client.py /v2/changes?follow=true > $HERE/follow.json"
+ tests.cleanup defer systemctl stop follow-changes.service
+
+ # ensure it's fully started up
+ retry -n 10 systemctl is-active follow-changes.service
+ # install a snap to get some changes on the socket
snap install test-snapd-sh
- # XXX: use retry instead to ensure everything is written out
- sleep 1
+ # use "retry" here to ensure that the change is fully written out
+ retry -n 20 /bin/sh -c "tail -1 follow.json | jq -r '.ready' | MATCH true"
# first change status is doing
head -1 follow.json | jq -r '.status' | MATCH "Doing"
@@ -20,3 +34,8 @@ execute: |
tail -1 follow.json | jq -r '.status' | MATCH "Done"
tail -1 follow.json | jq -r '.ready' | MATCH true
tail -1 follow.json | jq -r '.kind' | MATCH "install-snap"
+
+
+ # XXX: do install/remove a lot and ensure we do not block and memory
+ # does not grow out of bounds
+