|
1 | 1 | // Copyright (c) Tailscale Inc & AUTHORS
|
2 | 2 | // SPDX-License-Identifier: BSD-3-Clause
|
3 | 3 |
|
4 |
| -// testwrapper is a wrapper for retrying flaky tests, using the -exec flag of |
5 |
| -// 'go test'. Tests that are flaky can use the 'flakytest' subpackage to mark |
6 |
| -// themselves as flaky and be retried on failure. |
| 4 | +// testwrapper is a wrapper for retrying flaky tests. It is an alternative to |
| 5 | +// `go test` and re-runs failed marked flaky tests (using the flakytest pkg). It |
| 6 | +// takes different arguments than go test and requires the first positional |
| 7 | +// argument to be the pattern to test. |
7 | 8 | package main
|
8 | 9 |
|
9 | 10 | import (
|
| 11 | + "bytes" |
10 | 12 | "context"
|
| 13 | + "encoding/json" |
11 | 14 | "errors"
|
| 15 | + "flag" |
| 16 | + "fmt" |
| 17 | + "io" |
12 | 18 | "log"
|
13 | 19 | "os"
|
14 | 20 | "os/exec"
|
15 |
| -) |
| 21 | + "sort" |
| 22 | + "strings" |
| 23 | + "time" |
16 | 24 |
|
17 |
| -const ( |
18 |
| - retryStatus = 123 |
19 |
| - maxIterations = 3 |
| 25 | + "golang.org/x/exp/maps" |
| 26 | + "tailscale.com/cmd/testwrapper/flakytest" |
20 | 27 | )
|
21 | 28 |
|
22 |
| -func main() { |
23 |
| - ctx := context.Background() |
24 |
| - debug := os.Getenv("TS_TESTWRAPPER_DEBUG") != "" |
| 29 | +const maxAttempts = 3 |
| 30 | + |
| 31 | +type testAttempt struct { |
| 32 | + name testName |
| 33 | + outcome string // "pass", "fail", "skip" |
| 34 | + logs bytes.Buffer |
| 35 | + isMarkedFlaky bool // set if the test is marked as flaky |
| 36 | +} |
| 37 | + |
| 38 | +type testName struct { |
| 39 | + pkg string // "tailscale.com/types/key" |
| 40 | + name string // "TestFoo" |
| 41 | +} |
| 42 | + |
| 43 | +type packageTests struct { |
| 44 | + // pattern is the package pattern to run. |
| 45 | + // Must be a single pattern, not a list of patterns. |
| 46 | + pattern string // "./...", "./types/key" |
| 47 | + // tests is a list of tests to run. If empty, all tests in the package are |
| 48 | + // run. |
| 49 | + tests []string // ["TestFoo", "TestBar"] |
| 50 | +} |
| 51 | + |
| 52 | +type goTestOutput struct { |
| 53 | + Time time.Time |
| 54 | + Action string |
| 55 | + Package string |
| 56 | + Test string |
| 57 | + Output string |
| 58 | +} |
| 59 | + |
| 60 | +var debug = os.Getenv("TS_TESTWRAPPER_DEBUG") != "" |
25 | 61 |
|
26 |
| - log.SetPrefix("testwrapper: ") |
27 |
| - if !debug { |
28 |
| - log.SetFlags(0) |
| 62 | +func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []string) []*testAttempt { |
| 63 | + args := []string{"test", "-json", pt.pattern} |
| 64 | + args = append(args, otherArgs...) |
| 65 | + if len(pt.tests) > 0 { |
| 66 | + runArg := strings.Join(pt.tests, "|") |
| 67 | + args = append(args, "-run", runArg) |
29 | 68 | }
|
| 69 | + if debug { |
| 70 | + fmt.Println("running", strings.Join(args, " ")) |
| 71 | + } |
| 72 | + cmd := exec.CommandContext(ctx, "go", args...) |
| 73 | + r, err := cmd.StdoutPipe() |
| 74 | + if err != nil { |
| 75 | + log.Printf("error creating stdout pipe: %v", err) |
| 76 | + } |
| 77 | + cmd.Stderr = os.Stderr |
| 78 | + |
| 79 | + cmd.Env = os.Environ() |
| 80 | + cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%d", flakytest.FlakeAttemptEnv, attempt)) |
30 | 81 |
|
31 |
| - for i := 1; i <= maxIterations; i++ { |
32 |
| - if i > 1 { |
33 |
| - log.Printf("retrying flaky tests (%d of %d)", i, maxIterations) |
| 82 | + if err := cmd.Start(); err != nil { |
| 83 | + log.Printf("error starting test: %v", err) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + done := make(chan struct{}) |
| 87 | + go func() { |
| 88 | + defer close(done) |
| 89 | + cmd.Wait() |
| 90 | + }() |
| 91 | + |
| 92 | + jd := json.NewDecoder(r) |
| 93 | + resultMap := make(map[testName]*testAttempt) |
| 94 | + var out []*testAttempt |
| 95 | + for { |
| 96 | + var goOutput goTestOutput |
| 97 | + if err := jd.Decode(&goOutput); err != nil { |
| 98 | + if errors.Is(err, io.EOF) || errors.Is(err, os.ErrClosed) { |
| 99 | + break |
| 100 | + } |
| 101 | + panic(err) |
34 | 102 | }
|
35 |
| - cmd := exec.CommandContext(ctx, os.Args[1], os.Args[2:]...) |
36 |
| - cmd.Stdout = os.Stdout |
37 |
| - cmd.Stderr = os.Stderr |
38 |
| - cmd.Env = append(os.Environ(), "TS_IN_TESTWRAPPER=1") |
39 |
| - err := cmd.Run() |
40 |
| - if err == nil { |
41 |
| - return |
| 103 | + if goOutput.Test == "" { |
| 104 | + continue |
| 105 | + } |
| 106 | + name := testName{ |
| 107 | + pkg: goOutput.Package, |
| 108 | + name: goOutput.Test, |
| 109 | + } |
| 110 | + if test, _, isSubtest := strings.Cut(goOutput.Test, "/"); isSubtest { |
| 111 | + name.name = test |
| 112 | + if goOutput.Action == "output" { |
| 113 | + resultMap[name].logs.WriteString(goOutput.Output) |
| 114 | + } |
| 115 | + continue |
| 116 | + } |
| 117 | + switch goOutput.Action { |
| 118 | + case "start": |
| 119 | + // ignore |
| 120 | + case "run": |
| 121 | + resultMap[name] = &testAttempt{ |
| 122 | + name: name, |
| 123 | + } |
| 124 | + case "skip", "pass", "fail": |
| 125 | + resultMap[name].outcome = goOutput.Action |
| 126 | + out = append(out, resultMap[name]) |
| 127 | + case "output": |
| 128 | + if strings.TrimSpace(goOutput.Output) == flakytest.FlakyTestLogMessage { |
| 129 | + resultMap[name].isMarkedFlaky = true |
| 130 | + } else { |
| 131 | + resultMap[name].logs.WriteString(goOutput.Output) |
| 132 | + } |
42 | 133 | }
|
| 134 | + } |
| 135 | + <-done |
| 136 | + return out |
| 137 | +} |
| 138 | + |
| 139 | +func main() { |
| 140 | + ctx := context.Background() |
| 141 | + |
| 142 | + // We only need to parse the -v flag to figure out whether to print the logs |
| 143 | + // for a test. We don't need to parse any other flags, so we just use the |
| 144 | + // flag package to parse the -v flag and then pass the rest of the args |
| 145 | + // through to 'go test'. |
| 146 | + // We run `go test -json` which returns the same information as `go test -v`, |
| 147 | + // but in a machine-readable format. So this flag is only for testwrapper's |
| 148 | + // output. |
| 149 | + v := flag.Bool("v", false, "verbose") |
| 150 | + |
| 151 | + flag.Usage = func() { |
| 152 | + fmt.Println("usage: testwrapper [testwrapper-flags] [pattern] [build/test flags & test binary flags]") |
| 153 | + fmt.Println() |
| 154 | + fmt.Println("testwrapper-flags:") |
| 155 | + flag.CommandLine.PrintDefaults() |
| 156 | + fmt.Println() |
| 157 | + fmt.Println("examples:") |
| 158 | + fmt.Println("\ttestwrapper -v ./... -count=1") |
| 159 | + fmt.Println("\ttestwrapper ./pkg/foo -run TestBar -count=1") |
| 160 | + fmt.Println() |
| 161 | + fmt.Println("Unlike 'go test', testwrapper requires a package pattern as the first positional argument and only supports a single pattern.") |
| 162 | + } |
| 163 | + flag.Parse() |
| 164 | + |
| 165 | + args := flag.Args() |
| 166 | + if len(args) < 1 || strings.HasPrefix(args[0], "-") { |
| 167 | + fmt.Println("no pattern specified") |
| 168 | + flag.Usage() |
| 169 | + os.Exit(1) |
| 170 | + } else if len(args) > 1 && !strings.HasPrefix(args[1], "-") { |
| 171 | + fmt.Println("expected single pattern") |
| 172 | + flag.Usage() |
| 173 | + os.Exit(1) |
| 174 | + } |
| 175 | + pattern, otherArgs := args[0], args[1:] |
| 176 | + |
| 177 | + toRun := []*packageTests{ // packages still to test |
| 178 | + {pattern: pattern}, |
| 179 | + } |
43 | 180 |
|
44 |
| - var exitErr *exec.ExitError |
45 |
| - if !errors.As(err, &exitErr) { |
46 |
| - if debug { |
47 |
| - log.Printf("error isn't an ExitError") |
| 181 | + pkgAttempts := make(map[string]int) // tracks how many times we've tried a package |
| 182 | + |
| 183 | + attempt := 0 |
| 184 | + for len(toRun) > 0 { |
| 185 | + attempt++ |
| 186 | + var pt *packageTests |
| 187 | + pt, toRun = toRun[0], toRun[1:] |
| 188 | + |
| 189 | + toRetry := make(map[string][]string) // pkg -> tests to retry |
| 190 | + |
| 191 | + failed := false |
| 192 | + for _, tr := range runTests(ctx, attempt, pt, otherArgs) { |
| 193 | + if *v || tr.outcome == "fail" { |
| 194 | + io.Copy(os.Stderr, &tr.logs) |
48 | 195 | }
|
| 196 | + if tr.outcome != "fail" { |
| 197 | + continue |
| 198 | + } |
| 199 | + if tr.isMarkedFlaky { |
| 200 | + toRetry[tr.name.pkg] = append(toRetry[tr.name.pkg], tr.name.name) |
| 201 | + } else { |
| 202 | + failed = true |
| 203 | + } |
| 204 | + } |
| 205 | + if failed { |
49 | 206 | os.Exit(1)
|
50 | 207 | }
|
51 |
| - |
52 |
| - if code := exitErr.ExitCode(); code != retryStatus { |
53 |
| - if debug { |
54 |
| - log.Printf("code (%d) != retryStatus (%d)", code, retryStatus) |
| 208 | + pkgs := maps.Keys(toRetry) |
| 209 | + sort.Strings(pkgs) |
| 210 | + for _, pkg := range pkgs { |
| 211 | + tests := toRetry[pkg] |
| 212 | + sort.Strings(tests) |
| 213 | + pkgAttempts[pkg]++ |
| 214 | + if pkgAttempts[pkg] >= maxAttempts { |
| 215 | + fmt.Println("Too many attempts for flaky tests:", pkg, tests) |
| 216 | + continue |
55 | 217 | }
|
56 |
| - os.Exit(code) |
| 218 | + fmt.Println("\nRetrying flaky tests:", pkg, tests) |
| 219 | + toRun = append(toRun, &packageTests{ |
| 220 | + pattern: pkg, |
| 221 | + tests: tests, |
| 222 | + }) |
57 | 223 | }
|
58 | 224 | }
|
59 |
| - |
60 |
| - log.Printf("test did not pass in %d iterations", maxIterations) |
61 |
| - os.Exit(1) |
| 225 | + for _, a := range pkgAttempts { |
| 226 | + if a >= maxAttempts { |
| 227 | + os.Exit(1) |
| 228 | + } |
| 229 | + } |
| 230 | + fmt.Println("PASS") |
62 | 231 | }
|
0 commit comments