Skip to content

Commit

Permalink
Add junit support for non-e2e tests/multi-suite (#851)
Browse files Browse the repository at this point in the history
The e2e tests report via junit but with the top-level item
in the xml being a `<testsuite>`. Other implementations seem
to default to allowing multiple testsuites and so they have a
higher level `testsuites` object.

This PR enables a new post-processing path and makes the older
junit parsing only apply to the result-type `e2e`. The new,
`junit` result-type will use this more generalized logic.

Fixes #849

Signed-off-by: John Schnake <[email protected]>
  • Loading branch information
johnSchnake authored Aug 23, 2019
1 parent 451b313 commit a268401
Show file tree
Hide file tree
Showing 33 changed files with 631 additions and 1,749 deletions.
14 changes: 0 additions & 14 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions pkg/client/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ import (

"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/heptio/sonobuoy/pkg/client/results/e2e"
"github.com/onsi/ginkgo/reporters"
"github.com/pkg/errors"
)

// GetTests extracts the junit results from a sonobuoy archive and returns the requested tests.
func (*SonobuoyClient) GetTests(reader io.Reader, show string) ([]reporters.JUnitTestCase, error) {
func (*SonobuoyClient) GetTests(reader io.Reader, show string) ([]results.JUnitTestCase, error) {
read := results.NewReaderWithVersion(reader, "irrelevant")
junitResults := reporters.JUnitTestSuite{}
e2eJunitPath := path.Join(results.PluginsDir, e2e.ResultsSubdirectory, e2e.JUnitResultsFile)
legacye2eJunitPath := path.Join(results.PluginsDir, e2e.LegacyResultsSubdirectory, e2e.JUnitResultsFile)
junitResults := results.JUnitTestSuite{}
e2eJUnitPath := path.Join(results.PluginsDir, e2e.ResultsSubdirectory, e2e.JUnitResultsFile)
legacye2eJUnitPath := path.Join(results.PluginsDir, e2e.LegacyResultsSubdirectory, e2e.JUnitResultsFile)

found := false
err := read.WalkFiles(
Expand All @@ -46,7 +45,7 @@ func (*SonobuoyClient) GetTests(reader io.Reader, show string) ([]reporters.JUni
}
// TODO(chuckha) consider reusing this function for any generic e2e-esque plugin results.
// TODO(chuckha) consider using path.Join()
if path == e2eJunitPath || path == legacye2eJunitPath {
if path == e2eJUnitPath || path == legacye2eJUnitPath {
found = true
return results.ExtractFileIntoStruct(path, path, info, &junitResults)
}
Expand All @@ -57,35 +56,35 @@ func (*SonobuoyClient) GetTests(reader io.Reader, show string) ([]reporters.JUni
}

if !found {
return nil, fmt.Errorf("failed to find results file %q in archive", e2eJunitPath)
return nil, fmt.Errorf("failed to find results file %q in archive", e2eJUnitPath)
}

out := make([]reporters.JUnitTestCase, 0)
out := make([]results.JUnitTestCase, 0)
if show == "passed" || show == "all" {
out = append(out, results.Filter(results.Passed, junitResults)...)
out = append(out, results.JUnitFilter(results.JUnitPassed, junitResults)...)
}
if show == "failed" || show == "all" {
out = append(out, results.Filter(results.Failed, junitResults)...)
out = append(out, results.JUnitFilter(results.JUnitFailed, junitResults)...)
}
if show == "skipped" || show == "all" {
out = append(out, results.Filter(results.Skipped, junitResults)...)
out = append(out, results.JUnitFilter(results.JUnitSkipped, junitResults)...)
}
sort.Sort(results.AlphabetizedTestCases(out))
sort.Sort(results.JUnitAlphabetizedTestCases(out))
return out, nil
}

// Focus returns a value to be used in the E2E_FOCUS variable that is
// representative of the test cases in the struct.
func Focus(testCases []reporters.JUnitTestCase) string {
func Focus(testCases []results.JUnitTestCase) string {
testNames := make([]string, len(testCases))
for i, tc := range testCases {
testNames[i] = regexp.QuoteMeta(tc.Name)
}
return strings.Join(testNames, "|")
}

// PrintableTestCases nicely strings a []reporters.JunitTestCase
type PrintableTestCases []reporters.JUnitTestCase
// PrintableTestCases nicely strings a []results.JUnitTestCase
type PrintableTestCases []results.JUnitTestCase

func (p PrintableTestCases) String() string {
if len(p) == 0 {
Expand Down
23 changes: 12 additions & 11 deletions pkg/client/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"strings"
"testing"

"github.com/onsi/ginkgo/reporters"
"github.com/heptio/sonobuoy/pkg/client/results"

"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -95,15 +96,15 @@ func TestString(t *testing.T) {
}{
{
desc: "No tests should report empty string",
cases: PrintableTestCases([]reporters.JUnitTestCase{}),
cases: PrintableTestCases([]results.JUnitTestCase{}),
expect: "",
}, {
desc: "Nil tests should report empty string",
cases: PrintableTestCases(nil),
expect: "",
}, {
desc: "Should not end with extra new line",
cases: PrintableTestCases([]reporters.JUnitTestCase{
cases: PrintableTestCases([]results.JUnitTestCase{
{Name: "a"},
{Name: "b"},
}),
Expand All @@ -128,28 +129,28 @@ func TestFocus(t *testing.T) {
}{
{
desc: "No test should result in an empty string",
cases: []reporters.JUnitTestCase{},
cases: []results.JUnitTestCase{},
expect: "",
},
{
desc: "Single test with no regexp characters is not changed",
cases: []reporters.JUnitTestCase{
reporters.JUnitTestCase{Name: "this is a test"},
cases: []results.JUnitTestCase{
{Name: "this is a test"},
},
expect: "this is a test",
},
{
desc: "Test with special regexp characters should be escaped",
cases: []reporters.JUnitTestCase{
reporters.JUnitTestCase{Name: "[sig-apps] test-1 (1.15) [Conformance]"},
cases: []results.JUnitTestCase{
{Name: "[sig-apps] test-1 (1.15) [Conformance]"},
},
expect: `\[sig-apps\] test-1 \(1\.15\) \[Conformance\]`,
},
{
desc: "Multiple tests should be separated with '|'",
cases: []reporters.JUnitTestCase{
reporters.JUnitTestCase{Name: "[sig-apps] test-1 [Conformance]"},
reporters.JUnitTestCase{Name: "[sig-apps] test-2"},
cases: []results.JUnitTestCase{
{Name: "[sig-apps] test-1 [Conformance]"},
{Name: "[sig-apps] test-2"},
},
expect: `\[sig-apps\] test-1 \[Conformance\]|\[sig-apps\] test-2`,
},
Expand Down
Loading

0 comments on commit a268401

Please sign in to comment.