gunk-dev/the-valley

init-e2e.sh

source3705a182e12bmain's published tip history
4.2 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11# The rendered valley-init script, run as the host runs it. Relocating
12# the data directory is the only edit — the sandbox cannot write /srv —
13# and it leaves every store path and every decision the script makes
14# exactly as shipped.
15data="$TMPDIR/srv/git"
16sed "s|/srv/git|$data|g" "$initScriptPath" > init.sh
17mkdir -p "$data"
18
19fail() {
20 echo "init-e2e: $1" >&2
21 exit 1
22}
23hook() { echo "$data/$1.git/hooks/pre-receive"; }
24# The store path the declaration says a project's hook must be.
25declared() {
26 grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1
27}
28is_declared_hook() {
29 local link
30 link="$(readlink "$(hook "$1")")" || fail "$1 has no pre-receive symlink"
31 [ "$link" = "$(declared "$1")" ] || fail "$1 points at $link, not its declared hook"
32 [ -x "$(hook "$1")" ] || fail "$1 has a pre-receive that is not executable"
33}
34
35# Three bare repositories that already exist, each in a state the module
36# has to converge from. None of them was created by valley-init: this is
37# the path a host takes when protection is declared over repositories
38# that predate the declaration.
39#
40# guarded real history, no hooks at all — nothing to converge from
41# released a hand-written pre-receive, which is not the module's
42# open a stale managed hook, for protection no longer declared
43for name in guarded released open; do
44 git init --quiet --bare "$data/$name.git"
45done
46git clone --quiet "$data/guarded.git" work
47git -C work commit --quiet --allow-empty -m one
48git -C work push --quiet origin main
49before="$(git -C work rev-parse HEAD)"
50
51printf '#!/bin/sh\nexit 0\n' > "$(hook released)"
52chmod +x "$(hook released)"
53ln -s "$(declared guarded)" "$(hook open)"
54
55bash init.sh || fail "the rendered init script failed over existing repositories"
56
57# The declared hook is installed where there was none, and the history
58# that was already there is untouched — an existing repository is wired,
59# never re-initialized.
60is_declared_hook guarded
61[ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] ||
62 fail "guarded lost the history it already had"
63
64# A hook the module did not write is not the module's to replace, even
65# where the declaration protects the project.
66[ ! -L "$(hook released)" ] || fail "a hand-written hook was replaced by the managed one"
67[ "$(cat "$(hook released)")" = "$(printf '#!/bin/sh\nexit 0\n')" ] ||
68 fail "a hand-written hook was rewritten"
69
70# A project that declares no protection ends with no managed hook,
71# whatever it was carrying before.
72[ ! -e "$(hook open)" ] || fail "open kept a managed hook it no longer declares"
73
74# Running it again changes nothing, and running it over a hook that has
75# drifted to some other store path re-points it: the script is level
76# triggered, so what a repository carries now does not decide what it
77# ends up with.
78bash init.sh || fail "the rendered init script failed on a second run"
79is_declared_hook guarded
80ln -sfn "$(declared released)" "$(hook guarded)"
81rm -f "$(hook released)"
82bash init.sh || fail "the rendered init script failed over a drifted hook"
83is_declared_hook guarded
84is_declared_hook released
85
86# The converged hook is live. Pushing as a principal is pushing with the
87# tag on; pushing with an untagged key is pushing with it off — the same
88# substitution for sshd that protect-e2e makes.
89git -C work commit --quiet --allow-empty -m two
90if env -u VALLEY_PRINCIPAL git -C work push --quiet origin main 2> denied.err; then
91 fail "an untagged key wrote a protected ref of a pre-existing repository"
92fi
93grep -q '<untagged key>' denied.err
94grep -q 'refs/heads/main' denied.err
95[ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] ||
96 fail "guarded kept a ref through a rejected push"
97VALLEY_PRINCIPAL=integrator git -C work push --quiet origin main
98[ "$(git -C "$data/guarded.git" rev-parse main)" = "$(git -C work rev-parse HEAD)" ] ||
99 fail "a declared writer could not write a protected ref"
100
101touch "$out"