| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // countNix puts a nix and a nix-store ahead of the real ones on the |
| 13 | // fixture's PATH. Each writes the command it was given to the log it |
| 14 | // returns, one call per line, and then runs the real program with the same |
| 15 | // arguments, so every answer a test sees is nix's own. |
| 16 | func countNix(t *testing.T, f *nixFixture) string { |
| 17 | t.Helper() |
| 18 | dir := t.TempDir() |
| 19 | log := filepath.Join(dir, "calls") |
| 20 | for _, tool := range []string{"nix", "nix-store"} { |
| 21 | real, err := exec.LookPath(tool) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | script := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' \"%s $*\" >> '%s'\nexec '%s' \"$@\"\n", tool, log, real) |
| 26 | if err := os.WriteFile(filepath.Join(dir, tool), []byte(script), 0o755); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | f.env = append(f.env, "PATH="+dir+":"+os.Getenv("PATH")) |
| 31 | return log |
| 32 | } |
| 33 | |
| 34 | // calls reads the log countNix keeps and empties it, so each run is |
| 35 | // counted on its own. |
| 36 | func calls(t *testing.T, log string) []string { |
| 37 | t.Helper() |
| 38 | b, err := os.ReadFile(log) |
| 39 | if err != nil && !os.IsNotExist(err) { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if err := os.WriteFile(log, nil, 0o644); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | return strings.Split(strings.TrimSpace(string(b)), "\n") |
| 46 | } |
| 47 | |
| 48 | // count is how many logged calls begin with prefix. |
| 49 | func count(lines []string, prefix string) int { |
| 50 | n := 0 |
| 51 | for _, l := range lines { |
| 52 | if strings.HasPrefix(l, prefix) { |
| 53 | n++ |
| 54 | } |
| 55 | } |
| 56 | return n |
| 57 | } |
| 58 | |
| 59 | // noBuilds fails the test if any logged call could have built something |
| 60 | // on purpose: a nix build, or a nix-store asked to realise a path. |
| 61 | func noBuilds(t *testing.T, lines []string) { |
| 62 | t.Helper() |
| 63 | for _, l := range lines { |
| 64 | if strings.HasPrefix(l, "nix build") || (strings.HasPrefix(l, "nix-store") && strings.Contains(l, "--realise")) { |
| 65 | t.Errorf("attest inputs built something: %s", l) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // inputs runs `attest inputs` over the fixture's HEAD with one --check |
| 71 | // flag per check given, and returns what it printed and its exit status. |
| 72 | func (f nixFixture) inputs(t *testing.T, checks ...string) (stdout, stderr string, status int) { |
| 73 | t.Helper() |
| 74 | args := []string{"inputs", "--repo", f.repo} |
| 75 | for _, c := range checks { |
| 76 | args = append(args, "--check", c) |
| 77 | } |
| 78 | cmd := exec.Command(os.Args[0], args...) |
| 79 | cmd.Env = f.env |
| 80 | var out, errb strings.Builder |
| 81 | cmd.Stdout, cmd.Stderr = &out, &errb |
| 82 | if err := cmd.Run(); err != nil { |
| 83 | exit, ok := err.(*exec.ExitError) |
| 84 | if !ok { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | status = exit.ExitCode() |
| 88 | } |
| 89 | return out.String(), errb.String(), status |
| 90 | } |
| 91 | |
| 92 | // Several checks asked for at once are evaluated together, in one nix |
| 93 | // evaluation, and nothing is built. Each line printed is the line that |
| 94 | // asking for that check alone prints, in the order asked: an alias carries |
| 95 | // its own name and its attribute's digests, and two names for one |
| 96 | // attribute each get a line. |
| 97 | func TestInputsEvaluatesEveryCheckTogether(t *testing.T) { |
| 98 | f := newNixFixture(t, map[string]string{ |
| 99 | "a": `check "a" "echo a > $out"`, |
| 100 | "b": `check "b" "echo b > $out"`, |
| 101 | "c": `check "c" "echo c > $out"`, |
| 102 | }) |
| 103 | log := countNix(t, &f) |
| 104 | asked := []string{"c", "a", "alias=b", "b", "again=a"} |
| 105 | |
| 106 | var alone strings.Builder |
| 107 | for _, check := range asked { |
| 108 | stdout, stderr, status := f.inputs(t, check) |
| 109 | if status != 0 { |
| 110 | t.Fatalf("attest inputs --check %s exited %d:\n%s", check, status, stderr) |
| 111 | } |
| 112 | alone.WriteString(stdout) |
| 113 | lines := calls(t, log) |
| 114 | if n := count(lines, "nix eval"); n != 1 { |
| 115 | t.Errorf("one check alone took %d nix evaluations:\n%s", n, strings.Join(lines, "\n")) |
| 116 | } |
| 117 | noBuilds(t, lines) |
| 118 | } |
| 119 | |
| 120 | stdout, stderr, status := f.inputs(t, asked...) |
| 121 | if status != 0 { |
| 122 | t.Fatalf("attest inputs over %d checks exited %d:\n%s", len(asked), status, stderr) |
| 123 | } |
| 124 | if stdout != alone.String() { |
| 125 | t.Errorf("the checks together printed\n%s\nand one at a time\n%s", stdout, alone.String()) |
| 126 | } |
| 127 | lines := calls(t, log) |
| 128 | if n := count(lines, "nix eval"); n != 1 { |
| 129 | t.Errorf("%d checks took %d nix evaluations, not one:\n%s", len(asked), n, strings.Join(lines, "\n")) |
| 130 | } |
| 131 | noBuilds(t, lines) |
| 132 | |
| 133 | byName := map[string]string{} |
| 134 | for _, line := range strings.Split(strings.TrimSpace(stdout), "\n") { |
| 135 | name, digests, _ := strings.Cut(line, " ") |
| 136 | byName[name] = digests |
| 137 | } |
| 138 | if byName["alias"] != byName["b"] || byName["again"] != byName["a"] || byName["a"] == byName["b"] { |
| 139 | t.Errorf("a name for an attribute does not carry that attribute's digests:\n%s", stdout) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // A check that does not evaluate ends the command with its error, as it |
| 144 | // always has: what is printed on standard output is every check asked for |
| 145 | // before it, and nothing after, and the error is the one evaluating that |
| 146 | // check alone reports. Whether the shared evaluation survives the error is |
| 147 | // what it costs: nix can catch a missing attribute and a throw, so only |
| 148 | // the broken check is evaluated again, but not a type error, which ends |
| 149 | // the shared evaluation and has every check evaluated alone. |
| 150 | func TestInputsStopsAtTheFirstError(t *testing.T) { |
| 151 | for _, c := range []struct { |
| 152 | name string |
| 153 | broken string // "" leaves the check undefined |
| 154 | says string |
| 155 | evals int |
| 156 | }{ |
| 157 | {"an absent attribute", "", "does not provide attribute", 2}, |
| 158 | {"a throw", `throw "the broken check does not evaluate"`, "the broken check does not evaluate", 2}, |
| 159 | {"a type error", `check "broken" (1 + "the broken check does not evaluate")`, "cannot add a string to an integer", 4}, |
| 160 | } { |
| 161 | t.Run(c.name, func(t *testing.T) { |
| 162 | checks := map[string]string{ |
| 163 | "fine": `check "fine" "echo fine > $out"`, |
| 164 | "also": `check "also" "echo also > $out"`, |
| 165 | } |
| 166 | if c.broken != "" { |
| 167 | checks["broken"] = c.broken |
| 168 | } |
| 169 | f := newNixFixture(t, checks) |
| 170 | log := countNix(t, &f) |
| 171 | fine, _, status := f.inputs(t, "fine") |
| 172 | if status != 0 { |
| 173 | t.Fatalf("the fine check alone exited %d", status) |
| 174 | } |
| 175 | _, aloneErr, status := f.inputs(t, "broken") |
| 176 | if status != 1 || !strings.HasPrefix(aloneErr, "attest: evaluating ") || !strings.Contains(aloneErr, c.says) { |
| 177 | t.Fatalf("the broken check alone exited %d with:\n%s", status, aloneErr) |
| 178 | } |
| 179 | calls(t, log) |
| 180 | |
| 181 | stdout, stderr, status := f.inputs(t, "fine", "broken", "also") |
| 182 | if status != 1 { |
| 183 | t.Errorf("attest inputs exited %d, not 1", status) |
| 184 | } |
| 185 | if stdout != fine { |
| 186 | t.Errorf("standard output is\n%q\nnot the line before the error\n%q", stdout, fine) |
| 187 | } |
| 188 | if !strings.HasPrefix(stderr, "attest: evaluating ") || !strings.Contains(stderr, ".broken: exit status 1: ") || !strings.Contains(stderr, c.says) { |
| 189 | t.Errorf("standard error does not report the broken check's evaluation:\n%s", stderr) |
| 190 | } |
| 191 | lines := calls(t, log) |
| 192 | if n := count(lines, "nix eval"); n != c.evals { |
| 193 | t.Errorf("%d nix evaluations, not %d:\n%s", n, c.evals, strings.Join(lines, "\n")) |
| 194 | } |
| 195 | noBuilds(t, lines) |
| 196 | }) |
| 197 | } |
| 198 | } |