| 1 |
package main |
| 2 |
|
| 3 |
// Multi-package end-to-end test. |
| 4 |
// |
| 5 |
// The runtime_edges fixtures are all a single `package main`, so they exercise |
| 6 |
// the edge finder but cannot show what is structurally new about the -toolexec |
| 7 |
// front end: one coverage module and one symbol table *per package*, each |
| 8 |
// numbering its edges from 1, with a shared dependency appearing once rather than |
| 9 |
// once per dependent. |
| 10 |
// |
| 11 |
// testdata/multipackage is a four-package diamond -- main depends on alpha and |
| 12 |
// beta, both of which depend on shared -- and its golden artifacts are therefore |
| 13 |
// the only checked-in ones that reflect that structure. |
| 14 |
// |
| 15 |
// Regenerate after changing the fixture: |
| 16 |
// |
| 17 |
// go test ./tools/antithesis-go-toolexec -run TestMultiPackage -update |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"flag" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"path/filepath" |
| 25 |
"sort" |
| 26 |
"strconv" |
| 27 |
"strings" |
| 28 |
"testing" |
| 29 |
|
| 30 |
qt "github.com/go-quicktest/qt" |
| 31 |
) |
| 32 |
|
| 33 |
var update = flag.Bool("update", false, "rewrite the multipackage golden artifacts") |
| 34 |
|
| 35 |
const multiPackageDir = "testdata/multipackage" |
| 36 |
|
| 37 |
// multiPackageModule is the import path the fixture's packages use. |
| 38 |
const multiPackageModule = "multipkg" |
| 39 |
|
| 40 |
// multiPackageCase is one argv invocation and the notifications it must produce, |
| 41 |
// each written "<package> <edge>" so that per-package numbering is legible and |
| 42 |
// stable against symbol table renaming. |
| 43 |
type multiPackageCase struct { |
| 44 |
Args []string `json:"args"` |
| 45 |
Fired []string `json:"fired"` |
| 46 |
} |
| 47 |
|
| 48 |
type multiPackageCases struct { |
| 49 |
// Modules maps each package to the number of edges its module registers. |
| 50 |
Modules map[string]int `json:"modules"` |
| 51 |
Cases []multiPackageCase `json:"cases"` |
| 52 |
} |
| 53 |
|
| 54 |
func TestMultiPackage(t *testing.T) { |
| 55 |
if testing.Short() { |
| 56 |
t.Skip("builds a wrapper binary and a multi-package program") |
| 57 |
} |
| 58 |
|
| 59 |
binary, symbolsDir := buildMultiPackage(t) |
| 60 |
|
| 61 |
// One symbol table per package, named by content hash. Map each to the |
| 62 |
// package it describes, using the file column of its rows -- which is stable |
| 63 |
// where the hash in the file name is not. |
| 64 |
// Symbol table rows record absolute build paths, as the whole-tree |
| 65 |
// instrumentor's do, so they are rewritten relative to the module root before |
| 66 |
// anything is compared or checked in. |
| 67 |
tables := loadSymbolTables(t, symbolsDir, filepath.Dir(binary)) |
| 68 |
qt.Assert(t, qt.HasLen(tables, 4), |
| 69 |
qt.Commentf("expected one symbol table per package, got %d", len(tables))) |
| 70 |
|
| 71 |
names := symbolTableNames(t, symbolsDir, filepath.Dir(binary)) |
| 72 |
|
| 73 |
got := multiPackageCases{Modules: map[string]int{}} |
| 74 |
for pkg, table := range tables { |
| 75 |
got.Modules[pkg] = len(table.rows) |
| 76 |
} |
| 77 |
|
| 78 |
wanted := loadMultiPackageCases(t) |
| 79 |
for _, c := range wanted.Cases { |
| 80 |
got.Cases = append(got.Cases, multiPackageCase{ |
| 81 |
Args: c.Args, |
| 82 |
Fired: runForModuleEdges(t, binary, c.Args, names), |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
if *update { |
| 87 |
writeMultiPackageCases(t, got) |
| 88 |
for pkg, table := range tables { |
| 89 |
writeGolden(t, filepath.Join(multiPackageDir, "expected", pkg+".sym.tsv"), table.text) |
| 90 |
} |
| 91 |
t.Log("golden artifacts rewritten") |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
// Every package registers its own module, sized to its own edge count. |
| 96 |
qt.Check(t, qt.DeepEquals(got.Modules, wanted.Modules)) |
| 97 |
|
| 98 |
// Each package's edges are numbered from 1, independently. |
| 99 |
for pkg, table := range tables { |
| 100 |
qt.Check(t, qt.DeepEquals(table.edgeIDs(), sequence(len(table.rows))), |
| 101 |
qt.Commentf("package %s must number its edges 1..N", pkg)) |
| 102 |
} |
| 103 |
|
| 104 |
for i, c := range wanted.Cases { |
| 105 |
qt.Check(t, qt.DeepEquals(got.Cases[i].Fired, c.Fired), qt.Commentf("argv %q", c.Args)) |
| 106 |
} |
| 107 |
|
| 108 |
// And each symbol table matches its checked-in artifact. |
| 109 |
for pkg, table := range tables { |
| 110 |
golden := filepath.Join(multiPackageDir, "expected", pkg+".sym.tsv") |
| 111 |
want, err := os.ReadFile(golden) |
| 112 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("missing golden %s; run with -update", golden)) |
| 113 |
qt.Check(t, qt.Equals(table.text, string(want)), qt.Commentf("package %s", pkg)) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// symbolTable is one package's table: its normalized text, plus its rows. |
| 118 |
type symbolTable struct { |
| 119 |
text string |
| 120 |
rows []string |
| 121 |
} |
| 122 |
|
| 123 |
func (s symbolTable) edgeIDs() []int { |
| 124 |
ids := make([]int, 0, len(s.rows)) |
| 125 |
for _, row := range s.rows { |
| 126 |
columns := strings.Split(row, "\t") |
| 127 |
id, err := strconv.Atoi(columns[len(columns)-1]) |
| 128 |
if err != nil { |
| 129 |
continue |
| 130 |
} |
| 131 |
ids = append(ids, id) |
| 132 |
} |
| 133 |
sort.Ints(ids) |
| 134 |
return ids |
| 135 |
} |
| 136 |
|
| 137 |
// loadSymbolTables reads every table in dir and keys them by the package they |
| 138 |
// describe, taken from the directory component of their first row's file. |
| 139 |
func loadSymbolTables(t *testing.T, dir, moduleRoot string) map[string]symbolTable { |
| 140 |
t.Helper() |
| 141 |
paths := tablesForModule(t, dir, moduleRoot) |
| 142 |
|
| 143 |
tables := make(map[string]symbolTable, len(paths)) |
| 144 |
for _, path := range paths { |
| 145 |
raw, err := os.ReadFile(path) |
| 146 |
qt.Assert(t, qt.IsNil(err)) |
| 147 |
body := relativizePaths(string(raw), moduleRoot) |
| 148 |
rows := symbolTableRows(body) |
| 149 |
qt.Assert(t, qt.IsTrue(len(rows) > 0), qt.Commentf("%s has no rows", path)) |
| 150 |
|
| 151 |
pkg := packageOfRow(t, body) |
| 152 |
_, duplicate := tables[pkg] |
| 153 |
qt.Assert(t, qt.IsFalse(duplicate), |
| 154 |
qt.Commentf("two symbol tables describe package %s; a shared dependency must appear once", pkg)) |
| 155 |
tables[pkg] = symbolTable{text: normalizeSymbolTable(body, pkg), rows: rows} |
| 156 |
} |
| 157 |
return tables |
| 158 |
} |
| 159 |
|
| 160 |
// packageOfRow names the package a table describes. Rows carry module-relative |
| 161 |
// file paths, so "shared/shared.go" is package shared and "main.go" is main. |
| 162 |
func packageOfRow(t *testing.T, table string) string { |
| 163 |
t.Helper() |
| 164 |
for _, line := range strings.Split(table, "\n") { |
| 165 |
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "file\t") { |
| 166 |
continue |
| 167 |
} |
| 168 |
file := strings.SplitN(line, "\t", 2)[0] |
| 169 |
if dir := filepath.Dir(file); dir != "." { |
| 170 |
return dir |
| 171 |
} |
| 172 |
return "main" |
| 173 |
} |
| 174 |
t.Fatalf("no data rows in symbol table") |
| 175 |
return "" |
| 176 |
} |
| 177 |
|
| 178 |
// relativizePaths rewrites the absolute build paths a symbol table records so |
| 179 |
// they are relative to the module root, which is what makes the table stable |
| 180 |
// enough to check in. |
| 181 |
func relativizePaths(table, moduleRoot string) string { |
| 182 |
return strings.ReplaceAll(table, moduleRoot+string(filepath.Separator), "") |
| 183 |
} |
| 184 |
|
| 185 |
// normalizeSymbolTable makes a table comparable to a checked-in golden: the |
| 186 |
// content-hash module name and the absolute instrumentor path both vary by |
| 187 |
// build, so neither can be asserted verbatim. |
| 188 |
func normalizeSymbolTable(table, pkg string) string { |
| 189 |
var out []string |
| 190 |
for _, line := range strings.Split(table, "\n") { |
| 191 |
switch { |
| 192 |
case strings.HasPrefix(line, "# module = "): |
| 193 |
line = "# module = " + pkg |
| 194 |
case strings.HasPrefix(line, "# instrumentor = "): |
| 195 |
line = "# instrumentor = INSTRUMENTOR" |
| 196 |
} |
| 197 |
out = append(out, line) |
| 198 |
} |
| 199 |
return strings.Join(out, "\n") |
| 200 |
} |
| 201 |
|
| 202 |
// runForModuleEdges runs the program and returns its notifications as |
| 203 |
// "<package> <edge>", in order. |
| 204 |
func runForModuleEdges(t *testing.T, binary string, args []string, names map[string]string) []string { |
| 205 |
t.Helper() |
| 206 |
command := exec.Command(binary, args...) |
| 207 |
command.Dir = filepath.Dir(binary) |
| 208 |
out, err := command.Output() |
| 209 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("running %q failed", args)) |
| 210 |
|
| 211 |
// The recorder reports the symbol table name; translate it to the package it |
| 212 |
// describes so the expectation survives a rehash. |
| 213 |
fired := []string{} |
| 214 |
for _, n := range recorded(t, out) { |
| 215 |
pkg, ok := names[n.symbolTable] |
| 216 |
qt.Assert(t, qt.IsTrue(ok), qt.Commentf("unknown symbol table %q", n.symbolTable)) |
| 217 |
fired = append(fired, pkg+" "+strconv.Itoa(n.edge)) |
| 218 |
} |
| 219 |
return fired |
| 220 |
} |
| 221 |
|
| 222 |
// symbolTableNames maps each symbol table's file name to its package. |
| 223 |
func symbolTableNames(t *testing.T, dir, moduleRoot string) map[string]string { |
| 224 |
t.Helper() |
| 225 |
paths := tablesForModule(t, dir, moduleRoot) |
| 226 |
names := make(map[string]string, len(paths)) |
| 227 |
for _, path := range paths { |
| 228 |
raw, err := os.ReadFile(path) |
| 229 |
qt.Assert(t, qt.IsNil(err)) |
| 230 |
names[filepath.Base(path)] = packageOfRow(t, relativizePaths(string(raw), moduleRoot)) |
| 231 |
} |
| 232 |
return names |
| 233 |
} |
| 234 |
|
| 235 |
// buildMultiPackage assembles the fixture into a temporary module, builds it |
| 236 |
// through the wrapper, and returns the binary and its symbols directory. |
| 237 |
func buildMultiPackage(t *testing.T) (string, string) { |
| 238 |
t.Helper() |
| 239 |
dir := t.TempDir() |
| 240 |
|
| 241 |
goMod := "module " + multiPackageModule + "\n\ngo 1.24\n\n" + |
| 242 |
"require github.com/antithesishq/antithesis-sdk-go v0.0.0\n\n" + |
| 243 |
"replace github.com/antithesishq/antithesis-sdk-go => " + sharedRecorder + "\n" |
| 244 |
qt.Assert(t, qt.IsNil(os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goMod), 0o644))) |
| 245 |
copyTree(t, multiPackageDir, dir) |
| 246 |
|
| 247 |
symbolsDir := filepath.Join(dir, "symbols") |
| 248 |
binary := filepath.Join(dir, "prog") |
| 249 |
build := exec.Command("go", "build", "-toolexec="+sharedToolexec, "-o", binary, ".") |
| 250 |
build.Dir = dir |
| 251 |
build.Env = buildEnv(symbolsDir, multiPackageModule, "ANTITHESIS_SKIP_CATALOG=1") |
| 252 |
if out, err := build.CombinedOutput(); err != nil { |
| 253 |
t.Fatalf("building the instrumented program failed: %v\n%s", err, out) |
| 254 |
} |
| 255 |
return binary, symbolsDir |
| 256 |
} |
| 257 |
|
| 258 |
// copyTree copies the fixture's Go sources, preserving directory structure. Only |
| 259 |
// .go files are copied: cases.json and expected/ are the test's own data. |
| 260 |
func copyTree(t *testing.T, from, to string) { |
| 261 |
t.Helper() |
| 262 |
err := filepath.WalkDir(from, func(path string, entry os.DirEntry, err error) error { |
| 263 |
if err != nil || entry.IsDir() || !strings.HasSuffix(path, ".go") { |
| 264 |
return err |
| 265 |
} |
| 266 |
rel, err := filepath.Rel(from, path) |
| 267 |
if err != nil { |
| 268 |
return err |
| 269 |
} |
| 270 |
target := filepath.Join(to, rel) |
| 271 |
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { |
| 272 |
return err |
| 273 |
} |
| 274 |
body, err := os.ReadFile(path) |
| 275 |
if err != nil { |
| 276 |
return err |
| 277 |
} |
| 278 |
return os.WriteFile(target, body, 0o644) |
| 279 |
}) |
| 280 |
qt.Assert(t, qt.IsNil(err)) |
| 281 |
} |
| 282 |
|
| 283 |
func loadMultiPackageCases(t *testing.T) multiPackageCases { |
| 284 |
t.Helper() |
| 285 |
body, err := os.ReadFile(filepath.Join(multiPackageDir, "cases.json")) |
| 286 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("missing cases.json; run with -update")) |
| 287 |
var cases multiPackageCases |
| 288 |
qt.Assert(t, qt.IsNil(json.Unmarshal(body, &cases))) |
| 289 |
qt.Assert(t, qt.IsTrue(len(cases.Cases) > 0)) |
| 290 |
return cases |
| 291 |
} |
| 292 |
|
| 293 |
func writeMultiPackageCases(t *testing.T, cases multiPackageCases) { |
| 294 |
t.Helper() |
| 295 |
body, err := json.MarshalIndent(cases, "", " ") |
| 296 |
qt.Assert(t, qt.IsNil(err)) |
| 297 |
writeGolden(t, filepath.Join(multiPackageDir, "cases.json"), string(body)+"\n") |
| 298 |
} |
| 299 |
|
| 300 |
func writeGolden(t *testing.T, path, content string) { |
| 301 |
t.Helper() |
| 302 |
qt.Assert(t, qt.IsNil(os.MkdirAll(filepath.Dir(path), 0o755))) |
| 303 |
qt.Assert(t, qt.IsNil(os.WriteFile(path, []byte(content), 0o644))) |
| 304 |
} |
| 305 |
|
| 306 |
// sequence returns 1..n, the edge ids a package's module must use. |
| 307 |
func sequence(n int) []int { |
| 308 |
ids := make([]int, 0, n) |
| 309 |
for i := 1; i <= n; i++ { |
| 310 |
ids = append(ids, i) |
| 311 |
} |
| 312 |
return ids |
| 313 |
} |