|
| 1 | +package htmlreport |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "encoding/json" |
| 6 | + "encoding/xml" |
| 7 | + "fmt" |
| 8 | + "html/template" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/joshdk/go-junit" |
| 16 | + "github.com/kubevirt/ci-health/pkg/sigretests" |
| 17 | + "github.com/kubevirt/ci-health/pkg/types" |
| 18 | + log "github.com/sirupsen/logrus" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + //go:embed sig-failure-report.gohtml |
| 23 | + sigFailureReportTemplate string |
| 24 | +) |
| 25 | + |
| 26 | +type HTMLReportResults struct { |
| 27 | + Data struct { |
| 28 | + SIGRetests struct { |
| 29 | + FailedJobLeaderBoard []types.FailedJob `json:"FailedJobLeaderBoard"` |
| 30 | + } `json:"SIGRetests"` |
| 31 | + } `json:"Data"` |
| 32 | +} |
| 33 | + |
| 34 | +type Failure struct { |
| 35 | + XMLName xml.Name `xml:"failure"` |
| 36 | + Message string `xml:"message,attr"` |
| 37 | + Type string `xml:"type,attr"` |
| 38 | + Value string `xml:",chardata"` |
| 39 | +} |
| 40 | + |
| 41 | +type SigFailure struct { |
| 42 | + Sig string |
| 43 | + JobName string |
| 44 | + FailureURL string |
| 45 | + Testcase []junit.Test |
| 46 | +} |
| 47 | + |
| 48 | +var jobRegexAliases = map[string]string{ |
| 49 | + "compute": "sig-compute$|sig-compute-serial$|sig-compute-migrations$|sig-operator$|vgpu$|sev$", |
| 50 | + "network": "sig-network$|sriov$", |
| 51 | + "storage": "sig-storage$", |
| 52 | +} |
| 53 | + |
| 54 | +func fetchResults(resultsPath string) (*HTMLReportResults, error) { |
| 55 | + body, err := os.ReadFile(resultsPath) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to read results.json file: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + var results HTMLReportResults |
| 61 | + if err := json.Unmarshal(body, &results); err != nil { |
| 62 | + return nil, fmt.Errorf("failed to unmarshal results.json: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + return &results, nil |
| 66 | +} |
| 67 | + |
| 68 | +func fetchJunit(url string) ([]junit.Suite, error) { |
| 69 | + resp, err := sigretests.HttpGetWithRetry(url) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("failed to fetch %s: %s", url, err) |
| 72 | + } |
| 73 | + defer resp.Body.Close() |
| 74 | + |
| 75 | + // Ignore missing junit files as it suggests an issue with the job |
| 76 | + if resp.StatusCode == http.StatusNotFound { |
| 77 | + return nil, nil |
| 78 | + } |
| 79 | + |
| 80 | + if resp.StatusCode != http.StatusOK { |
| 81 | + return nil, fmt.Errorf("failed to fetch %s: status code %d", url, resp.StatusCode) |
| 82 | + } |
| 83 | + |
| 84 | + body, err := io.ReadAll(resp.Body) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to read %s body: %w", url, err) |
| 87 | + } |
| 88 | + |
| 89 | + testsuite, err := junit.Ingest(body) |
| 90 | + if err == nil { |
| 91 | + return testsuite, nil |
| 92 | + } |
| 93 | + |
| 94 | + return nil, fmt.Errorf("failed to unmarshal junit.functest.xml as <testsuites> or <testsuite>") |
| 95 | +} |
| 96 | + |
| 97 | +func constructReportFilePath(opt *types.Options) string { |
| 98 | + return fmt.Sprintf("%s/sig-%s-failure-report.html", opt.Path, opt.Sig) |
| 99 | +} |
| 100 | + |
| 101 | +func constructJunitURL(failureURL string) string { |
| 102 | + junitURL := strings.Replace(failureURL, "prow.ci.kubevirt.io//view/gs", "gcsweb.ci.kubevirt.io/gcs", 1) |
| 103 | + if !strings.HasSuffix(junitURL, "/") { |
| 104 | + junitURL += "/" |
| 105 | + } |
| 106 | + junitURL += "artifacts/junit.functest.xml" |
| 107 | + return junitURL |
| 108 | +} |
| 109 | + |
| 110 | +func Generate(opt *types.Options) error { |
| 111 | + |
| 112 | + if opt.ResultsPath == "" { |
| 113 | + return fmt.Errorf("the path to results.json is required") |
| 114 | + } |
| 115 | + |
| 116 | + jobRegex, ok := jobRegexAliases[opt.Sig] |
| 117 | + if !ok { |
| 118 | + return fmt.Errorf("unknown SIG: %s", opt.Sig) |
| 119 | + } |
| 120 | + |
| 121 | + results, err := fetchResults(opt.ResultsPath) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("failed to parse results.json: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + compiledRegex, err := regexp.Compile(jobRegex) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("invalid job regex provided: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + var sigFailures []SigFailure |
| 132 | + |
| 133 | + for _, job := range results.Data.SIGRetests.FailedJobLeaderBoard { |
| 134 | + if !compiledRegex.MatchString(job.JobName) { |
| 135 | + continue |
| 136 | + } |
| 137 | + for _, failureURL := range job.FailureURLs { |
| 138 | + var sigFail SigFailure |
| 139 | + junitURL := constructJunitURL(failureURL) |
| 140 | + testSuites, err := fetchJunit(junitURL) |
| 141 | + if err != nil { |
| 142 | + log.Warnf("failed to fetch junit results: %s", err) |
| 143 | + continue |
| 144 | + } |
| 145 | + if testSuites == nil { |
| 146 | + // SIG CI failure |
| 147 | + continue |
| 148 | + } |
| 149 | + sigFail.Sig = opt.Sig |
| 150 | + sigFail.JobName = job.JobName |
| 151 | + sigFail.FailureURL = failureURL |
| 152 | + |
| 153 | + for _, suite := range testSuites { |
| 154 | + for _, test := range suite.Tests { |
| 155 | + if test.Status == junit.StatusFailed { |
| 156 | + sigFail.Testcase = append(sigFail.Testcase, test) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + sigFailures = append(sigFailures, sigFail) |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + reportTemplate, err := template.New("sigFailures").Parse(sigFailureReportTemplate) |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("could not read template: %w", err) |
| 168 | + } |
| 169 | + |
| 170 | + outputFile, err := os.Create(constructReportFilePath(opt)) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("could not create report file: %w", err) |
| 173 | + } |
| 174 | + defer outputFile.Close() |
| 175 | + |
| 176 | + err = reportTemplate.Execute(outputFile, sigFailures) |
| 177 | + if err != nil { |
| 178 | + return fmt.Errorf("could not execute template: %w", err) |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments