From 3b14f646e4706277b058b42b7efb66daedbfe983 Mon Sep 17 00:00:00 2001 From: Kirsten Laskoski Date: Mon, 1 Jun 2026 17:38:09 -0400 Subject: [PATCH] junit: switch time to zero instead of timestamp The JUnit format expects the time attribute to represent the duration of a test, not the timestamp. The JUnit output from kube-compare already correctly adds the timestamp attribute as a timestamp, but then sets time to be the same. For tools which are stricter about the JUnit schema, this causes errors parsing the kube-compare output. This commit switches to always setting `time="0"` in the `testsuites`, `testsuite`, and `testcase` tags. Since the duration would not be relevant, `"0"` is chosen as a default. Alternatively, the attribute could be removed since it is optional, but since it is already present, setting it to zero works as well. Assisted-by: Cursor Signed-off-by: Kirsten Laskoski --- pkg/compare/testdata/JunitOutput/localout.golden | 14 +++++++------- pkg/junit/junit.go | 14 ++++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/compare/testdata/JunitOutput/localout.golden b/pkg/compare/testdata/JunitOutput/localout.golden index 53573998..2a4f9017 100644 --- a/pkg/compare/testdata/JunitOutput/localout.golden +++ b/pkg/compare/testdata/JunitOutput/localout.golden @@ -1,31 +1,31 @@ - - + + - + diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper --- TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper DATE +++ TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper DATE @@ -10,7 +10,7 @@ revisionHistoryLimit: 10 selector: matchLabels: - k8s-app: dashboard-metrics-scraper + k8s-app: dashboard-metrics-scraper-diff template: metadata: labels: - + - + - + - + diff --git a/pkg/junit/junit.go b/pkg/junit/junit.go index 0725a9d9..6bea7d38 100644 --- a/pkg/junit/junit.go +++ b/pkg/junit/junit.go @@ -7,6 +7,10 @@ import ( "time" ) +// zeroDuration is the time attribute value for testsuites, testsuite, and testcase elements. +// JUnit expects elapsed seconds; kube-compare does not track per-test duration. +const zeroDuration = "0" + // TestSuites is a collection of JUnit test suites. type TestSuites struct { XMLName xml.Name `xml:"testsuites"` @@ -22,7 +26,7 @@ type TestSuites struct { func NewTestSuites(name string) *TestSuites { testSuites := TestSuites{ Name: name, - Time: time.Now().Format(time.RFC3339), + Time: zeroDuration, } return &testSuites } @@ -54,6 +58,9 @@ type TestSuite struct { } func (id *TestSuite) AddCase(tcase TestCase) { + if tcase.Time == "" { + tcase.Time = zeroDuration + } id.TestCases = append(id.TestCases, tcase) id.Tests += 1 if tcase.Failure != nil { @@ -98,11 +105,10 @@ type Failure struct { } func NewTestSuite(name string) TestSuite { - timestamp := time.Now().Format(time.RFC3339) return TestSuite{ Name: name, - Timestamp: timestamp, - Time: timestamp, + Timestamp: time.Now().Format(time.RFC3339), + Time: zeroDuration, } }