From f5338b07f9e27c69448d2b2610055e8df4c15549 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 16 Oct 2022 13:52:04 +0200 Subject: [PATCH] Validate JUnit output against Jenkins JUnit XSD (#134) * Validate JUnit output against Jenkins JUnix XSD * Add missing Jenkins JUnit XSD * Add time to TestCase for #127 --- Dockerfile.bats | 2 +- Makefile | 3 + acceptance.bats | 7 +++ fixtures/junit.xsd | 118 +++++++++++++++++++++++++++++++++++++++ pkg/output/junit.go | 23 ++++---- pkg/output/junit_test.go | 6 +- 6 files changed, 142 insertions(+), 17 deletions(-) create mode 100644 fixtures/junit.xsd 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/fixtures/junit.xsd b/fixtures/junit.xsd new file mode 100644 index 0000000..9ee9cea --- /dev/null +++ b/fixtures/junit.xsd @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pkg/output/junit.go b/pkg/output/junit.go index 19177d3..e9e358d 100644 --- a/pkg/output/junit.go +++ b/pkg/output/junit.go @@ -33,22 +33,22 @@ 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 { XMLName xml.Name `xml:"testcase"` Name string `xml:"name,attr"` ClassName string `xml:"classname,attr"` + Time int `xml:"time,attr"` // Optional, but for Buildkite support https://github.com/yannh/kubeconform/issues/127 Skipped *TestCaseSkipped `xml:"skipped,omitempty"` Error *TestCaseError `xml:"error,omitempty"` Failure []TestCaseError `xml:"failure,omitempty"` @@ -100,8 +100,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..dd8e5b7 100644 --- a/pkg/output/junit_test.go +++ b/pkg/output/junit_test.go @@ -48,8 +48,7 @@ metadata: }, "\n" + " \n" + - " \n" + - " \n" + + " \n" + " \n" + "\n", }, @@ -82,8 +81,7 @@ metadata: }, "\n" + " \n" + - " \n" + - " \n" + + " \n" + " \n" + "\n", },