|
| 1 | +package junitxml |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/redhat-certification/chart-verifier/pkg/chartverifier/report" |
| 9 | +) |
| 10 | + |
| 11 | +type JUnitTestSuites struct { |
| 12 | + XMLName xml.Name `xml:"testsuites"` |
| 13 | + Suites []JUnitTestSuite `xml:"testsuite"` |
| 14 | +} |
| 15 | + |
| 16 | +type JUnitTestSuite struct { |
| 17 | + XMLName xml.Name `xml:"testsuite"` |
| 18 | + Tests int `xml:"tests,attr"` |
| 19 | + Failures int `xml:"failures,attr"` |
| 20 | + Skipped int `xml:"skipped,attr"` |
| 21 | + Unknown int `xml:"unknown,attr"` |
| 22 | + ReportDigest string `xml:"reportDigest,attr"` |
| 23 | + Name string `xml:"name,attr"` |
| 24 | + Properties []JUnitProperty `xml:"properties>property,omitempty"` |
| 25 | + TestCases []JUnitTestCase `xml:"testcase"` |
| 26 | +} |
| 27 | + |
| 28 | +type JUnitTestCase struct { |
| 29 | + XMLName xml.Name `xml:"testcase"` |
| 30 | + Classname string `xml:"classname,attr"` |
| 31 | + Name string `xml:"name,attr"` |
| 32 | + SkipMessage *JUnitSkipMessage `xml:"skipped,omitempty"` |
| 33 | + Failure *JUnitMessage `xml:"failure,omitempty"` |
| 34 | + Warning *JUnitMessage `xml:"warning,omitempty"` |
| 35 | + SystemOut string `xml:"system-out,omitempty"` |
| 36 | + Message string `xml:",chardata"` |
| 37 | +} |
| 38 | + |
| 39 | +type JUnitSkipMessage struct { |
| 40 | + Message string `xml:"message,attr"` |
| 41 | +} |
| 42 | + |
| 43 | +type JUnitProperty struct { |
| 44 | + Name string `xml:"name,attr"` |
| 45 | + Value string `xml:"value,attr"` |
| 46 | +} |
| 47 | + |
| 48 | +type JUnitMessage struct { |
| 49 | + Message string `xml:"message,attr"` |
| 50 | + Type string `xml:"type,attr"` |
| 51 | + Contents string `xml:",chardata"` |
| 52 | +} |
| 53 | + |
| 54 | +func Format(r report.Report) ([]byte, error) { |
| 55 | + results := r.Results |
| 56 | + checksByOutcome := map[string][]report.CheckReport{} |
| 57 | + |
| 58 | + for i, result := range results { |
| 59 | + checksByOutcome[result.Outcome] = append(checksByOutcome[result.Outcome], *results[i]) |
| 60 | + } |
| 61 | + |
| 62 | + digest, err := r.GetReportDigest() |
| 63 | + if err != nil { |
| 64 | + // Prefer to continue even if digest calculation fails for some reason. |
| 65 | + digest = "unknown" |
| 66 | + } |
| 67 | + |
| 68 | + testsuite := JUnitTestSuite{ |
| 69 | + Tests: len(results), |
| 70 | + Failures: len(checksByOutcome[report.FailOutcomeType]), |
| 71 | + Skipped: len(checksByOutcome[report.SkippedOutcomeType]), |
| 72 | + Unknown: len(checksByOutcome[report.UnknownOutcomeType]), |
| 73 | + ReportDigest: digest, |
| 74 | + Name: "Red Hat Helm Chart Certification", |
| 75 | + Properties: []JUnitProperty{ |
| 76 | + {Name: "profileType", Value: r.Metadata.ToolMetadata.Profile.VendorType}, |
| 77 | + {Name: "profileVersion", Value: r.Metadata.ToolMetadata.Profile.Version}, |
| 78 | + {Name: "webCatalogOnly", Value: strconv.FormatBool(r.Metadata.ToolMetadata.ProviderDelivery || r.Metadata.ToolMetadata.WebCatalogOnly)}, |
| 79 | + {Name: "verifierVersion", Value: r.Metadata.ToolMetadata.Version}, |
| 80 | + }, |
| 81 | + TestCases: []JUnitTestCase{}, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range checksByOutcome[report.PassOutcomeType] { |
| 85 | + c := JUnitTestCase{ |
| 86 | + Classname: r.Metadata.ToolMetadata.ChartUri, |
| 87 | + Name: string(tc.Check), |
| 88 | + Failure: nil, |
| 89 | + Message: tc.Reason, |
| 90 | + } |
| 91 | + testsuite.TestCases = append(testsuite.TestCases, c) |
| 92 | + } |
| 93 | + |
| 94 | + for _, tc := range checksByOutcome[report.FailOutcomeType] { |
| 95 | + c := JUnitTestCase{ |
| 96 | + Classname: r.Metadata.ToolMetadata.ChartUri, |
| 97 | + Name: string(tc.Check), |
| 98 | + Failure: &JUnitMessage{ |
| 99 | + Message: "Failed", |
| 100 | + Type: string(tc.Type), |
| 101 | + Contents: tc.Reason, |
| 102 | + }, |
| 103 | + Message: tc.Reason, |
| 104 | + } |
| 105 | + testsuite.TestCases = append(testsuite.TestCases, c) |
| 106 | + } |
| 107 | + |
| 108 | + for _, tc := range checksByOutcome[report.UnknownOutcomeType] { |
| 109 | + c := JUnitTestCase{ |
| 110 | + Classname: r.Metadata.ToolMetadata.ChartUri, |
| 111 | + Name: string(tc.Check), |
| 112 | + Failure: &JUnitMessage{ |
| 113 | + Message: "Unknown", |
| 114 | + Type: string(tc.Type), |
| 115 | + Contents: tc.Reason, |
| 116 | + }, |
| 117 | + Message: tc.Reason, |
| 118 | + } |
| 119 | + testsuite.TestCases = append(testsuite.TestCases, c) |
| 120 | + } |
| 121 | + |
| 122 | + for _, tc := range checksByOutcome[report.SkippedOutcomeType] { |
| 123 | + c := JUnitTestCase{ |
| 124 | + Classname: r.Metadata.ToolMetadata.ChartUri, |
| 125 | + Name: string(tc.Check), |
| 126 | + Failure: nil, |
| 127 | + Message: tc.Reason, |
| 128 | + SkipMessage: &JUnitSkipMessage{ |
| 129 | + Message: tc.Reason, |
| 130 | + }, |
| 131 | + } |
| 132 | + testsuite.TestCases = append(testsuite.TestCases, c) |
| 133 | + } |
| 134 | + |
| 135 | + suites := JUnitTestSuites{ |
| 136 | + Suites: []JUnitTestSuite{testsuite}, |
| 137 | + } |
| 138 | + |
| 139 | + bytes, err := xml.MarshalIndent(suites, "", "\t") |
| 140 | + if err != nil { |
| 141 | + o := fmt.Errorf("error formatting results with formatter %s: %v", |
| 142 | + "junitxml", |
| 143 | + err, |
| 144 | + ) |
| 145 | + |
| 146 | + return nil, o |
| 147 | + } |
| 148 | + |
| 149 | + return bytes, nil |
| 150 | +} |
0 commit comments