Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions examples/reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 48 additions & 8 deletions internal/pipeline/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,33 +560,73 @@ 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)
log, err := p.gh().JobLog(ctx, j.ID)
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")
}
178 changes: 178 additions & 0 deletions internal/pipeline/testlogs.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading