| 1 | package testci_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/testci" |
| 13 | ) |
| 14 | |
| 15 | // loadCITest is a python prelude that loads scripts/ci-test.py as m. |
| 16 | const loadCITest = ` |
| 17 | import importlib.util, json, sys |
| 18 | spec = importlib.util.spec_from_file_location("citest", "../../scripts/ci-test.py") |
| 19 | m = importlib.util.module_from_spec(spec) |
| 20 | spec.loader.exec_module(m) |
| 21 | ` |
| 22 | |
| 23 | func python(t *testing.T, env []string, args ...string) string { |
| 24 | t.Helper() |
| 25 | cmd := exec.Command("python3", args...) |
| 26 | cmd.Env = append(os.Environ(), env...) |
| 27 | out, err := cmd.CombinedOutput() |
| 28 | if err != nil { |
| 29 | t.Fatalf("python3 %v: %v: %s", args, err, out) |
| 30 | } |
| 31 | return string(out) |
| 32 | } |
| 33 | |
| 34 | // The retry reads which tests failed from what rastrillo budget test |
| 35 | // prints, and retries the deepest failed subtests: a parent fails only |
| 36 | // because one of them did. A test that never finished counts; go test's |
| 37 | // own "--- FAIL:" lines inside a test's output do not name a package and |
| 38 | // are not read as failures. |
| 39 | func TestRetryNamesTheFailedTests(t *testing.T) { |
| 40 | testci.Files(t, "../../scripts/ci-test.py") |
| 41 | report := strings.Join([]string{ |
| 42 | "--- FAIL amadan.net/oficina/equipo/internal/headless TestHeadless", |
| 43 | "--- FAIL: TestHeadless (45.10s)", |
| 44 | "--- FAIL amadan.net/oficina/equipo/internal/headless TestHeadless/1_first_token", |
| 45 | " --- FAIL: TestHeadless/1_first_token (45.02s)", |
| 46 | "--- FAIL amadan.net/oficina/equipo/internal/runner TestStuck (never finished)", |
| 47 | "--- FAIL amadan.net/oficina/equipo/internal/runner (package output)", |
| 48 | "panic: test timed out", |
| 49 | "--- FAIL amadan.net/oficina/equipo/internal/wire (package output)", |
| 50 | "setup failed", |
| 51 | "", |
| 52 | }, "\n") |
| 53 | out := python(t, nil, "-c", loadCITest+` |
| 54 | out = json.loads(sys.argv[1]) |
| 55 | found = {p: m.leaves(ts) for p, ts in m.failures(out).items()} |
| 56 | print(json.dumps({"found": found, "seconds": m.failed_seconds(out, "TestHeadless/1_first_token"), "unknown": m.failed_seconds(out, "TestStuck")})) |
| 57 | `, mustJSON(t, report)) |
| 58 | var got struct { |
| 59 | Found map[string][]string |
| 60 | Seconds, Unknown string |
| 61 | } |
| 62 | if err := json.Unmarshal([]byte(out), &got); err != nil { |
| 63 | t.Fatalf("%v: %s", err, out) |
| 64 | } |
| 65 | want := map[string][]string{ |
| 66 | "./internal/headless": {"TestHeadless/1_first_token"}, |
| 67 | "./internal/runner": {"TestStuck"}, |
| 68 | "./internal/wire": {}, |
| 69 | } |
| 70 | if !reflect.DeepEqual(got.Found, want) { |
| 71 | t.Errorf("failed tests = %v, want %v", got.Found, want) |
| 72 | } |
| 73 | if got.Seconds != "45.02s" || got.Unknown != "unknown" { |
| 74 | t.Errorf("failed attempt took %q and %q, want 45.02s and unknown", got.Seconds, got.Unknown) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func mustJSON(t *testing.T, v any) string { |
| 79 | t.Helper() |
| 80 | raw, err := json.Marshal(v) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | return string(raw) |
| 85 | } |
| 86 | |
| 87 | // A FLAKE line as scripts/ci-test.py prints it is what |
| 88 | // scripts/ci-flakes.py reads back. Three flakes of one test inside the |
| 89 | // week make it a repeat offender, with the quarantine line and the amadan |
| 90 | // commands to file; two do not. |
| 91 | func TestFlakeLinesAreReadBack(t *testing.T) { |
| 92 | testci.Files(t, "../../scripts/ci-test.py", "../../scripts/ci-flakes.py", "../../scripts/ci-quarantine.txt") |
| 93 | dir := t.TempDir() |
| 94 | var logs []string |
| 95 | for i, commit := range []string{strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("c", 40)} { |
| 96 | line := python(t, []string{"AMADAN_COMMIT=" + commit}, "-c", loadCITest+` |
| 97 | print("some output before") |
| 98 | print(m.flake_line("./internal/headless", "TestHeadless/1_first_token", "headless-rest", "45.02s", "31.50")) |
| 99 | print(m.flake_line("./internal/thintest", "TestOnce", "thintest", "unknown", "unknown")) if sys.argv[1] == "0" else None |
| 100 | `, string(rune('0'+i))) |
| 101 | if !strings.Contains(line, "\nFLAKE ./internal/headless:TestHeadless/1_first_token headless-rest "+commit+" 45.02s 31.50\n") { |
| 102 | t.Fatalf("the FLAKE line is not on a line of its own with its five fields:\n%s", line) |
| 103 | } |
| 104 | path := filepath.Join(dir, "run"+string(rune('0'+i))+".log") |
| 105 | if err := os.WriteFile(path, []byte(line), 0o644); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | logs = append(logs, "-log", path) |
| 109 | } |
| 110 | base := []string{"../../scripts/ci-flakes.py", "-no-fetch", "-history", filepath.Join(dir, "history.jsonl")} |
| 111 | |
| 112 | out := python(t, nil, append(base, logs...)...) |
| 113 | for _, want := range []string{ |
| 114 | "3 ./internal/headless TestHeadless/1_first_token", |
| 115 | "1 ./internal/thintest TestOnce", |
| 116 | "REPEAT OFFENDER ./internal/headless TestHeadless/1_first_token: 3 flakes in 7 days", |
| 117 | "amadan discuss new oficina/equipo -title 'Flaky test: ./internal/headless TestHeadless/1_first_token' -body -", |
| 118 | "amadan discuss type oficina/equipo <id> -type Issue", |
| 119 | "testci.Quarantine(t)", |
| 120 | } { |
| 121 | if !strings.Contains(out, want) { |
| 122 | t.Errorf("three flakes: output lacks %q:\n%s", want, out) |
| 123 | } |
| 124 | } |
| 125 | if strings.Contains(out, "REPEAT OFFENDER ./internal/thintest") { |
| 126 | t.Errorf("one flake made a repeat offender:\n%s", out) |
| 127 | } |
| 128 | |
| 129 | // The history holds what was read: the same logs again add nothing. |
| 130 | again := python(t, nil, append(base, logs...)...) |
| 131 | if !strings.Contains(again, "3 ./internal/headless TestHeadless/1_first_token") { |
| 132 | t.Errorf("reading the same runs twice changed the count:\n%s", again) |
| 133 | } |
| 134 | |
| 135 | two := python(t, nil, append([]string{"../../scripts/ci-flakes.py", "-no-fetch", "-history", filepath.Join(dir, "two.jsonl")}, logs[:4]...)...) |
| 136 | if !strings.Contains(two, "2 ./internal/headless TestHeadless/1_first_token") || strings.Contains(two, "REPEAT OFFENDER") { |
| 137 | t.Errorf("two flakes: want the count and no repeat offender:\n%s", two) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // A test process that prints PASS for every test and then exits non-zero |
| 142 | // (a panic in a cleanup, a TestMain that fails after m.Run) has failed. |
| 143 | // The retry is green only when its process exits 0 and every test it was |
| 144 | // asked for passed; otherwise the test failed twice and the step is red. |
| 145 | func TestRetryNeedsACleanExit(t *testing.T) { |
| 146 | testci.Files(t, "../../scripts/ci-test.py") |
| 147 | bin := t.TempDir() |
| 148 | // A fake go: every test passes, then the process dies as $FAKE_GO_EXIT says. |
| 149 | script := "#!/bin/sh\necho '=== RUN TestTeardown'\necho '--- PASS: TestTeardown (0.01s)'\necho PASS\nif [ \"$FAKE_GO_EXIT\" != 0 ]; then echo 'panic: cleanup failed after the test passed'; fi\nexit \"$FAKE_GO_EXIT\"\n" |
| 150 | if err := os.WriteFile(filepath.Join(bin, "go"), []byte(script), 0700); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | report := "--- FAIL amadan.net/oficina/equipo/internal/wire TestTeardown\n" |
| 154 | run := func(exit string) (int, string) { |
| 155 | out := python(t, []string{"PATH=" + bin + string(os.PathListSeparator) + os.Getenv("PATH"), "FAKE_GO_EXIT=" + exit}, "-c", loadCITest+` |
| 156 | import contextlib, io |
| 157 | buf = io.StringIO() |
| 158 | with contextlib.redirect_stdout(buf): |
| 159 | status = m.retry_failures(sys.argv[1], dict(__import__("os").environ), "rest", []) |
| 160 | _, _, passed, failed, _ = m.go_test("./internal/wire", ["TestTeardown"], dict(__import__("os").environ), []) |
| 161 | print(json.dumps({"status": status, "log": buf.getvalue(), "passed": sorted(passed), "failed": sorted(failed)})) |
| 162 | `, report) |
| 163 | var got struct { |
| 164 | Status int |
| 165 | Log string |
| 166 | Passed, Failed []string |
| 167 | } |
| 168 | if err := json.Unmarshal([]byte(out), &got); err != nil { |
| 169 | t.Fatalf("%v: %s", err, out) |
| 170 | } |
| 171 | if exit != "0" && (len(got.Passed) != 0 || !reflect.DeepEqual(got.Failed, []string{"TestTeardown"})) { |
| 172 | t.Errorf("exit %s: go_test passed %v failed %v, want the test failed", exit, got.Passed, got.Failed) |
| 173 | } |
| 174 | return got.Status, got.Log |
| 175 | } |
| 176 | if status, log := run("0"); status != 0 || !strings.Contains(log, "FLAKE ./internal/wire:TestTeardown rest ") { |
| 177 | t.Fatalf("a clean retry is not a flake: status %d\n%s", status, log) |
| 178 | } |
| 179 | for _, exit := range []string{"1", "2"} { |
| 180 | status, log := run(exit) |
| 181 | if status == 0 || strings.Contains(log, "FLAKE ") || !strings.Contains(log, "FAILED TWICE ./internal/wire:TestTeardown rest") || !strings.Contains(log, "panic: cleanup failed after the test passed") { |
| 182 | t.Fatalf("exit %s: a retry whose process failed went green: status %d\n%s", exit, status, log) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |