forked from konflux-ci/qe-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateReport.go
More file actions
151 lines (124 loc) · 5.81 KB
/
Copy pathcreateReport.go
File metadata and controls
151 lines (124 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package prowjob
import (
"bufio"
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/GoogleCloudPlatform/testgrid/metadata"
"github.com/redhat-appstudio/qe-tools/pkg/prow"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
reporters "github.com/onsi/ginkgo/v2/reporters"
ginkgoTypes "github.com/onsi/ginkgo/v2/types"
"github.com/redhat-appstudio-qe/junit2html/pkg/convert"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
buildLogFilename = "build-log.txt"
finishedFilename = "finished.json"
junitFilename = `/(j?unit|e2e).*\.xml`
gcsBrowserURLPrefix = "https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/"
)
// createReportCmd represents the createReport command
var createReportCmd = &cobra.Command{
Use: "create-report",
Short: "Analyze specified prow job and create a report in junit/html format",
PreRunE: func(cmd *cobra.Command, _ []string) error {
if viper.GetString(prowJobIDParamName) == "" {
_ = cmd.Usage()
return fmt.Errorf("parameter %q not provided, neither %s env var was set", prowJobIDParamName, prowJobIDEnv)
}
return nil
},
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
jobID := viper.GetString(prowJobIDParamName)
cfg := prow.ScannerConfig{
ProwJobID: jobID,
FileNameFilter: []string{finishedFilename, buildLogFilename, junitFilename},
}
scanner, err := prow.NewArtifactScanner(cfg)
if err != nil {
return fmt.Errorf("failed to initialize artifact scanner: %+v", err)
}
if err := scanner.Run(); err != nil {
return fmt.Errorf("failed to scan artifacts for prow job %s: %+v", jobID, err)
}
overallJUnitSuites := &reporters.JUnitTestSuites{}
openshiftCiJunit := reporters.JUnitTestSuite{Name: "openshift-ci job", Properties: reporters.JUnitProperties{Properties: []reporters.JUnitProperty{}}}
htmlReportLink := gcsBrowserURLPrefix + scanner.ObjectPrefix + "redhat-appstudio-report/artifacts/junit-summary.html"
openshiftCiJunit.Properties.Properties = append(openshiftCiJunit.Properties.Properties, reporters.JUnitProperty{Name: "html-report-link", Value: htmlReportLink})
for stepName, artifactsFilenameMap := range scanner.ArtifactStepMap {
for artifactFilename, artifact := range artifactsFilenameMap {
if artifactFilename == finishedFilename {
if strings.Contains(string(stepName), "gather") {
openshiftCiJunit.Properties.Properties = append(openshiftCiJunit.Properties.Properties, reporters.JUnitProperty{Name: string(stepName), Value: gcsBrowserURLPrefix + strings.TrimSuffix(artifact.FullName, finishedFilename) + "artifacts"})
}
finished := metadata.Finished{}
err = yaml.Unmarshal([]byte(artifact.Content), &finished)
if err != nil {
return fmt.Errorf("cannot unmarshal %s into finished struct: %+v", artifact.Content, err)
}
var buildLog string
if val, ok := artifactsFilenameMap[buildLogFilename]; ok {
buildLog = val.Content
}
if *finished.Passed {
openshiftCiJunit.TestCases = append(openshiftCiJunit.TestCases, reporters.JUnitTestCase{Name: string(stepName), Status: ginkgoTypes.SpecStatePassed.String(), SystemErr: buildLog})
} else {
failure := &reporters.JUnitFailure{Message: fmt.Sprintf("%s has failed", stepName)}
tc := reporters.JUnitTestCase{Name: string(stepName), Status: ginkgoTypes.SpecStateFailed.String(), Failure: failure, SystemErr: buildLog}
openshiftCiJunit.Failures++
openshiftCiJunit.TestCases = append(openshiftCiJunit.TestCases, tc)
}
openshiftCiJunit.Tests++
} else if strings.Contains(string(artifactFilename), ".xml") {
if err = xml.Unmarshal([]byte(artifact.Content), overallJUnitSuites); err != nil {
klog.Errorf("cannot decode JUnit suite %q into xml: %+v", artifactFilename, err)
}
}
}
}
artifactDir := viper.GetString(artifactDirParamName)
if artifactDir == "" {
artifactDir = "./tmp/" + jobID
klog.Warningf("path to artifact dir was not provided - using default %q\n", artifactDir)
}
if err := os.MkdirAll(artifactDir, 0o750); err != nil {
return fmt.Errorf("failed to create directory for results '%s': %+v", artifactDir, err)
}
overallJUnitSuites.TestSuites = append(overallJUnitSuites.TestSuites, openshiftCiJunit)
overallJUnitSuites.Failures += openshiftCiJunit.Failures
overallJUnitSuites.Errors += openshiftCiJunit.Errors
overallJUnitSuites.Tests += openshiftCiJunit.Tests
generatedJunitFilepath := filepath.Clean(artifactDir + "/junit.xml")
outFile, err := os.Create(generatedJunitFilepath)
if err != nil {
return fmt.Errorf("cannot create file '%s': %+v", generatedJunitFilepath, err)
}
if err := xml.NewEncoder(bufio.NewWriter(outFile)).Encode(overallJUnitSuites); err != nil {
return fmt.Errorf("cannot encode JUnit suites struct '%+v' into file located at '%s': %+v", overallJUnitSuites, generatedJunitFilepath, err)
}
html, err := convert.Convert(overallJUnitSuites)
if err != nil {
return fmt.Errorf("failed to convert junit suite to html: %+v", err)
}
if err := os.WriteFile(artifactDir+"/junit-summary.html", []byte(html), 0o600); err != nil {
return fmt.Errorf("failed to create HTML file with test summary: %+v", err)
}
klog.Infof("JUnit report saved to: %s/junit.xml", artifactDir)
klog.Infof("HTML report saved to: %s/junit-summary.html", artifactDir)
return nil
},
}
func init() {
createReportCmd.Flags().StringVar(&prowJobID, prowJobIDParamName, "", "Prow job ID to analyze")
_ = viper.BindPFlag(artifactDirParamName, createReportCmd.Flags().Lookup(artifactDirParamName))
_ = viper.BindPFlag(prowJobIDParamName, createReportCmd.Flags().Lookup(prowJobIDParamName))
// Bind environment variables to viper (in case the associated command's parameter is not provided)
_ = viper.BindEnv(prowJobIDParamName, prowJobIDEnv)
_ = viper.BindEnv(artifactDirParamName, artifactDirEnv)
}