diff --git a/examples/reference.yaml b/examples/reference.yaml index 0fdaf56f..3c13f5e1 100644 --- a/examples/reference.yaml +++ b/examples/reference.yaml @@ -133,8 +133,13 @@ report: # Email a report for every patch that applied, not only for failures. apply_success: true - # Reports of failed runs include the last log_lines lines of up to - # max_log_jobs failed jobs. + # Reports of failed runs cover up to max_log_jobs failed jobs. For + # each, they list the failed tests with the last log_lines lines of + # each test's log, cut to 100 lines and 10000 characters per job and + # 300 lines in all. A test's log shown whole for one job is skipped + # for the next ones, unless all of theirs were shown. A job without + # failed tests shows the last log_lines lines before the error of its + # failed step. log_lines: 25 max_log_jobs: 5 diff --git a/internal/pipeline/reports.go b/internal/pipeline/reports.go index 13041782..e8f56687 100644 --- a/internal/pipeline/reports.go +++ b/internal/pipeline/reports.go @@ -560,12 +560,13 @@ func (p *Pipeline) failureLogs(ctx context.Context, runID int64) (string, error) } } maxJobs := p.Cfg.Report.MaxLogJobs + // Later jobs skip the test logs shown whole, unless they have no others. + shown := map[string]bool{} + left, more := maxTotalReportLines, 0 for i, j := range failed { if i >= maxJobs { p.infof("Run %d: logs limited to %d failed jobs; omitting %d more.", runID, maxJobs, len(failed)-maxJobs) - fmt.Fprintf(&b, "\nLogs of %d more failed jobs are not included.\n", - len(failed)-maxJobs) break } p.infof("Fetching log tail for failed job %d (%s) in run %d.", j.ID, j.Name, runID) @@ -573,20 +574,59 @@ func (p *Pipeline) failureLogs(ctx context.Context, runID int64) (string, error) if err != nil { return "", err } - fmt.Fprintf(&b, "\nLast %d log lines of %q:\n", p.Cfg.Report.LogLines, j.Name) - b.WriteString(logTail(log, p.Cfg.Report.LogLines)) + limit := maxReportLines + if left > 0 { + limit = min(left, maxReportLines) + } + heading := fmt.Sprintf("Failed tests in %q:", j.Name) + tests := failedTests(log, p.Cfg.Report.LogLines) + lines, ends, skipped := testDetails(tests, shown) + if len(ends) > 0 { + lines, ends = introduceLogs(lines, ends, len(tests), skipped, j.Name, limit) + } + if len(lines) == 0 { + heading = fmt.Sprintf("Last %d log lines of %q:", p.Cfg.Report.LogLines, j.Name) + lines = strings.Split(logTailBeforeError(log, p.Cfg.Report.LogLines), "\n") + } + if left == 0 { + more += min(len(lines), maxReportLines) + continue + } + text, whole, cutChars := cutReport(lines, limit) + for key, end := range ends { + if end <= whole { + shown[key] = true + } + } + left -= min(len(lines), limit) + fmt.Fprintf(&b, "\n%s\n%s", heading, text) + switch { + case len(lines) > limit && limit < maxReportLines: + more += min(len(lines), maxReportLines) - limit + case cutChars: + fmt.Fprintf(&b, "\n... longer than %d characters, see the workflow run "+ + "for the full report.", maxReportChars) + case len(lines) > limit: + fmt.Fprintf(&b, "\n... %d more lines, see the workflow run for the full report.", + len(lines)-limit) + } b.WriteString("\n") } + if more > 0 { + fmt.Fprintf(&b, "... Total line limit reached, %d more lines. "+ + "See the workflow run for all detailed failed tests.\n", more) + } + if len(failed) > maxJobs { + fmt.Fprintf(&b, "\nLogs of %d more failed jobs are not included.\n", + len(failed)-maxJobs) + } return b.String(), nil } func logTail(log string, n int) string { - lines := strings.Split(strings.TrimRight(strings.ReplaceAll(log, "\r\n", "\n"), "\n"), "\n") + lines := logLines(log) if len(lines) > n { lines = lines[len(lines)-n:] } - for i, l := range lines { - lines[i] = logTimestampRe.ReplaceAllString(l, "") - } return strings.Join(lines, "\n") } diff --git a/internal/pipeline/testlogs.go b/internal/pipeline/testlogs.go new file mode 100644 index 00000000..1634c04f --- /dev/null +++ b/internal/pipeline/testlogs.go @@ -0,0 +1,178 @@ +package pipeline + +import ( + "fmt" + "regexp" + "strings" + "unicode/utf8" +) + +const ( + maxReportLines = 100 + maxReportChars = 10000 + maxTotalReportLines = 300 +) + +var ( + testResultRe = regexp.MustCompile(`^\s*(\d+): (.*?)\s+` + + `(ok|skipped \(.*\)|expected failure \(.*\)|UNEXPECTED PASS|FAILED \([^)]*\))$`) + compilationRe = regexp.MustCompile(`^#\s+-\*- compilation -\*-$`) +) + +func logLines(log string) []string { + lines := strings.Split(strings.TrimRight(strings.ReplaceAll(log, "\r\n", "\n"), "\n"), "\n") + for i, l := range lines { + lines[i] = logTimestampRe.ReplaceAllString(l, "") + } + return lines +} + +type failedTest struct { + num, name, line string + log []string // last lines of its detailed log, if found +} + +func (t failedTest) key() string { return t.num + ": " + t.name } + +// failedTests returns the tests that autotest reports as failed in a job +// log, with the last n lines of their detailed logs. +func failedTests(log string, n int) []failedTest { + lines := logLines(log) + type result struct { + test failedTest + failed bool + } + var order []string + results := map[string]result{} + details := -1 + for i, l := range lines { + if l == "## Detailed failed tests. ##" { + details = i + 1 + break + } + // A recheck runs failed tests again; their last result counts. + if m := testResultRe.FindStringSubmatch(l); m != nil { + if _, ok := results[m[1]]; !ok { + order = append(order, m[1]) + } + results[m[1]] = result{failedTest{num: m[1], name: m[2], line: l}, + strings.HasPrefix(m[3], "FAILED")} + } + } + var tests []failedTest + for _, num := range order { + r := results[num] + if !r.failed { + continue + } + t := r.test + start := details + for i := details; details >= 0 && i < len(lines); i++ { + if compilationRe.MatchString(lines[i]) { + start = i + } + if strings.HasPrefix(lines[i], t.num+". ") && + strings.Contains(lines[i], ": FAILED (") { + t.log = lines[max(start, i+1-n) : i+1] + break + } + } + tests = append(tests, t) + } + return tests +} + +// testDetails lists failed tests followed by their logs. It skips the +// logs in shown, unless all are, and returns the tests it skips. ends +// has, for each log, the number of lines up to its end. +func testDetails(tests []failedTest, shown map[string]bool) (lines []string, + ends map[string]int, skipped []string) { + fresh := false + for _, t := range tests { + lines = append(lines, t.line) + fresh = fresh || t.log != nil && !shown[t.key()] + } + ends = map[string]int{} + for _, t := range tests { + if t.log == nil { + continue + } + if fresh && shown[t.key()] { + skipped = append(skipped, t.num) + continue + } + lines = append(lines, "", fmt.Sprintf("Log of test %s: %s", t.num, t.name)) + lines = append(lines, t.log...) + ends[t.key()] = len(lines) + } + return lines, ends, skipped +} + +// introduceLogs puts lines before the logs after the list of n failed +// tests, saying which were skipped and how many lines of the limit +// follow. +func introduceLogs(lines []string, ends map[string]int, n int, skipped []string, + job string, limit int) ([]string, map[string]int) { + intro := []string{"", ""} + if len(skipped) > 0 { + what := "test" + if len(skipped) > 1 { + what = "tests" + } + intro = append(intro, "") + intro[1] = fmt.Sprintf("Skipping the logs of already shown %s %s (see workflow run).", + what, strings.Join(skipped, ", ")) + } + follow := max(min(len(lines)+len(intro), limit)-n-len(intro), 0) + if len(skipped) > 0 { + intro[2] = fmt.Sprintf("Next %d lines of %q:", follow, job) + } else { + intro[1] = fmt.Sprintf("First %d lines of failures in %q:", follow, job) + } + lines = append(lines[:n:n], append(intro, lines[n:]...)...) + for key := range ends { + ends[key] += len(intro) + } + return lines, ends +} + +// logTailBeforeError returns the last n lines of a job log before the +// error that ended its failed step, which skips the cleanup steps after +// it. +func logTailBeforeError(log string, n int) string { + lines := logLines(log) + for i, l := range lines { + if strings.HasPrefix(l, "##[error]Process completed with exit code") { + lines = lines[:i] + break + } + } + if len(lines) > n { + lines = lines[len(lines)-n:] + } + return strings.Join(lines, "\n") +} + +// cutReport joins at most maxLines lines and cuts them to maxReportChars +// bytes. whole is the number of lines it keeps whole. +func cutReport(lines []string, maxLines int) (text string, whole int, cutChars bool) { + lines = lines[:min(len(lines), maxLines)] + size := 0 + for i, l := range lines { + if i > 0 { + size++ + } + size += len(l) + if size > maxReportChars { + if i > 0 { + return strings.Join(lines[:i], "\n"), i, true + } + cut := l[:maxReportChars] + for len(cut) > 0 && !utf8.RuneStart(l[len(cut)]) { + cut = cut[:len(cut)-1] + } + return cut, 0, true + } + } + return strings.Join(lines, "\n"), len(lines), false +} diff --git a/internal/pipeline/testlogs_test.go b/internal/pipeline/testlogs_test.go new file mode 100644 index 00000000..0e2c94f7 --- /dev/null +++ b/internal/pipeline/testlogs_test.go @@ -0,0 +1,314 @@ +package pipeline + +import ( + "context" + "fmt" + "strings" + "testing" + "unicode/utf8" + + "leadtestkit/internal/gh" +) + +func testFailures(log string, n int) string { + lines, _, _ := testDetails(failedTests(log, n), nil) + return strings.Join(lines, "\n") +} + +func jobLog(lines ...string) string { + for i, l := range lines { + lines[i] = "2026-11-23T20:29:40.8229420Z " + l + } + return strings.Join(lines, "\r\n") + "\r\n" +} + +const pinning = "check ovn-northd and ovn-controller version pinning -- " + + "parallelization=yes -- ovn_monitor_all=yes" + +func testBlock(n int) []string { + lines := []string{ + "# -*- compilation -*-", + fmt.Sprintf("%d. ovn.at:34206: testing %s ...", n, pinning), + } + for i := 1; i <= 37; i++ { + lines = append(lines, fmt.Sprintf("test %d line %d", n, i)) + } + return append(lines, "./ovn.at:34206: exit code was 2, expected 0", + fmt.Sprintf("%d. ovn.at:34206: %d. %s (ovn.at:34206): FAILED (ovn.at:34206)", + n, n, pinning)) +} + +func failedTestsLog() []string { + lines := []string{ + "463: check ovn-northd -- parallelization=yes ok", + "464: " + pinning + " FAILED (ovn.at:34206)", + "465: " + pinning + " FAILED (ovn.at:34206)", + "466: another test skipped (ovn.at:1)", + "## ------------- ##", + "## Test results. ##", + "## ------------- ##", + "ERROR: 285 tests were run,", + "2 failed unexpectedly.", + "464: " + pinning + " FAILED (ovn.at:34206)", + "## ---------------------- ##", + "## Detailed failed tests. ##", + "## ---------------------- ##", + } + lines = append(lines, testBlock(464)...) + lines = append(lines, testBlock(465)...) + return append(lines, "## ../config.log ##", "config line") +} + +func blockTail(n int) string { + b := testBlock(n) + return strings.Join(b[len(b)-25:], "\n") +} + +func TestTestFailures(t *testing.T) { + got := testFailures(jobLog(failedTestsLog()...), 25) + want := "464: " + pinning + " FAILED (ovn.at:34206)\n" + + "465: " + pinning + " FAILED (ovn.at:34206)\n" + + "\nLog of test 464: " + pinning + "\n" + blockTail(464) + "\n" + + "\nLog of test 465: " + pinning + "\n" + blockTail(465) + if got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } +} + +func TestTestFailuresWithoutDetails(t *testing.T) { + log := jobLog(failedTestsLog()[:10]...) + got := testFailures(log, 25) + want := "464: " + pinning + " FAILED (ovn.at:34206)\n" + + "465: " + pinning + " FAILED (ovn.at:34206)" + if got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } +} + +func TestTestFailuresPaddedNumbers(t *testing.T) { + log := jobLog(" 5: lorem test FAILED (lorem.at:5)", + " 42: ipsum test FAILED (ipsum.at:42)", + " 43: dolor test ok") + got := testFailures(log, 25) + want := " 5: lorem test FAILED (lorem.at:5)\n" + + " 42: ipsum test FAILED (ipsum.at:42)" + if got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } + + log = jobLog(append(failedTestsLog()[:13], + "# -*- compilation -*-", + "5. lorem.at:5: testing lorem test ...", + "5. lorem.at:5: 5. lorem test (lorem.at:5): FAILED (lorem.at:5)")...) + log = jobLog(" 5: lorem test FAILED (lorem.at:5)") + log + if got := testFailures(log, 25); !strings.Contains(got, + "\nLog of test 5: lorem test\n# ") { + t.Errorf("no log for test 5 or name not trimmed:\n%s", got) + } +} + +func TestTestFailuresRecheck(t *testing.T) { + log := jobLog("464: "+pinning+" FAILED (ovn.at:34206)", + "465: "+pinning+" FAILED (ovn.at:34206)", + "ERROR: 285 tests were run,", + "464: "+pinning+" ok", + "465: "+pinning+" FAILED (ovn.at:34206)") + want := "465: " + pinning + " FAILED (ovn.at:34206)" + if got := testFailures(log, 25); got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } + + log = jobLog("464: "+pinning+" FAILED (ovn.at:34206)", "464: "+pinning+" ok") + if got := testFailures(log, 25); got != "" { + t.Errorf("all passed on recheck, got:\n%s", got) + } +} + +func TestTestFailuresNone(t *testing.T) { + log := jobLog("1: lorem ok", "./ovn.at:1: exit code was 2, expected 0", + "make: *** [Makefile:1: check] Error 1") + if got := testFailures(log, 25); got != "" { + t.Errorf("got %q", got) + } +} + +func numbered(from, to int, format string) []string { + var lines []string + for i := from; i <= to; i++ { + lines = append(lines, fmt.Sprintf(format, i)) + } + return lines +} + +func TestLogTailBeforeError(t *testing.T) { + lines := numbered(1, 40, "line %d") + lines = append(lines, "##[error]Process completed with exit code 1.") + lines = append(lines, numbered(1, 30, "cleanup %d")...) + got := logTailBeforeError(jobLog(lines...), 25) + if want := strings.Join(numbered(16, 40, "line %d"), "\n"); got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } + + log := jobLog(numbered(1, 40, "line %d")...) + if got, want := logTailBeforeError(log, 25), logTail(log, 25); got != want { + t.Errorf("without error line got:\n%s\nwant:\n%s", got, want) + } +} + +func TestCutReport(t *testing.T) { + lines := numbered(1, 150, "line %d") + text, whole, cutChars := cutReport(lines, 100) + if text != strings.Join(lines[:100], "\n") || whole != 100 || cutChars { + t.Errorf("lines: got %d whole, cutChars %v, tail %q", whole, cutChars, + text[len(text)-20:]) + } + + long := strings.Repeat("x", 199) + lines = strings.Split(strings.Repeat(long+"\n", 98)+long, "\n") + text, whole, cutChars = cutReport(lines, 100) + if !cutChars || whole != 50 || text != strings.Join(lines[:50], "\n") { + t.Errorf("chars: got %d bytes, %d whole, cutChars %v", len(text), whole, cutChars) + } + + text, whole, cutChars = cutReport([]string{strings.Repeat("é", 6000)}, 100) + if !cutChars || whole != 0 || len(text) != 10000 || !utf8.ValidString(text) { + t.Errorf("one line: got %d bytes, %d whole, cutChars %v", len(text), whole, cutChars) + } + + if text, whole, cutChars := cutReport([]string{"short", "text"}, 100); text != + "short\ntext" || whole != 2 || cutChars { + t.Errorf("short: got %q, %d, %v", text, whole, cutChars) + } +} + +func TestBuildFailureReport(t *testing.T) { + e := newEnv(t, "") + e.gh.jobs[77] = []gh.Job{ + {ID: 55, Name: "lorem-job", Conclusion: "failure", + Steps: []gh.Step{{Name: "build", Conclusion: "failure"}}}, + {ID: 56, Name: "ipsum-job", Conclusion: "failure", + Steps: []gh.Step{{Name: "build", Conclusion: "failure"}}}, + } + e.gh.logs[55] = jobLog(append(failedTestsLog(), + "##[error]Process completed with exit code 1.", "Post job cleanup.")...) + e.gh.logs[56] = jobLog(append(numbered(1, 30, "compile %d"), + "##[error]Process completed with exit code 2.", "Post job cleanup.")...) + got, err := e.p.failureLogs(context.Background(), 77) + if err != nil { + t.Fatal(err) + } + for _, s := range []string{ + "Failed tests in \"lorem-job\":\n464: ", + "\nLog of test 465: " + pinning + "\n", + "Last 25 log lines of \"ipsum-job\":\ncompile 6\n", + "compile 30\n", + } { + if !strings.Contains(got, s) { + t.Errorf("missing %q in:\n%s", s, got) + } + } + if strings.Contains(got, "Post job cleanup.") || strings.Contains(got, "##[error]") { + t.Errorf("cleanup in report:\n%s", got) + } +} + +func testsLog(nums ...int) []string { + var lines []string + for _, n := range nums { + lines = append(lines, fmt.Sprintf("%d: %s FAILED (ovn.at:34206)", n, pinning)) + } + lines = append(lines, "## Detailed failed tests. ##") + for _, n := range nums { + lines = append(lines, testBlock(n)...) + } + return lines +} + +func failedJobs(e *env, logs ...[]string) { + e.gh.jobs[77] = nil + for i, l := range logs { + id := int64(100 + i) + e.gh.jobs[77] = append(e.gh.jobs[77], gh.Job{ID: id, + Name: fmt.Sprintf("job %d", i+1), Conclusion: "failure", + Steps: []gh.Step{{Name: "build", Conclusion: "failure"}}}) + e.gh.logs[id] = jobLog(l...) + } +} + +func TestTestLogsShownAgainIfAllShown(t *testing.T) { + e := newEnv(t, "") + failedJobs(e, failedTestsLog(), failedTestsLog(), failedTestsLog()) + got, err := e.p.failureLogs(context.Background(), 77) + if err != nil { + t.Fatal(err) + } + for _, n := range []string{"464", "465"} { + if c := strings.Count(got, "\nLog of test "+n+": "); c != 3 { + t.Errorf("log of test %s shown %d times:\n%s", n, c, got) + } + } + for _, job := range []string{"job 1", "job 2", "job 3"} { + // 2 logs of 27 lines. + s := "465: " + pinning + " FAILED (ovn.at:34206)\n\n" + + "First 54 lines of failures in \"" + job + "\":\n\nLog of test 464: " + if !strings.Contains(got, s) { + t.Errorf("missing %q in:\n%s", s, got) + } + } + if strings.Contains(got, "Skipping") { + t.Errorf("skipped logs:\n%s", got) + } +} + +func TestCutTestLogShownAgain(t *testing.T) { + e := newEnv(t, "") + // 4 list lines and 4 logs of 27 lines: the log of 467 is cut. + failedJobs(e, testsLog(464, 465, 466, 467), testsLog(464, 465, 466, 467)) + got, err := e.p.failureLogs(context.Background(), 77) + if err != nil { + t.Fatal(err) + } + _, job2, _ := strings.Cut(got, "Failed tests in \"job 2\":") + for _, s := range []string{ + "467: " + pinning + " FAILED (ovn.at:34206)\n\n" + + "Skipping the logs of already shown tests 464, 465, 466 (see workflow run).\n" + + "Next 27 lines of \"job 2\":\n\nLog of test 467: ", + "\nLog of test 467: " + pinning + "\n", + "467. ovn.at:34206: 467. " + pinning, + } { + if !strings.Contains(job2, s) { + t.Errorf("missing %q in job 2:\n%s", s, job2) + } + } + if strings.Contains(job2, "Log of test 466") || strings.Contains(job2, "...") { + t.Errorf("log of 466 shown again:\n%s", job2) + } +} + +func TestTotalLineLimit(t *testing.T) { + e := newEnv(t, "") + failedJobs(e, numbered(1, 30, "compile %d"), + testsLog(411, 412, 413, 414), testsLog(421, 422, 423, 424), + testsLog(431, 432, 433, 434), testsLog(441, 442, 443, 444)) + got, err := e.p.failureLogs(context.Background(), 77) + if err != nil { + t.Fatal(err) + } + // 25 + 100 + 100 lines, then 75 of the 100 of job 4 and none of job 5. + _, job4, _ := strings.Cut(got, "Failed tests in \"job 4\":\n") + note := "\n... Total line limit reached, 125 more lines. " + + "See the workflow run for all detailed failed tests.\n" + body, rest, ok := strings.Cut(job4, note) + if !ok || rest != "" { + t.Fatalf("no total note at the end of job 4:\n%s", job4) + } + if n := len(strings.Split(body, "\n")); n != 75 { + t.Errorf("job 4 has %d lines:\n%s", n, body) + } + if strings.Contains(got, "job 5\":") { + t.Errorf("job 5 included:\n%s", got) + } + if c := strings.Count(got, "more lines, see the workflow run for the full report."); c != 2 { + t.Errorf("%d per-job notes:\n%s", c, got) + } +}