Cherry-pick #22541 to 7.x: Run unit and integration tests with gotestsum #23512
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.
Cherry-pick of PR #22541 to 7.x branch. Original message:
What does this PR do?
The PR introduces gotestsum to drive the go based unit and integration tests. This is meant as replacement for
github.com/jstemmer/go-junit-report
, which seems to be unmaintained for a while.Only the GoTest mage helper is modified to use gotestsum instead of go-junit-reporter. In addition to the change we add unit tests for GoTest that do run actual tests. The output of these helper-tests is captured and validated against known regular expressions.
Why is it important?
We use go-junit-reporter in order to create a xunit like report that is uploaded after the tests have finished. Unfortunately the project seems to be unmaintained for at least a year.
We discovered that we have a few issues with our test output. For example lines have been missing, wrong tests have been marked as failed (on panic in a leaked go-routine), or failed tests have not been reported at all. The new unit tests try to cover most cases that have not been reported correctly, in order to validate that the change actually fixes reporting.
goroutinepanic handling
This one might be a little flaky. But it seems that a 'badly' scheduled unmanaged go-routine can bring down and flag unrelated tests. The stack trace in the failed test actually matches the original test that did spawn the go-routine.
We did try to reproduce the issue by running 2 tests. The first test creates a go-routine that sleeps for 2 seconds and finally panics. The second tests waits for 5 seconds. Depending on the order the test is run we did see the second test to be marked as "Failed". A sample output using go-junit-reporter shows
false_positive_failure
as failed:Unfortunately the problem is not solved by switching to gotestsum (or running
go test
). Here it is up to us to ensure we never leak go-routines when a test returns. For example in auditbeat we found tests like:The tests did fail (especially on windows) and the go routine did bring down another unrelated test.
testify assert output missing
We use the testify package for validation. The package adds a newline before the actual report. Unfortunately the assert output was missing from all test output in jenkins. We finally tracked down the problem to go-junit-reporter "eating" our test output.
We did create some failing sample tests in order debug the issue. The expected output was:
Unfortunately the actual output was missing the failed assertions:
The new unit tests for GoTest show that the problem is solved by gotestsum
Checklist
- [ ] I have made corresponding changes to the documentation- [ ] I have made corresponding change to the default configuration files- [ ] I have added an entry inCHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.How to test this PR locally
make test
and the differentmage
testing commands will usegotestsum
now.Limitations
Tests must not print directly or indirectly to stdout/stdout. The
go test -json
command does not mark a test as passed/failed in that case, which will subsequently mark the test as failed (unknown state) in the junit report. The test run itself succeeds, though.Related issues