mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-19 13:19:06 +02:00
[management] Speed up test store setup and summarize the unit test run (#7518)
Management / Unit (amd64, mysql) hit the 20 minute go test budget on #7516. The package was not hung: each of the 133 test store creations in management/server paid about 1.6s on MySQL for CREATE DATABASE, the pre-migrations, a 40-table AutoMigrate and the post-migrations, which puts the package at 10 minutes on a healthy runner and over the budget on a slow one. The migration now runs once per test binary into a template database and each test database is cloned from it, with CREATE DATABASE ... TEMPLATE on Postgres and a replay of SHOW CREATE TABLE on MySQL. The MySQL test container also drops the binary log, doublewrite buffer and per-commit redo fsync. Two goroutine leaks in the test helpers are fixed. tools/gotestsummary turns the go test -json stream into a readable log, and the Management unit and integration jobs now pipe through it, so a timeout names the tests still running. On MySQL, management/server went from 10m16s to 6m36s.
This commit is contained in:
@@ -0,0 +1,402 @@
|
||||
// Package main turns a `go test -json` stream into a readable CI log.
|
||||
//
|
||||
// It prints one line per top-level test as it finishes, the captured output of
|
||||
// every failed test, the head of a package-level panic (which is where Go
|
||||
// reports "test timed out" and the list of still-running tests), and ends with
|
||||
// the per-package durations and the slowest tests. The exit code is always zero
|
||||
// unless the input cannot be read; the `go test` exit code is what CI should act
|
||||
// on, so run the two with `set -o pipefail`.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// go test -json ./... | go run ./tools/gotestsummary
|
||||
// go run ./tools/gotestsummary -slowest 60 test-output.jsonl
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
modulePrefix = "github.com/netbirdio/netbird/"
|
||||
|
||||
// failedTestOutputLines bounds how much captured output a single failed
|
||||
// test may print, so one noisy failure cannot flood the job log.
|
||||
failedTestOutputLines = 200
|
||||
// panicHeadLines is enough for the "panic: test timed out" header, the
|
||||
// "running tests:" list, and the first few goroutines of the dump.
|
||||
panicHeadLines = 150
|
||||
// bufferedOutputLines bounds the per-test output kept in memory while the
|
||||
// test runs; only the tail is kept once the cap is reached.
|
||||
bufferedOutputLines = 400
|
||||
)
|
||||
|
||||
type event struct {
|
||||
Action string `json:"Action"`
|
||||
Package string `json:"Package"`
|
||||
Test string `json:"Test"`
|
||||
Output string `json:"Output"`
|
||||
Elapsed float64 `json:"Elapsed"`
|
||||
// ImportPath is set instead of Package on build events. It carries a
|
||||
// " [pkg.test]" suffix naming the test binary the package was compiled
|
||||
// for, and the same package can be built for several binaries at once.
|
||||
ImportPath string `json:"ImportPath"`
|
||||
// FailedBuild names the ImportPath whose build failure made the package
|
||||
// fail; go test reports the package fail event after the build-fail one.
|
||||
FailedBuild string `json:"FailedBuild"`
|
||||
}
|
||||
|
||||
type testKey struct {
|
||||
pkg, name string
|
||||
}
|
||||
|
||||
type testResult struct {
|
||||
pkg, name string
|
||||
action string
|
||||
elapsed time.Duration
|
||||
}
|
||||
|
||||
type packageResult struct {
|
||||
pkg string
|
||||
action string
|
||||
elapsed time.Duration
|
||||
}
|
||||
|
||||
type summarizer struct {
|
||||
out io.Writer
|
||||
|
||||
output map[testKey][]string
|
||||
dropped map[testKey]int
|
||||
// pkgOutput keeps what a package printed outside any test, which is where
|
||||
// compiler diagnostics of a failed build end up.
|
||||
pkgOutput map[string][]string
|
||||
// failedBuilds holds the ImportPaths whose build failed and has not been
|
||||
// reported through a package fail event yet.
|
||||
failedBuilds map[string]bool
|
||||
tests []testResult
|
||||
packages []packageResult
|
||||
// panics holds the head of a panic per package. Package streams interleave
|
||||
// in a go test -json run, so one package's dump must not swallow another's
|
||||
// output.
|
||||
panics map[string][]string
|
||||
}
|
||||
|
||||
func newSummarizer(out io.Writer) *summarizer {
|
||||
return &summarizer{
|
||||
out: out,
|
||||
output: make(map[testKey][]string),
|
||||
dropped: make(map[testKey]int),
|
||||
pkgOutput: make(map[string][]string),
|
||||
failedBuilds: make(map[string]bool),
|
||||
panics: make(map[string][]string),
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
slowest := flag.Int("slowest", 40, "number of slowest top-level tests to list")
|
||||
flag.Parse()
|
||||
|
||||
if err := run(flag.Arg(0), *slowest); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(path string, slowest int) error {
|
||||
in := os.Stdin
|
||||
if path != "" {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open %s: %w", path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
in = f
|
||||
}
|
||||
|
||||
s := newSummarizer(os.Stdout)
|
||||
if err := s.consume(in); err != nil {
|
||||
return fmt.Errorf("read input: %w", err)
|
||||
}
|
||||
s.printSummary(slowest)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *summarizer) consume(r io.Reader) error {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Buffer(make([]byte, 0, 1024*1024), 16*1024*1024)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
var ev event
|
||||
if err := json.Unmarshal(line, &ev); err != nil {
|
||||
// Build errors and other non-JSON lines are passed through untouched.
|
||||
fmt.Fprintln(s.out, string(line))
|
||||
continue
|
||||
}
|
||||
s.handle(ev)
|
||||
}
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func (s *summarizer) handle(ev event) {
|
||||
if ev.Package == "" {
|
||||
// Keep the full path as the key so concurrent builds of one package for
|
||||
// different test binaries do not share, and delete, each other's state.
|
||||
ev.Package = ev.ImportPath
|
||||
}
|
||||
key := testKey{pkg: ev.Package, name: ev.Test}
|
||||
switch ev.Action {
|
||||
case "run":
|
||||
// Register the test even before it prints anything, so a test that
|
||||
// hangs silently still shows up as unfinished.
|
||||
if ev.Test != "" {
|
||||
if _, ok := s.output[key]; !ok {
|
||||
s.output[key] = []string{}
|
||||
}
|
||||
}
|
||||
case "output":
|
||||
s.handleOutput(key, strings.TrimRight(ev.Output, "\n"))
|
||||
case "build-output":
|
||||
// Compiler output may carry several lines per event and is never test
|
||||
// output, so it skips the panic detection.
|
||||
for _, line := range strings.Split(strings.TrimRight(ev.Output, "\n"), "\n") {
|
||||
s.pkgOutput[key.pkg] = appendBounded(s.pkgOutput[key.pkg], line)
|
||||
}
|
||||
case "build-fail":
|
||||
// The package fail event that follows carries FailedBuild and reports
|
||||
// the compiler output; this only remembers the build in case it never
|
||||
// comes.
|
||||
s.failedBuilds[key.pkg] = true
|
||||
case "pass", "fail", "skip":
|
||||
if ev.Test == "" {
|
||||
s.handlePackageResult(ev)
|
||||
return
|
||||
}
|
||||
s.handleTestResult(key, ev)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *summarizer) handleOutput(key testKey, line string) {
|
||||
if strings.HasPrefix(line, "panic: ") || strings.HasPrefix(line, "fatal error: ") {
|
||||
if _, ok := s.panics[key.pkg]; !ok {
|
||||
s.panics[key.pkg] = []string{}
|
||||
}
|
||||
}
|
||||
if head, ok := s.panics[key.pkg]; ok {
|
||||
// The goroutine dump that follows a panic is kept in the panic head only;
|
||||
// letting it flood the per-test buffers would hide the test's own output.
|
||||
if len(head) < panicHeadLines {
|
||||
s.panics[key.pkg] = append(head, line)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if key.name == "" {
|
||||
s.pkgOutput[key.pkg] = appendBounded(s.pkgOutput[key.pkg], line)
|
||||
return
|
||||
}
|
||||
if len(s.output[key]) >= bufferedOutputLines {
|
||||
s.dropped[key]++
|
||||
}
|
||||
s.output[key] = appendBounded(s.output[key], line)
|
||||
}
|
||||
|
||||
// appendBounded keeps the most recent bufferedOutputLines lines.
|
||||
func appendBounded(buf []string, line string) []string {
|
||||
if len(buf) >= bufferedOutputLines {
|
||||
buf = buf[1:]
|
||||
}
|
||||
return append(buf, line)
|
||||
}
|
||||
|
||||
func (s *summarizer) handleTestResult(key testKey, ev event) {
|
||||
elapsed := time.Duration(ev.Elapsed * float64(time.Second))
|
||||
s.tests = append(s.tests, testResult{
|
||||
pkg: ev.Package,
|
||||
name: ev.Test,
|
||||
action: ev.Action,
|
||||
elapsed: elapsed,
|
||||
})
|
||||
|
||||
if !strings.Contains(ev.Test, "/") || ev.Action == "fail" {
|
||||
fmt.Fprintf(s.out, "--- %s: %s.%s (%s)\n", strings.ToUpper(ev.Action), shortPkg(ev.Package), ev.Test, elapsed.Round(time.Millisecond))
|
||||
}
|
||||
if ev.Action == "fail" {
|
||||
s.printTestOutput(key)
|
||||
}
|
||||
delete(s.output, key)
|
||||
delete(s.dropped, key)
|
||||
}
|
||||
|
||||
func (s *summarizer) printTestOutput(key testKey) {
|
||||
lines := s.output[key]
|
||||
if len(lines) == 0 {
|
||||
return
|
||||
}
|
||||
skipped := s.dropped[key]
|
||||
if len(lines) > failedTestOutputLines {
|
||||
skipped += len(lines) - failedTestOutputLines
|
||||
lines = lines[len(lines)-failedTestOutputLines:]
|
||||
}
|
||||
if skipped > 0 {
|
||||
fmt.Fprintf(s.out, " ... %d earlier output lines omitted ...\n", skipped)
|
||||
}
|
||||
for _, l := range lines {
|
||||
fmt.Fprintf(s.out, " %s\n", l)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *summarizer) handlePackageResult(ev event) {
|
||||
elapsed := time.Duration(ev.Elapsed * float64(time.Second))
|
||||
s.packages = append(s.packages, packageResult{pkg: ev.Package, action: ev.Action, elapsed: elapsed})
|
||||
|
||||
label := "ok "
|
||||
switch ev.Action {
|
||||
case "fail", "build-fail":
|
||||
label = "FAIL"
|
||||
case "skip":
|
||||
label = "skip"
|
||||
}
|
||||
fmt.Fprintf(s.out, "%s %s %s\n", label, shortPkg(ev.Package), elapsed.Round(time.Millisecond))
|
||||
|
||||
if label == "FAIL" {
|
||||
if ev.FailedBuild != "" {
|
||||
// Several test binaries can share one failed dependency, so its
|
||||
// output stays available for the next package that names it.
|
||||
s.printPackageOutput(ev.FailedBuild, "build output of %s")
|
||||
delete(s.failedBuilds, ev.FailedBuild)
|
||||
}
|
||||
s.printPackageOutput(ev.Package, "output of %s outside tests")
|
||||
s.printUnfinished(ev.Package)
|
||||
s.printPanicHead(ev.Package)
|
||||
}
|
||||
delete(s.pkgOutput, ev.Package)
|
||||
delete(s.panics, ev.Package)
|
||||
}
|
||||
|
||||
// printUnclaimedBuildFailures reports the failed builds no package fail event
|
||||
// accounted for, so a compiler error never disappears from the log.
|
||||
func (s *summarizer) printUnclaimedBuildFailures() {
|
||||
var builds []string
|
||||
for b := range s.failedBuilds {
|
||||
builds = append(builds, b)
|
||||
}
|
||||
sort.Strings(builds)
|
||||
for _, b := range builds {
|
||||
fmt.Fprintf(s.out, "FAIL %s [build failed]\n", shortPkg(b))
|
||||
s.printPackageOutput(b, "build output of %s")
|
||||
}
|
||||
}
|
||||
|
||||
// printPackageOutput shows what a failed package printed outside its tests,
|
||||
// or the compiler errors of a failed build, under the given header.
|
||||
func (s *summarizer) printPackageOutput(pkg, header string) {
|
||||
lines := s.pkgOutput[pkg]
|
||||
if len(lines) == 0 {
|
||||
return
|
||||
}
|
||||
if len(lines) > failedTestOutputLines {
|
||||
lines = lines[len(lines)-failedTestOutputLines:]
|
||||
}
|
||||
fmt.Fprintf(s.out, "\n==== "+header+" ====\n", shortPkg(pkg))
|
||||
for _, l := range lines {
|
||||
fmt.Fprintf(s.out, " %s\n", l)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *summarizer) printPanicHead(pkg string) {
|
||||
head := s.panics[pkg]
|
||||
if len(head) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(s.out, "\n==== panic in %s (first %d lines) ====\n", shortPkg(pkg), len(head))
|
||||
for _, l := range head {
|
||||
fmt.Fprintln(s.out, l)
|
||||
}
|
||||
fmt.Fprintln(s.out, "==== end of panic head ====")
|
||||
fmt.Fprintln(s.out)
|
||||
}
|
||||
|
||||
// printUnfinished names the tests of a failed package that never reported a
|
||||
// result, which is what a timeout leaves behind, and shows their last output.
|
||||
func (s *summarizer) printUnfinished(pkg string) {
|
||||
var keys []testKey
|
||||
for key := range s.output {
|
||||
if key.pkg == pkg && key.name != "" {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
return
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i].name < keys[j].name })
|
||||
fmt.Fprintf(s.out, "\n==== tests in %s that did not finish (%d) ====\n", shortPkg(pkg), len(keys))
|
||||
for _, key := range keys {
|
||||
fmt.Fprintf(s.out, "--- UNFINISHED: %s.%s\n", shortPkg(key.pkg), key.name)
|
||||
s.printTestOutput(key)
|
||||
delete(s.output, key)
|
||||
delete(s.dropped, key)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *summarizer) printSummary(slowest int) {
|
||||
s.printUnclaimedBuildFailures()
|
||||
|
||||
fmt.Fprintln(s.out)
|
||||
fmt.Fprintln(s.out, "==== package durations ====")
|
||||
sort.Slice(s.packages, func(i, j int) bool { return s.packages[i].elapsed > s.packages[j].elapsed })
|
||||
for _, p := range s.packages {
|
||||
fmt.Fprintf(s.out, "%9s %-4s %s\n", p.elapsed.Round(time.Millisecond), p.action, shortPkg(p.pkg))
|
||||
}
|
||||
|
||||
var failed []testResult
|
||||
for _, t := range s.tests {
|
||||
if t.action == "fail" {
|
||||
failed = append(failed, t)
|
||||
}
|
||||
}
|
||||
if len(failed) > 0 {
|
||||
fmt.Fprintln(s.out)
|
||||
fmt.Fprintf(s.out, "==== failed tests (%d) ====\n", len(failed))
|
||||
for _, t := range failed {
|
||||
fmt.Fprintf(s.out, "%9s %s.%s\n", t.elapsed.Round(time.Millisecond), shortPkg(t.pkg), t.name)
|
||||
}
|
||||
}
|
||||
|
||||
s.printSlowest("slowest top-level tests", slowest, func(t testResult) bool { return !strings.Contains(t.name, "/") })
|
||||
s.printSlowest("slowest subtests", slowest/2, func(t testResult) bool { return strings.Contains(t.name, "/") })
|
||||
}
|
||||
|
||||
func (s *summarizer) printSlowest(title string, limit int, keep func(testResult) bool) {
|
||||
var tests []testResult
|
||||
for _, t := range s.tests {
|
||||
if keep(t) {
|
||||
tests = append(tests, t)
|
||||
}
|
||||
}
|
||||
if len(tests) == 0 || limit <= 0 {
|
||||
return
|
||||
}
|
||||
sort.Slice(tests, func(i, j int) bool { return tests[i].elapsed > tests[j].elapsed })
|
||||
if len(tests) > limit {
|
||||
tests = tests[:limit]
|
||||
}
|
||||
|
||||
fmt.Fprintln(s.out)
|
||||
fmt.Fprintf(s.out, "==== %s (%d) ====\n", title, len(tests))
|
||||
for _, t := range tests {
|
||||
fmt.Fprintf(s.out, "%9s %-4s %s.%s\n", t.elapsed.Round(time.Millisecond), t.action, shortPkg(t.pkg), t.name)
|
||||
}
|
||||
}
|
||||
|
||||
func shortPkg(pkg string) string {
|
||||
pkg, _, _ = strings.Cut(pkg, " [")
|
||||
return strings.TrimPrefix(pkg, modulePrefix)
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func feed(t *testing.T, events string) string {
|
||||
t.Helper()
|
||||
var out bytes.Buffer
|
||||
s := newSummarizer(&out)
|
||||
if err := s.consume(strings.NewReader(events)); err != nil {
|
||||
t.Fatalf("consume: %v", err)
|
||||
}
|
||||
s.printSummary(10)
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func TestTimeoutReportsUnfinishedTestsAndPanicHead(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"run","Package":"a","Test":"TestHang"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"=== RUN TestHang\n"}
|
||||
{"Action":"run","Package":"a","Test":"TestSilent"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"panic: test timed out after 1s\n"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"\trunning tests:\n"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"\t\tTestHang (1s)\n"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"goroutine 7 [running]:\n"}
|
||||
{"Action":"fail","Package":"a","Elapsed":1.0}
|
||||
`
|
||||
got := feed(t, events)
|
||||
for _, want := range []string{
|
||||
"--- UNFINISHED: a.TestHang",
|
||||
"--- UNFINISHED: a.TestSilent",
|
||||
"==== panic in a (first 4 lines) ====",
|
||||
"\t\tTestHang (1s)",
|
||||
" === RUN TestHang",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("output lacks %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
if strings.Contains(got, " goroutine 7 [running]:") {
|
||||
t.Errorf("goroutine dump leaked into the test's own output:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPanicInOnePackageKeepsOtherPackageOutput(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"run","Package":"a","Test":"TestHang"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"panic: test timed out after 1s\n"}
|
||||
{"Action":"run","Package":"b","Test":"TestOther"}
|
||||
{"Action":"output","Package":"b","Test":"TestOther","Output":" other_test.go:9: expected 1, got 2\n"}
|
||||
{"Action":"output","Package":"a","Test":"TestHang","Output":"goroutine 7 [running]:\n"}
|
||||
{"Action":"fail","Package":"b","Test":"TestOther","Elapsed":0.01}
|
||||
{"Action":"fail","Package":"b","Elapsed":0.02}
|
||||
{"Action":"fail","Package":"a","Elapsed":1.0}
|
||||
`
|
||||
got := feed(t, events)
|
||||
if !strings.Contains(got, " other_test.go:9: expected 1, got 2") {
|
||||
t.Errorf("other package's output was swallowed by the panic head:\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, "panic in b") {
|
||||
t.Errorf("panic head attributed to the wrong package:\n%s", got)
|
||||
}
|
||||
if !strings.Contains(got, "==== panic in a (first 2 lines) ====") {
|
||||
t.Errorf("panic head missing for package a:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildFailureShowsCompilerOutput(t *testing.T) {
|
||||
// The event sequence go test emits for a build failure: the build events
|
||||
// name the test binary, then the package itself fails with FailedBuild.
|
||||
events := `
|
||||
{"Action":"build-output","ImportPath":"a [a.test]","Output":"# a [a.test]\na_test.go:7:2: undefined: nope\na_test.go:9:2: undefined: nope2\n"}
|
||||
{"Action":"build-fail","ImportPath":"a [a.test]"}
|
||||
{"Action":"start","Package":"a"}
|
||||
{"Action":"output","Package":"a","Output":"FAIL\ta [build failed]\n"}
|
||||
{"Action":"fail","Package":"a","Elapsed":0,"FailedBuild":"a [a.test]"}
|
||||
`
|
||||
got := feed(t, events)
|
||||
for _, want := range []string{
|
||||
"==== build output of a ====",
|
||||
" a_test.go:7:2: undefined: nope\n a_test.go:9:2: undefined: nope2",
|
||||
"FAIL\ta [build failed]",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("output lacks %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
if n := strings.Count(got, "FAIL a 0s"); n != 1 {
|
||||
t.Errorf("expected one FAIL line for the package, got %d:\n%s", n, got)
|
||||
}
|
||||
if n := strings.Count(got, "undefined: nope2"); n != 1 {
|
||||
t.Errorf("expected the compiler output once, got %d:\n%s", n, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedDependencyOutputIsShownForEveryImporter(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"build-output","ImportPath":"m/x","Output":"# m/x\nx.go:3:11: undefined: y\n"}
|
||||
{"Action":"build-fail","ImportPath":"m/x"}
|
||||
{"Action":"start","Package":"m/a"}
|
||||
{"Action":"output","Package":"m/a","Output":"FAIL\tm/a [build failed]\n"}
|
||||
{"Action":"fail","Package":"m/a","Elapsed":0,"FailedBuild":"m/x"}
|
||||
{"Action":"start","Package":"m/b"}
|
||||
{"Action":"output","Package":"m/b","Output":"FAIL\tm/b [build failed]\n"}
|
||||
{"Action":"fail","Package":"m/b","Elapsed":0,"FailedBuild":"m/x"}
|
||||
`
|
||||
got := feed(t, events)
|
||||
if n := strings.Count(got, "x.go:3:11: undefined: y"); n != 2 {
|
||||
t.Errorf("expected the dependency's compiler output under both packages, got %d:\n%s", n, got)
|
||||
}
|
||||
if strings.Contains(got, "FAIL m/x") {
|
||||
t.Errorf("the dependency must not be reported as a package of its own:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildFailureWithoutPackageEventIsStillReported(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"build-output","ImportPath":"a [a.test]","Output":"a_test.go:7:2: undefined: nope\n"}
|
||||
{"Action":"build-fail","ImportPath":"a [a.test]"}
|
||||
`
|
||||
got := feed(t, events)
|
||||
for _, want := range []string{"FAIL a [build failed]", "==== build output of a ====", "undefined: nope"} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("output lacks %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompilerPanicIsBuildOutputNotTestPanic(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"build-output","ImportPath":"a [a.test]","Output":"# a [a.test]\npanic: internal compiler error\n\ngoroutine 1 [running]:\n"}
|
||||
{"Action":"build-fail","ImportPath":"a [a.test]"}
|
||||
{"Action":"start","Package":"a"}
|
||||
{"Action":"output","Package":"a","Output":"FAIL\ta [build failed]\n"}
|
||||
{"Action":"fail","Package":"a","Elapsed":0,"FailedBuild":"a [a.test]"}
|
||||
`
|
||||
got := feed(t, events)
|
||||
if !strings.Contains(got, "==== build output of a ====\n # a [a.test]\n panic: internal compiler error") {
|
||||
t.Errorf("compiler diagnostic missing from the build output block:\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, "==== panic in") {
|
||||
t.Errorf("compiler output must not be reported as a test panic:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildVariantsOfOnePackageKeepSeparateOutput(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"build-output","ImportPath":"a [a.test]","Output":"a.go:1:1: broken for a.test\n"}
|
||||
{"Action":"build-output","ImportPath":"a [b.test]","Output":"a.go:1:1: broken for b.test\n"}
|
||||
{"Action":"build-fail","ImportPath":"a [a.test]"}
|
||||
{"Action":"build-fail","ImportPath":"a [b.test]"}
|
||||
{"Action":"start","Package":"a"}
|
||||
{"Action":"fail","Package":"a","Elapsed":0,"FailedBuild":"a [a.test]"}
|
||||
{"Action":"start","Package":"b"}
|
||||
{"Action":"fail","Package":"b","Elapsed":0,"FailedBuild":"a [b.test]"}
|
||||
`
|
||||
got := feed(t, events)
|
||||
if strings.Count(got, "==== build output of a ====") != 2 {
|
||||
t.Errorf("expected one output block per build variant:\n%s", got)
|
||||
}
|
||||
for _, want := range []string{"broken for a.test", "broken for b.test"} {
|
||||
if strings.Count(got, want) != 1 {
|
||||
t.Errorf("expected %q exactly once:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPassingPackageOutputIsNotPrinted(t *testing.T) {
|
||||
events := `
|
||||
{"Action":"output","Package":"a","Output":"level=info msg=\"noise between tests\"\n"}
|
||||
{"Action":"pass","Package":"a","Elapsed":0.5}
|
||||
`
|
||||
got := feed(t, events)
|
||||
if strings.Contains(got, "noise between tests") {
|
||||
t.Errorf("package output of a passing package should stay quiet:\n%s", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user