2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2023-01-18 16:41:58 +00:00
|
|
|
|
2023-02-15 01:57:02 +00:00
|
|
|
// testwrapper is a wrapper for retrying flaky tests. It is an alternative to
|
|
|
|
// `go test` and re-runs failed marked flaky tests (using the flakytest pkg). It
|
|
|
|
// takes different arguments than go test and requires the first positional
|
|
|
|
// argument to be the pattern to test.
|
2023-01-18 16:41:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-07 16:18:26 +00:00
|
|
|
"bufio"
|
2023-02-15 01:57:02 +00:00
|
|
|
"bytes"
|
2023-01-18 16:41:58 +00:00
|
|
|
"context"
|
2023-02-15 01:57:02 +00:00
|
|
|
"encoding/json"
|
2023-01-18 16:41:58 +00:00
|
|
|
"errors"
|
2023-02-15 01:57:02 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-01-18 16:41:58 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-10-04 02:12:37 +00:00
|
|
|
"slices"
|
2023-02-15 01:57:02 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-01-18 16:41:58 +00:00
|
|
|
|
2023-08-17 16:40:19 +00:00
|
|
|
xmaps "golang.org/x/exp/maps"
|
2023-02-15 01:57:02 +00:00
|
|
|
"tailscale.com/cmd/testwrapper/flakytest"
|
2023-01-18 16:41:58 +00:00
|
|
|
)
|
|
|
|
|
2023-02-15 01:57:02 +00:00
|
|
|
const maxAttempts = 3
|
|
|
|
|
|
|
|
type testAttempt struct {
|
2023-09-04 21:56:08 +00:00
|
|
|
pkg string // "tailscale.com/types/key"
|
|
|
|
testName string // "TestFoo"
|
2023-02-15 01:57:02 +00:00
|
|
|
outcome string // "pass", "fail", "skip"
|
|
|
|
logs bytes.Buffer
|
2023-09-28 23:30:12 +00:00
|
|
|
isMarkedFlaky bool // set if the test is marked as flaky
|
|
|
|
issueURL string // set if the test is marked as flaky
|
2023-06-30 19:21:52 +00:00
|
|
|
|
|
|
|
pkgFinished bool
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 23:30:12 +00:00
|
|
|
// packageTests describes what to run.
|
|
|
|
// It's also JSON-marshalled to output for analysys tools to parse
|
|
|
|
// so the fields are all exported.
|
|
|
|
// TODO(bradfitz): move this type to its own types package?
|
2023-02-15 01:57:02 +00:00
|
|
|
type packageTests struct {
|
2023-09-28 21:08:56 +00:00
|
|
|
// Pattern is the package Pattern to run.
|
|
|
|
// Must be a single Pattern, not a list of patterns.
|
|
|
|
Pattern string // "./...", "./types/key"
|
|
|
|
// Tests is a list of Tests to run. If empty, all Tests in the package are
|
2023-02-15 01:57:02 +00:00
|
|
|
// run.
|
2023-09-28 21:08:56 +00:00
|
|
|
Tests []string // ["TestFoo", "TestBar"]
|
2023-09-28 23:30:12 +00:00
|
|
|
// IssueURLs maps from a test name to a URL tracking its flake.
|
|
|
|
IssueURLs map[string]string // "TestFoo" => "https://github.com/foo/bar/issue/123"
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type goTestOutput struct {
|
|
|
|
Time time.Time
|
|
|
|
Action string
|
|
|
|
Package string
|
|
|
|
Test string
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
|
|
|
var debug = os.Getenv("TS_TESTWRAPPER_DEBUG") != ""
|
2023-01-18 16:41:58 +00:00
|
|
|
|
2023-06-30 19:21:52 +00:00
|
|
|
// runTests runs the tests in pt and sends the results on ch. It sends a
|
|
|
|
// testAttempt for each test and a final testAttempt per pkg with pkgFinished
|
2023-09-07 16:18:26 +00:00
|
|
|
// set to true. Package build errors will not emit a testAttempt (as no valid
|
|
|
|
// JSON is produced) but the [os/exec.ExitError] will be returned.
|
2023-06-30 19:21:52 +00:00
|
|
|
// It calls close(ch) when it's done.
|
2023-09-07 16:18:26 +00:00
|
|
|
func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []string, ch chan<- *testAttempt) error {
|
2023-06-30 19:21:52 +00:00
|
|
|
defer close(ch)
|
2023-09-28 21:08:56 +00:00
|
|
|
args := []string{"test", "-json", pt.Pattern}
|
2023-02-15 01:57:02 +00:00
|
|
|
args = append(args, otherArgs...)
|
2023-09-28 21:08:56 +00:00
|
|
|
if len(pt.Tests) > 0 {
|
|
|
|
runArg := strings.Join(pt.Tests, "|")
|
2023-02-15 01:57:02 +00:00
|
|
|
args = append(args, "-run", runArg)
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
if debug {
|
|
|
|
fmt.Println("running", strings.Join(args, " "))
|
|
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, "go", args...)
|
|
|
|
r, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating stdout pipe: %v", err)
|
|
|
|
}
|
2023-07-02 16:01:53 +00:00
|
|
|
defer r.Close()
|
2023-02-15 01:57:02 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%d", flakytest.FlakeAttemptEnv, attempt))
|
2023-01-18 16:41:58 +00:00
|
|
|
|
2023-02-15 01:57:02 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Printf("error starting test: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:18:26 +00:00
|
|
|
s := bufio.NewScanner(r)
|
2023-09-04 21:56:08 +00:00
|
|
|
resultMap := make(map[string]map[string]*testAttempt) // pkg -> test -> testAttempt
|
2023-09-07 16:18:26 +00:00
|
|
|
for s.Scan() {
|
2023-02-15 01:57:02 +00:00
|
|
|
var goOutput goTestOutput
|
2023-09-07 16:18:26 +00:00
|
|
|
if err := json.Unmarshal(s.Bytes(), &goOutput); err != nil {
|
2023-02-15 01:57:02 +00:00
|
|
|
if errors.Is(err, io.EOF) || errors.Is(err, os.ErrClosed) {
|
|
|
|
break
|
|
|
|
}
|
2023-07-02 16:01:53 +00:00
|
|
|
|
|
|
|
// `go test -json` outputs invalid JSON when a build fails.
|
|
|
|
// In that case, discard the the output and start reading again.
|
|
|
|
// The build error will be printed to stderr.
|
|
|
|
// See: https://github.com/golang/go/issues/35169
|
|
|
|
if _, ok := err.(*json.SyntaxError); ok {
|
2023-09-07 16:18:26 +00:00
|
|
|
fmt.Println(s.Text())
|
2023-07-02 16:01:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
panic(err)
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
2023-09-04 21:56:08 +00:00
|
|
|
pkg := goOutput.Package
|
|
|
|
pkgTests := resultMap[pkg]
|
2023-02-15 01:57:02 +00:00
|
|
|
if goOutput.Test == "" {
|
2023-06-30 19:21:52 +00:00
|
|
|
switch goOutput.Action {
|
|
|
|
case "fail", "pass", "skip":
|
2023-09-04 21:56:08 +00:00
|
|
|
for _, test := range pkgTests {
|
|
|
|
if test.outcome == "" {
|
|
|
|
test.outcome = "fail"
|
|
|
|
ch <- test
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 19:21:52 +00:00
|
|
|
ch <- &testAttempt{
|
2023-09-04 21:56:08 +00:00
|
|
|
pkg: goOutput.Package,
|
2023-06-30 19:21:52 +00:00
|
|
|
outcome: goOutput.Action,
|
|
|
|
pkgFinished: true,
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-09-04 21:56:08 +00:00
|
|
|
if pkgTests == nil {
|
|
|
|
pkgTests = make(map[string]*testAttempt)
|
|
|
|
resultMap[pkg] = pkgTests
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
2023-09-04 21:56:08 +00:00
|
|
|
testName := goOutput.Test
|
2023-02-15 01:57:02 +00:00
|
|
|
if test, _, isSubtest := strings.Cut(goOutput.Test, "/"); isSubtest {
|
2023-09-04 21:56:08 +00:00
|
|
|
testName = test
|
2023-02-15 01:57:02 +00:00
|
|
|
if goOutput.Action == "output" {
|
2023-09-04 21:56:08 +00:00
|
|
|
resultMap[pkg][testName].logs.WriteString(goOutput.Output)
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch goOutput.Action {
|
|
|
|
case "start":
|
|
|
|
// ignore
|
|
|
|
case "run":
|
2023-09-04 21:56:08 +00:00
|
|
|
pkgTests[testName] = &testAttempt{
|
|
|
|
pkg: pkg,
|
|
|
|
testName: testName,
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
|
|
|
case "skip", "pass", "fail":
|
2023-09-04 21:56:08 +00:00
|
|
|
pkgTests[testName].outcome = goOutput.Action
|
|
|
|
ch <- pkgTests[testName]
|
2023-02-15 01:57:02 +00:00
|
|
|
case "output":
|
2023-09-28 23:30:12 +00:00
|
|
|
if suffix, ok := strings.CutPrefix(strings.TrimSpace(goOutput.Output), flakytest.FlakyTestLogMessage); ok {
|
2023-09-04 21:56:08 +00:00
|
|
|
pkgTests[testName].isMarkedFlaky = true
|
2023-09-28 23:30:12 +00:00
|
|
|
pkgTests[testName].issueURL = strings.TrimPrefix(suffix, ": ")
|
2023-02-15 01:57:02 +00:00
|
|
|
} else {
|
2023-09-04 21:56:08 +00:00
|
|
|
pkgTests[testName].logs.WriteString(goOutput.Output)
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
2023-09-11 18:06:11 +00:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2023-09-07 16:18:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-11 18:06:11 +00:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return fmt.Errorf("reading go test stdout: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// We only need to parse the -v flag to figure out whether to print the logs
|
|
|
|
// for a test. We don't need to parse any other flags, so we just use the
|
|
|
|
// flag package to parse the -v flag and then pass the rest of the args
|
|
|
|
// through to 'go test'.
|
|
|
|
// We run `go test -json` which returns the same information as `go test -v`,
|
|
|
|
// but in a machine-readable format. So this flag is only for testwrapper's
|
|
|
|
// output.
|
|
|
|
v := flag.Bool("v", false, "verbose")
|
|
|
|
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Println("usage: testwrapper [testwrapper-flags] [pattern] [build/test flags & test binary flags]")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("testwrapper-flags:")
|
|
|
|
flag.CommandLine.PrintDefaults()
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("examples:")
|
|
|
|
fmt.Println("\ttestwrapper -v ./... -count=1")
|
|
|
|
fmt.Println("\ttestwrapper ./pkg/foo -run TestBar -count=1")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Unlike 'go test', testwrapper requires a package pattern as the first positional argument and only supports a single pattern.")
|
|
|
|
}
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) < 1 || strings.HasPrefix(args[0], "-") {
|
|
|
|
fmt.Println("no pattern specified")
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
} else if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
|
|
|
|
fmt.Println("expected single pattern")
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
pattern, otherArgs := args[0], args[1:]
|
|
|
|
|
2023-06-30 16:31:07 +00:00
|
|
|
type nextRun struct {
|
|
|
|
tests []*packageTests
|
2023-09-28 21:08:56 +00:00
|
|
|
attempt int // starting at 1
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
2023-01-18 16:41:58 +00:00
|
|
|
|
2023-06-30 16:31:07 +00:00
|
|
|
toRun := []*nextRun{
|
|
|
|
{
|
2023-09-28 21:08:56 +00:00
|
|
|
tests: []*packageTests{{Pattern: pattern}},
|
2023-06-30 16:31:07 +00:00
|
|
|
attempt: 1,
|
|
|
|
},
|
|
|
|
}
|
2023-06-30 19:21:52 +00:00
|
|
|
printPkgOutcome := func(pkg, outcome string, attempt int) {
|
|
|
|
if outcome == "skip" {
|
|
|
|
fmt.Printf("?\t%s [skipped/no tests] \n", pkg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if outcome == "pass" {
|
|
|
|
outcome = "ok"
|
2023-06-30 16:31:07 +00:00
|
|
|
}
|
2023-06-30 19:21:52 +00:00
|
|
|
if outcome == "fail" {
|
|
|
|
outcome = "FAIL"
|
|
|
|
}
|
|
|
|
if attempt > 1 {
|
|
|
|
fmt.Printf("%s\t%s [attempt=%d]\n", outcome, pkg, attempt)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\t%s\n", outcome, pkg)
|
2023-06-30 16:31:07 +00:00
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
|
|
|
|
for len(toRun) > 0 {
|
2023-06-30 16:31:07 +00:00
|
|
|
var thisRun *nextRun
|
|
|
|
thisRun, toRun = toRun[0], toRun[1:]
|
2023-02-15 01:57:02 +00:00
|
|
|
|
2023-08-24 20:48:22 +00:00
|
|
|
if thisRun.attempt > maxAttempts {
|
2023-06-30 16:31:07 +00:00
|
|
|
fmt.Println("max attempts reached")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if thisRun.attempt > 1 {
|
2023-09-28 21:08:56 +00:00
|
|
|
j, _ := json.Marshal(thisRun.tests)
|
2023-09-28 23:30:12 +00:00
|
|
|
fmt.Printf("\n\nAttempt #%d: Retrying flaky tests:\n\nflakytest failures JSON: %s\n\n", thisRun.attempt, j)
|
2023-06-30 16:31:07 +00:00
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
|
2023-09-28 23:30:12 +00:00
|
|
|
toRetry := make(map[string][]*testAttempt) // pkg -> tests to retry
|
2023-06-30 16:31:07 +00:00
|
|
|
for _, pt := range thisRun.tests {
|
2023-06-30 19:21:52 +00:00
|
|
|
ch := make(chan *testAttempt)
|
2023-09-07 16:18:26 +00:00
|
|
|
runErr := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(runErr)
|
|
|
|
runErr <- runTests(ctx, thisRun.attempt, pt, otherArgs, ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var failed bool
|
2023-06-30 19:21:52 +00:00
|
|
|
for tr := range ch {
|
2023-09-07 16:18:26 +00:00
|
|
|
// Go assigns the package name "command-line-arguments" when you
|
|
|
|
// `go test FILE` rather than `go test PKG`. It's more
|
|
|
|
// convenient for us to to specify files in tests, so fix tr.pkg
|
|
|
|
// so that subsequent testwrapper attempts run correctly.
|
|
|
|
if tr.pkg == "command-line-arguments" {
|
|
|
|
tr.pkg = pattern
|
|
|
|
}
|
2023-06-30 19:21:52 +00:00
|
|
|
if tr.pkgFinished {
|
2023-09-04 21:56:08 +00:00
|
|
|
if tr.outcome == "fail" && len(toRetry[tr.pkg]) == 0 {
|
2023-08-30 01:03:13 +00:00
|
|
|
// If a package fails and we don't have any tests to
|
|
|
|
// retry, then we should fail. This typically happens
|
|
|
|
// when a package times out.
|
|
|
|
failed = true
|
|
|
|
}
|
2023-09-04 21:56:08 +00:00
|
|
|
printPkgOutcome(tr.pkg, tr.outcome, thisRun.attempt)
|
2023-06-30 19:21:52 +00:00
|
|
|
continue
|
2023-06-30 16:31:07 +00:00
|
|
|
}
|
|
|
|
if *v || tr.outcome == "fail" {
|
|
|
|
io.Copy(os.Stdout, &tr.logs)
|
|
|
|
}
|
|
|
|
if tr.outcome != "fail" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tr.isMarkedFlaky {
|
2023-09-28 23:30:12 +00:00
|
|
|
toRetry[tr.pkg] = append(toRetry[tr.pkg], tr)
|
2023-06-30 16:31:07 +00:00
|
|
|
} else {
|
|
|
|
failed = true
|
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
}
|
2023-09-07 16:18:26 +00:00
|
|
|
if failed {
|
|
|
|
fmt.Println("\n\nNot retrying flaky tests because non-flaky tests failed.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's nothing to retry and no non-retryable tests have
|
|
|
|
// failed then we've probably hit a build error.
|
|
|
|
if err := <-runErr; len(toRetry) == 0 && err != nil {
|
|
|
|
var exit *exec.ExitError
|
|
|
|
if errors.As(err, &exit) {
|
|
|
|
if code := exit.ExitCode(); code > -1 {
|
|
|
|
os.Exit(exit.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 18:06:11 +00:00
|
|
|
log.Printf("testwrapper: %s", err)
|
|
|
|
os.Exit(1)
|
2023-09-07 16:18:26 +00:00
|
|
|
}
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
2023-06-30 16:31:07 +00:00
|
|
|
if len(toRetry) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-17 16:40:19 +00:00
|
|
|
pkgs := xmaps.Keys(toRetry)
|
2023-02-15 01:57:02 +00:00
|
|
|
sort.Strings(pkgs)
|
2023-06-30 16:31:07 +00:00
|
|
|
nextRun := &nextRun{
|
|
|
|
attempt: thisRun.attempt + 1,
|
|
|
|
}
|
2023-02-15 01:57:02 +00:00
|
|
|
for _, pkg := range pkgs {
|
|
|
|
tests := toRetry[pkg]
|
2023-09-28 23:30:12 +00:00
|
|
|
slices.SortFunc(tests, func(a, b *testAttempt) int { return strings.Compare(a.testName, b.testName) })
|
|
|
|
issueURLs := map[string]string{} // test name => URL
|
|
|
|
var testNames []string
|
|
|
|
for _, ta := range tests {
|
|
|
|
issueURLs[ta.testName] = ta.issueURL
|
|
|
|
testNames = append(testNames, ta.testName)
|
|
|
|
}
|
2023-06-30 16:31:07 +00:00
|
|
|
nextRun.tests = append(nextRun.tests, &packageTests{
|
2023-09-28 23:30:12 +00:00
|
|
|
Pattern: pkg,
|
|
|
|
Tests: testNames,
|
|
|
|
IssueURLs: issueURLs,
|
2023-02-15 01:57:02 +00:00
|
|
|
})
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
2023-06-30 16:31:07 +00:00
|
|
|
toRun = append(toRun, nextRun)
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|
|
|
|
}
|