| 1 | # shellcheck shell=bash |
| 2 | # valley-restacker once --report-only, end to end over a real bare |
| 3 | # repository wired with the fixture 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 and restack to the principal requester, which |
| 6 | # files the requests, and restack alone to the principal restacker, which |
| 7 | # is the principal the restacker counts. |
| 8 | # |
| 9 | # In the planner's scenarios, outcome records are injected on the server |
| 10 | # side with update-ref, because no push may write one: only the |
| 11 | # integrator writes them. Each injection is marked where it is made. The |
| 12 | # fixtures after them run the real integrator. |
| 13 | # Paths and fixtures come from the derivation environment. |
| 14 | # shellcheck disable=SC2154 |
| 15 | set -euo pipefail |
| 16 | |
| 17 | w="$TMPDIR/restack-e2e" |
| 18 | mkdir -p "$w" |
| 19 | export HOME="$w" |
| 20 | export GIT_CONFIG_NOSYSTEM=1 |
| 21 | git config --global user.name restack-e2e |
| 22 | git config --global user.email restack-e2e@localhost |
| 23 | git config --global init.defaultBranch main |
| 24 | git config --global advice.detachedHead false |
| 25 | |
| 26 | fail() { |
| 27 | printf 'restack-e2e: %s\n' "$*" >&2 |
| 28 | for f in "$w"/out "$w"/err; do |
| 29 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 30 | done |
| 31 | exit 1 |
| 32 | } |
| 33 | |
| 34 | origin="$w/project.git" |
| 35 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 36 | test -x "$hook" |
| 37 | git init --quiet --bare "$origin" |
| 38 | cp "$hook" "$origin/hooks/pre-receive" |
| 39 | chmod +x "$origin/hooks/pre-receive" |
| 40 | |
| 41 | repo="$w/project" |
| 42 | git init --quiet "$repo" |
| 43 | cd "$repo" |
| 44 | git remote add origin "$origin" |
| 45 | # The project layer caps the restacker at two automatic versions a change. |
| 46 | mkdir -p policy/project |
| 47 | printf 'package verification\n\nproject: restack: maxAutomaticVersions: 2\n' > policy/project/policy.cue |
| 48 | echo base > readme |
| 49 | git add readme policy |
| 50 | git commit --quiet -m base |
| 51 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 52 | |
| 53 | as() { |
| 54 | local who="$1" |
| 55 | shift |
| 56 | git config valley.principal "$who" |
| 57 | VALLEY_PRINCIPAL="$who" "$@" |
| 58 | } |
| 59 | ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; } |
| 60 | |
| 61 | # change <owner> <branch> <base>: a commit on base, pushed and readied by |
| 62 | # its owner; prints the change ID. |
| 63 | change() { |
| 64 | local who="$1" branch="$2" base="$3" |
| 65 | git checkout --quiet -B "$branch" "$base" |
| 66 | echo "$branch" > "$branch" |
| 67 | git add "$branch" |
| 68 | git commit --quiet -m "$branch" |
| 69 | as "$who" git push --quiet --force origin "$branch" |
| 70 | ok as "$who" valley ready |
| 71 | sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out" |
| 72 | } |
| 73 | # branch <name> <base>: a plain branch, which no record is on. |
| 74 | branch() { |
| 75 | git checkout --quiet -B "$1" "$2" |
| 76 | echo "$1" > "$1" |
| 77 | git add "$1" |
| 78 | git commit --quiet -m "$1" |
| 79 | as alice git push --quiet origin "$1" |
| 80 | } |
| 81 | |
| 82 | base="$(git rev-parse main)" |
| 83 | a="$(change alice a "$base")" |
| 84 | b1="$(change bob b1 "$base")" |
| 85 | b2="$(change bob b2 b1)" |
| 86 | c1="$(change alice c1 "$base")" |
| 87 | c2="$(change alice c2 c1)" |
| 88 | c3="$(change bob c3 c1)" |
| 89 | d="$(change alice d "$base")" |
| 90 | ok as alice valley unready "$d" |
| 91 | wd="$(change bob wd "$base")" |
| 92 | ok as bob valley withdraw "$wd" |
| 93 | branch plain "$base" |
| 94 | e="$(change alice e "$base")" |
| 95 | branch e-plain e |
| 96 | g1="$(change alice g1 "$base")" |
| 97 | g2="$(change alice g2 g1)" |
| 98 | ok as alice valley unready "$g1" |
| 99 | u="$(change bob u "$base")" |
| 100 | # k gains two versions from the restacker principal. A version the restack |
| 101 | # grant adds keeps the active version's title, so each commit's subject is |
| 102 | # k's title; one under any other title is refused, and its branch is put |
| 103 | # back. |
| 104 | k="$(change alice k "$base")" |
| 105 | git checkout --quiet k |
| 106 | for n in 2 3; do |
| 107 | echo "$n" >> k |
| 108 | git commit --quiet -am k |
| 109 | as restacker git push --quiet origin k |
| 110 | ok as restacker valley ready --change "$k" k |
| 111 | done |
| 112 | echo forged >> k |
| 113 | git commit --quiet -am 'a title of its own' |
| 114 | as restacker git push --quiet origin k |
| 115 | if as restacker valley ready --change "$k" k > "$w/out" 2> "$w/err"; then |
| 116 | fail 'the restacker readied a version under a title of its own' |
| 117 | fi |
| 118 | grep -q 'under the title of the active one' "$w/err" || fail 'the refusal does not name the title' |
| 119 | git reset --quiet --hard HEAD~1 |
| 120 | as restacker git push --quiet --force origin k |
| 121 | # s lands by fast-forward before main moves, so it is submitted. |
| 122 | s="$(change alice s "$base")" |
| 123 | env -u VALLEY_PRINCIPAL git push --quiet origin s:main |
| 124 | git checkout --quiet main |
| 125 | git reset --quiet --hard origin/main |
| 126 | echo moved > moved |
| 127 | git add moved |
| 128 | git commit --quiet -m moved |
| 129 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 130 | tip="$(git rev-parse main)" |
| 131 | |
| 132 | # Server-side injection of an outcome record under a request name. |
| 133 | evidence() { |
| 134 | git -C "$origin" for-each-ref --format='%(refname) %(objectname)' refs/the-valley/attestations/ \ |
| 135 | | LC_ALL=C sort | sha256sum | cut -c1-16 |
| 136 | } |
| 137 | inject() { |
| 138 | local name="$1" line="$2" blob |
| 139 | blob="$(printf '%s\n' "$line" | git -C "$origin" hash-object -w --stdin)" |
| 140 | git -C "$origin" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob" |
| 141 | } |
| 142 | # u's request was judged failed, and no request is left: its landing is |
| 143 | # uncertain. |
| 144 | inject "c-$u" "$(git rev-parse u) $base $(evidence) failed 2026-10-18T00:00:00Z" |
| 145 | |
| 146 | # request <name> <head>: requester files an integration request. |
| 147 | request() { as requester git push --quiet origin "$2:refs/the-valley/integration-requests/main/$1"; } |
| 148 | |
| 149 | refs() { git -C "$origin" for-each-ref --format='%(refname) %(objectname)' | LC_ALL=C sort; } |
| 150 | plan() { |
| 151 | local before |
| 152 | before="$(refs)" |
| 153 | valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \ |
| 154 | --principal restacker --instance "$smokePolicy/instance" "$@" > "$w/plan.json" 2> "$w/err" \ |
| 155 | || fail "the planner failed" |
| 156 | [ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin' |
| 157 | } |
| 158 | # family <id>: the family plan a change is a layer of. |
| 159 | family() { jq -c --arg id "$1" '.families[] | select(any(.layers[]; .id == $id))' "$w/plan.json"; } |
| 160 | action() { |
| 161 | local got |
| 162 | got="$(family "$1" | jq -r .action)" |
| 163 | [ "$got" = "$2" ] || fail "the family of $1 is $got, not $2: $(family "$1")" |
| 164 | } |
| 165 | says() { family "$1" | jq -e --arg s "$2" 'any(.reasons[], .notes[]; contains($s))' > /dev/null \ |
| 166 | || fail "the family of $1 does not say: $2: $(family "$1")"; } |
| 167 | |
| 168 | # ---------------------------------------------------------------------- |
| 169 | # Selection and families. |
| 170 | plan |
| 171 | jq -e --arg t "$tip" '.tip == $t and .maxAutomaticVersions == 2' "$w/plan.json" > /dev/null \ |
| 172 | || fail "the plan does not read main's tip or the project's cap" |
| 173 | action "$a" restack |
| 174 | action "$b1" restack |
| 175 | [ "$(family "$b1" | jq -c '[.layers[].id]')" = "[\"$b1\",\"$b2\"]" ] || fail 'b1 and b2 are not one family' |
| 176 | [ "$(family "$c1" | jq '.layers | length')" = 3 ] || fail 'c1, c2 and c3 are not one family' |
| 177 | [ "$(family "$c2" | jq -r '.layers[0].id')" = "$c1" ] || fail 'the shared lower layer is not the bottom' |
| 178 | action "$c3" restack |
| 179 | action "$d" leave |
| 180 | action "$wd" leave |
| 181 | action "$u" leave |
| 182 | action "$e" hold |
| 183 | says "$e" 'e-plain is a plain branch' |
| 184 | action "$g2" hold |
| 185 | says "$g2" 'is a draft' |
| 186 | [ -z "$(family "$s")" ] || fail 'a submitted change was planned' |
| 187 | jq -e '[.families[].layers[] | select(.name == "plain")] | length == 0' "$w/plan.json" > /dev/null \ |
| 188 | || fail 'a lone plain branch was planned' |
| 189 | |
| 190 | # Capacity: k has two versions uploaded by the restacker principal, and a |
| 191 | # third is over the project's cap of 2. |
| 192 | action "$k" hold |
| 193 | says "$k" 'over the cap of 2' |
| 194 | family "$k" | jq -e '.layers[0].capacity.uploads == 3' > /dev/null || fail 'k does not count three uploads' |
| 195 | |
| 196 | # ---------------------------------------------------------------------- |
| 197 | # Request holds. A request by name with no outcome holds. |
| 198 | request a a |
| 199 | plan |
| 200 | action "$a" hold |
| 201 | says "$a" 'no outcome yet' |
| 202 | family "$a" | jq -e '.requests[0].by == ["name", "head"]' > /dev/null || fail 'request a was not related by name' |
| 203 | # A current stale releases. |
| 204 | inject a "$(git rev-parse a) $tip $(evidence) stale" |
| 205 | plan |
| 206 | action "$a" restack |
| 207 | # A mismatched head, tip or fingerprint holds. |
| 208 | inject a "$base $tip $(evidence) stale" |
| 209 | plan |
| 210 | action "$a" hold |
| 211 | says "$a" 'is about head' |
| 212 | inject a "$(git rev-parse a) $base $(evidence) stale" |
| 213 | plan |
| 214 | says "$a" 'judged on main at' |
| 215 | inject a "$(git rev-parse a) $tip 0000000000000000 stale" |
| 216 | plan |
| 217 | says "$a" 'judged on evidence' |
| 218 | # A malformed outcome holds. |
| 219 | inject a "not an outcome" |
| 220 | plan |
| 221 | says "$a" 'does not read' |
| 222 | # A current reject releases, with a note. |
| 223 | inject a "$(git rev-parse a) $tip $(evidence) reject" |
| 224 | plan |
| 225 | action "$a" restack |
| 226 | says "$a" 'was rejected' |
| 227 | |
| 228 | # By an exact head, under a name nothing else relates. |
| 229 | request zz-head "$(git rev-parse b2)" |
| 230 | # By range: a commit on c2 no branch holds, so only the request names it |
| 231 | # and the planner has to fetch it before reading its ancestry. |
| 232 | git checkout --quiet -B scratch c2 |
| 233 | echo over > over |
| 234 | git add over |
| 235 | git commit --quiet -m over |
| 236 | request zz-range "$(git rev-parse HEAD)" |
| 237 | git checkout --quiet main |
| 238 | git branch --quiet -D scratch |
| 239 | plan |
| 240 | action "$b1" hold |
| 241 | family "$b1" | jq -e '.requests[] | select(.name == "zz-head") | .by == ["head"]' > /dev/null \ |
| 242 | || fail 'zz-head was not related by head' |
| 243 | action "$c1" hold |
| 244 | family "$c1" | jq -e '.requests[] | select(.name == "zz-range") | .by == ["range"]' > /dev/null \ |
| 245 | || fail 'zz-range was not related by range' |
| 246 | action "$c3" hold |
| 247 | |
| 248 | # A request whose head is no commit cannot be related or not, so every |
| 249 | # family it might be about holds. Injected on the server side, since the |
| 250 | # hook takes no such request. |
| 251 | git -C "$origin" update-ref refs/the-valley/integration-requests/main/zz-blob \ |
| 252 | "$(echo blob | git -C "$origin" hash-object -w --stdin)" |
| 253 | plan |
| 254 | action "$a" hold |
| 255 | says "$a" 'cannot be determined' |
| 256 | |
| 257 | # The human summary reads the same plan. |
| 258 | valley-restacker once --report-only --repo "$origin" --state "$w/state" --principal restacker \ |
| 259 | --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err" || fail 'the summary failed' |
| 260 | grep -qE '^[k-z]+: hold$' "$w/out" || fail 'the summary holds no family' |
| 261 | |
| 262 | # ---------------------------------------------------------------------- |
| 263 | # A cap larger than any int. The schema admits it, so it composes, and the |
| 264 | # planner counts it as the largest int: no practical cap. k, held above by |
| 265 | # the cap of 2, is not held by this one. The request whose head is no |
| 266 | # commit would hold k too, so it is removed first, on the server side |
| 267 | # where it was injected. |
| 268 | git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-blob |
| 269 | git checkout --quiet main |
| 270 | printf 'package verification\n\nproject: restack: maxAutomaticVersions: 9223372036854775808\n' \ |
| 271 | > policy/project/policy.cue |
| 272 | git commit --quiet -am 'a cap larger than any int' |
| 273 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 274 | plan |
| 275 | grep -q '"maxAutomaticVersions": 9223372036854775807,' "$w/plan.json" \ |
| 276 | || fail "the plan does not count the cap as the largest int: $(cat "$w/plan.json")" |
| 277 | action "$k" restack |
| 278 | |
| 279 | # ---------------------------------------------------------------------- |
| 280 | # The writes a pass would make, built and not pushed: k's branch push, its |
| 281 | # fourth version and its report, as complete, and e's report alone, as |
| 282 | # blocked, since its family is held. plan has held every ref on origin |
| 283 | # still across every pass. |
| 284 | family "$k" | jq -e --arg id "$k" '.writes.result == "complete" |
| 285 | and (.writes.branches | map(.branch)) == ["k"] |
| 286 | and (.writes.versions | map([.change_id, .version])) == [[$id, 4]] |
| 287 | and (.writes.reports | map([.change_id, .result])) == [[$id, "complete"]]' > /dev/null \ |
| 288 | || fail "k's writes are not a complete restack: $(family "$k")" |
| 289 | family "$e" | jq -e --arg id "$e" '.writes.result == "blocked" and .writes.branches == [] and .writes.versions == [] |
| 290 | and (.writes.reports | map([.change_id, .result])) == [[$id, "blocked"]]' > /dev/null \ |
| 291 | || fail "e's writes are not a blocked report: $(family "$e")" |
| 292 | |
| 293 | # A request for an older version of a change holds its family, whatever |
| 294 | # its name: by that version's exact head, and by a range that holds it and |
| 295 | # not the active head. k's first version is no layer's head now. |
| 296 | k1="$(git -C "$origin" cat-file blob "refs/the-valley/changes/main/$k:record.json" | jq -r '.versions[0].head | ltrimstr("git-sha1:")')" |
| 297 | [ "$k1" != "$(git rev-parse k)" ] || fail "k's first version is its active head" |
| 298 | request zz-old "$k1" |
| 299 | plan |
| 300 | action "$k" hold |
| 301 | family "$k" | jq -e '.requests[] | select(.name == "zz-old") | .by == ["head"]' > /dev/null \ |
| 302 | || fail "a request at k's first version head was not related by head: $(family "$k")" |
| 303 | git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old |
| 304 | git checkout --quiet -B scratch "$k1" |
| 305 | echo "on k's first version" > on-k1 |
| 306 | git add on-k1 |
| 307 | git commit --quiet -m 'on k v1' |
| 308 | request zz-old-range "$(git rev-parse HEAD)" |
| 309 | git checkout --quiet main |
| 310 | git branch --quiet -D scratch |
| 311 | plan |
| 312 | action "$k" hold |
| 313 | family "$k" | jq -e '.requests[] | select(.name == "zz-old-range") | .by == ["range"]' > /dev/null \ |
| 314 | || fail "a request on k's first version was not related by range: $(family "$k")" |
| 315 | git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old-range |
| 316 | |
| 317 | # No git variable that points git elsewhere, or gives it configuration, |
| 318 | # reaches the restacker. With each pointing at a decoy, the pass plans and |
| 319 | # builds what it does without, and the decoy is untouched. |
| 320 | shape() { jq -S 'del(.operation) | .families |= map(.writes |= (if . then .result else . end))' "$w/plan.json"; } |
| 321 | plan |
| 322 | shape > "$w/without" |
| 323 | decoy="$w/decoy" |
| 324 | git init --quiet --bare "$decoy/repo.git" |
| 325 | mkdir -p "$decoy/wt" "$decoy/objects/info" "$decoy/objects/pack" |
| 326 | digest() { find "$decoy" -type f -exec sha256sum {} + | LC_ALL=C sort | sha256sum; } |
| 327 | before_decoy="$(digest)" |
| 328 | before="$(refs)" |
| 329 | env GIT_DIR="$decoy/repo.git" GIT_WORK_TREE="$decoy/wt" GIT_INDEX_FILE="$decoy/index" \ |
| 330 | GIT_OBJECT_DIRECTORY="$decoy/objects" GIT_ALTERNATE_OBJECT_DIRECTORIES="$decoy/no-objects" \ |
| 331 | GIT_COMMON_DIR="$decoy/repo.git" GIT_NAMESPACE=decoy GIT_DEFAULT_HASH=sha256 \ |
| 332 | GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.$decoy/repo.git.insteadOf" GIT_CONFIG_VALUE_0="$origin" \ |
| 333 | GIT_CONFIG_PARAMETERS="'url.$decoy/repo.git.insteadof'='$origin'" \ |
| 334 | valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \ |
| 335 | --principal restacker --instance "$smokePolicy/instance" > "$w/plan.json" 2> "$w/err" \ |
| 336 | || fail "the planner failed with git variables inherited" |
| 337 | [ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin' |
| 338 | shape > "$w/with" |
| 339 | diff "$w/without" "$w/with" > /dev/null || fail "with git variables inherited, the plan differs: $(diff "$w/without" "$w/with")" |
| 340 | [ "$(digest)" = "$before_decoy" ] || fail 'with git variables inherited, the pass changed the decoy' |
| 341 | ! grep -qF "$decoy" "$w/err" || fail "with git variables inherited, the pass printed: $(cat "$w/err")" |
| 342 | |
| 343 | # A pass whose stderr nobody reads still ends, with the error it could not |
| 344 | # print. Its stderr is a fifo that is held open, filled, and never read. |
| 345 | mkfifo "$w/unread-err" |
| 346 | exec 8<> "$w/unread-err" |
| 347 | dd if=/dev/zero of="$w/unread-err" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true |
| 348 | dd if=/dev/zero of="$w/unread-err" bs=1 count=4096 oflag=nonblock 2> /dev/null || true |
| 349 | status=0 |
| 350 | start=$SECONDS |
| 351 | timeout -k 5 60 valley-restacker once --report-only --no-such-flag 2> "$w/unread-err" > /dev/null || status=$? |
| 352 | took=$((SECONDS - start)) |
| 353 | exec 8<&- |
| 354 | [ "$status" = 1 ] || fail "a pass whose stderr nobody read ended with $status, not 1" |
| 355 | [ "$took" -lt 30 ] || fail "a pass whose stderr nobody read took ${took}s to end" |
| 356 | |
| 357 | |
| 358 | # ====================================================================== |
| 359 | # The fixtures the publishing slice's tests stand on, each proved here. |
| 360 | |
| 361 | # One verifier file, which the rendered hook checks pushed evidence |
| 362 | # against, and the integrator and the restacker read too. |
| 363 | signers="$keys/known_signers" |
| 364 | grep -qF -- "--known-signers $signers" "$hook" || fail 'the rendered hook does not read the shared verifier keys' |
| 365 | # ssh-keygen signs only with a key nobody else can read. |
| 366 | mkdir -p "$w/keys" |
| 367 | for who in contributor integrator; do install -m 600 "$keys/$who" "$w/keys/$who"; done |
| 368 | contributor=contributor.valley.invalid/attestations |
| 369 | integrator_name=integrator.valley.invalid/attestations |
| 370 | |
| 371 | # The transport (restack/e2e/transport): git reaches origin through it as |
| 372 | # ssh, as the principal the URL names, and the real receive-pack runs the |
| 373 | # rendered hook. A barrier is armed by writing it to $VALLEY_BARRIER/hold. |
| 374 | export GIT_SSH_COMMAND=valley-restack-transport GIT_SSH_VARIANT=simple |
| 375 | export VALLEY_BARRIER="$w/barrier" |
| 376 | mkdir -p "$VALLEY_BARRIER" |
| 377 | url() { printf 'ssh://%s@valley%s' "$1" "$2"; } |
| 378 | disarm() { rm -f "${VALLEY_BARRIER:?}"/*; } |
| 379 | arm() { |
| 380 | disarm |
| 381 | echo "$*" > "$VALLEY_BARRIER/hold" |
| 382 | } |
| 383 | arrived() { |
| 384 | local i=0 |
| 385 | until [ -e "$VALLEY_BARRIER/arrived" ]; do |
| 386 | i=$((i + 1)) |
| 387 | [ "$i" -lt 1200 ] || fail 'nothing reached the barrier' |
| 388 | sleep 0.05 |
| 389 | done |
| 390 | } |
| 391 | release() { touch "$VALLEY_BARRIER/release"; } |
| 392 | at() { git -C "$1" rev-parse --verify --quiet "$2" || true; } |
| 393 | |
| 394 | # ---------------------------------------------------------------------- |
| 395 | # The restacker principal holds restack and not request, so it cannot file |
| 396 | # a request, which requester can. |
| 397 | cd "$repo" |
| 398 | git checkout --quiet -B fixture main |
| 399 | if git push --quiet "$(url restacker "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-restacker \ |
| 400 | 2> "$w/err"; then |
| 401 | fail 'the restacker principal filed a request' |
| 402 | fi |
| 403 | git push --quiet "$(url requester "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-requester \ |
| 404 | 2> "$w/err" || fail 'requester could not file a request through the transport' |
| 405 | git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-by-requester |
| 406 | |
| 407 | # A barrier holds a push before origin sees it, and releases it. |
| 408 | echo held > held |
| 409 | git add held |
| 410 | git commit --quiet -m held |
| 411 | arm receive connect |
| 412 | git push --quiet "$(url alice "$origin")" fixture:refs/heads/held 2> "$w/err" & |
| 413 | pid=$! |
| 414 | arrived |
| 415 | [ -z "$(at "$origin" refs/heads/held)" ] || fail 'a push the barrier held reached origin' |
| 416 | kill -0 "$pid" 2> /dev/null || fail 'a push the barrier held ended' |
| 417 | release |
| 418 | wait "$pid" || fail 'a released push failed' |
| 419 | [ "$(at "$origin" refs/heads/held)" = "$(git rev-parse fixture)" ] || fail 'a released push did not reach origin' |
| 420 | |
| 421 | # A push held there loses its lease to one made while it waits. |
| 422 | was="$(git rev-parse fixture)" |
| 423 | git commit --quiet --allow-empty -m mine |
| 424 | arm receive connect |
| 425 | git push --quiet --force-with-lease="refs/heads/held:$was" "$(url restacker "$origin")" fixture:refs/heads/held \ |
| 426 | 2> "$w/err" & |
| 427 | pid=$! |
| 428 | arrived |
| 429 | git checkout --quiet -B author "$was" |
| 430 | git commit --quiet --allow-empty -m "the author's" |
| 431 | as alice git push --quiet origin author:held |
| 432 | release |
| 433 | if wait "$pid"; then fail 'a push held across another kept its lease'; fi |
| 434 | grep -q 'stale info' "$w/err" || fail "the held push was not refused for its lease: $(cat "$w/err")" |
| 435 | [ "$(at "$origin" refs/heads/held)" = "$(git rev-parse author)" ] || fail "the author's push did not stand" |
| 436 | |
| 437 | # A barrier holds a push that origin has taken, before its client hears. |
| 438 | git checkout --quiet fixture |
| 439 | arm receive taken refs/heads/taken |
| 440 | git push --quiet "$(url alice "$origin")" fixture:refs/heads/taken 2> "$w/err" & |
| 441 | pid=$! |
| 442 | arrived |
| 443 | [ "$(at "$origin" refs/heads/taken)" = "$(git rev-parse fixture)" ] || fail 'origin had not taken the held push' |
| 444 | kill -0 "$pid" 2> /dev/null || fail 'the client heard before the release' |
| 445 | release |
| 446 | wait "$pid" || fail 'a push held after origin took it failed' |
| 447 | |
| 448 | # A dropped answer: origin takes the push, and its client reports that |
| 449 | # origin never said so. |
| 450 | arm receive drop refs/heads/dropped |
| 451 | if git push --porcelain "$(url alice "$origin")" fixture:refs/heads/dropped > "$w/out" 2> "$w/err"; then |
| 452 | fail 'a push whose answer was lost succeeded' |
| 453 | fi |
| 454 | cat "$w/out" "$w/err" | grep -q 'remote failed to report status' || fail 'the client did not lose the answer' |
| 455 | [ "$(at "$origin" refs/heads/dropped)" = "$(git rev-parse fixture)" ] || fail 'origin did not take the push whose answer was lost' |
| 456 | disarm |
| 457 | |
| 458 | # ---------------------------------------------------------------------- |
| 459 | # A repository served with the integrator's evidence fixtures: the nix |
| 460 | # stand-in (integrator/e2e), and a policy whose checks are pure, under a |
| 461 | # floor that requires nothing. |
| 462 | cp -r "$integratorE2e" "$w/stand-ins" |
| 463 | chmod -R +w "$w/stand-ins" |
| 464 | patchShebangs "$w/stand-ins" > /dev/null |
| 465 | export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store" |
| 466 | mkdir -p "$w/floor" |
| 467 | printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue" |
| 468 | |
| 469 | served="$w/served.git" |
| 470 | git init --quiet --bare "$served" |
| 471 | cp "$hook" "$served/hooks/pre-receive" |
| 472 | chmod +x "$served/hooks/pre-receive" |
| 473 | git init --quiet "$w/fixture" |
| 474 | cd "$w/fixture" |
| 475 | git remote add origin "$served" |
| 476 | mkdir -p docs src checks policy/project |
| 477 | echo "the readme" > docs/readme.md |
| 478 | echo "the source" > src/main.txt |
| 479 | echo docs > checks/prose-format.inputs |
| 480 | echo src > checks/code-build.inputs |
| 481 | cat > policy/project/policy.cue << 'CUE' |
| 482 | package verification |
| 483 | |
| 484 | project: { |
| 485 | checks: { |
| 486 | "prose-format": {runner: "nix", attribute: "prose-format"} |
| 487 | "code-build": {runner: "nix", attribute: "code-build"} |
| 488 | } |
| 489 | classes: { |
| 490 | prose: {paths: "docs/**": true, requires: "prose-format": true} |
| 491 | code: {paths: "src/**": true, requires: "code-build": true} |
| 492 | } |
| 493 | unclassified: {} |
| 494 | } |
| 495 | CUE |
| 496 | git add -A |
| 497 | git commit --quiet -m 'seed the project' |
| 498 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 499 | |
| 500 | # attested <branch> <attest run flags…>: alice's change, its evidence signed |
| 501 | # by the contributor's key and pushed with it through the hook, readied; |
| 502 | # prints the change ID. |
| 503 | attested() { |
| 504 | local branch="$1" |
| 505 | shift |
| 506 | as alice attest run --repo . --key "$w/keys/contributor" --name "$contributor" \ |
| 507 | --push origin --branch "$branch" "$@" > "$w/out" 2> "$w/err" || fail "attest run on $branch failed" |
| 508 | ok as alice valley ready |
| 509 | sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out" |
| 510 | } |
| 511 | git checkout --quiet -b lands main |
| 512 | echo "a second source line" >> src/main.txt |
| 513 | git commit --quiet -am 'a change the integrator lands' |
| 514 | lands="$(attested lands --check code-build)" |
| 515 | # Its evidence is edited after it was signed, so the signature no longer |
| 516 | # verifies. The integrator rejects a tampered note. (A note of the wrong |
| 517 | # kind, an unknown signer's note or a failing note is no evidence, and the |
| 518 | # request is stale.) |
| 519 | git checkout --quiet -b lies main |
| 520 | echo "a readme whose evidence lies" > docs/readme.md |
| 521 | git commit --quiet -am 'a change whose evidence lies about its signed result' |
| 522 | lies="$(attested lies --check prose-format)" |
| 523 | lies_ref="$(git -C "$served" for-each-ref --format='%(refname)' "refs/the-valley/attestations/$(attest digest --repo . --rev lies | cut -d: -f2)" | head -n1)" |
| 524 | [ -n "$lies_ref" ] || fail "lies has no evidence on the served repository" |
| 525 | git -C "$served" cat-file blob "$lies_ref:prose-format/statement.note" \ |
| 526 | | sed 's/^predicate.result passed$/predicate.result failed/' > "$w/lies.note" |
| 527 | cmp -s "$w/lies.note" <(git -C "$served" cat-file blob "$lies_ref:prose-format/statement.note") \ |
| 528 | && fail 'the tamper changed nothing' |
| 529 | lies_blob="$(git -C "$served" hash-object -w --stdin < "$w/lies.note")" |
| 530 | lies_sub="$(printf '100644 blob %s\tstatement.note\n' "$lies_blob" | git -C "$served" mktree)" |
| 531 | git -C "$served" update-ref "$lies_ref" "$(printf '040000 tree %s\tprose-format\n' "$lies_sub" | git -C "$served" mktree)" |
| 532 | git checkout --quiet main |
| 533 | echo moved > moved.txt |
| 534 | git add moved.txt |
| 535 | git commit --quiet -m 'main moves on' |
| 536 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 537 | |
| 538 | integrate() { |
| 539 | integrator reconcile --repo "$served" --key "$w/keys/integrator" --name "$integrator_name" \ |
| 540 | --known-signers "$signers" --instance "$w/floor" --schema "$SCHEMA_VERIFICATION" \ |
| 541 | --attest-schema "$SCHEMA_ATTESTATION" --event-schema "$SCHEMA_EVENTS" > "$w/integrator.out" 2>&1 \ |
| 542 | || fail "the integrator failed: $(cat "$w/integrator.out")" |
| 543 | } |
| 544 | judged() { |
| 545 | grep -q "^c-$1 -> refs/heads/main .*: $2\$" "$w/integrator.out" \ |
| 546 | || fail "the integrator did not $2 c-$1: $(cat "$w/integrator.out")" |
| 547 | } |
| 548 | # requester files each request through the transport, one pass each, so |
| 549 | # the reject is judged on the tip the landing left. |
| 550 | git push --quiet "$(url requester "$served")" "lands:refs/the-valley/integration-requests/main/c-$lands" |
| 551 | integrate |
| 552 | judged "$lands" land |
| 553 | git push --quiet "$(url requester "$served")" "lies:refs/the-valley/integration-requests/main/c-$lies" |
| 554 | integrate |
| 555 | judged "$lies" reject |
| 556 | grep -q 'does not verify as the statement it claims to be' "$w/integrator.out" \ |
| 557 | || fail "the reject is not for the tampered evidence: $(cat "$w/integrator.out")" |
| 558 | |
| 559 | # The planner, reading the shared verifier keys, sees the landed change |
| 560 | # submitted: it is no layer. Without the keys, the same landing is only |
| 561 | # uncertain. The rejected change's family is released by the real |
| 562 | # outcome, current at its head, tip and evidence, and the reject is noted. |
| 563 | served_plan() { |
| 564 | valley-restacker once --report-only --json --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 565 | --principal restacker --instance "$w/floor" "$@" > "$w/plan.json" 2> "$w/err" || fail 'the planner failed' |
| 566 | } |
| 567 | served_plan --known-signers "$signers" --integrator-name "$integrator_name" |
| 568 | [ -z "$(family "$lands")" ] || fail "the change the integrator landed was planned: $(family "$lands")" |
| 569 | action "$lies" restack |
| 570 | says "$lies" 'was rejected' |
| 571 | family "$lies" | jq -e --arg n "c-$lies" '.requests[] | select(.name == $n) | .decision == "reject" and .current' \ |
| 572 | > /dev/null || fail "the reject was not read as current: $(family "$lies")" |
| 573 | served_plan |
| 574 | action "$lands" leave |
| 575 | family "$lands" | jq -e '.layers[0].state == "uncertain"' > /dev/null \ |
| 576 | || fail "without the keys, the landing is not uncertain: $(family "$lands")" |
| 577 | # A signer projects file that restricts the integrator name says that |
| 578 | # name is not the integrator's, so with the keys the same landing is again |
| 579 | # only uncertain. A file that does not read |
| 580 | # stops the pass. |
| 581 | printf '%s served\n' "$integrator_name" > "$w/restricts-the-integrator" |
| 582 | served_plan --known-signers "$signers" --integrator-name "$integrator_name" \ |
| 583 | --signer-projects "$w/restricts-the-integrator" |
| 584 | action "$lands" leave |
| 585 | family "$lands" | jq -e '.layers[0].state == "uncertain"' > /dev/null \ |
| 586 | || fail "with the integrator name restricted, the landing is not uncertain: $(family "$lands")" |
| 587 | printf '%s served\n%s served\n' "$integrator_name" "$integrator_name" > "$w/named-twice" |
| 588 | if valley-restacker once --report-only --json --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 589 | --principal restacker --instance "$w/floor" --known-signers "$signers" --integrator-name "$integrator_name" \ |
| 590 | --signer-projects "$w/named-twice" > "$w/plan.json" 2> "$w/err"; then |
| 591 | fail 'a pass ran with a signer projects file that does not read' |
| 592 | fi |
| 593 | grep -q -- "--signer-projects $w/named-twice does not read" "$w/err" || fail "the pass did not say why: $(cat "$w/err")" |
| 594 | |
| 595 | # ---------------------------------------------------------------------- |
| 596 | # A pass locks its state directory: a second pass fails while a barrier |
| 597 | # holds the first one's read of origin, and the first goes on once |
| 598 | # released. The directory belongs to the restacker principal, so a pass |
| 599 | # for another is refused it. |
| 600 | pass() { |
| 601 | valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 602 | --principal restacker --instance "$w/floor" "$@" |
| 603 | } |
| 604 | arm upload connect |
| 605 | pass > "$w/first.out" 2> "$w/first.err" & |
| 606 | pid=$! |
| 607 | arrived |
| 608 | if pass > "$w/out" 2> "$w/err"; then fail 'a second pass ran while the first held the state'; fi |
| 609 | grep -q 'another pass is using the state directory' "$w/err" || fail "the second pass did not say why: $(cat "$w/err")" |
| 610 | release |
| 611 | wait "$pid" || fail "the first pass failed once released: $(cat "$w/first.err")" |
| 612 | if valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 613 | --principal requester --instance "$w/floor" > "$w/out" 2> "$w/err"; then |
| 614 | fail 'a pass for another principal used the state directory' |
| 615 | fi |
| 616 | grep -q 'give each its own --state' "$w/err" || fail "the refusal does not say why: $(cat "$w/err")" |
| 617 | |
| 618 | # A pass whose read of origin never returns ends at its --timeout. |
| 619 | arm upload connect |
| 620 | if pass --timeout 2s > "$w/out" 2> "$w/err"; then fail 'a pass held at the barrier finished'; fi |
| 621 | grep -q 'ran past its --timeout of 2s' "$w/err" || fail "the pass did not end at its timeout: $(cat "$w/err")" |
| 622 | disarm |
| 623 | |
| 624 | # A pass whose output nobody reads ends at its --timeout too, and lets go |
| 625 | # of the lock on its state, so the next pass takes it. Its stdout is a |
| 626 | # fifo that is held open, filled, and never read, so the pass's first |
| 627 | # write to it blocks. The outer timeout only bounds this check: it kills |
| 628 | # a pass that outlives the stop it sends. |
| 629 | mkfifo "$w/unread" |
| 630 | exec 7<> "$w/unread" |
| 631 | dd if=/dev/zero of="$w/unread" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true |
| 632 | dd if=/dev/zero of="$w/unread" bs=1 count=4096 oflag=nonblock 2> /dev/null || true |
| 633 | status=0 |
| 634 | start=$SECONDS |
| 635 | timeout -k 5 60 valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 636 | --principal restacker --instance "$w/floor" --timeout 15s > "$w/unread" 2> "$w/err" || status=$? |
| 637 | took=$((SECONDS - start)) |
| 638 | exec 7<&- |
| 639 | case "$status" in |
| 640 | 0) fail 'a pass whose output nobody read finished' ;; |
| 641 | 124 | 137) fail 'a pass whose output nobody read did not end at its timeout' ;; |
| 642 | esac |
| 643 | [ "$took" -lt 45 ] || fail "a pass with a timeout of 15s took ${took}s" |
| 644 | grep -q 'ran past its --timeout of 15s: the plan was not written' "$w/err" \ |
| 645 | || fail "the pass did not end at its timeout while writing: $(cat "$w/err")" |
| 646 | pass > "$w/out" 2> "$w/err" || fail "the next pass did not take the lock: $(cat "$w/err")" |
| 647 | |
| 648 | # ---------------------------------------------------------------------- |
| 649 | # Write passes through the transport, as the restacker principal, on the |
| 650 | # repository the real integrator lands in. bob's change, readied on the |
| 651 | # seed, and lies, which its current reject releases, are each restacked |
| 652 | # after the real landing, with no bus: each branch is rebased onto main's |
| 653 | # tip, each record gains one version the restacker uploaded under its |
| 654 | # owner, and each report says complete. lies's request still names its |
| 655 | # old head. The answer to the first record push is dropped: origin takes |
| 656 | # it and the client hears nothing, so the pass's next read finds the |
| 657 | # version on origin, and adds it once. A second pass pushes nothing. |
| 658 | seed="$(git -C "$served" rev-list --max-parents=0 main)" |
| 659 | bobs="$(change bob bobs "$seed")" |
| 660 | lies_was="$(at "$served" refs/heads/lies)" |
| 661 | write_pass() { |
| 662 | valley-restacker once --json --repo "$(url restacker "$served")" --state "$w/served-state" \ |
| 663 | --principal restacker --instance "$w/floor" --known-signers "$signers" --integrator-name "$integrator_name" \ |
| 664 | > "$w/plan.json" 2> "$w/err" || fail "the write pass failed: $(cat "$w/err")" |
| 665 | } |
| 666 | arm receive drop refs/the-valley/changes/ |
| 667 | write_pass |
| 668 | disarm |
| 669 | main_tip="$(at "$served" refs/heads/main)" |
| 670 | for pair in "$lies:lies:alice" "$bobs:bobs:bob"; do |
| 671 | IFS=: read -r id branch owner <<< "$pair" |
| 672 | head="$(at "$served" "refs/heads/$branch")" |
| 673 | [ "$(git -C "$served" rev-parse "$head^")" = "$main_tip" ] || fail "$branch was not rebased onto main's tip" |
| 674 | record="$(git -C "$served" cat-file blob "refs/the-valley/changes/main/$id:record.json")" |
| 675 | jq -e --arg h "git-sha1:$head" --arg o "$owner" '.seq == 2 and .active == 2 and (.versions | length) == 2 |
| 676 | and .versions[1].head == $h and .versions[1].uploader == "restacker" and .owner == $o' <<< "$record" > /dev/null \ |
| 677 | || fail "$branch's record is not restacked once: $record" |
| 678 | git -C "$served" cat-file blob "refs/the-valley/restack/main/$id:report.json" | jq -e '.result == "complete"' > /dev/null \ |
| 679 | || fail "$branch's report does not say complete" |
| 680 | done |
| 681 | jq -e '[.actions[] | select(.write == "version")] | length == 2 and any(.[]; .result == "unknown")' "$w/plan.json" > /dev/null \ |
| 682 | || fail "the dropped answer was not a push of unknown result: $(jq -c .actions "$w/plan.json")" |
| 683 | [ "$(at "$served" "refs/the-valley/integration-requests/main/c-$lies")" = "$lies_was" ] || fail "lies's request was moved" |
| 684 | before="$(git -C "$served" for-each-ref --format='%(refname) %(objectname)')" |
| 685 | write_pass |
| 686 | jq -e '.actions == []' "$w/plan.json" > /dev/null || fail "a second write pass pushed: $(jq -c .actions "$w/plan.json")" |
| 687 | [ "$(git -C "$served" for-each-ref --format='%(refname) %(objectname)')" = "$before" ] || fail 'a second write pass moved a ref' |
| 688 | |
| 689 | # ---------------------------------------------------------------------- |
| 690 | # main -> under-p -> under-a -> under-b, with under-p a plain branch. The |
| 691 | # real integrator lands under-a as one new commit, so main reaches neither |
| 692 | # under-a's commits nor under-p. The planner reads under-p as no layer: |
| 693 | # under-b's family is under-b alone, and it is restacked. |
| 694 | git checkout --quiet -B under-p "$(at "$served" refs/heads/main)" |
| 695 | echo "a plain branch below a landed change" > under-p.txt |
| 696 | git add under-p.txt |
| 697 | git commit --quiet -m 'a plain branch below a landed change' |
| 698 | as alice git push --quiet origin under-p |
| 699 | git checkout --quiet -B under-a |
| 700 | echo "a line the landed change adds" >> src/main.txt |
| 701 | git commit --quiet -am 'a change the integrator lands on the plain branch' |
| 702 | under_a="$(attested under-a --check code-build)" |
| 703 | git checkout --quiet -B under-b |
| 704 | echo "a change above the landed one" > under-b.txt |
| 705 | git add under-b.txt |
| 706 | git commit --quiet -m 'a change above the landed one' |
| 707 | as alice git push --quiet origin under-b |
| 708 | ok as alice valley ready |
| 709 | under_b="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 710 | git push --quiet "$(url requester "$served")" "under-a:refs/the-valley/integration-requests/main/c-$under_a" |
| 711 | integrate |
| 712 | judged "$under_a" land |
| 713 | git -C "$served" merge-base --is-ancestor "$(git rev-parse under-p)" main \ |
| 714 | && fail 'main reaches under-p; the fixture proves nothing' |
| 715 | served_plan --known-signers "$signers" --integrator-name "$integrator_name" |
| 716 | [ "$(family "$under_b" | jq -c '[.layers[].id]')" = "[\"$under_b\"]" ] \ |
| 717 | || fail "under-b's family is not under-b alone: $(family "$under_b")" |
| 718 | action "$under_b" restack |
| 719 | jq -e '[.families[].layers[] | select(.name == "under-p")] | length == 0' "$w/plan.json" > /dev/null \ |
| 720 | || fail "the plain branch below the landed change was planned: $(jq -c .families "$w/plan.json")" |
| 721 | |
| 722 | touch "$out" |