From f1ca64184a7f54f3b4da4f3523f2e6bcec4b6a3c Mon Sep 17 00:00:00 2001 From: Peter Schuurman Date: Fri, 24 Jan 2025 16:20:10 -0800 Subject: [PATCH 1/2] Add XMLName back to TestSuite struct --- test/k8s-integration/filter-junit.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/k8s-integration/filter-junit.go b/test/k8s-integration/filter-junit.go index 02b2a30f7..abf30f574 100644 --- a/test/k8s-integration/filter-junit.go +++ b/test/k8s-integration/filter-junit.go @@ -38,6 +38,7 @@ type TestSuites struct { TestSuite []TestSuite `xml:"testsuite"` } type TestSuite struct { + XMLName string `xml:"testsuite"` TestCases []TestCase `xml:"testcase"` } From b4d5b064f2561e8d261229fec52137d877e1dc2e Mon Sep 17 00:00:00 2001 From: Peter Schuurman Date: Wed, 29 Jan 2025 10:00:10 -0800 Subject: [PATCH 2/2] Update filter-junit.go parsing code to combine test cases from multiple test directories --- test/k8s-integration/filter-junit.go | 38 ++++++++++++++++------------ 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/test/k8s-integration/filter-junit.go b/test/k8s-integration/filter-junit.go index abf30f574..e140de636 100644 --- a/test/k8s-integration/filter-junit.go +++ b/test/k8s-integration/filter-junit.go @@ -72,13 +72,15 @@ func (s SkipReason) MarshalText() ([]byte, error) { // MergeJUnit merges all junit xml files found in sourceDirectories into a single xml file at destination, using the filter. // The merging removes duplicate skipped tests. The original files are deleted. func MergeJUnit(testFilter string, sourceDirectories []string, destination string) error { - var junit TestSuite var data []byte re := regexp.MustCompile(testFilter) var mergeErrors []string var filesToDelete []string + + // Keep only matching testcases. Testcases skipped in all test runs are only stored once. + filtered := map[string]TestCase{} for _, dir := range sourceDirectories { files, err := os.ReadDir(dir) if err != nil { @@ -87,7 +89,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin continue } for _, file := range files { - if !strings.HasSuffix(file.Name(), ".xml") { + if !strings.HasSuffix(file.Name(), ".xml") || file.Name() == "junit_runner.xml" { continue } fullFilename := filepath.Join(dir, file.Name()) @@ -96,24 +98,28 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin if err != nil { return err } - if err = xml.Unmarshal(data, &junit); err != nil { - return err + var testSuiteData TestSuites + if err = xml.Unmarshal(data, &testSuiteData); err != nil { + return fmt.Errorf("failed to unmarshal XML file %v: %w", fullFilename, err) + } + + for _, testsuite := range testSuiteData.TestSuite { + for _, testcase := range testsuite.TestCases { + if !re.MatchString(testcase.Name) { + continue + } + entry, ok := filtered[testcase.Name] + if !ok || // not present yet + entry.Skipped != "" && testcase.Skipped == "" { // replaced skipped test with real test run + filtered[testcase.Name] = testcase + } + } } } } // Keep only matching testcases. Testcases skipped in all test runs are only stored once. - filtered := map[string]TestCase{} - for _, testcase := range junit.TestCases { - if !re.MatchString(testcase.Name) { - continue - } - entry, ok := filtered[testcase.Name] - if !ok || // not present yet - entry.Skipped != "" && testcase.Skipped == "" { // replaced skipped test with real test run - filtered[testcase.Name] = testcase - } - } + var junit TestSuite junit.TestCases = nil for _, testcase := range filtered { junit.TestCases = append(junit.TestCases, testcase) @@ -122,7 +128,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin // Re-encode. data, err := xml.MarshalIndent(junit, "", " ") if err != nil { - return err + return fmt.Errorf("failed to marshal junit data: %w", err) } if err = os.WriteFile(destination, data, 0644); err != nil {