| 1 | # shellcheck shell=bash |
| 2 | # valley changes and valley pending, end to end and unmocked, over the |
| 3 | # repositories one test host serves. The host is made by the integrator |
| 4 | # host's rendered valley-init, relocated as init-e2e relocates it, so the |
| 5 | # project list discovery reads is the one a host publishes, and every |
| 6 | # record is pushed through the hook that host installs. What the |
| 7 | # integrator writes on disk when it lands a change — main, the outcome |
| 8 | # record, the transfer statement — is written here the same way, with a |
| 9 | # key generated for the run. |
| 10 | # Paths and fixtures come from the derivation environment. |
| 11 | # shellcheck disable=SC2154 |
| 12 | set -euo pipefail |
| 13 | |
| 14 | w="$TMPDIR/valley-changes" |
| 15 | mkdir -p "$w" |
| 16 | export HOME="$w" |
| 17 | export XDG_CACHE_HOME="$w/cache" |
| 18 | export GIT_CONFIG_NOSYSTEM=1 |
| 19 | git config --global user.name valley-changes-check |
| 20 | git config --global user.email valley-changes-check@localhost |
| 21 | git config --global init.defaultBranch main |
| 22 | git config --global advice.detachedHead false |
| 23 | unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER |
| 24 | |
| 25 | fail() { |
| 26 | printf 'valley-changes: %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 | # ok <command…>: the command succeeds, and its output is kept. |
| 34 | ok() { |
| 35 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 36 | } |
| 37 | |
| 38 | # holds <jq condition…>: the last JSON printed holds each. |
| 39 | holds() { |
| 40 | local cond |
| 41 | for cond in "$@"; do |
| 42 | jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold" |
| 43 | done |
| 44 | } |
| 45 | |
| 46 | # line <ERE>: the last output has a line matching it. |
| 47 | line() { |
| 48 | grep -qE -- "$1" "$w/out" || fail "no line matches $1" |
| 49 | } |
| 50 | |
| 51 | no_line() { |
| 52 | if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi |
| 53 | } |
| 54 | |
| 55 | # The host: guarded, open, released and sealed, with sealed the instance |
| 56 | # repository. valley-init creates them, wires their hooks, and publishes |
| 57 | # the project list in sealed. |
| 58 | data="$w/srv/git" |
| 59 | mkdir -p "$data" |
| 60 | sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh" |
| 61 | bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")" |
| 62 | instance="$data/sealed.git" |
| 63 | # The integrator's repositories are group-shared, and git then sets the |
| 64 | # setgid bit on every directory it makes, which this sandbox refuses. The |
| 65 | # setting is dropped so the pushes below can write objects; it decides |
| 66 | # nothing this check reads. |
| 67 | for name in guarded open released sealed; do |
| 68 | git -C "$data/$name.git" config --unset core.sharedRepository || true |
| 69 | done |
| 70 | [ "$(git -C "$instance" cat-file blob refs/the-valley/projects)" = "$(printf '%s\n' guarded open released sealed)" ] || |
| 71 | fail "valley-init published another project list" |
| 72 | |
| 73 | # A change's ID, by the letter it repeats. Each is 32 letters from k to z. |
| 74 | id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; } |
| 75 | A="$(id nxvpwlkm)" B="$(id mmmm)" C="$(id pppp)" D="$(id qqqq)" E="$(id rrrr)" |
| 76 | W="$(id ssss)" F="$(id tttt)" G="$(id uuuu)" U="$(id vvvv)" H="$(id wwww)" |
| 77 | R="$(id nxvpwlkz)" D2="$(id xxxx)" E2="$(id yyyy)" |
| 78 | |
| 79 | # A checkout of a project, whose main names sealed as its owning valley. |
| 80 | # Every push carries the principal, as the key sshd authenticated would. |
| 81 | export VALLEY_PRINCIPAL=klaus-env |
| 82 | checkout() { |
| 83 | local name="$1" dir="$w/$1" |
| 84 | git clone --quiet "$data/$name.git" "$dir" 2> /dev/null |
| 85 | git -C "$dir" config valley.principal klaus-env |
| 86 | mkdir -p "$dir/policy" |
| 87 | printf '# the owning valley\n%s\n' "$instance" > "$dir/policy/valley" |
| 88 | git -C "$dir" add policy/valley |
| 89 | git -C "$dir" commit --quiet -m base |
| 90 | VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main |
| 91 | } |
| 92 | |
| 93 | # branch <name> <from> <subject…>: a branch from a commit, with a commit |
| 94 | # per subject, each adding a file of its own, pushed to origin. |
| 95 | branch() { |
| 96 | local name="$1" from="$2" s |
| 97 | shift 2 |
| 98 | git checkout --quiet -b "$name" "$from" |
| 99 | for s in "$@"; do |
| 100 | echo "$s" > "$s.txt" |
| 101 | git add "$s.txt" |
| 102 | git commit --quiet -m "$s" |
| 103 | done |
| 104 | git push --quiet origin "$name" |
| 105 | } |
| 106 | |
| 107 | # ready <id> <branch>: the branch's head readied as the change. |
| 108 | ready() { |
| 109 | ok valley ready --change "$1" "$2" |
| 110 | } |
| 111 | |
| 112 | # ---------------------------------------------------------------------- |
| 113 | # open: a stack of three, a draft, a withdrawn change with a ready change |
| 114 | # on top of it, a change landed by fast-forward, one landed as a new |
| 115 | # commit with its transfer statement, one whose request was consumed with |
| 116 | # no evidence stored, one whose transfer statement another key signed, |
| 117 | # and a branch no record is on. |
| 118 | checkout open |
| 119 | cd "$w/open" |
| 120 | base="$(git rev-parse HEAD)" |
| 121 | branch stack-a "$base" stack-a |
| 122 | ready "$A" stack-a |
| 123 | branch stack-b stack-a stack-b |
| 124 | ready "$B" stack-b |
| 125 | branch stack-c stack-b stack-c |
| 126 | ready "$C" stack-c |
| 127 | branch paused "$base" paused |
| 128 | ready "$D" paused |
| 129 | ok valley unready "$D" |
| 130 | branch gone "$base" gone |
| 131 | ready "$E" gone |
| 132 | ok valley withdraw "$E" |
| 133 | branch atop-gone gone atop-gone |
| 134 | ready "$W" atop-gone |
| 135 | branch ff "$base" ff |
| 136 | ready "$F" ff |
| 137 | branch reparent "$base" reparent |
| 138 | ready "$G" reparent |
| 139 | branch lost "$base" lost |
| 140 | ready "$U" lost |
| 141 | branch forged "$base" forged |
| 142 | ready "$H" forged |
| 143 | branch loose "$base" loose |
| 144 | |
| 145 | # Before anything lands, ff names its change. |
| 146 | ok valley-go resolve ff |
| 147 | line "^change"$'\t'"$F"$'\t' |
| 148 | |
| 149 | # The integrator's keys: the integrator's own, and another a known |
| 150 | # signer holds, which may sign evidence and not a landing. |
| 151 | ssh-keygen -q -t ed25519 -N '' -C integrator -f "$w/integrator.key" |
| 152 | ssh-keygen -q -t ed25519 -N '' -C mallory -f "$w/mallory.key" |
| 153 | { |
| 154 | attest key --key "$w/integrator.key" --name integrator |
| 155 | attest key --key "$w/mallory.key" --name mallory |
| 156 | } > "$w/known-signers" |
| 157 | |
| 158 | # F lands by fast-forward. |
| 159 | VALLEY_PRINCIPAL=integrator git push --quiet origin ff:main |
| 160 | fhead="$(git rev-parse ff)" |
| 161 | |
| 162 | # G lands as a new commit on main, and the integrator records it: main |
| 163 | # moves, the outcome record says land, the request is consumed, and the |
| 164 | # transfer statement is stored under the landed tree. |
| 165 | git checkout --quiet -b landing ff |
| 166 | git cherry-pick reparent > /dev/null |
| 167 | landed="$(git rev-parse HEAD)" |
| 168 | VALLEY_PRINCIPAL=integrator git push --quiet origin landing:main |
| 169 | tree_digest() { attest digest --repo "$w/open" --rev "$1" | cut -d: -f2; } |
| 170 | |
| 171 | # memo <name> <head> <decision…>: the integrator's outcome record. |
| 172 | memo() { |
| 173 | local name="$1" head="$2" blob |
| 174 | shift 2 |
| 175 | blob="$(printf '%s %s %s %s\n' "$head" "$fhead" 0123456789abcdef "$*" | git -C "$data/open.git" hash-object -w --stdin)" |
| 176 | git -C "$data/open.git" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob" |
| 177 | } |
| 178 | |
| 179 | # transfer <change> <head> <landed> <key> <name>: a transfer statement, |
| 180 | # as the integrator composes, signs and stores one. |
| 181 | transfer() { |
| 182 | local change="$1" head="$2" at="$3" key="$4" name="$5" ldigest blob tree |
| 183 | ldigest="$(tree_digest "$at")" |
| 184 | jq -n --arg change "$change" --arg base "$(tree_digest "$head")" --arg landed "$ldigest" \ |
| 185 | --arg commit "$at" --arg tip "$fhead" '{ |
| 186 | statementType: "the-valley/attestation/v1", |
| 187 | subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $landed, "git-sha1": $commit}}, |
| 188 | predicateType: "the-valley/integration/transfer/v1", |
| 189 | predicate: {change: $change, target: "refs/heads/main", base: {"valley-tree-v1": $base}, |
| 190 | policy: {sha256: ("ab" * 32), "git-sha1": $tip}, required: "none"} |
| 191 | }' > "$w/statement.json" |
| 192 | attest render "$w/statement.json" | attest sign --key "$key" --name "$name" > "$w/transfer.note" |
| 193 | blob="$(git -C "$data/open.git" hash-object -w "$w/transfer.note")" |
| 194 | tree="$(printf '100644 blob %s\ttransfer.note\n' "$blob" | git -C "$data/open.git" mktree)" |
| 195 | git -C "$data/open.git" update-ref "refs/the-valley/attestations/$ldigest/$(printf '%08x' "${#name}")" "$tree" |
| 196 | } |
| 197 | memo reparent "$(git rev-parse reparent)" land |
| 198 | transfer reparent "$(git rev-parse reparent)" "$landed" "$w/integrator.key" integrator |
| 199 | |
| 200 | # U's request was consumed, and no evidence was stored. |
| 201 | memo lost "$(git rev-parse lost)" land |
| 202 | # H's request was consumed too, and its transfer statement is signed by a |
| 203 | # known key that is not the integrator's: it is no record of a landing. |
| 204 | memo forged "$(git rev-parse forged)" land |
| 205 | git checkout --quiet -b landing-h landing |
| 206 | git cherry-pick forged > /dev/null |
| 207 | VALLEY_PRINCIPAL=integrator git push --quiet origin landing-h:main |
| 208 | transfer forged "$(git rev-parse forged)" "$(git rev-parse HEAD)" "$w/mallory.key" mallory |
| 209 | # A's request is pending. |
| 210 | git -C "$data/open.git" update-ref refs/the-valley/integration-requests/main/stack-a "$(git rev-parse stack-a)" |
| 211 | |
| 212 | # ---------------------------------------------------------------------- |
| 213 | # guarded: a ready change whose ID shares seven letters with A's, a draft |
| 214 | # and a withdrawn change. |
| 215 | checkout guarded |
| 216 | cd "$w/guarded" |
| 217 | gbase="$(git rev-parse HEAD)" |
| 218 | branch g-ready "$gbase" g-ready |
| 219 | ready "$R" g-ready |
| 220 | branch g-draft "$gbase" g-draft |
| 221 | ready "$D2" g-draft |
| 222 | ok valley unready "$D2" |
| 223 | branch g-gone "$gbase" g-gone |
| 224 | ready "$E2" g-gone |
| 225 | ok valley withdraw "$E2" |
| 226 | |
| 227 | # released is listed and cannot be read. |
| 228 | mv "$data/released.git" "$data/released.moved" |
| 229 | |
| 230 | export VALLEY_KNOWN_SIGNERS="$w/known-signers" VALLEY_INTEGRATOR_SIGNER=integrator |
| 231 | |
| 232 | # ---------------------------------------------------------------------- |
| 233 | # One repository: every record in every state, as valley-changes/v1. |
| 234 | cd "$w/open" |
| 235 | ok valley changes --json |
| 236 | [ "$(wc -l < "$w/out")" = 1 ] || fail "changes printed more than one line" |
| 237 | holds '.schema == "valley-changes/v1"' '.host == "localhost"' '(.repos | length) == 1' \ |
| 238 | '.repos[0].repo == "open"' ".repos[0].url == \"$data/open.git\"" '.repos[0].error == null' \ |
| 239 | '(.repos[0].changes | length) == 10' |
| 240 | c() { printf '.repos[0].changes[] | select(.change_id == "%s")' "$1"; } |
| 241 | holds "$(c "$A") | .handle == \"nxvp\" and .state == \"ready\" and .owner == \"klaus-env\" and .target == \"main\" and .active == 1 and .seq == 1" \ |
| 242 | "$(c "$A") | .record == \"$(git -C "$data/open.git" rev-parse "refs/the-valley/changes/main/$A")\"" \ |
| 243 | "$(c "$A") | .versions[0] | .n == 1 and .title == \"stack-a\" and .branch == \"stack-a\" and .uploader == \"klaus-env\"" \ |
| 244 | "$(c "$A") | .versions[0] | .head == \"$(git rev-parse stack-a)\" and .landing == \"not-landed\" and .landed_commit == null" \ |
| 245 | "$(c "$A") | .versions[0].request == {ref: \"refs/the-valley/integration-requests/main/stack-a\", commit: \"$(git rev-parse stack-a)\"}" \ |
| 246 | "$(c "$A") | .versions[0].outcome == null" \ |
| 247 | "$(c "$D") | .state == \"draft\" and .seq == 2" \ |
| 248 | "$(c "$E") | .state == \"withdrawn\"" |
| 249 | # Landing evidence, one version each way. |
| 250 | holds "$(c "$F") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$fhead\"" \ |
| 251 | "$(c "$G") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed\"" \ |
| 252 | "$(c "$G") | .versions[0].outcome == {decision: \"land\", head: \"$(git rev-parse reparent)\", tip: \"$fhead\"}" \ |
| 253 | "$(c "$G") | .versions[0].request == null" \ |
| 254 | "$(c "$U") | .versions[0] | .landing == \"uncertain\" and .landed_commit == null" \ |
| 255 | "$(c "$H") | .versions[0].landing == \"uncertain\"" |
| 256 | # includes is ancestry, nearest only. |
| 257 | holds "$(c "$A") | .includes == []" "$(c "$B") | .includes == [\"$A\"]" \ |
| 258 | "$(c "$C") | .includes == [\"$B\"]" "$(c "$W") | .includes == [\"$E\"]" |
| 259 | # The one branch no record is on, and no branch main reaches. |
| 260 | holds '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]' |
| 261 | |
| 262 | # Without the registry's keys, the transfer statement is not believed, and |
| 263 | # that is said. |
| 264 | env -u VALLEY_KNOWN_SIGNERS valley changes --json > "$w/out" 2> "$w/err" || fail "changes failed without the keys" |
| 265 | holds "$(c "$G") | .versions[0].landing == \"uncertain\"" |
| 266 | grep -q 'could not be verified' "$w/err" || fail "an unverified transfer statement was not reported" |
| 267 | |
| 268 | # ---------------------------------------------------------------------- |
| 269 | # Every project the host publishes. Handles are unique across both |
| 270 | # repositories, the repository that cannot be read carries its error, and |
| 271 | # the others are listed all the same. |
| 272 | ok valley changes --json --all |
| 273 | holds '[.repos[].repo] == ["guarded", "open", "released", "sealed"]' \ |
| 274 | '.repos[] | select(.repo == "released") | .error != null and .changes == [] and .url == "'"$data/released.git"'"' \ |
| 275 | '.repos[] | select(.repo != "released") | .error == null' \ |
| 276 | '.repos[] | select(.repo == "sealed") | .changes == []' \ |
| 277 | '[.repos[].changes[].handle] | length == (unique | length)' \ |
| 278 | "[.repos[].changes[] | select(.change_id == \"$A\" or .change_id == \"$R\") | .handle] == [\"nxvpwlkz\", \"nxvpwlkm\"]" \ |
| 279 | '.repos[] | select(.repo == "guarded") | [.changes[].state] == ["ready", "draft", "withdrawn"]' |
| 280 | # --repo limits the listing to one repository, read through its clone. |
| 281 | ok valley changes --json --repo guarded |
| 282 | holds '[.repos[].repo] == ["guarded"]' "[.repos[0].changes[].change_id] == [\"$R\", \"$D2\", \"$E2\"]" \ |
| 283 | '.repos[0].changes[0].handle == "nxvp"' |
| 284 | cd "$w/guarded" |
| 285 | ok valley changes --json |
| 286 | holds '[.repos[].repo] == ["guarded"]' '.repos[0].changes[0].handle == "nxvp"' |
| 287 | if valley changes --json --repo nowhere > "$w/out" 2> "$w/err"; then fail "--repo took a project the host does not list"; fi |
| 288 | grep -q 'lists no project nowhere' "$w/err" || fail "--repo nowhere was refused without saying why" |
| 289 | cd "$w/open" |
| 290 | |
| 291 | # A repository whose main names no owning valley lists itself alone, and |
| 292 | # says discovery is unavailable. |
| 293 | git init --quiet --bare "$w/solo.git" |
| 294 | git clone --quiet "$w/solo.git" "$w/solo" 2> /dev/null |
| 295 | git -C "$w/solo" commit --quiet --allow-empty -m solo |
| 296 | git -C "$w/solo" push --quiet origin main |
| 297 | (cd "$w/solo" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed without discovery" |
| 298 | holds '[.repos[].repo] == ["solo"]' |
| 299 | grep -q 'discovery is unavailable' "$w/err" || fail "changes --all did not say discovery is unavailable" |
| 300 | |
| 301 | # ---------------------------------------------------------------------- |
| 302 | # pending: the ready changes that have not landed, by handle, version and |
| 303 | # title, then the branches no record is on, then the drafts. |
| 304 | ok valley pending |
| 305 | line '^nxvp v1 \| stack-a$' |
| 306 | line '^mmmm v1 \| stack-b \| includes nxvp$' |
| 307 | line '^pppp v1 \| stack-c \| includes mmmm$' |
| 308 | line '^ssss v1 \| atop-gone \| includes rrrr$' |
| 309 | line '^vvvv v1 \| lost \| uncertain$' |
| 310 | line '^wwww v1 \| forged \| uncertain$' |
| 311 | no_line '^(tttt|uuuu|rrrr|qqqq) v1 \|' |
| 312 | line '^branches$' |
| 313 | line '^loose \| loose \| .* \| ahead 1 \| 1 file changed' |
| 314 | line '^drafts$' |
| 315 | line '^qqqq v1 on paused \| paused \| .* \| ahead 1 \| ' |
| 316 | [ "$(grep -n '^branches$' "$w/out" | cut -d: -f1)" -lt "$(grep -n '^drafts$' "$w/out" | cut -d: -f1)" ] || |
| 317 | fail "pending listed the drafts before the branches" |
| 318 | # --json is the same listing in valley-changes/v1's shape. |
| 319 | ok valley pending --json |
| 320 | holds '.schema == "valley-pending/v1"' \ |
| 321 | '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "pppp", "qqqq", "ssss", "vvvv", "wwww"]' \ |
| 322 | '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]' |
| 323 | # Across the host, each repository is headed by its name. |
| 324 | ok valley pending --all |
| 325 | line '^guarded$' |
| 326 | line '^nxvpwlkz v1 \| g-ready$' |
| 327 | line '^guarded drafts$' |
| 328 | line '^open$' |
| 329 | line '^nxvpwlkm v1 \| stack-a$' |
| 330 | line '^released: could not read ' |
| 331 | |
| 332 | # --owes ends each change's line with the checks it owes, by cost, as |
| 333 | # valley checks derives them, here and in a repository read through its |
| 334 | # clone. The instance gets a floor for that, written to its main on disk |
| 335 | # as the integrator would. |
| 336 | git init --quiet "$w/floor" |
| 337 | mkdir -p "$w/floor/policy/instance" |
| 338 | cp "$floor"/*.cue "$w/floor/policy/instance/" |
| 339 | git -C "$w/floor" add policy |
| 340 | git -C "$w/floor" commit --quiet -m floor |
| 341 | git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor |
| 342 | git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)" |
| 343 | ok valley pending --owes --all |
| 344 | line '^nxvpwlkm v1 \| stack-a \| owes 1 quick, 0 slow$' |
| 345 | line '^mmmm v1 \| stack-b \| includes nxvpwlkm \| owes 1 quick, 0 slow$' |
| 346 | line '^nxvpwlkz v1 \| g-ready \| owes 1 quick, 0 slow$' |
| 347 | |
| 348 | # ---------------------------------------------------------------------- |
| 349 | # A branch name is used again once its change has landed: ff now names no |
| 350 | # change, and reparent none either, once its transfer statement verifies. |
| 351 | ok valley-go resolve ff |
| 352 | line "^branch"$'\t'"ff$" |
| 353 | ok valley-go resolve reparent |
| 354 | line "^branch"$'\t'"reparent$" |
| 355 | env -u VALLEY_KNOWN_SIGNERS valley-go resolve reparent > "$w/out" 2> "$w/err" || fail "resolve failed" |
| 356 | line "^change"$'\t'"$G"$'\t' |
| 357 | ok valley-go resolve stack-a |
| 358 | line "^change"$'\t'"$A"$'\t' |
| 359 | |
| 360 | # valley help names the verbs klaus looks for. |
| 361 | valley help > "$w/out" |
| 362 | line '^ ready ' |
| 363 | line '^ changes ' |
| 364 | |
| 365 | touch "$out" |