From b9a7f1fa63df169b7375621c56ea0841d98e3014 Mon Sep 17 00:00:00 2001 From: Rosemarie O'Riorden Date: Wed, 25 Nov 2026 02:30:27 -0500 Subject: [PATCH 1/2] pipeline: Report the failed tests of Build and Test runs. Reports of failed runs showed the last 25 lines of each failed job's log. Those lines come from GitHub's cleanup after the job, so they never said what failed. Parse the autotest output that OVN and OVS print instead: list the tests that failed, then the last log_lines lines of each one's log from the "Detailed failed tests" section, which end with the check that failed. The section of each job is cut to 100 lines and 10000 characters, with a note to see the workflow run for the full report. A job without failed tests, such as one that failed to build, shows the last log_lines lines before the error that ended its failed step, again skipping the cleanup. Fixes fuzzfactor/leadtestkit#1 Signed-off-by: Rosemarie O'Riorden --- examples/reference.yaml | 7 +- internal/pipeline/reports.go | 14 +- internal/pipeline/testlogs.go | 133 ++++++++++++++++++ internal/pipeline/testlogs_test.go | 212 +++++++++++++++++++++++++++++ 4 files changed, 358 insertions(+), 8 deletions(-) create mode 100644 internal/pipeline/testlogs.go create mode 100644 internal/pipeline/testlogs_test.go diff --git a/examples/reference.yaml b/examples/reference.yaml index 0fdaf56f..83fa894f 100644 --- a/examples/reference.yaml +++ b/examples/reference.yaml @@ -133,8 +133,11 @@ 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. 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..800f683e 100644 --- a/internal/pipeline/reports.go +++ b/internal/pipeline/reports.go @@ -573,20 +573,22 @@ 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)) + if tests := testFailures(log, p.Cfg.Report.LogLines); tests != "" { + fmt.Fprintf(&b, "\nFailed tests in %q:\n", j.Name) + b.WriteString(capReport(tests)) + } else { + fmt.Fprintf(&b, "\nLast %d log lines of %q:\n", p.Cfg.Report.LogLines, j.Name) + b.WriteString(logTailBeforeError(log, p.Cfg.Report.LogLines)) + } b.WriteString("\n") } 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..271bc647 --- /dev/null +++ b/internal/pipeline/testlogs.go @@ -0,0 +1,133 @@ +package pipeline + +import ( + "fmt" + "regexp" + "strings" + "unicode/utf8" +) + +const ( + maxReportLines = 100 + maxReportChars = 10000 +) + +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 +} + +// testFailures lists the tests that autotest reports as failed in a job +// log, each followed by the last n lines of its detailed log. It returns +// "" if no test failed. +func testFailures(log string, n int) string { + lines := logLines(log) + type test struct { + num, name, line string + failed bool + } + var order []string + results := map[string]test{} + 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]] = test{m[1], m[2], l, strings.HasPrefix(m[3], "FAILED")} + } + } + var tests []test + for _, num := range order { + if results[num].failed { + tests = append(tests, results[num]) + } + } + if len(tests) == 0 { + return "" + } + var b strings.Builder + for _, t := range tests { + b.WriteString(t.line + "\n") + } + if details < 0 { + return strings.TrimSuffix(b.String(), "\n") + } + for _, t := range tests { + start, end := details, -1 + for i := details; i < len(lines); i++ { + if compilationRe.MatchString(lines[i]) { + start = i + } + if strings.HasPrefix(lines[i], t.num+". ") && + strings.Contains(lines[i], ": FAILED (") { + end = i + break + } + } + if end < 0 { + continue + } + block := lines[start : end+1] + if len(block) > n { + block = block[len(block)-n:] + } + fmt.Fprintf(&b, "\nLog of test %s: %s\n%s\n", t.num, t.name, strings.Join(block, "\n")) + } + return strings.TrimSuffix(b.String(), "\n") +} + +// 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") +} + +// capReport cuts text to maxReportLines lines and then maxReportChars +// bytes. +func capReport(text string) string { + note := "" + if lines := strings.Split(text, "\n"); len(lines) > maxReportLines { + text = strings.Join(lines[:maxReportLines], "\n") + note = fmt.Sprintf("\n... %d more lines, see the workflow run for the full report.", + len(lines)-maxReportLines) + } + if len(text) <= maxReportChars { + return text + note + } + cut := text[:maxReportChars] + if i := strings.LastIndexByte(cut, '\n'); i >= 0 { + cut = cut[:i] + } else { + for len(cut) > 0 && !utf8.RuneStart(text[len(cut)]) { + cut = cut[:len(cut)-1] + } + } + return cut + fmt.Sprintf("\n... longer than %d characters, see the workflow run for the full report.", + maxReportChars) +} diff --git a/internal/pipeline/testlogs_test.go b/internal/pipeline/testlogs_test.go new file mode 100644 index 00000000..3bcbf698 --- /dev/null +++ b/internal/pipeline/testlogs_test.go @@ -0,0 +1,212 @@ +package pipeline + +import ( + "context" + "fmt" + "strings" + "testing" + + "leadtestkit/internal/gh" +) + +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 TestCapReport(t *testing.T) { + lines := numbered(1, 150, "line %d") + got := capReport(strings.Join(lines, "\n")) + want := strings.Join(lines[:100], "\n") + + "\n... 50 more lines, see the workflow run for the full report." + if got != want { + t.Errorf("lines: got tail %q", got[len(got)-80:]) + } + + long := strings.Repeat("x", 199) + text := strings.Repeat(long+"\n", 98) + long + got = capReport(text) + note := "\n... longer than 10000 characters, see the workflow run for the full report." + if !strings.HasSuffix(got, note) { + t.Fatalf("chars: got tail %q", got[len(got)-80:]) + } + cut := strings.TrimSuffix(got, note) + if len(cut) > 10000 || !strings.HasPrefix(text, cut+"\n") { + t.Errorf("chars: cut to %d bytes, not at a newline", len(cut)) + } + + got = capReport(strings.Repeat(long+"\n", 150)) + if !strings.HasSuffix(got, note) || len(strings.TrimSuffix(got, note)) > 10000 { + t.Errorf("lines and chars: got %d bytes, tail %q", len(got), got[len(got)-80:]) + } + + if got := capReport("short\ntext"); got != "short\ntext" { + t.Errorf("short: got %q", got) + } +} + +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) + } +} From d3068868ee8d5811be268f4ee816b3462814388a Mon Sep 17 00:00:00 2001 From: Rosemarie O'Riorden Date: Wed, 25 Nov 2026 09:56:49 -0500 Subject: [PATCH 2/2] pipeline: Show each failed test's log once in Build and Test reports. The same tests often fail in every job of a run, so reports repeated the same logs for each job, and with many failed jobs the email grew to hundreds of lines. Skip the logs of tests already shown whole for an earlier job, with a line naming the tests skipped, so the space goes to failures not shown yet. A job whose failed tests were all shown shows them again. A log that was cut is shown again. Cut all jobs together to 300 lines, with a note of how many lines more there were. Signed-off-by: Rosemarie O'Riorden --- examples/reference.yaml | 8 +- internal/pipeline/reports.go | 54 ++++++++-- internal/pipeline/testlogs.go | 155 +++++++++++++++++++---------- internal/pipeline/testlogs_test.go | 142 ++++++++++++++++++++++---- 4 files changed, 273 insertions(+), 86 deletions(-) diff --git a/examples/reference.yaml b/examples/reference.yaml index 83fa894f..3c13f5e1 100644 --- a/examples/reference.yaml +++ b/examples/reference.yaml @@ -135,9 +135,11 @@ report: # 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. A job - # without failed tests shows the last log_lines lines before the error - # of its failed step. + # 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 800f683e..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,15 +574,52 @@ func (p *Pipeline) failureLogs(ctx context.Context, runID int64) (string, error) if err != nil { return "", err } - if tests := testFailures(log, p.Cfg.Report.LogLines); tests != "" { - fmt.Fprintf(&b, "\nFailed tests in %q:\n", j.Name) - b.WriteString(capReport(tests)) - } else { - fmt.Fprintf(&b, "\nLast %d log lines of %q:\n", p.Cfg.Report.LogLines, j.Name) - b.WriteString(logTailBeforeError(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 } diff --git a/internal/pipeline/testlogs.go b/internal/pipeline/testlogs.go index 271bc647..1634c04f 100644 --- a/internal/pipeline/testlogs.go +++ b/internal/pipeline/testlogs.go @@ -8,8 +8,9 @@ import ( ) const ( - maxReportLines = 100 - maxReportChars = 10000 + maxReportLines = 100 + maxReportChars = 10000 + maxTotalReportLines = 300 ) var ( @@ -26,17 +27,23 @@ func logLines(log string) []string { return lines } -// testFailures lists the tests that autotest reports as failed in a job -// log, each followed by the last n lines of its detailed log. It returns -// "" if no test failed. -func testFailures(log string, n int) string { +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 test struct { - num, name, line string - failed bool + type result struct { + test failedTest + failed bool } var order []string - results := map[string]test{} + results := map[string]result{} details := -1 for i, l := range lines { if l == "## Detailed failed tests. ##" { @@ -48,47 +55,85 @@ func testFailures(log string, n int) string { if _, ok := results[m[1]]; !ok { order = append(order, m[1]) } - results[m[1]] = test{m[1], m[2], l, strings.HasPrefix(m[3], "FAILED")} + results[m[1]] = result{failedTest{num: m[1], name: m[2], line: l}, + strings.HasPrefix(m[3], "FAILED")} } } - var tests []test + var tests []failedTest for _, num := range order { - if results[num].failed { - tests = append(tests, results[num]) + r := results[num] + if !r.failed { + continue } - } - if len(tests) == 0 { - return "" - } - var b strings.Builder - for _, t := range tests { - b.WriteString(t.line + "\n") - } - if details < 0 { - return strings.TrimSuffix(b.String(), "\n") - } - for _, t := range tests { - start, end := details, -1 - for i := details; i < len(lines); i++ { + 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 (") { - end = i + t.log = lines[max(start, i+1-n) : i+1] break } } - if end < 0 { + 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 } - block := lines[start : end+1] - if len(block) > n { - block = block[len(block)-n:] + 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" } - fmt.Fprintf(&b, "\nLog of test %s: %s\n%s\n", t.num, t.name, strings.Join(block, "\n")) + 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 strings.TrimSuffix(b.String(), "\n") + return lines, ends } // logTailBeforeError returns the last n lines of a job log before the @@ -108,26 +153,26 @@ func logTailBeforeError(log string, n int) string { return strings.Join(lines, "\n") } -// capReport cuts text to maxReportLines lines and then maxReportChars -// bytes. -func capReport(text string) string { - note := "" - if lines := strings.Split(text, "\n"); len(lines) > maxReportLines { - text = strings.Join(lines[:maxReportLines], "\n") - note = fmt.Sprintf("\n... %d more lines, see the workflow run for the full report.", - len(lines)-maxReportLines) - } - if len(text) <= maxReportChars { - return text + note - } - cut := text[:maxReportChars] - if i := strings.LastIndexByte(cut, '\n'); i >= 0 { - cut = cut[:i] - } else { - for len(cut) > 0 && !utf8.RuneStart(text[len(cut)]) { - cut = cut[:len(cut)-1] +// 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 cut + fmt.Sprintf("\n... longer than %d characters, see the workflow run for the full report.", - maxReportChars) + return strings.Join(lines, "\n"), len(lines), false } diff --git a/internal/pipeline/testlogs_test.go b/internal/pipeline/testlogs_test.go index 3bcbf698..0e2c94f7 100644 --- a/internal/pipeline/testlogs_test.go +++ b/internal/pipeline/testlogs_test.go @@ -5,10 +5,16 @@ import ( "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 @@ -149,34 +155,29 @@ func TestLogTailBeforeError(t *testing.T) { } } -func TestCapReport(t *testing.T) { +func TestCutReport(t *testing.T) { lines := numbered(1, 150, "line %d") - got := capReport(strings.Join(lines, "\n")) - want := strings.Join(lines[:100], "\n") + - "\n... 50 more lines, see the workflow run for the full report." - if got != want { - t.Errorf("lines: got tail %q", got[len(got)-80:]) + 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) - text := strings.Repeat(long+"\n", 98) + long - got = capReport(text) - note := "\n... longer than 10000 characters, see the workflow run for the full report." - if !strings.HasSuffix(got, note) { - t.Fatalf("chars: got tail %q", got[len(got)-80:]) - } - cut := strings.TrimSuffix(got, note) - if len(cut) > 10000 || !strings.HasPrefix(text, cut+"\n") { - t.Errorf("chars: cut to %d bytes, not at a newline", len(cut)) + 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) } - got = capReport(strings.Repeat(long+"\n", 150)) - if !strings.HasSuffix(got, note) || len(strings.TrimSuffix(got, note)) > 10000 { - t.Errorf("lines and chars: got %d bytes, tail %q", len(got), got[len(got)-80:]) + 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 got := capReport("short\ntext"); got != "short\ntext" { - t.Errorf("short: got %q", got) + 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) } } @@ -210,3 +211,104 @@ func TestBuildFailureReport(t *testing.T) { 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) + } +}