gunk-dev/the-valley

env_test.go

sourced9f7bacd171bmain's published tip history
4.3 KiB
1package main
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "strings"
8 "testing"
9
10 "the-valley/composed"
11)
12
13// The unit hands every controller its safe.directory exceptions as
14// command-scope configuration, and git reads those entries only up to the
15// count. Dropping everything else means renumbering what is kept, and an
16// entry left past the count would be one git silently ignores.
17func TestOnlySafeDirectoryConfigurationIsInheritedAndReplacementRefsAreOff(t *testing.T) {
18 got := gitEnvironment([]string{
19 "PATH=/bin",
20 "GIT_DIR=/elsewhere",
21 "GIT_CONFIG_COUNT=3",
22 "GIT_CONFIG_KEY_0=core.useReplaceRefs",
23 "GIT_CONFIG_VALUE_0=true",
24 "GIT_CONFIG_KEY_1=safe.directory",
25 "GIT_CONFIG_VALUE_1=/srv/git/project.git",
26 "GIT_CONFIG_KEY_2=safe.directory",
27 "GIT_CONFIG_VALUE_2=/srv/git/instance.git",
28 "GIT_CONFIG_PARAMETERS='core.usereplacerefs'='true'",
29 })
30 for _, want := range []string{
31 "PATH=/bin",
32 "GIT_CONFIG_COUNT=2",
33 "GIT_CONFIG_KEY_0=safe.directory",
34 "GIT_CONFIG_VALUE_0=/srv/git/project.git",
35 "GIT_CONFIG_KEY_1=safe.directory",
36 "GIT_CONFIG_VALUE_1=/srv/git/instance.git",
37 "GIT_NO_REPLACE_OBJECTS=1",
38 "GIT_CONFIG_GLOBAL=/dev/null",
39 } {
40 if !slices.Contains(got, want) {
41 t.Errorf("environment lacks %s: %q", want, got)
42 }
43 }
44 for _, kv := range got {
45 switch kv {
46 case "GIT_DIR=/elsewhere", "GIT_CONFIG_VALUE_0=true", "GIT_CONFIG_PARAMETERS='core.usereplacerefs'='true'":
47 t.Errorf("environment kept %s", kv)
48 }
49 }
50}
51
52// The approval inputs are paths, taken without opening them: a file in a
53// directory that does not exist is taken, a relative path is taken
54// absolute, and none is required. The two signers files are two files.
55func TestTheApprovalInputsAreTakenAsPaths(t *testing.T) {
56 missing := filepath.Join(t.TempDir(), "absent", "approval-signers")
57 a, err := newApprovalInputs(missing, "approval-signers-hardware", "")
58 if err != nil {
59 t.Fatal(err)
60 }
61 if a.signers != missing || !filepath.IsAbs(a.hardware) || filepath.Base(a.hardware) != "approval-signers-hardware" || a.pushPolicy != "" {
62 t.Errorf("the inputs were taken as %+v", a)
63 }
64 if a, err := newApprovalInputs("", "", ""); err != nil || a != (approvalInputs{}) {
65 t.Errorf("no input was taken as %+v, %v", a, err)
66 }
67 if a, err := newApprovalInputs(missing, missing, ""); err == nil {
68 t.Errorf("one file as both signers files was taken as %+v", a)
69 }
70}
71
72// The approval inputs change no pass. A pass with the three paths given
73// and a pass with none read the same inputs digest, refuse the same
74// request with the same report, and leave the same outcome and inputs
75// records.
76func TestTheApprovalInputsChangeNoPass(t *testing.T) {
77 s, r := refusedRequest(t)
78 floor := t.TempDir()
79 if err := os.WriteFile(filepath.Join(floor, "floor.cue"), []byte("package verification\n"), 0o644); err != nil {
80 t.Fatal(err)
81 }
82 known := filepath.Join(t.TempDir(), "known-signers")
83 if err := os.WriteFile(known, []byte(verifierLine("valley-host/attestations", 1)), 0o644); err != nil {
84 t.Fatal(err)
85 }
86 given, err := newApprovalInputs(filepath.Join(t.TempDir(), "approval-signers"),
87 filepath.Join(t.TempDir(), "approval-signers-hardware"), filepath.Join(t.TempDir(), "push-policy.json"))
88 if err != nil {
89 t.Fatal(err)
90 }
91 type pass struct{ digest, report, memo, inputs string }
92 run := func(a approvalInputs) pass {
93 t.Helper()
94 var out strings.Builder
95 in := &integrator{
96 repo: s.repo, project: "project", out: &out, signerName: "valley-host/attestations",
97 knownSigners: known, policy: policySource{layer: composed.Floor{Path: floor}}, approval: a,
98 }
99 state, done := in.inputState()
100 done()
101 if err := in.reconcile(reasonOnce); err != nil {
102 t.Fatal(err)
103 }
104 memo, err := in.readMemo(r)
105 if err != nil {
106 t.Fatal(err)
107 }
108 p := pass{state.digest, out.String(), memo, in.readInputs(r)}
109 // The next pass starts from no record, as this one did.
110 s.git("update-ref", "-d", in.memoRef(r))
111 s.git("update-ref", "-d", in.inputsRef(r))
112 return p
113 }
114 without, with := run(approvalInputs{}), run(given)
115 if without != with {
116 t.Errorf("a pass without the approval inputs gave\n%+v\nand one with them\n%+v", without, with)
117 }
118 if !strings.Contains(without.report, string(refusedNotReady)) || without.memo == "" || without.inputs != without.digest {
119 t.Errorf("the pass refused nothing, so this test proves nothing: %+v", without)
120 }
121}