interplane.Get Omega
Read in your language

interplane.io

Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.

Brought by drakestapleton

branch main Branches Tags History

interplane.io / scripts / tests / check-sync.sh

507 lines · 30043 bytes Raw Blame History
1#!/bin/sh
2# Controls for scripts/sync-to-hub.sh. Nothing here contacts a server or writes outside its own
3# temporary folder. git is the real git, working on local repositories made here (the history,
4# and the project repository of a signed store that holds it); the git wrapper only traces calls
5# and refuses any remote, clone, fetch or ref change. ipl is a stand-in for `ipl verify`: it answers
6# with the real tool's OK line for the store's actual main, or refuses (exit 3) as the real tool
7# does when the project does not verify or the owner asked for is not the pinned one. quietlock,
8# uname, the compiler, the builder, rsync and sudo are stubs. The sudo stub replays the root copy
9# into a temporary folder so that "the destination is empty" is measured, not assumed.
10set -eu
11ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
12TEST_ROOT=$(mktemp -d)
13trap 'rm -rf "$TEST_ROOT"' 0
14trap 'exit 1' 1 2 15
15REAL_SHA256=$(command -v sha256sum)
16REAL_GIT=$(command -v git)
17REAL_RSYNC=$(command -v rsync)
18GIT_CONFIG_GLOBAL=/dev/null
19GIT_CONFIG_NOSYSTEM=1
20GIT_AUTHOR_NAME=fixture GIT_AUTHOR_EMAIL=fixture GIT_COMMITTER_NAME=fixture GIT_COMMITTER_EMAIL=fixture
21GIT_AUTHOR_DATE='2000-01-01T00:00:00Z' GIT_COMMITTER_DATE='2000-01-01T00:00:00Z'
22export TEST_ROOT REAL_SHA256 REAL_GIT REAL_RSYNC GIT_CONFIG_GLOBAL GIT_CONFIG_NOSYSTEM
23export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_AUTHOR_DATE GIT_COMMITTER_DATE
24OWNER_FP=SHA256:FixtureOwnerKey0000000000000000000000000000
25OTHER_FP=SHA256:SomeOtherOwnerKey11111111111111111111111111
26mkdir -p "$TEST_ROOT/bin"
27
28# ---- the history (A, then B, then C) and the signed store's repository for the project
29UP="$TEST_ROOT/upstream"
30STORE="$TEST_ROOT/store"
31SRC="$STORE/projects/interplane.io/repo.git"
32mkdir -p "$UP/scripts" "$UP/native/lib" "$UP/content"
33"$REAL_GIT" -C "$UP" init -q
34"$REAL_GIT" -C "$UP" symbolic-ref HEAD refs/heads/main
35cp "$ROOT/scripts/build-native.sh" "$UP/scripts/build-native.sh"
36cp "$ROOT/native/site.om" "$UP/native/site.om"
37cp "$ROOT/native/translate.om" "$UP/native/translate.om"
38cp "$ROOT/native/lib/sha2.om" "$UP/native/lib/sha2.om"
39cp "$ROOT/native/omc.sha256" "$UP/native/omc.sha256"
40read -r PIN_HASH _ < "$ROOT/native/omc.sha256"
41export PIN_HASH
42commit() {
43 printf '%s\n' "$1" > "$UP/content/marker.txt"
44 "$REAL_GIT" -C "$UP" add -A
45 "$REAL_GIT" -C "$UP" commit -q -m "$1"
46 "$REAL_GIT" -C "$UP" rev-parse HEAD
47}
48A=$(commit release-a)
49B=$(commit release-b)
50printf 'note changed\n' >> "$UP/native/lib/sha2.om"
51C=$(commit release-c-with-a-changed-sha2-unit)
52"$REAL_GIT" init -q --bare "$SRC"
53"$REAL_GIT" -C "$SRC" symbolic-ref HEAD refs/heads/main
54"$REAL_GIT" -C "$UP" push -q "$SRC" "$A:refs/heads/main" "$C:refs/heads/history"
55# store_main COMMIT: what `ipl land` would leave, the store's main at COMMIT
56store_main() { "$REAL_GIT" -C "$SRC" update-ref refs/heads/main "$1"; }
57
58cat > "$TEST_ROOT/bin/git" <<'STUB'
59#!/bin/sh
60set -eu
61printf 'git %s\n' "$*" >> "$TEST_ROOT/trace"
62for a in "$@"; do
63 case "$a" in
64 ls-remote|clone|fetch|pull|push|remote|update-ref|*://*) echo "git $a in deployment test: the source is the store, read in place" >&2; exit 90 ;;
65 esac
66done
67exec "$REAL_GIT" "$@"
68STUB
69cat > "$TEST_ROOT/bin/ipl" <<'STUB'
70#!/bin/sh
71set -eu
72printf 'ipl %s\n' "$*" >> "$TEST_ROOT/trace"
73[ "$#" -eq 5 ] && [ "$1" = verify ] && [ "$2" = "$STORE" ] && [ "$3" = interplane.io ] && [ "$4" = --expect-owner ] || { echo 'unexpected ipl command' >&2; exit 90; }
74# As the real tool: exit 3 when the owner asked for is not the pinned owner, or nothing verifies.
75[ "$5" = "$OWNER_FP" ] || { printf 'ipl: current owner pin is %s, not %s\n' "$OWNER_FP" "$5" >&2; exit 3; }
76[ "${STUB_IPL:-ok}" != fail ] || { echo 'ipl: interplane.io: does not verify: fixture' >&2; exit 3; }
77HEAD=$("$REAL_GIT" --git-dir="$STORE/projects/interplane.io/repo.git" rev-parse refs/heads/main)
78ID=$(printf '%064d' 1) CH=$(printf '%064d' 2)
79ok() { printf 'OK %s %s %s %s public %s 8 %s\n' "$1" "$ID" "$2" "$HEAD" "$3" "$CH"; }
80case "${STUB_IPL:-ok}" in
81 ok) ok interplane.io main "$5" ;;
82 other-owner) ok interplane.io main "$OTHER_FP" ;;
83 other-project) ok interplane-forge main "$5" ;;
84 other-branch) ok interplane.io next "$5" ;;
85 two) ok interplane.io main "$5"; ok interplane.io main "$5" ;;
86 empty) ;;
87 *) exit 95 ;;
88esac
89STUB
90cat > "$TEST_ROOT/bin/quietlock" <<'STUB'
91#!/bin/sh
92exit "${STUB_QUIET:-0}"
93STUB
94cat > "$TEST_ROOT/bin/uname" <<'STUB'
95#!/bin/sh
96printf '%s\n' "${STUB_MACHINE:-aarch64}"
97STUB
98cat > "$TEST_ROOT/bin/sha256sum" <<'STUB'
99#!/bin/sh
100set -eu
101case "$1" in
102 "$TEST_ROOT/compiler"|"$TEST_ROOT/omega/seed/out/omc")
103 if [ "${STUB_BAD_COMPILER:-0}" = 1 ]; then
104 printf '%s %s\n' invalid "$1"
105 else
106 printf '%s %s\n' "$PIN_HASH" "$1"
107 fi ;;
108 */native/lib/sha2.om) exec "$REAL_SHA256" "$@" ;;
109 *) [ "${STUB_HASH_FAIL:-0}" = 0 ] || exit 91
110 exec "$REAL_SHA256" "$@" ;;
111esac
112STUB
113cat > "$TEST_ROOT/compiler" <<'STUB'
114#!/bin/sh
115set -eu
116echo compile >> "$TEST_ROOT/trace"
117cat > "$TEST_ROOT/compiled-input"
118[ "${STUB_COMPILE_FAIL:-0}" = 0 ] || exit 92
119cat "$TEST_ROOT/builder-template"
120STUB
121# The stand-in builder runs from the exported commit, like the real one, and copies the commit's
122# marker into the page so a control can tell which commit was built.
123cat > "$TEST_ROOT/builder-template" <<'STUB'
124#!/bin/sh
125set -eu
126printf 'builder %s\n' "$1" >> "$TEST_ROOT/trace"
127case "$1" in
128 build)
129 [ "${STUB_BUILD_FAIL:-0}" = 0 ] || exit 93
130 test ! -e "$2"
131 mkdir -p "$2/public"
132 BANNER=
133 [ "${STUB_TESTDATA:-}" != banner ] || BANNER='<p data-test-data>TEST DATA</p>'
134 printf '<!doctype html><title>Fixture</title><p>%s</p>%s\n' "$(cat content/marker.txt)" "$BANNER" > "$2/index.html"
135 cp "$2/index.html" "$TEST_ROOT/built-index"
136 printf '%s\n' '{"source":"committed"}' > "$2/facts.json"
137 printf '%s\n' "${STUB_ASSET:-base}" > "$2/public/style.css"
138 [ "${STUB_TESTDATA:-}" != file ] || printf 'fixture output\n' > "$2/TEST-DATA"
139 [ "${STUB_TESTDATA:-}" != manifest ] || printf '%064d TEST-DATA\n' 0 > "$2/manifest.sha256"
140 if [ "${STUB_HOP:-0}" != 0 ]; then
141 mkdir -p "$3"
142 printf '<!doctype html><title>Hop fixture %s</title>\n' "$STUB_HOP" > "$3/index.html"
143 printf '#!/bin/sh\n' > "$3/install.sh"
144 [ "${STUB_TESTDATA:-}" != hopfile ] || printf 'fixture output\n' > "$3/TEST-DATA"
145 fi
146 ;;
147 verify) [ "${STUB_VERIFY_FAIL:-0}" = 0 ] || exit 94 ;;
148 *) exit 95 ;;
149esac
150STUB
151cat > "$TEST_ROOT/bin/rsync" <<'STUB'
152#!/bin/sh
153set -eu
154printf 'rsync %s\n' "$*" >> "$TEST_ROOT/trace"
155[ "${STUB_RSYNC_FAIL:-0}" = 0 ] || exit 96
156STUB
157# sudo: records the exact command, then replays the copy into the temporary destination without
158# the ownership change (the test is not root). Only the one expected command shape is accepted.
159cat > "$TEST_ROOT/bin/sudo" <<'STUB'
160#!/bin/sh
161set -eu
162printf 'sudo %s\n' "$*" >> "$TEST_ROOT/trace"
163[ "$#" -eq 9 ] && [ "$1" = -n ] && [ "$2" = /usr/bin/rsync ] && [ "$3" = -rt ] && [ "$4" = --delete ] &&
164 [ "$5" = --chmod=D755,F644 ] && [ "$6" = --chown=root:root ] && [ "$7" = --exclude=.well-known ] || { echo 'unexpected root command' >&2; exit 97; }
165case "$8" in "$TEST_ROOT"/*/hopbot.stage/) ;; *) exit 97 ;; esac
166case "$9" in "$TEST_ROOT"/*) ;; *) exit 97 ;; esac
167[ "${STUB_SUDO_FAIL:-0}" = 0 ] || exit 98
168exec "$REAL_RSYNC" -rt --delete --chmod=D755,F644 --exclude=.well-known "$8" "$9"
169STUB
170chmod +x "$TEST_ROOT/bin/"* "$TEST_ROOT/compiler"
171PATH="$TEST_ROOT/bin:$PATH"
172OMC="$TEST_ROOT/compiler"
173IPL="$TEST_ROOT/bin/ipl"
174HUB=fixture-hub
175WEBDIR=/fixture-web
176export PATH OMC IPL STORE OWNER_FP OTHER_FP HUB WEBDIR
177unset OMC_RUNNER IPL_OWNER_PINS
178
179N=0
180passed() { N=$((N + 1)); printf 'PASS control %02d: %s\n' "$N" "$1"; }
181fail() { printf 'FAIL control: %s\n' "$1" >&2; exit 1; }
182# prepare NAME: a new receipt folder and hopbot.org folder, every stub at its quiet default,
183# the store's main at A and the pinned owner set.
184prepare() {
185 LOGDIR="$TEST_ROOT/log-$1"
186 HOPWEBDIR="$TEST_ROOT/www-$1/hopbot.org"
187 mkdir -p "$HOPWEBDIR"
188 store_main "$A"; STUB_IPL=ok; IPL_EXPECT_OWNER=$OWNER_FP
189 STUB_QUIET=0 STUB_BAD_COMPILER=0 STUB_HASH_FAIL=0 STUB_COMPILE_FAIL=0 STUB_BUILD_FAIL=0
190 STUB_VERIFY_FAIL=0 STUB_RSYNC_FAIL=0 STUB_SUDO_FAIL=0 STUB_MACHINE=aarch64 STUB_ASSET=base
191 STUB_HOP=0 STUB_TESTDATA=
192 export LOGDIR HOPWEBDIR STUB_IPL IPL_EXPECT_OWNER STUB_QUIET STUB_BAD_COMPILER STUB_HASH_FAIL STUB_COMPILE_FAIL
193 export STUB_BUILD_FAIL STUB_VERIFY_FAIL STUB_RSYNC_FAIL STUB_SUDO_FAIL STUB_MACHINE STUB_ASSET
194 export STUB_HOP STUB_TESTDATA
195 : > "$TEST_ROOT/trace"
196}
197again() { : > "$TEST_ROOT/trace"; }
198run() { RC=0; sh "$ROOT/scripts/sync-to-hub.sh" "$@" > "$TEST_ROOT/stdout" 2> "$TEST_ROOT/stderr" || RC=$?; }
199transfers() { grep -c -e '^rsync ' -e '^sudo ' "$TEST_ROOT/trace" || :; }
200no_transfer() { [ "$(transfers)" -eq 0 ] || fail "$1: a transfer was started"; }
201# refused NAME CODE TEXT [COMMIT]: that exit code, the reason in the output, no transfer, no receipt
202refused() {
203 run ${4:+"$4"}
204 [ "$RC" -eq "$2" ] || { cat "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" >&2; fail "$1: expected exit $2, got $RC"; }
205 grep -q -- "$3" "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" || { cat "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" >&2; fail "$1: the refusal does not say: $3"; }
206 no_transfer "$1"
207 if ls "$LOGDIR"/receipt.* > /dev/null 2>&1; then fail "$1: a refusal left a receipt"; fi
208 passed "$1 refused, exit $RC, no transfer, no receipt"
209}
210published() { run "$1"; [ "$RC" -eq 0 ] || { cat "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" >&2; fail "expected a publish, got exit $RC"; }; }
211field() { sed -n "s/^$2 //p" "$1" | head -n 1; }
212receipt() { # receipt SITE STATE DESTINATION COMMIT
213 f="$LOGDIR/receipt.$1"
214 [ "$(field "$f" state)" = "$2" ] && [ "$(field "$f" destination)" = "$3" ] && [ "$(field "$f" commit)" = "$4" ] ||
215 { cat "$f" >&2; fail "receipt.$1 is not: $2 $3 $4"; }
216 [ ! -e "$LOGDIR/pending.$1" ] || fail "a pending marker for $1 is left after success"
217}
218SUDO_LINE() { printf 'sudo -n /usr/bin/rsync -rt --delete --chmod=D755,F644 --chown=root:root --exclude=.well-known %s/hopbot.stage/ %s/' "$LOGDIR" "$HOPWEBDIR"; }
219
220# ---- usage: the approved commit is required, in full
221prepare usage
222for bad in '' "${A%????????}" "$(printf %s "$A" | tr a-f A-F)x" main; do
223 RC=0
224 sh "$ROOT/scripts/sync-to-hub.sh" "$bad" > "$TEST_ROOT/stdout" 2> "$TEST_ROOT/stderr" || RC=$?
225 [ "$RC" -eq 2 ] && grep -q '^usage: sync-to-hub.sh \[--log-dir DIR\] \[COMMIT\]' "$TEST_ROOT/stderr" || fail "commit argument '$bad' gave exit $RC"
226 [ ! -s "$TEST_ROOT/trace" ] && [ ! -e "$LOGDIR" ] || fail "a usage refusal did some work"
227done
228passed 'an empty, a short and a malformed commit and a branch name are usage refusals, exit 2, before any work'
229run "$A" extra; [ "$RC" -eq 2 ] && [ ! -s "$TEST_ROOT/trace" ] || fail "two arguments gave exit $RC"
230passed 'a second argument is a usage refusal, exit 2'
231
232prepare quiet
233STUB_QUIET=1
234run "$A"
235[ "$RC" -eq 0 ] && [ ! -s "$TEST_ROOT/trace" ] || fail 'quiet flag did not skip'
236passed 'quiet flag held: skipped before any work'
237
238# ---- the signed head: ipl verify of the store for the pinned owner, and COMMIT is the store's main
239VERIFY_LINE="ipl verify $STORE interplane.io --expect-owner $OWNER_FP"
240no_export() { if grep -q -e ' archive ' -e '^compile$' "$TEST_ROOT/trace"; then fail "$1: the source was exported or compiled"; fi; }
241prepare owner-unset; store_main "$B"; unset IPL_EXPECT_OWNER
242refused 'no owner pinned (IPL_EXPECT_OWNER unset)' 5 'no owner is pinned' "$B"
243if grep -q '^ipl ' "$TEST_ROOT/trace"; then fail 'ipl was run with no owner pinned'; fi
244no_export 'owner unset'
245prepare owner-empty; store_main "$B"; IPL_EXPECT_OWNER=
246refused 'no owner pinned (IPL_EXPECT_OWNER empty)' 5 'no owner is pinned' "$B"
247prepare owner-unset-nocommit; store_main "$B"; unset IPL_EXPECT_OWNER
248refused 'no owner pinned and no commit named' 5 'no owner is pinned'
249no_export 'owner unset, no commit'
250prepare verify-refused; store_main "$B"; STUB_IPL=fail
251refused 'the store project does not verify (ipl exit 3)' 5 'does not verify for owner' "$B"
252no_export 'verify refused'
253prepare verify-refused-nocommit; store_main "$B"; STUB_IPL=fail
254refused 'the store project does not verify and no commit named' 5 'does not verify for owner'
255no_export 'verify refused, no commit'
256prepare owner-mismatch; store_main "$B"; IPL_EXPECT_OWNER=$OTHER_FP
257refused 'pinned owner is not the current owner (ipl refuses --expect-owner)' 5 'does not verify for owner' "$B"
258grep -q 'current owner pin is' "$LOGDIR/sync.log" || fail 'the owner mismatch from ipl is not in the log'
259no_export 'owner mismatch'
260prepare owner-reported-other; store_main "$B"; STUB_IPL=other-owner
261refused 'verify names a current owner other than the pinned one' 5 'no single OK line' "$B"
262no_export 'other owner reported'
263for kind in other-project other-branch two empty; do
264 prepare "verify-$kind"; store_main "$B"; STUB_IPL=$kind
265 refused "verify answer is not one OK line for interplane.io main ($kind)" 5 'no single OK line' "$B"
266 no_export "verify $kind"
267done
268prepare ipl-missing; store_main "$B"; IPL="$TEST_ROOT/absent-ipl"
269refused 'ipl missing' 5 'does not verify for owner' "$B"
270IPL="$TEST_ROOT/bin/ipl"
271prepare not-main; store_main "$B"
272refused 'commit that is not the store main (main moved on)' 5 'is not the signed main' "$A"
273grep -Fxq "$VERIFY_LINE" "$TEST_ROOT/trace" || fail 'the head was not taken from ipl verify of the store for the pinned owner'
274no_export 'not main'
275prepare not-landed; store_main "$A"
276refused 'commit ahead of the store main (not landed)' 5 'is not the signed main' "$B"
277no_export 'not landed'
278
279# ---- exactly the signed main is built, read in place from the store, which is left as it was
280prepare exact-commit; store_main "$B"
281"$REAL_GIT" -C "$SRC" for-each-ref > "$TEST_ROOT/refs.before"
282: > "$TEST_ROOT/store.mark"
283published "$B"
284grep -q '<p>release-b</p>' "$TEST_ROOT/built-index" || fail 'the page was not built from the signed main'
285[ "$(sed -n 1p "$TEST_ROOT/trace")" = "$VERIFY_LINE" ] || fail 'ipl verify was not the first step'
286grep -Fq "git --git-dir=$SRC archive --format=tar " "$TEST_ROOT/trace" || fail 'the source was not exported from the store'
287"$REAL_GIT" -C "$SRC" for-each-ref | cmp -s - "$TEST_ROOT/refs.before" || fail 'the publisher changed a ref in the store'
288[ -z "$(find "$STORE" -newer "$TEST_ROOT/store.mark" -print)" ] || fail 'the publisher wrote into the store'
289if grep -q -e 'rad:' -e 'ls-remote' -e ' clone ' -e ' fetch ' "$TEST_ROOT/trace"; then fail 'the publisher asked a remote'; fi
290receipt interplane.io published fixture-hub:/fixture-web "$B"
291passed 'signed main named: verify first, then an export of exactly that commit from the store; the store is not written and no remote is asked'
292
293prepare no-commit; store_main "$B"
294run
295[ "$RC" -eq 0 ] || { cat "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" >&2; fail "no commit named: expected a publish, got exit $RC"; }
296grep -q "commit: the signed main of interplane.io, $B" "$TEST_ROOT/stdout" && grep -q '<p>release-b</p>' "$TEST_ROOT/built-index" || fail 'no commit named: the signed main was not the commit built'
297receipt interplane.io published fixture-hub:/fixture-web "$B"
298passed 'no commit named (the service start): the verified signed main is resolved, built and published'
299
300# ---- compiler, build, verify and digest refusals
301prepare bad-compiler; store_main "$B"; STUB_BAD_COMPILER=1
302refused 'compiler with the wrong SHA-256' 3 'compilation failed' "$B"
303if grep -q '^compile$' "$TEST_ROOT/trace"; then fail 'a compiler with the wrong hash was run'; fi
304prepare missing-compiler; store_main "$B"; OMC="$TEST_ROOT/absent"
305refused 'missing compiler' 3 'compilation failed' "$B"
306OMC="$TEST_ROOT/compiler"
307prepare compile-failed; store_main "$B"; STUB_COMPILE_FAIL=1
308refused 'compilation failure' 3 'compilation failed' "$B"
309prepare build-failed; store_main "$B"; STUB_BUILD_FAIL=1
310refused 'build failure' 4 'native build failed' "$B"
311prepare verify-failed; store_main "$B"; STUB_VERIFY_FAIL=1
312refused 'verify failure' 4 'verify failed' "$B"
313prepare hash-failed; store_main "$B"; STUB_HASH_FAIL=1
314refused 'digest failure' 8 'could not take the digest' "$B"
315prepare changed-sha2; store_main "$C"
316refused 'commit whose SHA-2 unit is not the pinned one' 3 'compilation failed' "$C"
317if grep -q '^compile$' "$TEST_ROOT/trace"; then fail 'a changed SHA-2 unit reached the compiler'; fi
318prepare unit-composition; store_main "$B"
319published "$B"
320"$REAL_GIT" -C "$UP" show "$B:native/lib/sha2.om" > "$TEST_ROOT/sha2.b"
321"$REAL_GIT" -C "$UP" show "$B:native/site.om" > "$TEST_ROOT/site.b"
322"$REAL_GIT" -C "$UP" show "$B:native/translate.om" > "$TEST_ROOT/translate.b"
323{ printf 'om2\n'; cat "$TEST_ROOT/sha2.b" "$TEST_ROOT/site.b" "$TEST_ROOT/translate.b"; } > "$TEST_ROOT/expected-unit.om"
324cmp "$TEST_ROOT/compiled-input" "$TEST_ROOT/expected-unit.om" || fail 'compiler input differs'
325passed "compiler input is the om2 line, the commit's SHA-2 unit, then the commit's builder and translation unit"
326
327# ---- fixture output is never published
328for kind in file manifest banner; do
329 prepare "testdata-$kind"; store_main "$B"; STUB_TESTDATA=$kind
330 refused "fixture output ($kind marker) in the interplane.io build" 6 'fixture output is never published' "$B"
331done
332prepare testdata-hop; store_main "$B"; STUB_HOP=1; STUB_TESTDATA=hopfile
333refused 'fixture output (file marker) in the hopbot.org build' 6 'fixture output is never published' "$B"
334
335# ---- interplane.io: transfer, receipt, unchanged, pending
336prepare publish; store_main "$B"
337published "$B"
338grep -q '^rsync -a --delete --exclude .well-known .*/dist/ fixture-hub:/fixture-web/$' "$TEST_ROOT/trace" || fail 'transfer options or destination differ'
339[ "$(transfers)" -eq 1 ] || fail 'a build without a Hop release made more than one transfer'
340receipt interplane.io published fixture-hub:/fixture-web "$B"
341[ ! -e "$LOGDIR/receipt.hopbot.org" ] && [ -z "$(ls -A "$HOPWEBDIR")" ] || fail 'the hopbot.org folder was touched without a Hop release'
342D1=$(field "$LOGDIR/receipt.interplane.io" digest)
343[ "${#D1}" -eq 64 ] || fail 'the receipt holds no digest'
344passed 'publish without a Hop release: one transfer to the hub, a receipt with destination, digest and the full commit; hopbot.org untouched'
345again; published "$B"
346no_transfer unchanged; grep -q '^.* unchanged: interplane.io' "$TEST_ROOT/stdout" || fail 'unchanged run did not say so'
347[ "$(field "$LOGDIR/receipt.interplane.io" digest)" = "$D1" ] || fail 'unchanged run rewrote the receipt'
348passed 'same commit, same content, same destination: nothing is transferred'
349STUB_ASSET=changed; again; published "$B"
350[ "$(transfers)" -eq 1 ] && [ "$(field "$LOGDIR/receipt.interplane.io" digest)" != "$D1" ] || fail 'a stylesheet-only change was not published'
351passed 'a stylesheet-only change is published'
352D2=$(field "$LOGDIR/receipt.interplane.io" digest)
353STUB_ASSET=changed-again STUB_RSYNC_FAIL=1; again; run "$B"
354[ "$RC" -eq 8 ] && [ "$(field "$LOGDIR/receipt.interplane.io" digest)" = "$D2" ] && [ -f "$LOGDIR/pending.interplane.io" ] || fail 'failed transfer: receipt or pending marker wrong'
355passed 'a failed transfer exits 8, keeps the old receipt and leaves the pending marker'
356STUB_ASSET=changed STUB_RSYNC_FAIL=0; again; published "$B"
357[ "$(transfers)" -eq 1 ] && [ ! -e "$LOGDIR/pending.interplane.io" ] && [ "$(field "$LOGDIR/receipt.interplane.io" digest)" = "$D2" ] || fail 'a pending transfer was not repaired'
358passed 'content back at the last published digest is still transferred while a pending marker is left'
359STUB_ASSET=changed-again; again; published "$B"
360[ "$(transfers)" -eq 1 ] && [ "$(field "$LOGDIR/receipt.interplane.io" digest)" != "$D2" ] || fail 'later release not published'
361passed 'the later release is then published'
362
363# A receipt belongs to one commit: the same bytes from another commit are transferred and recorded anew.
364prepare commit-bound; store_main "$A"
365published "$A"
366E=$("$REAL_GIT" -C "$UP" commit-tree -p "$B" -m 'same tree as A' "$A^{tree}")
367"$REAL_GIT" -C "$UP" update-ref refs/heads/same-tree "$E"
368"$REAL_GIT" -C "$UP" push -q "$SRC" "$E:refs/heads/same-tree"
369store_main "$E"; again; published "$E"
370[ "$(transfers)" -eq 1 ] || fail 'a new commit with the same content was not transferred'
371receipt interplane.io published fixture-hub:/fixture-web "$E"
372passed 'a new approved commit with identical content is transferred and its receipt names the new commit'
373
374# ---- destinations: interplane.io
375prepare main-dest; store_main "$B"
376published "$B"
377WEBDIR=/fixture-web-two; again; published "$B"
378grep -q ' fixture-hub:/fixture-web-two/$' "$TEST_ROOT/trace" && grep -q 'destination changed from fixture-hub:/fixture-web to fixture-hub:/fixture-web-two' "$TEST_ROOT/stdout" || fail 'changed interplane.io destination was skipped'
379receipt interplane.io published fixture-hub:/fixture-web-two "$B"
380WEBDIR=/fixture-web
381passed 'interplane.io destination change: the same content is transferred to the new destination and the receipt names it'
382
383# ---- destinations: hopbot.org, on this machine, by one root command
384prepare hop-missing; store_main "$B"; STUB_HOP=1; rmdir "$HOPWEBDIR"
385refused 'Hop release while the hopbot.org folder is missing' 7 'hopbot.org folder is missing' "$B"
386prepare hop-unnamed; store_main "$B"; STUB_HOP=1; HOPWEBDIR=
387refused 'Hop release while HOPWEBDIR is empty' 7 'hopbot.org folder is missing' "$B"
388
389prepare hop; store_main "$B"; STUB_HOP=1
390published "$B"
391[ "$(grep -c '^rsync ' "$TEST_ROOT/trace")" -eq 1 ] && [ "$(grep -c '^sudo ' "$TEST_ROOT/trace")" -eq 1 ] || fail 'a Hop release must make one hub transfer and one root copy'
392grep -Fxq "$(SUDO_LINE)" "$TEST_ROOT/trace" || { cat "$TEST_ROOT/trace" >&2; fail 'the root command is not the one documented command'; }
393if grep '^rsync ' "$TEST_ROOT/trace" | grep -q hopbot; then fail 'hopbot.org was sent to the hub'; fi
394cmp "$HOPWEBDIR/index.html" "$LOGDIR/hopbot.stage/index.html" && [ -f "$HOPWEBDIR/install.sh" ] || fail 'the hopbot.org folder does not hold the built page and installer'
395receipt interplane.io published fixture-hub:/fixture-web "$B"
396receipt hopbot.org published "local:$HOPWEBDIR" "$B"
397passed 'Hop release: interplane.io to the hub, hopbot.org to its folder on this machine by the one documented root command, one receipt each'
398again; published "$B"
399no_transfer 'unchanged with Hop'
400passed 'same Hop release again: neither destination is transferred'
401
402# The reviewer's case: the content was already published when the Hop destination comes into play.
403prepare enable; store_main "$B"
404published "$B"
405[ "$(transfers)" -eq 1 ] && [ ! -e "$LOGDIR/receipt.hopbot.org" ] || fail 'enable-after-skip: unexpected first run'
406STUB_HOP=1; again; published "$B"
407grep -q '^.* unchanged: interplane.io' "$TEST_ROOT/stdout" || fail 'enable-after-skip: interplane.io content was unchanged in this control'
408[ "$(grep -c '^rsync ' "$TEST_ROOT/trace")" -eq 0 ] && [ "$(grep -c '^sudo ' "$TEST_ROOT/trace")" -eq 1 ] || fail 'a newly enabled hopbot.org destination was skipped'
409receipt hopbot.org published "local:$HOPWEBDIR" "$B"
410passed 'enable after skip: interplane.io unchanged and skipped, the newly enabled hopbot.org destination is still transferred'
411
412prepare hop-fail; store_main "$B"; STUB_HOP=1; STUB_SUDO_FAIL=1
413run "$B"
414[ "$RC" -eq 8 ] && [ -f "$LOGDIR/pending.hopbot.org" ] && [ ! -e "$LOGDIR/receipt.hopbot.org" ] || fail 'failed root copy: wrong exit, receipt or marker'
415receipt interplane.io published fixture-hub:/fixture-web "$B"
416STUB_SUDO_FAIL=0; again; published "$B"
417[ "$(grep -c '^rsync ' "$TEST_ROOT/trace")" -eq 0 ] && [ "$(grep -c '^sudo ' "$TEST_ROOT/trace")" -eq 1 ] || fail 'retry did not transfer hopbot.org alone'
418receipt hopbot.org published "local:$HOPWEBDIR" "$B"
419passed 'a failed root copy exits 8 with interplane.io receipted and hopbot.org pending; the retry transfers hopbot.org alone'
420
421prepare hop-dest; store_main "$B"; STUB_HOP=1
422published "$B"
423OLDHOP=$HOPWEBDIR
424HOPWEBDIR="$TEST_ROOT/www-hop-dest/hopbot-two"; mkdir -p "$HOPWEBDIR"
425again; published "$B"
426[ "$(grep -c '^rsync ' "$TEST_ROOT/trace")" -eq 0 ] && grep -Fxq "$(SUDO_LINE)" "$TEST_ROOT/trace" || fail 'changed hopbot.org destination was skipped'
427[ -f "$HOPWEBDIR/index.html" ] && [ -f "$OLDHOP/index.html" ] || fail 'destination change: new folder empty or old folder altered'
428grep -q "destination changed from local:$OLDHOP to local:$HOPWEBDIR" "$TEST_ROOT/stdout" || fail 'the log does not name the destination change'
429receipt hopbot.org published "local:$HOPWEBDIR" "$B"
430passed 'hopbot.org destination change: the same content is transferred to the new folder; the old folder is left as it was and the log says so'
431
432# ---- withdrawal
433prepare withdraw; store_main "$B"; STUB_HOP=1
434published "$B"
435[ -f "$HOPWEBDIR/index.html" ] || fail 'withdrawal control: nothing was published first'
436STUB_HOP=0; again; published "$B"
437grep -Fxq "$(SUDO_LINE)" "$TEST_ROOT/trace" || fail 'withdrawal did not use the one documented root command'
438[ -d "$HOPWEBDIR" ] && [ -z "$(ls -A "$HOPWEBDIR")" ] || fail 'withdrawal left files in the hopbot.org folder'
439[ "$(field "$LOGDIR/receipt.hopbot.org" state)" = withdrawn ] && [ "$(field "$LOGDIR/receipt.hopbot.org" digest)" = none ] || fail 'no withdrawal record'
440receipt hopbot.org withdrawn "local:$HOPWEBDIR" "$B"
441passed 'withdrawal: a commit without a Hop release empties the hopbot.org folder with the same root command and leaves a withdrawal record'
442again; published "$B"
443no_transfer 'after withdrawal'
444passed 'after a withdrawal nothing more is transferred'
445STUB_HOP=2; again; published "$B"
446[ "$(grep -c '^sudo ' "$TEST_ROOT/trace")" -eq 1 ] && [ -f "$HOPWEBDIR/index.html" ] || fail 'a Hop release after a withdrawal was not published'
447receipt hopbot.org published "local:$HOPWEBDIR" "$B"
448passed 'a Hop release after a withdrawal is published again'
449
450prepare withdraw-missing; store_main "$B"; STUB_HOP=1
451published "$B"
452cp "$LOGDIR/receipt.hopbot.org" "$TEST_ROOT/hop-receipt"
453STUB_HOP=0; HOPWEBDIR="$TEST_ROOT/www-withdraw-missing/elsewhere"; mkdir -p "$HOPWEBDIR"; again; run "$B"
454[ "$RC" -eq 7 ] && grep -q 'name that folder in HOPWEBDIR to withdraw it' "$TEST_ROOT/stdout" || fail 'withdrawal from another folder was not refused'
455no_transfer 'withdrawal from another folder'; cmp "$LOGDIR/receipt.hopbot.org" "$TEST_ROOT/hop-receipt" || fail 'refused withdrawal changed the receipt'
456HOPWEBDIR="$TEST_ROOT/www-withdraw-missing/hopbot.org"; rm -rf "$HOPWEBDIR"; again; run "$B"
457[ "$RC" -eq 7 ] && grep -q 'withdrawn and the hopbot.org folder is missing' "$TEST_ROOT/stdout" || fail 'withdrawal with a missing folder was not refused'
458no_transfer 'withdrawal with a missing folder'; cmp "$LOGDIR/receipt.hopbot.org" "$TEST_ROOT/hop-receipt" || fail 'refused withdrawal changed the receipt'
459passed 'withdrawal is refused, exit 7, with no transfer and the receipt kept, when HOPWEBDIR names another folder or the folder is missing'
460
461# ---- the log: a run that names no folder leaves the default (live) folder alone unless it publishes
462prepare log-default; store_main "$B"
463OLDHOME=$HOME; HOME="$TEST_ROOT/home-log"; mkdir -p "$HOME"; export HOME
464LIVE="$HOME/.local/share/interplane-site"
465unset LOGDIR
466run "$A"
467[ "$RC" -eq 5 ] && grep -q 'is not the signed main' "$TEST_ROOT/stdout" || fail "default log folder: expected refusal 5, got $RC"
468grep -q 'not logged: no log folder was named' "$TEST_ROOT/stderr" || fail 'the refusal does not say that it was not logged'
469[ ! -e "$LIVE" ] || fail 'a refused run that named no log folder wrote to the default log folder'
470passed 'no log folder named: a refusal (commit is not the signed main) is printed, exit 5, and the default log folder is not created or written'
471STUB_QUIET=1; run "$B"; STUB_QUIET=0
472[ "$RC" -eq 0 ] && [ ! -e "$LIVE" ] || fail 'the quiet-flag skip wrote to the default log folder'
473passed 'no log folder named: the quiet-flag skip leaves the default log folder alone'
474NAMEDDIR="$TEST_ROOT/log-named"
475run --log-dir "$NAMEDDIR" "$A"
476[ "$RC" -eq 5 ] && grep -q 'refuse: .* is not the signed main' "$NAMEDDIR/sync.log" || fail '--log-dir: the refusal is not in the named log'
477[ ! -e "$LIVE" ] || fail '--log-dir: the default log folder was written'
478passed '--log-dir DIR: the same refusal is recorded in DIR/sync.log and the default folder stays untouched'
479LOGDIR="$TEST_ROOT/log-env"; export LOGDIR
480run "$A"
481[ "$RC" -eq 5 ] && grep -q 'refuse: .* is not the signed main' "$LOGDIR/sync.log" || fail 'LOGDIR: the refusal is not in the named log'
482[ ! -e "$LIVE" ] || fail 'LOGDIR: the default log folder was written'
483passed 'LOGDIR set: the refusal is recorded there'
484unset LOGDIR; again
485run "$B"
486[ "$RC" -eq 0 ] && [ "$(transfers)" -eq 1 ] || { cat "$TEST_ROOT/stdout" "$TEST_ROOT/stderr" >&2; fail "default log folder: a publish gave $RC"; }
487grep -q "published interplane.io $B" "$LIVE/sync.log" && [ "$(field "$LIVE/receipt.interplane.io" commit)" = "$B" ] || fail 'a publish with no log folder named did not write the default log and receipt'
488LINES=$(wc -l < "$LIVE/sync.log"); again; run "$B"
489[ "$RC" -eq 0 ] && [ "$(transfers)" -eq 0 ] && [ "$(wc -l < "$LIVE/sync.log")" -eq "$LINES" ] || fail 'an unchanged run added a line to the default log'
490again; run "$A"
491[ "$RC" -eq 5 ] && [ "$(wc -l < "$LIVE/sync.log")" -eq "$LINES" ] || fail 'a refusal after a publish added a line to the default log'
492passed 'no log folder named: a publish writes the default log and receipt; a later unchanged run and a later refusal add no line to it'
493HOME=$OLDHOME; export HOME
494LOGDIR="$TEST_ROOT/log-after"; export LOGDIR
495
496# ---- the default compiler lookup is local and still passes the checksum gate
497prepare default-compiler; store_main "$B"
498OMEGA_SRC="$TEST_ROOT/omega"
499export OMEGA_SRC
500mkdir -p "$OMEGA_SRC/seed/out"
501cp "$TEST_ROOT/compiler" "$OMEGA_SRC/seed/out/omc"
502unset OMC
503published "$B"
504[ "$(transfers)" -eq 1 ] || fail 'default compiler lookup did not publish'
505passed 'default compiler lookup'
506
507printf 'RESULT pass=%d fail=0 (publisher controls; git is real and local, ipl, compiler, builder, rsync and sudo are stubbed)\n' "$N"