gunk-dev/the-valley

web-push-e2e.sh

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