interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / patch / tests / e2e.sh

122 lines · 8215 bytes Raw Blame History
1#!/bin/bash
2# End-to-end loopback run of the git patch intake (contract interplane-connector r1, Change item 5;
3# docs/API.md "Git patches", docs/BRING.md "patch"). HOST test on the Spark: a private copy of forged
4# with scratch folders (patch/tests/lib.sh: fixture repository, data folder, git store, port), a
5# throwaway key in a scratch HOME, the interplane program built from this tree. Nothing of the live
6# forge, ~/git or ~/forge-data is written; the build reads the site's git store as lib.sh says.
7# Steps, one line each:
8# 1. an upload without a session, and one whose statement another key signed, are refused (403)
9# and keep nothing
10# 2. interplane patch --yes, run in a scratch clone with an empty environment, is accepted (exit 0)
11# 3. the patch page, the JSON list, the JSON patch and the raw .patch and .diff routes answer
12# 4. the raw mbox is byte-equal to git format-patch --stdout with the tool's flags
13# 5. git am of the raw route onto the base in a fresh clone succeeds and gives the tool's tree
14# Usage: PATCH_TEST_PORT=18488 PATCH_TEST_CORES=18 bash patch/tests/e2e.sh
15# E2E_TARGET=x86linux builds and runs interplane-linux-x86_64 instead (on the AArch64 Spark it runs
16# under qemu-x86_64 through binfmt); the default is linux (interplane-linux-aarch64).
17# E2E_OUT=<dir> keeps the answers (upload texts, JSON, headers) there for docs/API.md examples;
18# KEEP=1 keeps the temp folder. Exit 0 only when every step held.
19. "$(dirname "$0")/lib.sh"
20REPOF=$OWNER/fixrepo
21OUT=${E2E_OUT:-$T/out}
22mkdir -p "$OUT"
23start
24save() { cp "$T/body" "$OUT/$1"; tr -d '\r' < "$T/hdr" > "$OUT/$1.hdr"; }
25
26echo "== 1. unsigned uploads are refused"
27newkey k1; newkey k2
28signin k1 10.4.0.1 || exit 1
29newclone w; commitf "$T/w" "notes/e2e.txt" "e2e line" "e2e: one note"; mkmbox "$T/w" origin/main
30TITLE="E2E note"; DESC="Written by patch/tests/e2e.sh."; sigmake
31KEPT=$(kept); s=$SES; SES=; C=$(upload); SES=$s; save refused-nosession.txt
32refused "$C" 403 "sign in first: this request carries no session" "upload without a session"
33KEPT=$(kept); sigmake "$T/k2"; C=$(upload); save refused-otherkey.txt
34refused "$C" 403 "the signature does not verify for this session's key and this patch" "statement signed by a key that is not the session's"
35
36echo "== 2. interplane patch --yes from a scratch clone"
37mkdir -p "$T/tool" "$T/home"
38case ${E2E_TARGET:-linux} in
39 linux) TOOL=$T/tool/interplane-linux-aarch64;;
40 x86linux) TOOL=$T/tool/interplane-linux-x86_64;;
41 *) echo "E2E_TARGET must be linux or x86linux"; exit 2;;
42esac
43if OMC="$OMC" taskset -c "$CORES" nice -n 19 sh tools/bring/build.sh "${E2E_TARGET:-linux}" "$T/tool" > "$T/tool.build" 2>&1; then
44 ok "tool built: tools/bring/build.sh ${E2E_TARGET:-linux}, ${TOOL##*/} sha256 $(sha256sum < "$TOOL" | cut -c1-16)"
45else bad "tool build: $(tail -3 "$T/tool.build" | tr '\n' ' ')"; fi
46newclone tc
47commitf "$T/tc" "docs/e2e.md" "# End to end" "docs: add the e2e page"
48commitf "$T/tc" "src/e2e.txt" "e2e source line" "src: e2e line, the patch title"
49MAIN=$(git -C "$T/tc" rev-parse origin/main); HEAD=$(git -C "$T/tc" rev-parse HEAD)
50KEPT=$(kept)
51(cd "$T/tc" && env -i HOME="$T/home" INTERPLANE_BASE="$B" "$TOOL" patch --yes "$OWNER/fixrepo") > "$OUT/tool.out" 2>&1; rc=$?
52URL=$(sed -n 's/^Patch page: //p' "$OUT/tool.out")
53PID16=${URL##*/}
54if [ "$rc" = 0 ] && [ "$(kept)" = $((KEPT+1)) ] && [ "$URL" = "$B$R/patches/$PID16" ] && [ -f "$T/home/.interplane/key" ]; then
55 ok "interplane patch --yes: exit 0, one patch kept, page $URL, key made in the scratch HOME"
56else bad "interplane patch --yes: rc $rc, kept $(kept) (was $KEPT), out: $(tr '\n' '|' < "$OUT/tool.out")"; fi
57grep -qx "Base: $MAIN" "$OUT/tool.out" && grep -qx 'Commits: 2' "$OUT/tool.out" && grep -qx 'Files changed: 2' "$OUT/tool.out" \
58 && grep -qx 'Title: src: e2e line, the patch title' "$OUT/tool.out" \
59 && ok "the tool showed base $MAIN, 2 commits, 2 files and the HEAD subject as title" || bad "tool summary: $(tr '\n' '|' < "$OUT/tool.out")"
60
61echo "== 3. read routes"
62c=$(code GET "$R/patches/$PID16"); save page.html
63ct=$(hdr content-type)
64[ "$c" = 200 ] && [ "${ct%%;*}" = text/html ] && grep -qF 'src: e2e line, the patch title' "$T/body" \
65 && ok "patch page: 200 text/html with the title" || bad "patch page: $c $ct"
66c=$(code GET "$R/patches" -H 'Accept: application/json'); save patch-list.json
67TDID=$(did "$T/home/.interplane/key.pub")
68python3 - "$T/body" "$PID16" "$TDID" "$MAIN" "$HEAD" > "$T/py.out" 2>&1 <<'EOF'
69import json, sys
70d = json.load(open(sys.argv[1]))
71assert d["schema"] == "forge/patch-list/1" and d["per_page"] == 30 and d["page"] == 1, d
72assert d["count"] == len(d["patches"]) and d["next"] is None and d["prev"] is None, d
73r = d["patches"][0]
74assert r["id"] == sys.argv[2] and r["author"]["did"] == sys.argv[3], r
75assert r["base_commit"] == sys.argv[4] and r["head_commit"] != sys.argv[4] and r["commits"] == 2 and r["files"] == 2, r
76print("ok")
77EOF
78[ "$c" = 200 ] && grep -qx ok "$T/py.out" && ok "JSON list: forge/patch-list/1, newest first is the tool's patch, its key as author" || bad "JSON list $c: $(tail -2 "$T/py.out")"
79c=$(code GET "$R/patches/$PID16" -H 'Accept: application/json'); save patch.json
80python3 - "$T/body" "$PID16" "$TDID" > "$T/py.out" 2>&1 <<'EOF'
81import json, sys
82d = json.load(open(sys.argv[1]))
83assert d["schema"] == "forge/patch/1" and d["id"] == sys.argv[2] and d["author"]["did"] == sys.argv[3], d
84assert d["title"] == "src: e2e line, the patch title" and d["description"] == "" and d["status"] == "open", d
85print("ok")
86EOF
87[ "$c" = 200 ] && grep -qx ok "$T/py.out" && ok "JSON patch: forge/patch/1 with id, title, empty description, status open" || bad "JSON patch $c: $(tail -2 "$T/py.out")"
88c=$(code GET "$R/patches/$PID16.patch"); cp "$T/body" "$T/raw.mbox"; save raw.patch
89[ "$c" = 200 ] && [ -s "$T/raw.mbox" ] && ok "raw .patch: 200, $(wc -c < "$T/raw.mbox") bytes, $(hdr content-type)" || bad "raw .patch: $c"
90c=$(code GET "$R/patches/$PID16.diff"); cp "$T/body" "$T/raw.diff"; save raw.diff
91git -C "$T/tc" diff --full-index -M "$MAIN" HEAD > "$T/want.diff"
92[ "$c" = 200 ] && cmp -s "$T/raw.diff" "$T/want.diff" && ok "raw .diff: 200, byte-equal to git diff --full-index -M base HEAD" || bad "raw .diff: $c, $(wc -c < "$T/raw.diff") bytes vs $(wc -c < "$T/want.diff")"
93c=$(code GET "$R/pulls/$PID16"); l=$(hdr location)
94[ "$c" = 302 ] && [ "$l" = "$R/patches/$PID16" ] && ok "/pulls/<id>: 302 to $l" || bad "/pulls/<id>: $c $l"
95
96echo "== 4. raw mbox against git format-patch"
97git -C "$T/tc" format-patch --stdout --no-color --no-ext-diff --no-textconv --no-attach --no-thread --no-cover-letter \
98 --src-prefix=a/ --dst-prefix=b/ --no-signature --no-relative --no-notes "$MAIN..HEAD" > "$T/want.mbox"
99cmp -s "$T/raw.mbox" "$T/want.mbox" && ok "the raw route is byte-equal to git format-patch --stdout of $MAIN..HEAD with the tool's flags ($(sha256sum < "$T/raw.mbox" | cut -c1-16))" \
100 || bad "raw mbox differs from format-patch: $(cmp "$T/raw.mbox" "$T/want.mbox" 2>&1)"
101
102echo "== 5. git am of the raw route in a fresh clone"
103git clone -q "$GITS" "$T/am" && git -C "$T/am" checkout -q --detach "$MAIN" || {
104 bad "could not clone the signed-store fixture at $MAIN"
105 exit 1
106}
107curl -s "$B$R/patches/$PID16.patch" | G -C "$T/am" am -q > "$T/am.out" 2>&1; rc=${PIPESTATUS[1]}
108if [ "$rc" = 0 ] && [ "$(git -C "$T/am" rev-parse 'HEAD^{tree}')" = "$(git -C "$T/tc" rev-parse 'HEAD^{tree}')" ] \
109 && [ "$(git -C "$T/am" rev-list --count "$MAIN..HEAD")" = 2 ]; then
110 ok "git am onto base $MAIN: exit 0, 2 commits, the tree of the tool's HEAD"
111else bad "git am: rc $rc $(tr '\n' ' ' < "$T/am.out")"; fi
112
113echo "== upload answers (curl, for docs/API.md)"
114newclone w2; commitf "$T/w2" "notes/answer.txt" "answer line" "notes: an answer"; mkmbox "$T/w2" origin/main
115TITLE="Answer example"; DESC="Sent with curl."; sigmake
116KEPT=$(kept); C=$(upload); save upload-201.txt; accepted "$C" "a signed upload with curl"
117C=$(upload); save upload-200.txt
118[ "$C" = 200 ] && [ "$(head -1 "$T/body")" = exists ] && ok "the same statement again: 200 exists, nothing new kept ($(kept) kept)" || bad "replay: $C"
119c=$(code GET "$R/patches" -H 'Accept: application/json'); save patch-list2.json
120
121echo "E2E: pass=$PASS fail=$FAIL"
122[ "$FAIL" = 0 ]