From f6cdd37046459fd76dd7b91c0c85dbf039d9a99e Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 14 Nov 2022 14:57:31 +0100 Subject: [PATCH] e2e framework: move JUnit write function This makes it possible to unit test it. --- test/e2e/framework/internal/junit/junit.go | 42 +++++++++++++++++++ test/e2e/framework/test_context.go | 47 +++++++++------------- 2 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 test/e2e/framework/internal/junit/junit.go diff --git a/test/e2e/framework/internal/junit/junit.go b/test/e2e/framework/internal/junit/junit.go new file mode 100644 index 00000000000..ffdd7cdddef --- /dev/null +++ b/test/e2e/framework/internal/junit/junit.go @@ -0,0 +1,42 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package junit + +import ( + "github.com/onsi/ginkgo/v2" + "github.com/onsi/ginkgo/v2/reporters" + "github.com/onsi/ginkgo/v2/types" +) + +// WriteJUnitReport generates a JUnit file that is shorter than the one +// normally written by `ginkgo --junit-report`. This is needed because the full +// report can become too large for tools like Spyglass +// (https://github.com/kubernetes/kubernetes/issues/111510). +func WriteJUnitReport(report ginkgo.Report, filename string) { + config := reporters.JunitReportConfig{ + // Remove details for specs where we don't care. + OmitTimelinesForSpecState: types.SpecStatePassed | types.SpecStateSkipped, + + // Don't write . The same text is + // also in the full text for the failure. If we were to write + // both, then tools like kettle and spyglass would concatenate + // the two strings and thus show duplicated information. + OmitFailureMessageAttr: true, + } + + reporters.GenerateJUnitReportWithConfig(report, filename, config) +} diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go index 4fdcd0219cf..52bef216713 100644 --- a/test/e2e/framework/test_context.go +++ b/test/e2e/framework/test_context.go @@ -30,7 +30,6 @@ import ( "time" "github.com/onsi/ginkgo/v2" - "github.com/onsi/ginkgo/v2/reporters" "github.com/onsi/ginkgo/v2/types" gomegaformat "github.com/onsi/gomega/format" @@ -40,6 +39,7 @@ import ( "k8s.io/klog/v2" kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" + "k8s.io/kubernetes/test/e2e/framework/internal/junit" "k8s.io/kubernetes/test/utils/kubeconfig" ) @@ -522,32 +522,23 @@ func AfterReadingAllFlags(t *TestContextType) { } if TestContext.ReportDir != "" { - ginkgo.ReportAfterSuite("Kubernetes e2e JUnit report", writeJUnitReport) + ginkgo.ReportAfterSuite("Kubernetes e2e JUnit report", func(report ginkgo.Report) { + // With Ginkgo v1, we used to write one file per + // parallel node. Now Ginkgo v2 automatically merges + // all results into a report for us. The 01 suffix is + // kept in case that users expect files to be called + // "junit_.xml". + junitReport := path.Join(TestContext.ReportDir, "junit_"+TestContext.ReportPrefix+"01.xml") + + // writeJUnitReport generates a JUnit file in the e2e + // report directory that is shorter than the one + // normally written by `ginkgo --junit-report`. This is + // needed because the full report can become too large + // for tools like Spyglass + // (https://github.com/kubernetes/kubernetes/issues/111510). + // + // Users who want the full report can use `--junit-report`. + junit.WriteJUnitReport(report, junitReport) + }) } } - -// writeJUnitReport generates a JUnit file in the e2e report directory that is -// shorter than the one normally written by `ginkgo --junit-report`. This is -// needed because the full report can become too large for tools like Spyglass -// (https://github.com/kubernetes/kubernetes/issues/111510). -// -// Users who want the full report can use `--junit-report`. -func writeJUnitReport(report ginkgo.Report) { - config := reporters.JunitReportConfig{ - // Remove details for specs where we don't care. - OmitTimelinesForSpecState: types.SpecStatePassed | types.SpecStateSkipped, - - // Don't write . The same text is - // also in the full text for the failure. If we were to write - // both, then tools like kettle and spyglass would concatenate - // the two strings and thus show duplicated information. - OmitFailureMessageAttr: true, - } - - // With Ginkgo v1, we used to write one file per parallel node. Now - // Ginkgo v2 automatically merges all results into a report for us. The - // 01 suffix is kept in case that users expect files to be called - // "junit_.xml". - junitReport := path.Join(TestContext.ReportDir, "junit_"+TestContext.ReportPrefix+"01.xml") - reporters.GenerateJUnitReportWithConfig(report, junitReport, config) -}