| 1 |
package instrumentor_test |
| 2 |
|
| 3 |
// Runtime edge-notification tests. |
| 4 |
// |
| 5 |
// These check that an instrumented program *fires the expected edges at runtime*. Each case under |
| 6 |
// testdata/runtime_edges/ is a self-contained, side-effect-free, argv-driven program: |
| 7 |
// |
| 8 |
// testdata/runtime_edges/<case>/program.go -- runnable `package main` |
| 9 |
// testdata/runtime_edges/<case>/<embed assets> -- files referenced via //go:embed |
| 10 |
// testdata/runtime_edges/<case>/cases.json -- argv -> expected edge ids (in order) |
| 11 |
// testdata/runtime_edges/<case>/expected.sym.tsv -- the symbol table (edge id -> source span) |
| 12 |
// |
| 13 |
// Regenerate after changing a program or (intentionally) changing instrumentor |
| 14 |
// runtime behavior: |
| 15 |
// |
| 16 |
// go test ./scanners/coverage/instrumentor -run TestRuntimeEdges -update |
| 17 |
// |
| 18 |
// (-update rewrites each cases.json's edge lists and expected.sym.tsv.) |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/json" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"path/filepath" |
| 25 |
"strconv" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/symboltable" |
| 30 |
|
| 31 |
qt "github.com/go-quicktest/qt" |
| 32 |
) |
| 33 |
|
| 34 |
// recorderSrc is the stand-in for libvoidstar: the instrumented program's |
| 35 |
// Notify(edge) calls land here, and each edge id is streamed to stdout. |
| 36 |
const recorderSrc = `package rec |
| 37 |
|
| 38 |
import ( |
| 39 |
"fmt" |
| 40 |
"os" |
| 41 |
) |
| 42 |
|
| 43 |
func Notify(edge int) { fmt.Fprintln(os.Stdout, edge) } |
| 44 |
` |
| 45 |
|
| 46 |
type edgeCase struct { |
| 47 |
Args []string `json:"args"` |
| 48 |
Edges []int `json:"edges"` |
| 49 |
} |
| 50 |
|
| 51 |
type edgeCases struct { |
| 52 |
Cases []edgeCase `json:"cases"` |
| 53 |
} |
| 54 |
|
| 55 |
func TestRuntimeEdges(t *testing.T) { |
| 56 |
programs, err := filepath.Glob("testdata/runtime_edges/*/program.go") |
| 57 |
qt.Assert(t, qt.IsNil(err)) |
| 58 |
qt.Assert(t, qt.IsTrue(len(programs) > 0)) |
| 59 |
|
| 60 |
for _, progPath := range programs { |
| 61 |
caseDir := filepath.Dir(progPath) |
| 62 |
t.Run(filepath.Base(caseDir), func(t *testing.T) { |
| 63 |
cases := loadCases(t, filepath.Join(caseDir, "cases.json")) |
| 64 |
|
| 65 |
syms := make([]string, len(backends)) |
| 66 |
edges := make([][][]int, len(backends)) |
| 67 |
for bi, be := range backends { |
| 68 |
syms[bi], edges[bi] = instrumentAndRun(t, caseDir, progPath, be.name, be.create, cases) |
| 69 |
} |
| 70 |
|
| 71 |
checkGolden(t, filepath.Join(caseDir, "expected.sym.tsv"), syms[0]) |
| 72 |
qt.Check(t, qt.Equals(syms[1], syms[0]), |
| 73 |
qt.Commentf("%s and %s must emit identical symbol tables", backends[0].name, backends[1].name)) |
| 74 |
|
| 75 |
for i := range cases.Cases { |
| 76 |
if *update { |
| 77 |
cases.Cases[i].Edges = edges[0][i] |
| 78 |
} else { |
| 79 |
qt.Check(t, qt.DeepEquals(edges[0][i], cases.Cases[i].Edges), |
| 80 |
qt.Commentf("%s argv %v", backends[0].name, cases.Cases[i].Args)) |
| 81 |
} |
| 82 |
qt.Check(t, qt.DeepEquals(edges[1][i], edges[0][i]), |
| 83 |
qt.Commentf("%s vs %s argv %v", backends[1].name, backends[0].name, cases.Cases[i].Args)) |
| 84 |
} |
| 85 |
if *update { |
| 86 |
writeCases(t, filepath.Join(caseDir, "cases.json"), cases) |
| 87 |
} |
| 88 |
}) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func instrumentAndRun(t *testing.T, caseDir, progPath, name string, create func(basePath, shimPkg string, table *symboltable.SymbolTable) instrumenter, cases edgeCases) (string, [][]int) { |
| 93 |
t.Helper() |
| 94 |
|
| 95 |
// Build a self-contained temp module: recorder + the instrumented program + |
| 96 |
// any embed assets. |
| 97 |
dir := t.TempDir() |
| 98 |
write(t, filepath.Join(dir, "go.mod"), "module edgetest\n\ngo 1.24\n") |
| 99 |
qt.Assert(t, qt.IsNil(os.MkdirAll(filepath.Join(dir, "rec"), 0755))) |
| 100 |
write(t, filepath.Join(dir, "rec", "rec.go"), recorderSrc) |
| 101 |
copyEmbedAssets(t, caseDir, dir) |
| 102 |
|
| 103 |
// Instrument program.go with the notifier shim pointed at the recorder. |
| 104 |
progTmp := filepath.Join(dir, "program.go") |
| 105 |
write(t, progTmp, read(t, progPath)) |
| 106 |
symPath := filepath.Join(dir, "edges.sym.tsv") |
| 107 |
sym, err := symboltable.CreateSymbolTableFile(symPath, "testmodule") |
| 108 |
qt.Assert(t, qt.IsNil(err)) |
| 109 |
out, err := create(dir, "edgetest/rec", sym).Instrument(progTmp) |
| 110 |
qt.Assert(t, qt.IsNil(err)) |
| 111 |
qt.Assert(t, qt.IsTrue(out != ""), qt.Commentf("%s: program.go was skipped (not instrumented)", name)) |
| 112 |
write(t, progTmp, out) |
| 113 |
qt.Assert(t, qt.IsNil(sym.Close())) |
| 114 |
|
| 115 |
symNorm := strings.ReplaceAll(read(t, symPath), progTmp, "program.go") |
| 116 |
symNorm = instrumentorLineRe.ReplaceAllString(symNorm, "# instrumentor = INSTRUMENTOR") |
| 117 |
|
| 118 |
// Build once, then run every argv case against the recorder. |
| 119 |
bin := filepath.Join(dir, "prog") |
| 120 |
build := exec.Command("go", "build", "-o", bin, ".") |
| 121 |
build.Dir = dir |
| 122 |
build.Env = append(os.Environ(), "GOFLAGS=") |
| 123 |
if outB, err := build.CombinedOutput(); err != nil { |
| 124 |
t.Fatalf("%s: building instrumented program failed: %v\n%s", name, err, outB) |
| 125 |
} |
| 126 |
|
| 127 |
got := make([][]int, len(cases.Cases)) |
| 128 |
for i := range cases.Cases { |
| 129 |
got[i] = runEdges(t, bin, cases.Cases[i].Args) |
| 130 |
} |
| 131 |
return symNorm, got |
| 132 |
} |
| 133 |
|
| 134 |
// runEdges runs the built program with args and returns the edge ids it printed. |
| 135 |
func runEdges(t *testing.T, bin string, args []string) []int { |
| 136 |
t.Helper() |
| 137 |
cmd := exec.Command(bin, args...) |
| 138 |
out, err := cmd.Output() |
| 139 |
if err != nil { |
| 140 |
stderr := "" |
| 141 |
if ee, ok := err.(*exec.ExitError); ok { |
| 142 |
stderr = string(ee.Stderr) |
| 143 |
} |
| 144 |
t.Fatalf("running %s %v failed: %v\n%s", filepath.Base(bin), args, err, stderr) |
| 145 |
} |
| 146 |
var edges []int |
| 147 |
for _, line := range strings.Fields(strings.TrimSpace(string(out))) { |
| 148 |
n, err := strconv.Atoi(line) |
| 149 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("non-integer edge output: %q", line)) |
| 150 |
edges = append(edges, n) |
| 151 |
} |
| 152 |
return edges |
| 153 |
} |
| 154 |
|
| 155 |
func copyEmbedAssets(t *testing.T, caseDir, dir string) { |
| 156 |
t.Helper() |
| 157 |
entries, err := os.ReadDir(caseDir) |
| 158 |
qt.Assert(t, qt.IsNil(err)) |
| 159 |
for _, e := range entries { |
| 160 |
switch { |
| 161 |
case e.IsDir(), e.Name() == "program.go", e.Name() == "cases.json", e.Name() == "expected.sym.tsv": |
| 162 |
continue |
| 163 |
} |
| 164 |
write(t, filepath.Join(dir, e.Name()), read(t, filepath.Join(caseDir, e.Name()))) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func loadCases(t *testing.T, path string) edgeCases { |
| 169 |
t.Helper() |
| 170 |
var c edgeCases |
| 171 |
qt.Assert(t, qt.IsNil(json.Unmarshal([]byte(read(t, path)), &c))) |
| 172 |
return c |
| 173 |
} |
| 174 |
|
| 175 |
func writeCases(t *testing.T, path string, c edgeCases) { |
| 176 |
t.Helper() |
| 177 |
content, _ := json.MarshalIndent(c, "", " ") |
| 178 |
write(t, path, string(content)) |
| 179 |
} |
| 180 |
|
| 181 |
func read(t *testing.T, path string) string { |
| 182 |
t.Helper() |
| 183 |
b, err := os.ReadFile(path) |
| 184 |
qt.Assert(t, qt.IsNil(err)) |
| 185 |
return string(b) |
| 186 |
} |
| 187 |
|
| 188 |
func write(t *testing.T, path, content string) { |
| 189 |
t.Helper() |
| 190 |
qt.Assert(t, qt.IsNil(os.WriteFile(path, []byte(content), 0644))) |
| 191 |
} |