-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run unit and integration tests with gotestsum #22541
Merged
Merged
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d35fd1c
Creating failing tests, test cases
b629d7b
Rewrite mage go test based on gotestsum
81a34eb
Merge branch 'master' into test_reports_incomplete
13b252b
Fix lint on tests
9d8e563
print go test output directly
747a429
clean up go.mod after installing go based tool
53717ea
Merge branch 'master' into test_reports_incomplete
4f7cb68
lets try with go get
0b98e70
try to switch to another directory before when installing gotestsum
7629ccb
Add gotestsum to heartbeat docker image for testing
2170d7e
fix verbosity mode
6e659b9
Merge branch 'master' into test_reports_incomplete
d2eff71
remove leftover debug output
1f3c661
Merge branch 'master' into test_reports_incomplete
8268937
Add gotestsum to metricbeat dockerfile
d5af77d
Merge branch 'master' into test_reports_incomplete
c576b27
Add missing gotestsum to Dockerfiles
c1ddf3f
Add unit tests for GoTest
91a86ed
remove test output tests from filebeat
e4a7dfc
fix notice file
ac578cb
Update common/cli tests to capture output as well
2d7b0e3
one more missing gotestsum
f393fdf
Do not upgrade x/sys
c567e11
use go.mod from master
a06fcd2
gotest testing cleanups and minor fixes
6448cb2
remove GoTestSummary
b015a84
Merge branch 'master' into test_reports_incomplete
e61ea51
Merge branch 'master' into test_reports_incomplete
942f413
remove go-junit-reporter from go.mod
5cabf71
update notice
e75a323
Install gotestsum via mage
28de5cc
Merge branch 'master' into test_reports_incomplete
703fcf5
fix import order
69b3387
Try to fix build by updating indirect dependency psutil
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
package mage | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
@@ -28,16 +27,14 @@ import ( | |
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jstemmer/go-junit-report/formatter" | ||
"github.com/jstemmer/go-junit-report/parser" | ||
"github.com/magefile/mage/mg" | ||
"github.com/magefile/mage/sh" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/v7/dev-tools/mage/gotool" | ||
) | ||
|
||
// GoTestArgs are the arguments used for the "go*Test" targets and they define | ||
|
@@ -52,6 +49,7 @@ type GoTestArgs struct { | |
OutputFile string // File to write verbose test output to. | ||
JUnitReportFile string // File to write a JUnit XML test report to. | ||
CoverageProfileFile string // Test coverage profile file (enables -cover). | ||
Output io.Writer // Write stderr and stdout to Output if set | ||
} | ||
|
||
// TestBinaryArgs are the arguments used when building binary for testing. | ||
|
@@ -183,40 +181,80 @@ func GoTestIntegrationForModule(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
// InstallGoTestTools installs additional tools that are required to run unit and integration tests. | ||
func InstallGoTestTools() { | ||
gotool.Install( | ||
gotool.Install.Package("gotest.tools/gotestsum"), | ||
) | ||
} | ||
|
||
// GoTest invokes "go test" and reports the results to stdout. It returns an | ||
// error if there was any failure executing the tests or if there were any | ||
// test failures. | ||
func GoTest(ctx context.Context, params GoTestArgs) error { | ||
mg.Deps(InstallGoTestTools) | ||
|
||
fmt.Println(">> go test:", params.TestName, "Testing") | ||
|
||
// Build args list to Go. | ||
args := []string{"test"} | ||
args = append(args, "-v") | ||
// We use gotestsum to drive the tests and produce a junit report. | ||
// The tool runs `go test -json` in order to produce a structured log which makes it easier | ||
// to parse the actual test output. | ||
// Of OutputFile is given the original JSON file will be written as well. | ||
// | ||
// The runner needs to set CLI flags for gotestsum and for "go test". We track the different | ||
// CLI flags in the gotestsumArgs and testArgs variables, such that we can finally produce command like: | ||
// $ gotestsum <gotestsum args> -- <go test args> | ||
// | ||
// The additional arguments given via GoTestArgs are applied to `go test` only. Callers can not | ||
// modify any of the gotestsum arguments. | ||
|
||
gotestsumArgs := []string{"--no-color"} | ||
if mg.Verbose() { | ||
gotestsumArgs = append(gotestsumArgs, "-f", "standard-verbose") | ||
} else { | ||
gotestsumArgs = append(gotestsumArgs, "-f", "standard-quiet") | ||
} | ||
if params.JUnitReportFile != "" { | ||
CreateDir(params.JUnitReportFile) | ||
gotestsumArgs = append(gotestsumArgs, "--junitfile", params.JUnitReportFile) | ||
} | ||
if params.OutputFile != "" { | ||
CreateDir(params.OutputFile) | ||
gotestsumArgs = append(gotestsumArgs, "--jsonfile", params.OutputFile+".json") | ||
} | ||
|
||
var testArgs []string | ||
|
||
// -race is only supported on */amd64 | ||
if os.Getenv("DEV_ARCH") == "amd64" { | ||
if params.Race { | ||
args = append(args, "-race") | ||
testArgs = append(testArgs, "-race") | ||
} | ||
} | ||
if len(params.Tags) > 0 { | ||
args = append(args, "-tags", strings.Join(params.Tags, " ")) | ||
params := strings.Join(params.Tags, " ") | ||
if params != "" { | ||
testArgs = append(testArgs, "-tags", params) | ||
} | ||
} | ||
if params.CoverageProfileFile != "" { | ||
params.CoverageProfileFile = createDir(filepath.Clean(params.CoverageProfileFile)) | ||
args = append(args, | ||
testArgs = append(testArgs, | ||
"-covermode=atomic", | ||
"-coverprofile="+params.CoverageProfileFile, | ||
) | ||
} | ||
args = append(args, params.ExtraFlags...) | ||
args = append(args, params.Packages...) | ||
testArgs = append(testArgs, params.ExtraFlags...) | ||
testArgs = append(testArgs, params.Packages...) | ||
|
||
goTest := makeCommand(ctx, params.Env, "go", args...) | ||
args := append(gotestsumArgs, append([]string{"--"}, testArgs...)...) | ||
|
||
goTest := makeCommand(ctx, params.Env, "gotestsum", args...) | ||
// Wire up the outputs. | ||
bufferOutput := new(bytes.Buffer) | ||
outputs := []io.Writer{bufferOutput} | ||
var outputs []io.Writer | ||
if params.Output != nil { | ||
outputs = append(outputs, params.Output) | ||
} | ||
|
||
if params.OutputFile != "" { | ||
fileOutput, err := os.Create(createDir(params.OutputFile)) | ||
|
@@ -227,18 +265,16 @@ func GoTest(ctx context.Context, params GoTestArgs) error { | |
outputs = append(outputs, fileOutput) | ||
} | ||
output := io.MultiWriter(outputs...) | ||
goTest.Stdout = output | ||
goTest.Stderr = output | ||
|
||
if mg.Verbose() { | ||
if params.Output == nil { | ||
goTest.Stdout = io.MultiWriter(output, os.Stdout) | ||
goTest.Stderr = io.MultiWriter(output, os.Stderr) | ||
} else { | ||
goTest.Stdout = output | ||
goTest.Stderr = output | ||
} | ||
|
||
// Execute 'go test' and measure duration. | ||
start := time.Now() | ||
err := goTest.Run() | ||
duration := time.Since(start) | ||
|
||
var goTestErr *exec.ExitError | ||
if err != nil { | ||
// Command ran. | ||
|
@@ -251,30 +287,11 @@ func GoTest(ctx context.Context, params GoTestArgs) error { | |
goTestErr = exitErr | ||
} | ||
|
||
// Parse the verbose test output. | ||
report, err := parser.Parse(bytes.NewBuffer(bufferOutput.Bytes()), BeatName) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to parse go test output") | ||
} | ||
if goTestErr != nil && len(report.Packages) == 0 { | ||
if goTestErr != nil { | ||
// No packages were tested. Probably the code didn't compile. | ||
fmt.Println(bytes.NewBuffer(bufferOutput.Bytes()).String()) | ||
return errors.Wrap(goTestErr, "go test returned a non-zero value") | ||
} | ||
|
||
// Generate a JUnit XML report. | ||
if params.JUnitReportFile != "" { | ||
junitReport, err := os.Create(createDir(params.JUnitReportFile)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create junit report") | ||
} | ||
defer junitReport.Close() | ||
|
||
if err = formatter.JUnitReportXML(report, false, runtime.Version(), junitReport); err != nil { | ||
return errors.Wrap(err, "failed to write junit report") | ||
} | ||
} | ||
|
||
// Generate a HTML code coverage report. | ||
var htmlCoverReport string | ||
if params.CoverageProfileFile != "" { | ||
|
@@ -288,27 +305,9 @@ func GoTest(ctx context.Context, params GoTestArgs) error { | |
} | ||
} | ||
|
||
// Summarize the results and log to stdout. | ||
summary, err := NewGoTestSummary(duration, report, map[string]string{ | ||
"Output File": params.OutputFile, | ||
"JUnit Report": params.JUnitReportFile, | ||
"Coverage Report": htmlCoverReport, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if !mg.Verbose() && summary.Fail > 0 { | ||
fmt.Println(summary.Failures()) | ||
} | ||
fmt.Println(summary.String()) | ||
|
||
// Return an error indicating that testing failed. | ||
if summary.Fail > 0 || goTestErr != nil { | ||
if goTestErr != nil { | ||
fmt.Println(">> go test:", params.TestName, "Test Failed") | ||
if summary.Fail > 0 { | ||
return errors.Errorf("go test failed: %d test failures", summary.Fail) | ||
} | ||
|
||
return errors.Wrap(goTestErr, "go test returned a non-zero value") | ||
} | ||
|
||
|
@@ -329,117 +328,10 @@ func makeCommand(ctx context.Context, env map[string]string, cmd string, args .. | |
c.Stderr = os.Stderr | ||
c.Stdin = os.Stdin | ||
log.Println("exec:", cmd, strings.Join(args, " ")) | ||
fmt.Println("exec:", cmd, strings.Join(args, " ")) | ||
return c | ||
} | ||
|
||
// GoTestSummary is a summary of test results. | ||
type GoTestSummary struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GoTestSummary is not used anymore. |
||
*parser.Report // Report generated by parsing test output. | ||
Pass int // Number of passing tests. | ||
Fail int // Number of failed tests. | ||
Skip int // Number of skipped tests. | ||
Packages int // Number of packages tested. | ||
Duration time.Duration // Total go test running duration. | ||
Files map[string]string | ||
} | ||
|
||
// NewGoTestSummary builds a new GoTestSummary. It returns an error if it cannot | ||
// resolve the absolute paths to the given files. | ||
func NewGoTestSummary(d time.Duration, r *parser.Report, outputFiles map[string]string) (*GoTestSummary, error) { | ||
files := map[string]string{} | ||
for name, file := range outputFiles { | ||
if file == "" { | ||
continue | ||
} | ||
absFile, err := filepath.Abs(file) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed resolving absolute path for %v", file) | ||
} | ||
files[name+":"] = absFile | ||
} | ||
|
||
summary := &GoTestSummary{ | ||
Report: r, | ||
Duration: d, | ||
Packages: len(r.Packages), | ||
Files: files, | ||
} | ||
|
||
for _, pkg := range r.Packages { | ||
for _, t := range pkg.Tests { | ||
switch t.Result { | ||
case parser.PASS: | ||
summary.Pass++ | ||
case parser.FAIL: | ||
summary.Fail++ | ||
case parser.SKIP: | ||
summary.Skip++ | ||
default: | ||
return nil, errors.Errorf("Unknown test result value: %v", t.Result) | ||
} | ||
} | ||
} | ||
|
||
return summary, nil | ||
} | ||
|
||
// Failures returns a string containing the list of failed test cases and their | ||
// output. | ||
func (s *GoTestSummary) Failures() string { | ||
b := new(strings.Builder) | ||
|
||
if s.Fail > 0 { | ||
fmt.Fprintln(b, "FAILURES:") | ||
for _, pkg := range s.Report.Packages { | ||
for _, t := range pkg.Tests { | ||
if t.Result != parser.FAIL { | ||
continue | ||
} | ||
fmt.Fprintln(b, "Package:", pkg.Name) | ||
fmt.Fprintln(b, "Test: ", t.Name) | ||
for _, line := range t.Output { | ||
if strings.TrimSpace(line) != "" { | ||
fmt.Fprintln(b, line) | ||
} | ||
} | ||
fmt.Fprintln(b, "----") | ||
} | ||
} | ||
} | ||
|
||
return strings.TrimRight(b.String(), "\n") | ||
} | ||
|
||
// String returns a summary of the testing results (number of fail/pass/skip, | ||
// test duration, number packages, output files). | ||
func (s *GoTestSummary) String() string { | ||
b := new(strings.Builder) | ||
|
||
fmt.Fprintln(b, "SUMMARY:") | ||
fmt.Fprintln(b, " Fail: ", s.Fail) | ||
fmt.Fprintln(b, " Skip: ", s.Skip) | ||
fmt.Fprintln(b, " Pass: ", s.Pass) | ||
fmt.Fprintln(b, " Packages:", len(s.Report.Packages)) | ||
fmt.Fprintln(b, " Duration:", s.Duration) | ||
|
||
// Sort the list of files and compute the column width. | ||
var names []string | ||
var nameWidth int | ||
for name := range s.Files { | ||
if len(name) > nameWidth { | ||
nameWidth = len(name) | ||
} | ||
names = append(names, name) | ||
} | ||
sort.Strings(names) | ||
|
||
for _, name := range names { | ||
fmt.Fprintf(b, " %-*s %s\n", nameWidth, name, s.Files[name]) | ||
} | ||
|
||
return strings.TrimRight(b.String(), "\n") | ||
} | ||
|
||
// BuildSystemTestBinary runs BuildSystemTestGoBinary with default values. | ||
func BuildSystemTestBinary() error { | ||
return BuildSystemTestGoBinary(DefaultTestBinaryArgs()) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we create a
InstallGotestsum
target and add it here as dependency?We do this with
InstallGoLicenser
for example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will give this a try. Thanks.