| 1 | # shellcheck shell=bash |
| 2 | # valley-restacker once --report-only, end to end over a real bare |
| 3 | # repository wired with the protected host's rendered pre-receive hook. |
| 4 | # Every change is readied with the repository's own valley as its owner; |
| 5 | # the hook grants request to the principal requester, which files the |
| 6 | # requests and is the principal the restacker counts. |
| 7 | # |
| 8 | # Outcome records are injected on the server side with update-ref, |
| 9 | # because no push may write one: only the integrator writes them. Each |
| 10 | # injection is marked where it is made. No scenario here runs the real |
| 11 | # integrator. |
| 12 | # Paths and fixtures come from the derivation environment. |
| 13 | # shellcheck disable=SC2154 |
| 14 | set -euo pipefail |
| 15 | |
| 16 | w="$TMPDIR/restack-e2e" |
| 17 | mkdir -p "$w" |
| 18 | export HOME="$w" |
| 19 | export GIT_CONFIG_NOSYSTEM=1 |
| 20 | git config --global user.name restack-e2e |
| 21 | git config --global user.email restack-e2e@localhost |
| 22 | git config --global init.defaultBranch main |
| 23 | git config --global advice.detachedHead false |
| 24 | |
| 25 | fail() { |
| 26 | printf 'restack-e2e: %s\n' "$*" >&2 |
| 27 | for f in "$w"/out "$w"/err; do |
| 28 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 29 | done |
| 30 | exit 1 |
| 31 | } |
| 32 | |
| 33 | origin="$w/project.git" |
| 34 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 35 | test -x "$hook" |
| 36 | git init --quiet --bare "$origin" |
| 37 | cp "$hook" "$origin/hooks/pre-receive" |
| 38 | chmod +x "$origin/hooks/pre-receive" |
| 39 | |
| 40 | repo="$w/project" |
| 41 | git init --quiet "$repo" |
| 42 | cd "$repo" |
| 43 | git remote add origin "$origin" |
| 44 | # The project layer caps the restacker at two automatic versions a change. |
| 45 | mkdir -p policy/project |
| 46 | printf 'package verification\n\nproject: restack: maxAutomaticVersions: 2\n' > policy/project/policy.cue |
| 47 | echo base > readme |
| 48 | git add readme policy |
| 49 | git commit --quiet -m base |
| 50 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 51 | |
| 52 | as() { |
| 53 | local who="$1" |
| 54 | shift |
| 55 | git config valley.principal "$who" |
| 56 | VALLEY_PRINCIPAL="$who" "$@" |
| 57 | } |
| 58 | ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; } |
| 59 | |
| 60 | # change <owner> <branch> <base>: a commit on base, pushed and readied by |
| 61 | # its owner; prints the change ID. |
| 62 | change() { |
| 63 | local who="$1" branch="$2" base="$3" |
| 64 | git checkout --quiet -B "$branch" "$base" |
| 65 | echo "$branch" > "$branch" |
| 66 | git add "$branch" |
| 67 | git commit --quiet -m "$branch" |
| 68 | as "$who" git push --quiet --force origin "$branch" |
| 69 | ok as "$who" valley ready |
| 70 | sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out" |
| 71 | } |
| 72 | # branch <name> <base>: a plain branch, which no record is on. |
| 73 | branch() { |
| 74 | git checkout --quiet -B "$1" "$2" |
| 75 | echo "$1" > "$1" |
| 76 | git add "$1" |
| 77 | git commit --quiet -m "$1" |
| 78 | as alice git push --quiet origin "$1" |
| 79 | } |
| 80 | |
| 81 | base="$(git rev-parse main)" |
| 82 | a="$(change alice a "$base")" |
| 83 | b1="$(change bob b1 "$base")" |
| 84 | b2="$(change bob b2 b1)" |
| 85 | c1="$(change alice c1 "$base")" |
| 86 | c2="$(change alice c2 c1)" |
| 87 | c3="$(change bob c3 c1)" |
| 88 | d="$(change alice d "$base")" |
| 89 | ok as alice valley unready "$d" |
| 90 | wd="$(change bob wd "$base")" |
| 91 | ok as bob valley withdraw "$wd" |
| 92 | branch plain "$base" |
| 93 | e="$(change alice e "$base")" |
| 94 | branch e-plain e |
| 95 | g1="$(change alice g1 "$base")" |
| 96 | g2="$(change alice g2 g1)" |
| 97 | ok as alice valley unready "$g1" |
| 98 | u="$(change bob u "$base")" |
| 99 | k="$(change requester k "$base")" |
| 100 | git checkout --quiet k |
| 101 | echo again >> k |
| 102 | git commit --quiet -am again |
| 103 | as requester git push --quiet origin k |
| 104 | ok as requester valley ready --change "$k" k |
| 105 | # s lands by fast-forward before main moves, so it is submitted. |
| 106 | s="$(change alice s "$base")" |
| 107 | env -u VALLEY_PRINCIPAL git push --quiet origin s:main |
| 108 | git checkout --quiet main |
| 109 | git reset --quiet --hard origin/main |
| 110 | echo moved > moved |
| 111 | git add moved |
| 112 | git commit --quiet -m moved |
| 113 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 114 | tip="$(git rev-parse main)" |
| 115 | |
| 116 | # Server-side injection of an outcome record under a request name. |
| 117 | evidence() { |
| 118 | git -C "$origin" for-each-ref --format='%(refname) %(objectname)' refs/the-valley/attestations/ \ |
| 119 | | LC_ALL=C sort | sha256sum | cut -c1-16 |
| 120 | } |
| 121 | inject() { |
| 122 | local name="$1" line="$2" blob |
| 123 | blob="$(printf '%s\n' "$line" | git -C "$origin" hash-object -w --stdin)" |
| 124 | git -C "$origin" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob" |
| 125 | } |
| 126 | # u's request was judged failed, and no request is left: its landing is |
| 127 | # uncertain. |
| 128 | inject "c-$u" "$(git rev-parse u) $base $(evidence) failed 2026-10-18T00:00:00Z" |
| 129 | |
| 130 | # request <name> <head>: requester files an integration request. |
| 131 | request() { as requester git push --quiet origin "$2:refs/the-valley/integration-requests/main/$1"; } |
| 132 | |
| 133 | refs() { git -C "$origin" for-each-ref --format='%(refname) %(objectname)' | LC_ALL=C sort; } |
| 134 | plan() { |
| 135 | local before |
| 136 | before="$(refs)" |
| 137 | valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \ |
| 138 | --principal requester --instance "$smokePolicy/instance" "$@" > "$w/plan.json" 2> "$w/err" \ |
| 139 | || fail "the planner failed" |
| 140 | [ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin' |
| 141 | } |
| 142 | # family <id>: the family plan a change is a layer of. |
| 143 | family() { jq -c --arg id "$1" '.families[] | select(any(.layers[]; .id == $id))' "$w/plan.json"; } |
| 144 | action() { |
| 145 | local got |
| 146 | got="$(family "$1" | jq -r .action)" |
| 147 | [ "$got" = "$2" ] || fail "the family of $1 is $got, not $2: $(family "$1")" |
| 148 | } |
| 149 | says() { family "$1" | jq -e --arg s "$2" 'any(.reasons[], .notes[]; contains($s))' > /dev/null \ |
| 150 | || fail "the family of $1 does not say: $2: $(family "$1")"; } |
| 151 | |
| 152 | # ---------------------------------------------------------------------- |
| 153 | # Selection and families. |
| 154 | plan |
| 155 | jq -e --arg t "$tip" '.tip == $t and .maxAutomaticVersions == 2' "$w/plan.json" > /dev/null \ |
| 156 | || fail "the plan does not read main's tip or the project's cap" |
| 157 | action "$a" restack |
| 158 | action "$b1" restack |
| 159 | [ "$(family "$b1" | jq -c '[.layers[].id]')" = "[\"$b1\",\"$b2\"]" ] || fail 'b1 and b2 are not one family' |
| 160 | [ "$(family "$c1" | jq '.layers | length')" = 3 ] || fail 'c1, c2 and c3 are not one family' |
| 161 | [ "$(family "$c2" | jq -r '.layers[0].id')" = "$c1" ] || fail 'the shared lower layer is not the bottom' |
| 162 | action "$c3" restack |
| 163 | action "$d" leave |
| 164 | action "$wd" leave |
| 165 | action "$u" leave |
| 166 | action "$e" hold |
| 167 | says "$e" 'e-plain is a plain branch' |
| 168 | action "$g2" hold |
| 169 | says "$g2" 'is a draft' |
| 170 | [ -z "$(family "$s")" ] || fail 'a submitted change was planned' |
| 171 | jq -e '[.families[].layers[] | select(.name == "plain")] | length == 0' "$w/plan.json" > /dev/null \ |
| 172 | || fail 'a lone plain branch was planned' |
| 173 | |
| 174 | # Capacity: k has two versions uploaded by requester, and a third is over |
| 175 | # the project's cap of 2. |
| 176 | action "$k" hold |
| 177 | says "$k" 'over the cap of 2' |
| 178 | family "$k" | jq -e '.layers[0].capacity.uploads == 3' > /dev/null || fail 'k does not count three uploads' |
| 179 | |
| 180 | # ---------------------------------------------------------------------- |
| 181 | # Request holds. A request by name with no outcome holds. |
| 182 | request a a |
| 183 | plan |
| 184 | action "$a" hold |
| 185 | says "$a" 'no outcome yet' |
| 186 | family "$a" | jq -e '.requests[0].by == ["name", "head"]' > /dev/null || fail 'request a was not related by name' |
| 187 | # A current stale releases. |
| 188 | inject a "$(git rev-parse a) $tip $(evidence) stale" |
| 189 | plan |
| 190 | action "$a" restack |
| 191 | # A mismatched head, tip or fingerprint holds. |
| 192 | inject a "$base $tip $(evidence) stale" |
| 193 | plan |
| 194 | action "$a" hold |
| 195 | says "$a" 'is about head' |
| 196 | inject a "$(git rev-parse a) $base $(evidence) stale" |
| 197 | plan |
| 198 | says "$a" 'judged on main at' |
| 199 | inject a "$(git rev-parse a) $tip 0000000000000000 stale" |
| 200 | plan |
| 201 | says "$a" 'judged on evidence' |
| 202 | # A malformed outcome holds. |
| 203 | inject a "not an outcome" |
| 204 | plan |
| 205 | says "$a" 'does not read' |
| 206 | # A current reject releases, with a note. |
| 207 | inject a "$(git rev-parse a) $tip $(evidence) reject" |
| 208 | plan |
| 209 | action "$a" restack |
| 210 | says "$a" 'was rejected' |
| 211 | |
| 212 | # By an exact head, under a name nothing else relates. |
| 213 | request zz-head "$(git rev-parse b2)" |
| 214 | # By range: a commit on c2 no branch holds, so only the request names it |
| 215 | # and the planner has to fetch it before reading its ancestry. |
| 216 | git checkout --quiet -B scratch c2 |
| 217 | echo over > over |
| 218 | git add over |
| 219 | git commit --quiet -m over |
| 220 | request zz-range "$(git rev-parse HEAD)" |
| 221 | git checkout --quiet main |
| 222 | git branch --quiet -D scratch |
| 223 | plan |
| 224 | action "$b1" hold |
| 225 | family "$b1" | jq -e '.requests[] | select(.name == "zz-head") | .by == ["head"]' > /dev/null \ |
| 226 | || fail 'zz-head was not related by head' |
| 227 | action "$c1" hold |
| 228 | family "$c1" | jq -e '.requests[] | select(.name == "zz-range") | .by == ["range"]' > /dev/null \ |
| 229 | || fail 'zz-range was not related by range' |
| 230 | action "$c3" hold |
| 231 | |
| 232 | # A request whose head is no commit cannot be related or not, so every |
| 233 | # family it might be about holds. Injected on the server side, since the |
| 234 | # hook takes no such request. |
| 235 | git -C "$origin" update-ref refs/the-valley/integration-requests/main/zz-blob \ |
| 236 | "$(echo blob | git -C "$origin" hash-object -w --stdin)" |
| 237 | plan |
| 238 | action "$a" hold |
| 239 | says "$a" 'cannot be determined' |
| 240 | |
| 241 | # The human summary reads the same plan. |
| 242 | valley-restacker once --report-only --repo "$origin" --state "$w/state" --principal requester \ |
| 243 | --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err" || fail 'the summary failed' |
| 244 | grep -qE '^[k-z]+: hold$' "$w/out" || fail 'the summary holds no family' |
| 245 | # And without --report-only, nothing runs. |
| 246 | if valley-restacker once --repo "$origin" --state "$w/state" --principal requester \ |
| 247 | --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err"; then |
| 248 | fail 'once ran without --report-only' |
| 249 | fi |
| 250 | |
| 251 | # ---------------------------------------------------------------------- |
| 252 | # A cap larger than any int. The schema admits it, so it composes, and the |
| 253 | # planner counts it as the largest int: no practical cap. k, held above by |
| 254 | # the cap of 2, is not held by this one. The request whose head is no |
| 255 | # commit would hold k too, so it is removed first, on the server side |
| 256 | # where it was injected. |
| 257 | git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-blob |
| 258 | git checkout --quiet main |
| 259 | printf 'package verification\n\nproject: restack: maxAutomaticVersions: 9223372036854775808\n' \ |
| 260 | > policy/project/policy.cue |
| 261 | git commit --quiet -am 'a cap larger than any int' |
| 262 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 263 | plan |
| 264 | grep -q '"maxAutomaticVersions": 9223372036854775807,' "$w/plan.json" \ |
| 265 | || fail "the plan does not count the cap as the largest int: $(cat "$w/plan.json")" |
| 266 | action "$k" restack |
| 267 | |
| 268 | touch "$out" |