Skip to content

Commit

Permalink
remove duplicate writeJUnit func and migrate test
Browse files Browse the repository at this point in the history
Signed-off-by: Jose R. Gonzalez <jose@flutes.dev>
  • Loading branch information
komish committed Oct 25, 2022
1 parent 8e152e2 commit 0d08e68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
30 changes: 0 additions & 30 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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}, ".")
}
Expand Down
29 changes: 0 additions & 29 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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())
})
})
})
})
27 changes: 27 additions & 0 deletions internal/lib/lib_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"os"
"path"
"path/filepath"
"strings"

"github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
Expand Down Expand Up @@ -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())
})
})
})
})

0 comments on commit 0d08e68

Please sign in to comment.