From 0d08e685ab63ee3e980cf0a75274382d7ab19239 Mon Sep 17 00:00:00 2001 From: "Jose R. Gonzalez" Date: Tue, 25 Oct 2022 12:21:28 -0500 Subject: [PATCH] remove duplicate writeJUnit func and migrate test Signed-off-by: Jose R. Gonzalez --- cmd/check.go | 30 ------------------------------ cmd/check_test.go | 29 ----------------------------- internal/lib/lib_container_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 59 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index ad9283b5..39561c36 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -1,15 +1,8 @@ package cmd import ( - "bytes" - "context" "strings" - "github.com/redhat-openshift-ecosystem/openshift-preflight/certification/artifacts" - "github.com/redhat-openshift-ecosystem/openshift-preflight/certification/formatters" - "github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime" - - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -35,29 +28,6 @@ func checkCmd() *cobra.Command { return checkCmd } -// writeJUnit will write results as JUnit XML using the built-in formatter. -func writeJUnit(ctx context.Context, results runtime.Results) error { - var cfg runtime.Config - cfg.ResponseFormat = "junitxml" - - junitformatter, err := formatters.NewForConfig(cfg.ReadOnly()) - if err != nil { - return err - } - junitResults, err := junitformatter.Format(ctx, results) - if err != nil { - return err - } - - junitFilename, err := artifacts.WriteFile("results-junit.xml", bytes.NewReader((junitResults))) - if err != nil { - return err - } - log.Tracef("JUnitXML written to %s", junitFilename) - - return nil -} - func resultsFilenameWithExtension(ext string) string { return strings.Join([]string{"results", ext}, ".") } diff --git a/cmd/check_test.go b/cmd/check_test.go index 9eace766..82671901 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -1,12 +1,9 @@ package cmd import ( - "context" "os" - "path/filepath" "github.com/redhat-openshift-ecosystem/openshift-preflight/certification/artifacts" - "github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime" "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/lib" . "github.com/onsi/ginkgo/v2" @@ -97,30 +94,4 @@ var _ = Describe("cmd package check command", func() { }) }) }) - - Describe("JUnit", func() { - var results *runtime.Results - var junitfile string - - BeforeEach(func() { - results = &runtime.Results{ - TestedImage: "registry.example.com/example/image:0.0.1", - PassedOverall: true, - TestedOn: runtime.UnknownOpenshiftClusterVersion(), - CertificationHash: "sha256:deadb33f", - Passed: []runtime.Result{}, - Failed: []runtime.Result{}, - Errors: []runtime.Result{}, - } - junitfile = filepath.Join(artifacts.Path(), "results-junit.xml") - }) - - When("The additional JUnitXML results file is requested", func() { - It("should be written to the artifacts directory without error", func() { - Expect(writeJUnit(context.TODO(), *results)).To(Succeed()) - _, err := os.Stat(junitfile) - Expect(err).ToNot(HaveOccurred()) - }) - }) - }) }) diff --git a/internal/lib/lib_container_test.go b/internal/lib/lib_container_test.go index dbf33b42..e46c4ffd 100644 --- a/internal/lib/lib_container_test.go +++ b/internal/lib/lib_container_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "os" "path" + "path/filepath" "strings" "github.com/redhat-openshift-ecosystem/openshift-preflight/certification" @@ -428,4 +429,30 @@ var _ = Describe("Lib Container Functions", func() { Expect(rwIsExpectedType).To(BeTrue()) }) }) + + Describe("JUnit", func() { + var results *runtime.Results + var junitfile string + + BeforeEach(func() { + results = &runtime.Results{ + TestedImage: "registry.example.com/example/image:0.0.1", + PassedOverall: true, + TestedOn: runtime.UnknownOpenshiftClusterVersion(), + CertificationHash: "sha256:deadb33f", + Passed: []runtime.Result{}, + Failed: []runtime.Result{}, + Errors: []runtime.Result{}, + } + junitfile = filepath.Join(artifacts.Path(), "results-junit.xml") + }) + + When("The additional JUnitXML results file is requested", func() { + It("should be written to the artifacts directory without error", func() { + Expect(writeJUnit(context.TODO(), *results)).To(Succeed()) + _, err := os.Stat(junitfile) + Expect(err).ToNot(HaveOccurred()) + }) + }) + }) })