gunk-dev/the-valley

web-push-e2e.sh

source8575227271d8main's published tip history
9.0 KiB
1# End-to-end test script for the web push path (see web-push-e2e.nix).
2# Each background server is launched with start, which waits up to 30
3# seconds for its listening address, and the EXIT trap stops all running
4# servers when the script exits.
5set -euo pipefail
6
7export HOME="$TMPDIR/home"
8export XDG_CACHE_HOME="$TMPDIR/cache"
9export XDG_STATE_HOME="$TMPDIR/xdg-state"
10export GIT_CONFIG_NOSYSTEM=1
11export GIT_CONFIG_GLOBAL="$TMPDIR/gitconfig"
12printf '[user]\n\tname = operator\n\temail = operator@example.org\n[init]\n\tdefaultBranch = main\n' > "$GIT_CONFIG_GLOBAL"
13cd "$TMPDIR"
14
15fail() {
16 echo "web-push-e2e: $*" >&2
17 exit 1
18}
19
20# The servers still running, stopped on every exit.
21pids=()
22stop() {
23 local pid=$1 rest=() p
24 kill -TERM "$pid" 2> /dev/null || true
25 for _ in $(seq 150); do
26 kill -0 "$pid" 2> /dev/null || break
27 sleep 0.1
28 done
29 kill -KILL "$pid" 2> /dev/null || true
30 wait "$pid" 2> /dev/null || true
31 for p in "${pids[@]}"; do
32 if [ "$p" != "$pid" ]; then rest+=("$p"); fi
33 done
34 pids=("${rest[@]}")
35}
36cleanup() {
37 for pid in "${pids[@]}"; do stop "$pid"; done
38}
39trap cleanup EXIT
40
41# start NAME LOG COMMAND...: start a server that prints "NAME: listening
42# on ADDR" on stderr once it listens, and wait at most 30 seconds for it.
43# It sets pid and addr.
44start() {
45 local name=$1 log=$2
46 shift 2
47 "$@" 2> "$log" &
48 pid=$!
49 pids+=("$pid")
50 for _ in $(seq 300); do
51 addr=$(sed -n "s/^$name: listening on //p" "$log")
52 if [ -n "$addr" ]; then return; fi
53 kill -0 "$pid" 2> /dev/null || fail "$name exited before it listened: $(cat "$log")"
54 sleep 0.1
55 done
56 fail "$name did not say where it listens within 30 seconds"
57}
58
59# get PATH: valley-web's status and Location for a GET of PATH, with its
60# body in $TMPDIR/body. follow PATH: the same after following redirects.
61get() {
62 curl -sS --max-time 10 -o "$TMPDIR/body" -w '%{http_code} %header{location}' "http://$web$1"
63}
64follow() {
65 curl -sS --max-time 10 --max-redirs 3 -L -o "$TMPDIR/body" -w '%{http_code}' "http://$web$1"
66}
67# serves PATH TEXT: valley-web serves PATH, after its redirect to the
68# tree, with TEXT in the page. gone PATH: it answers 404.
69serves() {
70 local status
71 status=$(follow "$1")
72 [ "$status" = 200 ] || fail "valley-web answered $1 with $status, want 200"
73 grep -qF "$2" "$TMPDIR/body" || fail "valley-web's page for $1 does not show $2"
74}
75gone() {
76 local status
77 status=$(get "$1")
78 [ "$status" = "404 " ] || fail "valley-web answered $1 with $status, want 404"
79}
80
81# One pass of the publisher as the package wraps it, with its pinned git,
82# cue, valley CLI and schemas.
83publish() {
84 timeout -k 10 300 valley-web-publish --instance-repo "$TMPDIR/srv/valley.git" \
85 --instance valley-host --destination example.org --host "$TMPDIR/host.json" \
86 --state "$TMPDIR/publisher" --principals "$TMPDIR/principals.json" "$@"
87}
88
89commit() {
90 git -C "work/$1" add --all
91 git -C "work/$1" commit --quiet -m "$1: $2"
92 git -C "work/$1" push --quiet origin main
93}
94
95# The projects' publication identities, which their policy/read declares
96# and the registry's grants name.
97declare -A id=(
98 [valley]=f83927d5a4ed068cdd02a8cdaa09f26d
99 [app]=9da6809f038df92138c131cd16bed76d
100 [site]=273d5668702918ea7ae079de374db96b
101)
102read_policy() {
103 printf 'package read\n\nproject: "%s"\nidentity: "%s"\nreaders: {%s}\n' "$1" "${id[$1]}" "$2"
104}
105# The instance's registry: the operator governs it, and anyone may read
106# the projects of the scope given at the edge.
107registry() {
108 cat << EOF
109package identity
110
111boundaries: {
112 registry: kind: "registry"
113 web: {kind: "edge-read", destination: "example.org"}
114}
115genesis: "operator"
116principals: operator: {
117 kind: "human"
118 keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"}]
119 grants: {
120 govern: boundary: "registry"
121 read: {boundary: "web", projects: "all"}
122 }
123}
124anyone: grants: read: {boundary: "web", projects: $1}
125EOF
126}
127
128# 1. The instance and its two projects, each with its own README, and both
129# readable by anyone, in the group tools. app has the short alias a, and
130# site the former name homepage.
131for name in valley app site; do
132 git init --quiet --bare "srv/$name.git"
133 git init --quiet "work/$name"
134 git -C "work/$name" remote add origin "$TMPDIR/srv/$name.git"
135 git -C "work/$name" config valley.principal operator
136done
137mkdir -p work/valley/policy/instance work/valley/policy/read work/valley/identity
138echo 'package verification' > work/valley/policy/instance/floor.cue
139read_policy valley 'operator: true' > work/valley/policy/read/read.cue
140registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
141commit valley "the instance"
142projects=$(printf 'app\nsite\nvalley\n' | git -C srv/valley.git hash-object -w --stdin)
143git -C srv/valley.git update-ref refs/the-valley/projects "$projects"
144for name in app site; do
145 mkdir -p "work/$name/policy/read"
146 echo "$TMPDIR/srv/valley.git" > "work/$name/policy/valley"
147 printf '# %s\n\n%s-readme-text\n' "$name" "$name" > "work/$name/README.md"
148 read_policy "$name" 'anyone: true, operator: true' > "work/$name/policy/read/read.cue"
149 commit "$name" "the first commit"
150done
151knot=$(cd work/app && valley knot new outcome "Open the edge" --json | jq -r .knot.id)
152
153cat > host.cue << 'EOF'
154package valley
155
156groups: core: name: "tools"
157projects: app: {group: "core", aliases: ["a"]}
158projects: site: {group: "core", formerNames: ["homepage"]}
159projects: valley: {}
160EOF
161cue export --out json "$schema" host.cue > host.json
162echo '{"operator": "operator"}' > principals.json
163
164# 2. A pass to a directory makes the export key, whose verifier line the
165# receiver pins from its first start. Then the receiver and valley-web
166# start over one state directory, and a pass uploads.
167publish --out first > /dev/null
168test -s publisher/verifier || fail "the first pass made no verifier line"
169echo edge-token-0123456789abcdef0123456789 > token
170echo another-token-0123456789abcdef0123456 > wrong
171receive() {
172 start valley-web-receive receive.log valley-web-receive --state edge --listen 127.0.0.1:0 \
173 --instance valley-host --destination example.org --verifiers publisher/verifier --token-file token
174 receiver_pid=$pid receiver=http://$addr
175}
176serve() {
177 start valley-web web.log valley-web --state edge --listen 127.0.0.1:0
178 web_pid=$pid web=$addr
179}
180receive
181publish --receiver "$receiver" --receiver-token token > pass.out
182grep -q "the receiver activated sequence" pass.out || fail "the pass did not say the receiver activated it: $(cat pass.out)"
183serve
184serves /tools/app app-readme-text
185serves /tools/site site-readme-text
186serves "/tools/app/knots/$knot" "Open the edge"
187[ "$(get "/a/knots/$knot?x=1")" = "308 /tools/app/knots/$knot?x=1" ] ||
188 fail "the alias a answered $(get "/a/knots/$knot?x=1")"
189[ "$(get /tools/homepage/source/)" = "308 /tools/site/source/" ] ||
190 fail "the former name homepage answered $(get /tools/homepage/source/)"
191
192# 3. A comment changes the export, so the next pass uploads. With another
193# token it is refused as unauthenticated, and the edge keeps serving what
194# it had.
195(cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null)
196if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then
197 fail "a pass with another token succeeded"
198fi
199grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)"
200serves "/tools/app/knots/$knot" "Open the edge"
201if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi
202
203# 4. Withdrawing anyone's grant for app takes app off the edge on the
204# next pass, its alias with it, and site stays.
205registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
206commit valley "withdraw app"
207publish --receiver "$receiver" --receiver-token token > /dev/null
208gone /tools/app
209gone /a
210serves /tools/site site-readme-text
211
212# 5. Both servers restart onto the same content.
213stop "$receiver_pid"
214stop "$web_pid"
215receive
216serve
217gone /tools/app
218serves /tools/site site-readme-text
219
220# 6. With the receiver stopped, valley-web keeps serving. A pass that
221# withdraws site fails because its upload cannot connect, not because it
222# failed earlier or hung until timeout ended it, and changes nothing
223# served. Once the receiver is back, the next pass takes site off the edge.
224stop "$receiver_pid"
225serves /tools/site site-readme-text
226read_policy site 'operator: true' > work/site/policy/read/read.cue
227commit site "withdraw it"
228status=0
229publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$?
230case $status in
231 0) fail "a pass to a stopped receiver succeeded" ;;
232 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;;
233esac
234grep -q "the upload failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err ||
235 fail "a pass to a stopped receiver failed with status $status, but not because its upload could not connect: $(cat pass.err)"
236serves /tools/site site-readme-text
237receive
238publish --receiver "$receiver" --receiver-token token > /dev/null
239gone /tools/site
240gone /tools/homepage
241
242touch "$out"