cmd/testwrapper: output machine-readable JSON on test flakes

For parsing by other tools.

Updates tailscale/corp#14975

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/9582/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent 5429ee2566
commit 2c92f94e2a

@ -40,12 +40,12 @@ type testAttempt struct {
} }
type packageTests struct { type packageTests struct {
// pattern is the package pattern to run. // Pattern is the package Pattern to run.
// Must be a single pattern, not a list of patterns. // Must be a single Pattern, not a list of patterns.
pattern string // "./...", "./types/key" Pattern string // "./...", "./types/key"
// tests is a list of tests to run. If empty, all tests in the package are // Tests is a list of Tests to run. If empty, all Tests in the package are
// run. // run.
tests []string // ["TestFoo", "TestBar"] Tests []string // ["TestFoo", "TestBar"]
} }
type goTestOutput struct { type goTestOutput struct {
@ -65,10 +65,10 @@ var debug = os.Getenv("TS_TESTWRAPPER_DEBUG") != ""
// It calls close(ch) when it's done. // It calls close(ch) when it's done.
func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []string, ch chan<- *testAttempt) error { func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []string, ch chan<- *testAttempt) error {
defer close(ch) defer close(ch)
args := []string{"test", "-json", pt.pattern} args := []string{"test", "-json", pt.Pattern}
args = append(args, otherArgs...) args = append(args, otherArgs...)
if len(pt.tests) > 0 { if len(pt.Tests) > 0 {
runArg := strings.Join(pt.tests, "|") runArg := strings.Join(pt.Tests, "|")
args = append(args, "-run", runArg) args = append(args, "-run", runArg)
} }
if debug { if debug {
@ -208,12 +208,12 @@ func main() {
type nextRun struct { type nextRun struct {
tests []*packageTests tests []*packageTests
attempt int attempt int // starting at 1
} }
toRun := []*nextRun{ toRun := []*nextRun{
{ {
tests: []*packageTests{{pattern: pattern}}, tests: []*packageTests{{Pattern: pattern}},
attempt: 1, attempt: 1,
}, },
} }
@ -245,6 +245,8 @@ func main() {
} }
if thisRun.attempt > 1 { if thisRun.attempt > 1 {
fmt.Printf("\n\nAttempt #%d: Retrying flaky tests:\n\n", thisRun.attempt) fmt.Printf("\n\nAttempt #%d: Retrying flaky tests:\n\n", thisRun.attempt)
j, _ := json.Marshal(thisRun.tests)
fmt.Printf("\n\nflakytest failures JSON: %s\n\n", j)
} }
toRetry := make(map[string][]string) // pkg -> tests to retry toRetry := make(map[string][]string) // pkg -> tests to retry
@ -317,8 +319,8 @@ func main() {
tests := toRetry[pkg] tests := toRetry[pkg]
sort.Strings(tests) sort.Strings(tests)
nextRun.tests = append(nextRun.tests, &packageTests{ nextRun.tests = append(nextRun.tests, &packageTests{
pattern: pkg, Pattern: pkg,
tests: tests, Tests: tests,
}) })
} }
toRun = append(toRun, nextRun) toRun = append(toRun, nextRun)

Loading…
Cancel
Save