| 1 | package main |
| 2 | |
| 3 | // The policy bridge, where this program is coupled to two others: the |
| 4 | // deriver's output, and the window a policy writes down. Both are pinned |
| 5 | // here rather than assumed. |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "reflect" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | func TestDeriverOutputIsRead(t *testing.T) { |
| 17 | out := `abc1234..def5678: 3 changed path(s) |
| 18 | policy: instance + policy |
| 19 | classes matched: code, prose |
| 20 | |
| 21 | required checks: |
| 22 | code-build mandatory code |
| 23 | prose-format default prose, knowledge |
| 24 | |
| 25 | unclassified: 1 path(s) matched no class |
| 26 | odd.txt |
| 27 | ` |
| 28 | d, err := parseDerived(out) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if !d.classes["code"] || !d.classes["prose"] || len(d.classes) != 2 { |
| 33 | t.Fatalf("classes = %v", d.classes) |
| 34 | } |
| 35 | if got := strings.Join(d.requires["prose-format"], "+"); got != "prose+knowledge" { |
| 36 | t.Fatalf("prose-format required by %q", got) |
| 37 | } |
| 38 | if got := strings.Join(d.requires["code-build"], "+"); got != "code" { |
| 39 | t.Fatalf("code-build required by %q", got) |
| 40 | } |
| 41 | if len(d.requires) != 2 { |
| 42 | t.Fatalf("required = %v", d.requires) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // The deriver ends its report with what each required check costs. Those |
| 47 | // lines group the required checks and require nothing more, so a report |
| 48 | // carrying them reads as exactly the report without them โ with an |
| 49 | // unclassified block between the table and the summary, and with the |
| 50 | // summary straight after the table. |
| 51 | func TestTheCostSummaryChangesNothingRead(t *testing.T) { |
| 52 | table := `abc1234..def5678: 2 changed path(s) |
| 53 | policy: instance + policy |
| 54 | classes matched: code, prose |
| 55 | |
| 56 | required checks: |
| 57 | code-build mandatory code |
| 58 | vm-test default code |
| 59 | prose-format default prose, knowledge |
| 60 | ` |
| 61 | unclassified := ` |
| 62 | unclassified: 1 path(s) matched no class |
| 63 | odd.txt |
| 64 | ` |
| 65 | summary := ` |
| 66 | quick: code-build, prose-format |
| 67 | slow: vm-test |
| 68 | ` |
| 69 | for _, before := range []string{table, table + unclassified} { |
| 70 | want, err := parseDerived(before) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if len(want.requires) != 3 || len(want.classes) != 2 { |
| 75 | t.Fatalf("the report without a summary reads as %+v", want) |
| 76 | } |
| 77 | got, err := parseDerived(before + summary) |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | if !reflect.DeepEqual(got, want) { |
| 82 | t.Errorf("with the cost summary, the report reads as %+v, not %+v", got, want) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // A deriver that fails partway and still exits zero prints something that |
| 88 | // is not a whole report. Read as one, each of these requires nothing, so |
| 89 | // each is refused. |
| 90 | func TestAPartialReportIsRefused(t *testing.T) { |
| 91 | for _, c := range []struct{ name, out string }{ |
| 92 | {"a required-checks heading with no checks under it", `abc1234..def5678: 2 changed path(s) |
| 93 | policy: instance + policy |
| 94 | classes matched: code |
| 95 | |
| 96 | required checks: |
| 97 | |
| 98 | quick: none |
| 99 | slow: none |
| 100 | `}, |
| 101 | {"a required-checks heading that ends the output", `abc1234..def5678: 2 changed path(s) |
| 102 | policy: instance + policy |
| 103 | classes matched: code |
| 104 | |
| 105 | required checks: |
| 106 | `}, |
| 107 | {"no required-checks section", `abc1234..def5678: 2 changed path(s) |
| 108 | policy: instance + policy |
| 109 | classes matched: code |
| 110 | `}, |
| 111 | {"no header", `policy: instance + policy |
| 112 | classes matched: code |
| 113 | |
| 114 | required checks: |
| 115 | code-build mandatory code |
| 116 | `}, |
| 117 | {"nothing at all", ``}, |
| 118 | {"changed paths and no class", `abc1234..def5678: 2 changed path(s) |
| 119 | policy: instance + policy |
| 120 | classes matched: none |
| 121 | |
| 122 | required checks: none |
| 123 | `}, |
| 124 | {"no changed paths and a class", `abc1234..def5678: 0 changed path(s) |
| 125 | policy: instance + policy |
| 126 | classes matched: code |
| 127 | |
| 128 | required checks: |
| 129 | code-build mandatory code |
| 130 | `}, |
| 131 | } { |
| 132 | if d, err := parseDerived(c.out); err == nil { |
| 133 | t.Errorf("%s: read as %+v", c.name, d) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // The report of a diff with nothing in it is still a whole report, and it |
| 139 | // requires nothing. |
| 140 | func TestAnEmptyDiffReadsAsRequiringNothing(t *testing.T) { |
| 141 | d, err := parseDerived(`abc1234..abc1234: 0 changed path(s) |
| 142 | policy: instance + policy |
| 143 | classes matched: none |
| 144 | |
| 145 | required checks: none |
| 146 | `) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | if d.paths != 0 || len(d.classes) != 0 || len(d.requires) != 0 { |
| 151 | t.Fatalf("an empty diff reads as %+v", d) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // A report of no changed paths is believed only when git agrees. The |
| 156 | // stand-in deriver prints a whole, well-formed report of no changed paths โ |
| 157 | // what a deriver whose diff failed silently would print โ and the |
| 158 | // derivation is read over two commits with one tree, and refused over two |
| 159 | // commits whose trees differ. |
| 160 | func TestAReportOfNoChangedPathsIsCheckedAgainstGit(t *testing.T) { |
| 161 | repo := t.TempDir() |
| 162 | run := func(args ...string) string { |
| 163 | t.Helper() |
| 164 | out, err := git(repo, append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | return strings.TrimSpace(out) |
| 169 | } |
| 170 | run("init", "--quiet") |
| 171 | run("commit", "--quiet", "--allow-empty", "-m", "one") |
| 172 | one := run("rev-parse", "HEAD") |
| 173 | run("commit", "--quiet", "--allow-empty", "-m", "the same tree") |
| 174 | same := run("rev-parse", "HEAD") |
| 175 | if err := os.WriteFile(filepath.Join(repo, "changed.txt"), []byte("x\n"), 0o644); err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | run("add", "changed.txt") |
| 179 | run("commit", "--quiet", "-m", "a change") |
| 180 | changed := run("rev-parse", "HEAD") |
| 181 | |
| 182 | deriver := filepath.Join(t.TempDir(), "valley") |
| 183 | report := `printf '%s\n' 'x..y: 0 changed path(s)' 'policy: a + b' 'classes matched: none' '' 'required checks: none'` |
| 184 | if err := os.WriteFile(deriver, []byte("#!/bin/sh\n"+report+"\n"), 0o755); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | p := policySource{valley: deriver} |
| 188 | tip := &worktree{dir: repo} |
| 189 | if _, err := p.derive(tip, one, same); err != nil { |
| 190 | t.Fatalf("an empty diff reported as empty was refused: %v", err) |
| 191 | } |
| 192 | if d, err := p.derive(tip, one, changed); err == nil { |
| 193 | t.Fatalf("a change reported as touching nothing was read as %+v", d) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) { |
| 198 | for _, c := range []struct { |
| 199 | in string |
| 200 | want time.Duration |
| 201 | }{ |
| 202 | {"", 0}, |
| 203 | {"30s", 30 * time.Second}, |
| 204 | {"15m", 15 * time.Minute}, |
| 205 | {"6h", 6 * time.Hour}, |
| 206 | {"2d", 48 * time.Hour}, |
| 207 | } { |
| 208 | got, err := parseWindow(c.in) |
| 209 | if err != nil || got != c.want { |
| 210 | t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want) |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Each layer's absence defaults in the safe direction of that layer's job, |
| 216 | // and the two directions are opposite (dcr-f41f718). Both are pinned here, |
| 217 | // because the value of the rule is that neither is the other. |
| 218 | |
| 219 | func TestATargetWithNoProjectLayerIsJudgedUnderTheFloorAlone(t *testing.T) { |
| 220 | tip := &worktree{dir: t.TempDir()} |
| 221 | p := policySource{projectDir: "policy/project"} |
| 222 | done, err := p.openProject(tip) |
| 223 | if err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | defer done() |
| 227 | if p.note == "" { |
| 228 | t.Fatal("the pass said nothing about composing the floor alone") |
| 229 | } |
| 230 | // What the deriver and the composer are handed is a layer that adds |
| 231 | // nothing, not a directory that is not there. |
| 232 | docs, err := filepath.Glob(filepath.Join(p.project, "*.cue")) |
| 233 | if err != nil || len(docs) != 1 { |
| 234 | t.Fatalf("the resolved project layer holds %v (%v)", docs, err) |
| 235 | } |
| 236 | if body, err := os.ReadFile(docs[0]); err != nil || strings.TrimSpace(string(body)) != "package verification" { |
| 237 | t.Fatalf("the layer that adds nothing reads %q (%v)", body, err) |
| 238 | } |
| 239 | done() |
| 240 | if _, err := os.Stat(p.project); !os.IsNotExist(err) { |
| 241 | t.Fatalf("the pass left its scratch layer behind: %v", err) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | func TestATargetsOwnProjectLayerIsUsedWhereItIs(t *testing.T) { |
| 246 | tip := &worktree{dir: t.TempDir()} |
| 247 | own := filepath.Join(tip.dir, "policy/project") |
| 248 | if err := os.MkdirAll(own, 0o755); err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | if err := os.WriteFile(filepath.Join(own, "policy.cue"), []byte("package verification\n"), 0o644); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | p := policySource{projectDir: "policy/project"} |
| 255 | done, err := p.openProject(tip) |
| 256 | if err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | defer done() |
| 260 | if p.project != own { |
| 261 | t.Fatalf("the project layer resolved to %s, not to %s", p.project, own) |
| 262 | } |
| 263 | if p.note != "" { |
| 264 | t.Fatalf("an ordinary composition said %q", p.note) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | func TestAFloorThatIsNotThereIsRefused(t *testing.T) { |
| 269 | // The materialized source, which is the one a test can drive without a |
| 270 | // repository. The repository source refuses the same way, and the e2e |
| 271 | // observes it there. |
| 272 | _, done, err := instanceLayer{path: t.TempDir()}.open() |
| 273 | defer done() |
| 274 | if err == nil { |
| 275 | t.Fatal("a floor directory with no documents in it was accepted") |
| 276 | } |
| 277 | if !strings.Contains(err.Error(), floorless) { |
| 278 | t.Fatalf("the refusal reads %q", err) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func TestARefusalIsReadAsTheSentenceItIs(t *testing.T) { |
| 283 | // attest reports what it checked, then refuses, then elaborates. The |
| 284 | // sentence is the refusal; the elaboration is not. |
| 285 | out := `note /tmp/x.note |
| 286 | text ok (411 bytes signed) |
| 287 | attest: tree digest mismatch: the statement is about a different tree |
| 288 | recorded aaaa |
| 289 | found bbbb |
| 290 | ` |
| 291 | if got := refusalLine(out); got != "tree digest mismatch: the statement is about a different tree" { |
| 292 | t.Fatalf("refusalLine = %q", got) |
| 293 | } |
| 294 | if got := refusalLine("something with no prefix at all\n"); got != "something with no prefix at all" { |
| 295 | t.Fatalf("refusalLine fallback = %q", got) |
| 296 | } |
| 297 | } |