From 119edd4c0f5d36be8db2c5dbe5aab7c8ab91dcdd Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 16 Oct 2022 13:36:14 +0200 Subject: [PATCH] Validate JUnit output against Jenkins JUnix XSD --- Dockerfile.bats | 2 +- Makefile | 3 +++ acceptance.bats | 7 +++++++ pkg/output/junit.go | 22 ++++++++++------------ pkg/output/junit_test.go | 2 -- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Dockerfile.bats b/Dockerfile.bats index 7915050..f859749 100644 --- a/Dockerfile.bats +++ b/Dockerfile.bats @@ -1,5 +1,5 @@ FROM bats/bats:v1.2.1 -RUN apk --no-cache add ca-certificates parallel +RUN apk --no-cache add ca-certificates parallel libxml2-utils COPY dist/kubeconform_linux_amd64/kubeconform /code/bin/ COPY acceptance.bats acceptance-nonetwork.bats /code/ COPY fixtures /code/fixtures diff --git a/Makefile b/Makefile index 2faefe4..bfd4cb3 100644 --- a/Makefile +++ b/Makefile @@ -40,3 +40,6 @@ release: update-deps: go get -u ./... go mod tidy + +update-junit-xsd: + curl https://raw.githubusercontent.com/junit-team/junit5/main/platform-tests/src/test/resources/jenkins-junit.xsd > fixtures/junit.xsd diff --git a/acceptance.bats b/acceptance.bats index fde5999..e16c220 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -316,3 +316,10 @@ resetCacheFolder() { [ "$status" -eq 0 ] [ "$output" = 'Summary: 100000 resources found parsing stdin - Valid: 100000, Invalid: 0, Errors: 0, Skipped: 0' ] } + +@test "JUnit output can be validated against the Junit schema definition" { + run bash -c "bin/kubeconform -output junit -summary fixtures/valid.yaml > output.xml" + [ "$status" -eq 0 ] + run xmllint --noout --schema fixtures/junit.xsd output.xml + [ "$status" -eq 0 ] +} diff --git a/pkg/output/junit.go b/pkg/output/junit.go index 19177d3..50d4e77 100644 --- a/pkg/output/junit.go +++ b/pkg/output/junit.go @@ -33,16 +33,15 @@ type Property struct { } type TestSuite struct { - XMLName xml.Name `xml:"testsuite"` - Properties []*Property `xml:"properties>property,omitempty"` - Cases []TestCase `xml:"testcase"` - Name string `xml:"name,attr"` - Id int `xml:"id,attr"` - Tests int `xml:"tests,attr"` - Failures int `xml:"failures,attr"` - Errors int `xml:"errors,attr"` - Disabled int `xml:"disabled,attr"` - Skipped int `xml:"skipped,attr"` + XMLName xml.Name `xml:"testsuite"` + Cases []TestCase `xml:"testcase"` + Name string `xml:"name,attr"` + Id int `xml:"id,attr"` + Tests int `xml:"tests,attr"` + Failures int `xml:"failures,attr"` + Errors int `xml:"errors,attr"` + Disabled int `xml:"disabled,attr"` + Skipped int `xml:"skipped,attr"` } type TestCase struct { @@ -100,8 +99,7 @@ func (o *junito) Write(result validator.Result) error { Name: result.Resource.Path, Id: o.id, Tests: 0, Failures: 0, Errors: 0, Disabled: 0, Skipped: 0, - Cases: make([]TestCase, 0), - Properties: make([]*Property, 0), + Cases: make([]TestCase, 0), } o.suites[result.Resource.Path] = suite } diff --git a/pkg/output/junit_test.go b/pkg/output/junit_test.go index c8e6ee6..c9ab96c 100644 --- a/pkg/output/junit_test.go +++ b/pkg/output/junit_test.go @@ -48,7 +48,6 @@ metadata: }, "\n" + " \n" + - " \n" + " \n" + " \n" + "\n", @@ -82,7 +81,6 @@ metadata: }, "\n" + " \n" + - " \n" + " \n" + " \n" + "\n",