Skip to content

Commit

Permalink
Moving JUnit support to secondary flag rather than main output. Using…
Browse files Browse the repository at this point in the history
… structs to reorganize results and remove unnecessary metadata from Junit output
  • Loading branch information
Dan Curran committed May 1, 2024
1 parent 9102a3f commit fec18ea
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 210 deletions.
18 changes: 13 additions & 5 deletions cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var (
disabledChecksFlag []string
// outputFormatFlag contains the output format the user has specified: default, yaml or json.
outputFormatFlag string
// junitFileFlag set the output file to output results in junit format.
junitFileFlag string
// TODO(komish): The description of this would imply that it's important. We generally
// allow users to set overrides using the --set flag. The compiler says it's unused, so something doesn't
// quite align. For now, we'll ignore this.
Expand Down Expand Up @@ -146,17 +148,13 @@ func NewVerifyCmd(config *viper.Viper) *cobra.Command {
reportFormat := apireport.YamlReport
if outputFormatFlag == "json" {
reportFormat = apireport.JSONReport
} else if outputFormatFlag == "junit" {
reportFormat = apireport.JUnitReport
}

reportName := ""
if reportToFile {
reportName = "report.yaml"
if outputFormatFlag == "json" {
reportName = "report.json"
} else if outputFormatFlag == "junit" {
reportName = "report.xml"
}
}

Expand Down Expand Up @@ -225,6 +223,14 @@ func NewVerifyCmd(config *viper.Viper) *cobra.Command {
return reportErr
}

if junitFileFlag != "" {
junitReport, junitReportErr := verifier.GetReport().JUnitContent()
if junitReportErr != nil {
utils.LogWarning(fmt.Sprintf("Failed to write JUnit output: %s", junitReportErr))
}
utils.WriteToFile(junitReport, junitFileFlag)
}

utils.WriteStdOut(report)

utils.WriteLogs(outputFormatFlag)
Expand All @@ -247,7 +253,9 @@ func NewVerifyCmd(config *viper.Viper) *cobra.Command {

cmd.Flags().StringSliceVarP(&disabledChecksFlag, "disable", "x", nil, "all checks will be enabled except the informed ones")

cmd.Flags().StringVarP(&outputFormatFlag, "output", "o", "", "the output format: default, json, junit or yaml")
cmd.Flags().StringVarP(&outputFormatFlag, "output", "o", "", "the output format: default, json, or yaml")

cmd.Flags().StringVarP(&junitFileFlag, "write-junit-to", "j", "", "set the output file to output results in junit format")

cmd.Flags().StringSliceVarP(&verifyOpts.Values, "set", "s", []string{}, "overrides a configuration, e.g: dummy.ok=false")

Expand Down
10 changes: 10 additions & 0 deletions internal/chartverifier/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ func WriteStdOut(output string) {
}
}

func WriteToFile(output string, filename string) {
fileWriteSuccess := false
if len(filename) > 0 {
fileWriteSuccess = writeToFile(output, filename)
}
if !fileWriteSuccess {
LogError(fmt.Sprintf("Failed writing output to: %s", filename))
}
}

func writeToStdOut(output string) {
savedOut := cmd.OutOrStdout()
cmd.SetOut(CmdStdout)
Expand Down
198 changes: 0 additions & 198 deletions pkg/chartverifier/report/junitConverter.go

This file was deleted.

88 changes: 81 additions & 7 deletions pkg/chartverifier/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,87 @@ func (r *Report) Init() APIReport {
return r
}

func (r *Report) JUnitContent() (string, error) {
junitContent := ""

var passedTests []*CheckReport
var failedTests []*CheckReport
var skippedTests []*CheckReport
var unknownTests []*CheckReport

for _, element := range r.Results {
switch element.Outcome {
case PassOutcomeType:
passedTests = append(passedTests, element)
case FailOutcomeType:
failedTests = append(failedTests, element)
case SkippedOutcomeType:
skippedTests = append(skippedTests, element)
case UnknownOutcomeType:
unknownTests = append(unknownTests, element)
}
}

suites := JUnitTestSuites{}
testsuite := JUnitTestSuite{
Tests: len(passedTests) + len(failedTests) + len(skippedTests) + len(unknownTests),
Failures: len(failedTests),
Skipped: len(skippedTests),
Name: "Red Hat Chart Verifier",
Properties: []JUnitProperty{},
TestCases: []JUnitTestCase{},
}

for _, result := range passedTests {
testCase := JUnitTestCase{
Classname: string(result.Type),
Name: string(result.Check),
Failure: nil,
Message: result.Reason,
}
testsuite.TestCases = append(testsuite.TestCases, testCase)
}

for _, result := range append(failedTests, unknownTests...) {
testCase := JUnitTestCase{
Classname: string(result.Type),
Name: string(result.Check),
Failure: &JUnitMessage{
Message: string(result.Outcome),
Type: "",
Contents: result.Reason,
},
}
testsuite.TestCases = append(testsuite.TestCases, testCase)
}

for _, result := range skippedTests {
testCase := JUnitTestCase{
Classname: string(result.Type),
Name: string(result.Check),
SkipMessage: &JUnitSkipMessage{
Message: fmt.Sprintf("Skipped: %s", result.Reason),
},
}
testsuite.TestCases = append(testsuite.TestCases, testCase)
}

suites.Suites = append(suites.Suites, testsuite)

bytes, err := xml.MarshalIndent(suites, "", "\t")
if err != nil {
o := fmt.Errorf("error formatting results with formatter %s: %v",
"junitxml",
err,
)

return "", o
}
junitContent = xml.Header + string(bytes)

return junitContent, nil
}

func (r *Report) GetContent(format ReportFormat) (string, error) {
reportContent := ""

Expand All @@ -61,13 +142,6 @@ func (r *Report) GetContent(format ReportFormat) (string, error) {
return "", fmt.Errorf("report xml marshal failed : %v", marshalErr)
}
reportContent = string(b)
} else if format == JUnitReport {
// convert report to JUnit
out, marshalErr := xml.MarshalIndent(r, " ", " ")
if marshalErr != nil {
return "", fmt.Errorf("report JUnit marshal failed: %v", marshalErr)
}
reportContent = xml.Header + string(out)
} else {
b, marshalErr := yaml.Marshal(report)
if marshalErr != nil {
Expand Down
Loading

0 comments on commit fec18ea

Please sign in to comment.