gunk-dev/the-valley

read_test.go

sourcea7ab0ac9051amain's published tip history
23.4 KiB
1package main
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "reflect"
12 "strconv"
13 "strings"
14 "testing"
15 "time"
16
17 "the-valley/webui/export"
18)
19
20// fails runs one pass through run, the program's entry point, and fails the
21// test unless the pass exits 1 having sent and written nothing: to rv when
22// it is given, and otherwise to a fresh --out directory. It returns what
23// the pass said on stderr.
24func (v *testValley) fails(rv *testReceiver, more ...string) string {
25 v.t.Helper()
26 out := filepath.Join(v.t.TempDir(), "export")
27 to := []string{"--out", out}
28 if rv != nil {
29 rv.taken()
30 to = []string{"--receiver", rv.server.URL}
31 }
32 var stdout, stderr bytes.Buffer
33 if code := run(context.Background(), v.args(append(to, more...)...), &stdout, &stderr); code != 1 {
34 v.t.Fatalf("the pass exited %d, want 1\nstdout:\n%s\nstderr:\n%s", code, stdout.String(), stderr.String())
35 }
36 if rv != nil {
37 sameRequests(v.t, "a failed pass", rv.taken())
38 } else if _, err := os.Stat(out); !errors.Is(err, os.ErrNotExist) {
39 v.t.Errorf("a failed pass wrote %s", out)
40 }
41 return stderr.String()
42}
43
44func mustSay(t *testing.T, what, said, want string) {
45 t.Helper()
46 if !strings.Contains(said, want) {
47 t.Errorf("%s: the pass did not say %q:\n%s", what, want, said)
48 }
49}
50
51// elsewhere makes a second instance beside the first, with projects of the
52// same names: valley, which allows app and site; app, which declares
53// itself public and carries a sentinel of its own; and site, with no main.
54// It returns the instance repository.
55func (v *testValley) elsewhere(sentinel string) string {
56 v.t.Helper()
57 srv := filepath.Join(v.root, "elsewhere")
58 for _, name := range []string{"valley", "app", "site"} {
59 v.run("", "git", "init", "--quiet", "--bare", filepath.Join(srv, name+".git"))
60 }
61 for _, name := range []string{"valley", "app"} {
62 work := filepath.Join(v.root, "elsewhere-work", name)
63 v.run("", "git", "init", "--quiet", work)
64 v.run(work, "git", "remote", "add", "origin", filepath.Join(srv, name+".git"))
65 v.work["elsewhere/"+name] = work
66 }
67 v.write(filepath.Join(v.work["elsewhere/valley"], "policy", "instance", "floor.cue"), "package verification\n")
68 v.declare("elsewhere/valley", `instance: projects: {
69 app: visibility: "public"
70 site: visibility: "public"
71}`)
72 v.write(filepath.Join(v.work["elsewhere/app"], "policy", "valley"), filepath.Join(srv, "valley.git")+"\n")
73 v.write(filepath.Join(v.work["elsewhere/app"], "source.txt"), sentinel+"\n")
74 v.declare("elsewhere/app", `project: visibility: "public"`)
75 list := v.runInput("", "app\nsite\nvalley\n", "git", "-C", filepath.Join(srv, "valley.git"), "hash-object", "-w", "--stdin")
76 v.run("", "git", "-C", filepath.Join(srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list))
77 return filepath.Join(srv, "valley.git")
78}
79
80// Two instances publish a project called app. When the instance repository
81// the publisher is given points at the other with policy/valley, the CLI
82// would read the other's app, and the pass refuses before it reads any
83// project.
84func TestAnotherInstanceIsNeverRead(t *testing.T) {
85 v := newValley(t)
86 other := v.elsewhere("elsewhere-app-source-c3d1")
87 v.write(filepath.Join(v.work["valley"], "policy", "valley"), other+"\n")
88 v.commit("valley", "valley: point at another instance")
89
90 mustSay(t, "--out", v.fails(nil), "holds policy/valley on main")
91 rv := newReceiver(t, v)
92 mustSay(t, "--receiver", v.fails(rv), "holds policy/valley on main")
93 if _, err := os.Stat(filepath.Join(v.root, "cache", "valley-web-publish", "repos", "app.git")); !errors.Is(err, os.ErrNotExist) {
94 t.Errorf("the pass fetched a project before it refused the instance repository")
95 }
96
97 // The CLI reads policy/valley up to a # and without the spaces around
98 // it, so an instance repository whose URL holds them is refused before
99 // the pass runs: policy/valley would name another.
100 instance := filepath.Join(v.srv, "valley.git")
101 for _, url := range []string{instance + "#other", " " + instance, instance + "\n" + other} {
102 var stdout, stderr bytes.Buffer
103 if code := run(context.Background(), v.args("--out", t.TempDir(), "--instance-repo", url), &stdout, &stderr); code != 2 {
104 t.Errorf("--instance-repo %q: the pass exited %d, want 2", url, code)
105 }
106 }
107}
108
109// The pass reads the instance repository at the main it began with, and
110// the CLI never reads that main again. So a policy/valley that lands on
111// main while the CLI reads changes nothing in the read: not when main is
112// put back before the read ends, which no comparison of main before and
113// after the read could see, and not when it stays, which the next pass
114// refuses. Read through the pointer, the export would hold the other
115// instance's app.
116func TestAPointerLandingDuringAReadIsNeverRead(t *testing.T) {
117 const otherSource = "elsewhere-app-source-c3d1"
118 for _, restored := range []bool{true, false} {
119 t.Run(fmt.Sprintf("restored %v", restored), func(t *testing.T) {
120 v := newValley(t)
121 other := v.elsewhere(otherSource)
122 instance := filepath.Join(v.srv, "valley.git")
123 began := strings.TrimSpace(v.run("", "git", "-C", instance, "rev-parse", "main"))
124 work := v.work["valley"]
125 v.run(work, "git", "checkout", "--quiet", "-b", "pointer")
126 v.write(filepath.Join(work, "policy", "valley"), other+"\n")
127 v.run(work, "git", "add", "--all")
128 v.run(work, "git", "commit", "--quiet", "-m", "valley: point at another instance")
129 pointer := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD"))
130 v.run(work, "git", "push", "--quiet", "origin", "pointer")
131
132 // The CLI's first command moves main to the pointer before it
133 // runs. When restored, main is put back once the second, the
134 // pending listing, is done: the CLI has then read the pointer
135 // twice, if it reads main at all, and main is where the pass
136 // began.
137 wrapper := filepath.Join(v.root, "bin", "valley")
138 data, err := os.ReadFile(wrapper)
139 if err != nil {
140 t.Fatal(err)
141 }
142 shebang, rest, _ := strings.Cut(string(data), "\n")
143 rest = strings.Replace(rest, "exec ", "", 1)
144 count := filepath.Join(v.root, "count")
145 restore := ":"
146 if restored {
147 restore = fmt.Sprintf("git -C %s update-ref refs/heads/main %s", instance, began)
148 }
149 v.write(wrapper, fmt.Sprintf(`%s
150n=$(( $(cat %[2]s 2>/dev/null || echo 0) + 1 )); echo "$n" > %[2]s
151if [ "$n" = 1 ]; then git -C %[3]s update-ref refs/heads/main %[4]s; fi
152rc=0
153%[5]s || rc=$?
154if [ "$n" = 2 ]; then %[6]s; fi
155exit "$rc"
156`, shebang, count, instance, pointer, strings.TrimSpace(rest), restore))
157
158 dir, _, e := v.out()
159 if calls, _ := strconv.Atoi(strings.TrimSpace(v.run("", "cat", count))); calls < 2 {
160 t.Fatalf("the CLI ran %d commands, so the pointer was never in place while it read", calls)
161 }
162 if got := names(e); !reflect.DeepEqual(got, []string{"app"}) {
163 t.Fatalf("the export holds %v, want app alone", got)
164 }
165 app := strings.TrimSpace(v.run("", "git", "-C", filepath.Join(v.srv, "app.git"), "rev-parse", "main"))
166 if e.Projects[0].SourceTip != app {
167 t.Errorf("the export's app is at %s, and this instance's app is at %s", e.Projects[0].SourceTip, app)
168 }
169 if strings.Contains(v.unbundled(filepath.Join(dir, "bundles", "app.bundle")), otherSource) {
170 t.Errorf("the bundle of app holds the other instance's source")
171 }
172 if len(knotOf(t, e.Projects[0], v.knots["bug"]).Comments) != 1 {
173 t.Errorf("the export's app does not hold this instance's plan")
174 }
175
176 if restored {
177 return
178 }
179 v.write(wrapper, shebang+"\n"+rest)
180 mustSay(t, "the next pass", v.fails(nil), "holds policy/valley on main")
181 })
182 }
183}
184
185// A project the instance allows that cannot be read fails the pass, which
186// sends and writes nothing: published without it, the export would take
187// it off the web. A project with no main yet is private, and the pass
188// goes on.
189func TestAProjectThatCannotBeReadFailsThePass(t *testing.T) {
190 cases := []struct {
191 name string
192 upload bool
193 breaks func(v *testValley) []string
194 says string
195 }{
196 {
197 name: "the pending listing cannot read site",
198 upload: true,
199 breaks: func(v *testValley) []string {
200 if err := os.Rename(filepath.Join(v.srv, "site.git"), filepath.Join(v.srv, "site.moved")); err != nil {
201 v.t.Fatal(err)
202 }
203 return nil
204 },
205 says: "site: the pending listing could not read it",
206 },
207 {
208 name: "site's main cannot be fetched into its mirror",
209 breaks: func(v *testValley) []string {
210 mirror := filepath.Join(v.root, "cache", "valley-web-publish", "repos", "site.git")
211 v.run("", "git", "init", "--quiet", "--bare", mirror)
212 v.run("", "git", "-C", mirror, "config", "core.repositoryformatversion", "99")
213 return nil
214 },
215 says: "site: could not read main",
216 },
217 {
218 name: "discovery fails, and the CLI lists its own repository alone",
219 breaks: func(v *testValley) []string {
220 list := v.runInput("", "app\nsite\nvalley\nnot a name\n", "git", "-C", filepath.Join(v.srv, "valley.git"), "hash-object", "-w", "--stdin")
221 v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list))
222 return nil
223 },
224 says: "valley plan --all lists _pointer, and the instance repository publishes app, not a name, site, valley",
225 },
226 {
227 name: "the instance publishes no project list",
228 breaks: func(v *testValley) []string {
229 v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", "-d", projectsRef)
230 return nil
231 },
232 says: "could not read main and refs/the-valley/projects in the instance repository",
233 },
234 {
235 name: "the schema cannot be read",
236 breaks: func(v *testValley) []string { return []string{"--schema", filepath.Join(v.root, "no-such.cue")} },
237 says: "does not load",
238 },
239 }
240 for _, c := range cases {
241 t.Run(c.name, func(t *testing.T) {
242 v := newValley(t)
243 more := c.breaks(v)
244 var rv *testReceiver
245 if c.upload {
246 rv = newReceiver(t, v)
247 }
248 mustSay(t, c.name, v.fails(rv, more...), c.says)
249 })
250 }
251
252 t.Run("a project with no main is private", func(t *testing.T) {
253 v := newValley(t)
254 v.run("", "git", "init", "--quiet", "--bare", filepath.Join(v.srv, "fresh.git"))
255 list := v.runInput("", "app\nfresh\nsite\nvalley\n", "git", "-C", filepath.Join(v.srv, "valley.git"), "hash-object", "-w", "--stdin")
256 v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list))
257 v.declare("valley", `instance: projects: {
258 app: visibility: "public"
259 fresh: visibility: "public"
260 site: visibility: "public"
261}`)
262 stdout, _ := v.publish("--out", filepath.Join(t.TempDir(), "export"))
263 mustSay(t, "a project with no main", stdout, "public: app; private: fresh, site, valley")
264 })
265}
266
267// Only a cue run that ends by refusing a declaration makes it invalid, and
268// so private. A cue that is killed, that crashes or that runs past its
269// timeout has read nothing, and the pass fails, sending and writing
270// nothing: taken as invalid, the instance's declaration would make every
271// project private and take them all off the web.
272func TestACueThatDoesNotFinishFailsThePass(t *testing.T) {
273 cases := []struct {
274 name, does, says string
275 timeout time.Duration
276 }{
277 {name: "killed", does: "kill -KILL $$", says: "cue did not finish reading the declaration: signal: killed"},
278 {name: "crashed", does: "echo 'panic: runtime error' >&2; exit 2",
279 says: "cue did not finish reading the declaration: exit status 2: panic: runtime error"},
280 {name: "past its timeout", does: "exec sleep 60", says: "cue took longer than 3s", timeout: 3 * time.Second},
281 }
282 for _, c := range cases {
283 t.Run(c.name, func(t *testing.T) {
284 v := newValley(t)
285 bash, cue := needTool(t, "bash"), needTool(t, "cue")
286 // The schema is checked with cue over it alone; a declaration is
287 // read with cue over the schema and the declaration's files.
288 wrapper := filepath.Join(v.root, "bin", "cue")
289 v.write(wrapper, fmt.Sprintf("#!%s\nif [ \"$#\" -gt 4 ]; then %s; fi\nexec %s \"$@\"\n", bash, c.does, cue))
290 if err := os.Chmod(wrapper, 0o755); err != nil {
291 t.Fatal(err)
292 }
293 if c.timeout != 0 {
294 was := cueTimeout
295 cueTimeout = c.timeout
296 t.Cleanup(func() { cueTimeout = was })
297 }
298 said := v.fails(nil)
299 mustSay(t, c.name, said, "could not read the instance's policy/web")
300 mustSay(t, c.name, said, c.says)
301 })
302 }
303}
304
305// The bundles of an export are held to --max-bundles together, and the
306// state and the cache to --max-storage, before anything is sent. What a
307// program prints is held to a bound: git's output past it fails the call,
308// and a declaration whose export is past cue's bound is invalid.
309func TestBoundsStopThePass(t *testing.T) {
310 v := newValley(t)
311 rv := newReceiver(t, v)
312 mustSay(t, "--max-bundles", v.fails(rv, "--max-bundles", "100"), "more than --max-bundles 100")
313 mustSay(t, "--max-storage", v.fails(nil, "--max-storage", "1K"), "more than --max-storage 1024")
314 for _, size := range []string{"0", "-1", "1X", "K", "9000000000T"} {
315 var stdout, stderr bytes.Buffer
316 if code := run(context.Background(), v.args("--out", t.TempDir(), "--max-bundles", size), &stdout, &stderr); code != 2 {
317 t.Errorf("--max-bundles %s: the pass exited %d, want 2", size, code)
318 }
319 }
320
321 big := v.runInput("", strings.Repeat("x", maxGitOutput+1), "git", "-C", filepath.Join(v.srv, "app.git"), "hash-object", "-w", "--stdin")
322 if _, err := git(context.Background(), filepath.Join(v.srv, "app.git"), "cat-file", "blob", strings.TrimSpace(big)); !errors.Is(err, errTooLong) {
323 t.Errorf("git printing more than %d bytes returned %v, want errTooLong", maxGitOutput, err)
324 }
325
326 v.declare("valley", `import "list"
327
328instance: projects: {for i in list.Range(0, 9000, 1) {"project-with-a-long-name-for-the-bound-on-cue-output-\(i)": visibility: "public"}}`)
329 stdout, stderr := v.publish("--out", filepath.Join(t.TempDir(), "export"))
330 mustSay(t, "a declaration past cue's bound", stdout, "public: none")
331 mustSay(t, "a declaration past cue's bound", stderr, "cue exported more than")
332}
333
334// objects is every object a repository holds, decompressed, as git
335// cat-file --batch-all-objects prints them.
336func (v *testValley) objects(repo string) string {
337 v.t.Helper()
338 return v.run("", "git", "-C", repo, "cat-file", "--batch-all-objects", "--batch")
339}
340
341// unbundled is every object a bundle carries, decompressed.
342func (v *testValley) unbundled(bundle string) string {
343 v.t.Helper()
344 repo := filepath.Join(v.t.TempDir(), "unbundled.git")
345 v.run("", "git", "init", "--quiet", "--bare", repo)
346 v.run("", "git", "-C", repo, "bundle", "unbundle", bundle)
347 return v.objects(repo)
348}
349
350// noSentinels fails the test if any private sentinel appears in what.
351func noSentinels(t *testing.T, where, what string) {
352 t.Helper()
353 for _, s := range []string{sentinelSource, sentinelBody, sentinelComment} {
354 if strings.Contains(what, s) {
355 t.Errorf("%s holds the private sentinel %s", where, s)
356 }
357 }
358}
359
360// inActive runs f over the generation the receiver serves.
361func (rv *testReceiver) inActive(f func(export.Generation)) {
362 rv.t.Helper()
363 rv.mu.Lock()
364 defer rv.mu.Unlock()
365 g, release, err := rv.store.Open()
366 defer release()
367 if err != nil {
368 rv.t.Fatal(err)
369 }
370 f(g)
371}
372
373// No private sentinel leaves the host: not in export.json, and not in any
374// object of any bundle, whether the export is written to --out or
375// uploaded and activated by a receiver.
376func TestPrivateSentinelsNeverLeave(t *testing.T) {
377 v := newValley(t)
378 held := v.objects(filepath.Join(v.srv, "site.git"))
379 for _, s := range []string{sentinelSource, sentinelBody, sentinelComment} {
380 if !strings.Contains(held, s) {
381 t.Fatalf("site's repository does not hold the sentinel %s, so the test proves nothing", s)
382 }
383 }
384
385 dir, _, _ := v.out()
386 data, err := os.ReadFile(filepath.Join(dir, export.ExportFile))
387 if err != nil {
388 t.Fatal(err)
389 }
390 noSentinels(t, "--out's export.json", string(data))
391 bundles, _ := filepath.Glob(filepath.Join(dir, "bundles", "*"))
392 if len(bundles) != 1 {
393 t.Fatalf("--out holds the bundles %v, want app's alone", bundles)
394 }
395 noSentinels(t, "--out's bundle of app", v.unbundled(bundles[0]))
396
397 rv := newReceiver(t, v)
398 v.publish("--receiver", rv.server.URL)
399 rv.inActive(func(g export.Generation) {
400 data, err := os.ReadFile(g.ExportPath())
401 if err != nil {
402 t.Fatal(err)
403 }
404 noSentinels(t, "the receiver's export.json", string(data))
405 repos, _ := filepath.Glob(filepath.Join(g.Dir, "repos", "*"))
406 if len(repos) != 1 {
407 t.Fatalf("the receiver serves the repositories %v, want app's alone", repos)
408 }
409 noSentinels(t, "the receiver's repository of app", v.objects(repos[0]))
410 })
411}
412
413// A project that declares itself private again is gone from the next
414// export the receiver activates: from export.json and from its
415// repositories.
416func TestAProjectMadePrivateLeavesTheReceiver(t *testing.T) {
417 v := newValley(t)
418 rv := newReceiver(t, v)
419 v.declare("site", `project: visibility: "public"`)
420 v.publish("--receiver", rv.server.URL)
421 if got := names(rv.active()); !reflect.DeepEqual(got, []string{"app", "site"}) {
422 t.Fatalf("the receiver serves %v, want app and site", got)
423 }
424
425 v.declare("site", `project: visibility: "private"`)
426 stdout, _ := v.publish("--receiver", rv.server.URL)
427 mustSay(t, "site made private", stdout, "public: app; private: site, valley")
428 if got := names(rv.active()); !reflect.DeepEqual(got, []string{"app"}) {
429 t.Errorf("the receiver serves %v once site is private, want app alone", got)
430 }
431 rv.inActive(func(g export.Generation) {
432 repo, _ := g.Repo("site")
433 if _, err := os.Stat(repo); !errors.Is(err, os.ErrNotExist) {
434 t.Errorf("the receiver's active generation still holds site's repository")
435 }
436 data, err := os.ReadFile(g.ExportPath())
437 if err != nil {
438 t.Fatal(err)
439 }
440 noSentinels(t, "the receiver's export.json once site is private", string(data))
441 })
442}
443
444// landedChangeID is a change of app's that the integrator lands as a new
445// commit, so only its transfer statement shows it landed.
446const landedChangeID = "kpqrstuvwxyzkpqrstuvwxyzkpqrstuv"
447
448// publishSigners publishes, in the instance repository, the signers a
449// client verifies the integrator's landings with, as the host's signers
450// publisher writes them: a tree at refs/the-valley/signers holding the
451// known-signers file and the integrator's name. The integrator's key is
452// made here; its verifier line is the file's one line.
453func (v *testValley) publishSigners(name string) (key string) {
454 v.t.Helper()
455 key = filepath.Join(v.root, "integrator.key")
456 v.run("", "ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "integrator", "-f", key)
457 instance := filepath.Join(v.srv, "valley.git")
458 keys := v.runInput("", v.run("", "attest", "key", "--key", key, "--name", name), "git", "-C", instance, "hash-object", "-w", "--stdin")
459 named := v.runInput("", name, "git", "-C", instance, "hash-object", "-w", "--stdin")
460 tree := v.runInput("", fmt.Sprintf("100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers\n",
461 strings.TrimSpace(named), strings.TrimSpace(keys)), "git", "-C", instance, "mktree")
462 v.run("", "git", "-C", instance, "update-ref", "refs/the-valley/signers", strings.TrimSpace(tree))
463 return key
464}
465
466// landByIntegration readies a change on app and lands it as the
467// integrator does: a new commit on main, the outcome record, and the
468// transfer statement, signed with key under name.
469func (v *testValley) landByIntegration(key, name string) {
470 v.t.Helper()
471 work, bare := v.work["app"], filepath.Join(v.srv, "app.git")
472 v.run(work, "git", "checkout", "--quiet", "-b", "landed", "main")
473 v.write(filepath.Join(work, "landed.txt"), "landed\n")
474 v.run(work, "git", "add", "landed.txt")
475 v.run(work, "git", "commit", "--quiet", "-m", "app: a change the integrator lands")
476 head := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD"))
477 v.run(work, "git", "push", "--quiet", "origin", "landed")
478 v.valley("app", "ready", "--change", landedChangeID, "landed")
479 // main moves on first, so the landing is a new commit and never the
480 // change's head.
481 v.run(work, "git", "checkout", "--quiet", "main")
482 v.write(filepath.Join(work, "moved.txt"), "moved\n")
483 v.commit("app", "app: main moves on")
484 tip := strings.TrimSpace(v.run(work, "git", "rev-parse", "main"))
485 v.run(work, "git", "checkout", "--quiet", "-b", "landing", "main")
486 v.run(work, "git", "cherry-pick", head)
487 landed := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD"))
488 v.run(work, "git", "push", "--quiet", "origin", "landing:main")
489 v.run(work, "git", "checkout", "--quiet", "main")
490 v.run(work, "git", "pull", "--quiet", "--ff-only", "origin", "main")
491
492 request := "c-" + landedChangeID
493 memo := v.runInput("", fmt.Sprintf("%s %s 0123456789abcdef land\n", head, tip), "git", "-C", bare, "hash-object", "-w", "--stdin")
494 v.run("", "git", "-C", bare, "update-ref", "refs/the-valley/integration-outcomes/main/"+request, strings.TrimSpace(memo))
495 digest := func(rev string) string {
496 out := strings.TrimSpace(v.run("", "attest", "digest", "--repo", work, "--rev", rev))
497 _, d, _ := strings.Cut(out, ":")
498 return d
499 }
500 statement, err := json.Marshal(map[string]any{
501 "statementType": "the-valley/attestation/v1",
502 "subject": map[string]any{"primary": "valley-tree-v1",
503 "digest": map[string]string{"valley-tree-v1": digest(landed), "git-sha1": landed}},
504 "predicateType": "the-valley/integration/transfer/v1",
505 "predicate": map[string]any{"change": request, "target": "refs/heads/main",
506 "base": map[string]string{"valley-tree-v1": digest(head)},
507 "policy": map[string]string{"sha256": strings.Repeat("ab", 32), "git-sha1": tip}, "required": "none"},
508 })
509 if err != nil {
510 v.t.Fatal(err)
511 }
512 path := filepath.Join(v.root, "statement.json")
513 v.write(path, string(statement))
514 note := v.runInput("", v.run("", "attest", "render", path), "attest", "sign", "--key", key, "--name", name)
515 blob := v.runInput("", note, "git", "-C", bare, "hash-object", "-w", "--stdin")
516 tree := v.runInput("", "100644 blob "+strings.TrimSpace(blob)+"\ttransfer.note\n", "git", "-C", bare, "mktree")
517 v.run("", "git", "-C", bare, "update-ref",
518 fmt.Sprintf("refs/the-valley/attestations/%s/%08x", digest(landed), len(name)), strings.TrimSpace(tree))
519}
520
521// The CLI the publisher runs verifies a landing with the signers the
522// instance repository publishes, and with nothing the caller's environment
523// names: a landed change is not exported as ready for approval. Without
524// the publication the landing is not believed, and the change is.
525func TestLandingsAreVerifiedWithThePublishedSigners(t *testing.T) {
526 v := newValley(t)
527 needTool(t, "attest")
528 needTool(t, "ssh-keygen")
529 const signer = "valley-host/integration"
530 key := v.publishSigners(signer)
531 v.landByIntegration(key, signer)
532
533 // The caller's own material names the signer with another key, under
534 // which the statement would not verify.
535 other := filepath.Join(v.root, "other.key")
536 v.run("", "ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "other", "-f", other)
537 otherSigners := filepath.Join(v.root, "other-signers")
538 v.write(otherSigners, v.run("", "attest", "key", "--key", other, "--name", signer))
539 t.Setenv("VALLEY_KNOWN_SIGNERS", otherSigners)
540 t.Setenv("VALLEY_INTEGRATOR_SIGNER", signer)
541
542 changes := func() []string {
543 _, _, e := v.out()
544 ids := []string{}
545 for _, c := range e.Projects[0].Changes {
546 ids = append(ids, c.ChangeID)
547 }
548 return ids
549 }
550 if got := changes(); !reflect.DeepEqual(got, []string{changeID}) {
551 t.Errorf("with the published signers, app's changes ready for approval are %v, want %s alone", got, changeID)
552 }
553 v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", "-d", "refs/the-valley/signers")
554 got := changes()
555 if len(got) != 2 || !strings.Contains(strings.Join(got, " "), landedChangeID) {
556 t.Errorf("with no published signers, app's changes ready for approval are %v, want the landed change among them", got)
557 }
558}