Skip to content

Commit ebb60b4

Browse files
authored
[CI] Refactor testsreporter tool (#13411)
This PR applies the following changes into the testsreporter tool: adding verbose mode, removing some unused fields in tests and other functions and building the common data in its own structs (errorLinks and dataError) to avoid duplication.
1 parent 49d46c2 commit ebb60b4

File tree

10 files changed

+131
-95
lines changed

10 files changed

+131
-95
lines changed

dev/testsreporter/_static/summary.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- {{ . }}
2121
{{- end }}
2222
{{ end -}}
23-
{{ if ne (len .owners) 0 -}}
23+
{{ if and (ne .owners nil) (ne (len .owners) 0) -}}
2424
- Owners:
2525
{{- range .owners }}
2626
- {{ . }}

dev/testsreporter/builderror.go

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package testsreporter
66

77
import (
8-
"fmt"
98
"strings"
109
)
1110

@@ -14,14 +13,6 @@ const (
1413
buildReportingTeamLabel = "Team:Ecosystem"
1514
)
1615

17-
type dataError struct {
18-
errorLinks
19-
serverless bool
20-
serverlessProject string
21-
logsDB bool
22-
stackVersion string
23-
}
24-
2516
type buildError struct {
2617
dataError
2718
teams []string
@@ -65,53 +56,35 @@ func newBuildError(options buildErrorOptions) (*buildError, error) {
6556
func (b *buildError) String() string {
6657
var sb strings.Builder
6758

68-
if b.logsDB {
69-
sb.WriteString("[LogsDB] ")
70-
}
71-
if b.serverless {
72-
sb.WriteString(fmt.Sprintf("[Serverless %s] ", b.serverlessProject))
73-
}
74-
if b.stackVersion != "" {
75-
sb.WriteString("[Stack ")
76-
sb.WriteString(b.stackVersion)
77-
sb.WriteString("] ")
78-
}
59+
sb.WriteString(b.dataError.String())
7960
sb.WriteString("Too many packages failing in daily job")
8061

8162
return sb.String()
8263
}
8364

84-
func (p *buildError) FirstBuild() string {
85-
return p.errorLinks.firstBuild
65+
func (b *buildError) FirstBuild() string {
66+
return b.errorLinks.firstBuild
8667
}
8768

88-
func (p *buildError) UpdateLinks(links errorLinks) {
89-
p.errorLinks = links
69+
func (b *buildError) UpdateLinks(links errorLinks) {
70+
b.errorLinks = links
9071
}
9172

92-
func (p *buildError) Teams() []string {
93-
return p.teams
73+
func (b *buildError) Teams() []string {
74+
return b.teams
9475
}
9576

96-
func (p *buildError) SummaryData() map[string]any {
97-
return map[string]any{
98-
"stackVersion": p.stackVersion,
99-
"serverless": p.serverless,
100-
"serverlessProject": p.serverlessProject,
101-
"logsDB": p.logsDB,
102-
"packages": p.packages,
103-
"owners": p.teams,
104-
}
77+
func (b *buildError) SummaryData() map[string]any {
78+
data := b.dataError.Data()
79+
data["packages"] = b.packages
80+
data["owners"] = b.teams
81+
return data
10582
}
10683

107-
func (p *buildError) DescriptionData() map[string]any {
108-
return map[string]any{
109-
"firstBuild": p.errorLinks.firstBuild,
110-
"closedIssueURL": p.errorLinks.closedIssueURL,
111-
"previousBuilds": p.errorLinks.previousBuilds,
112-
}
84+
func (b *buildError) DescriptionData() map[string]any {
85+
return b.errorLinks.Data()
11386
}
11487

115-
func (p *buildError) Labels() []string {
88+
func (b *buildError) Labels() []string {
11689
return []string{buildReportingTeamLabel}
11790
}

dev/testsreporter/dataerror.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package testsreporter
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
)
11+
12+
type dataError struct {
13+
errorLinks
14+
serverless bool
15+
serverlessProject string
16+
logsDB bool
17+
stackVersion string
18+
}
19+
20+
func (d *dataError) String() string {
21+
var sb strings.Builder
22+
23+
if d.logsDB {
24+
sb.WriteString("[LogsDB] ")
25+
}
26+
if d.serverless {
27+
sb.WriteString(fmt.Sprintf("[Serverless %s] ", d.serverlessProject))
28+
}
29+
if d.stackVersion != "" {
30+
sb.WriteString("[Stack ")
31+
sb.WriteString(d.stackVersion)
32+
sb.WriteString("] ")
33+
}
34+
return sb.String()
35+
}
36+
37+
func (d *dataError) Data() map[string]any {
38+
return map[string]any{
39+
"stackVersion": d.stackVersion,
40+
"serverless": d.serverless,
41+
"serverlessProject": d.serverlessProject,
42+
"logsDB": d.logsDB,
43+
}
44+
}

dev/testsreporter/errorlinks.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package testsreporter
6+
7+
type errorLinks struct {
8+
currentIssueURL string
9+
firstBuild string
10+
previousBuilds []string
11+
closedIssueURL string
12+
}
13+
14+
func (e *errorLinks) Data() map[string]any {
15+
return map[string]any{
16+
"firstBuild": e.firstBuild,
17+
"closedIssueURL": e.closedIssueURL,
18+
"previousBuilds": e.previousBuilds,
19+
}
20+
}

dev/testsreporter/format_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestSummary(t *testing.T) {
3636
`,
3737
},
3838
{
39-
title: "summary stack version with owners wihtout data stream",
39+
title: "summary stack version with owners without data stream",
4040
resultError: &packageError{
4141
dataError: dataError{
4242
stackVersion: "8.14",
@@ -181,14 +181,12 @@ func TestSummary(t *testing.T) {
181181
func TestDescription(t *testing.T) {
182182
cases := []struct {
183183
title string
184-
summary string
185184
resultError failureObserver
186185
maxLinks int
187186
expected string
188187
}{
189188
{
190-
title: "description error all fields",
191-
summary: "summary",
189+
title: "description error all fields",
192190
resultError: &packageError{
193191
dataError: dataError{
194192
stackVersion: "8.14",
@@ -226,8 +224,7 @@ Latest failed builds:
226224
`,
227225
},
228226
{
229-
title: "description failure all fields",
230-
summary: "summary",
227+
title: "description failure all fields",
231228
resultError: &packageError{
232229
dataError: dataError{
233230
stackVersion: "8.14",
@@ -265,8 +262,7 @@ Latest failed builds:
265262
`,
266263
},
267264
{
268-
title: "description no closed issue",
269-
summary: "summary",
265+
title: "description no closed issue",
270266
resultError: &packageError{
271267
dataError: dataError{
272268
stackVersion: "8.14",
@@ -302,7 +298,6 @@ Latest failed builds:
302298
},
303299
{
304300
title: "description max links",
305-
summary: "summary",
306301
maxLinks: 2,
307302
resultError: &packageError{
308303
dataError: dataError{

dev/testsreporter/packageerror.go

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ import (
1111
"github.com/elastic/integrations/dev/codeowners"
1212
)
1313

14-
type errorLinks struct {
15-
currentIssueURL string
16-
firstBuild string
17-
previousBuilds []string
18-
closedIssueURL string
19-
}
20-
2114
type packageError struct {
2215
testCase
2316
dataError
@@ -88,17 +81,7 @@ func (p *packageError) Teams() []string {
8881
func (p *packageError) String() string {
8982
var sb strings.Builder
9083

91-
if p.logsDB {
92-
sb.WriteString("[LogsDB] ")
93-
}
94-
if p.serverless {
95-
sb.WriteString(fmt.Sprintf("[Serverless %s] ", p.serverlessProject))
96-
}
97-
if p.stackVersion != "" {
98-
sb.WriteString("[Stack ")
99-
sb.WriteString(p.stackVersion)
100-
sb.WriteString("] ")
101-
}
84+
sb.WriteString(p.dataError.String())
10285
sb.WriteString("[")
10386
sb.WriteString(p.packageName)
10487
sb.WriteString("] ")
@@ -109,26 +92,19 @@ func (p *packageError) String() string {
10992
}
11093

11194
func (p *packageError) SummaryData() map[string]any {
112-
return map[string]any{
113-
"stackVersion": p.stackVersion,
114-
"serverless": p.serverless,
115-
"serverlessProject": p.serverlessProject,
116-
"logsDB": p.logsDB,
117-
"packageName": p.packageName,
118-
"testName": p.Name,
119-
"dataStream": p.dataStream,
120-
"owners": p.teams,
121-
}
95+
data := p.dataError.Data()
96+
data["packageName"] = p.packageName
97+
data["testName"] = p.Name
98+
data["dataStream"] = p.dataStream
99+
data["owners"] = p.teams
100+
return data
122101
}
123102

124103
func (p *packageError) DescriptionData() map[string]any {
125-
return map[string]any{
126-
"failure": truncateText(p.Failure, defaultMaxLengthMessages),
127-
"error": truncateText(p.Error, defaultMaxLengthMessages),
128-
"firstBuild": p.errorLinks.firstBuild,
129-
"closedIssueURL": p.errorLinks.closedIssueURL,
130-
"previousBuilds": p.errorLinks.previousBuilds,
131-
}
104+
data := p.errorLinks.Data()
105+
data["failure"] = truncateText(p.Failure, defaultMaxLengthMessages)
106+
data["error"] = truncateText(p.Error, defaultMaxLengthMessages)
107+
return data
132108
}
133109

134110
func (p *packageError) Labels() []string {

dev/testsreporter/reporter.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ import (
1414
type reporter struct {
1515
ghCli *ghCli
1616
maxPreviousLinks int
17+
verbose bool
1718
}
1819

19-
func newReporter(ghCli *ghCli, maxPreviousLinks int) reporter {
20+
type reporterOptions struct {
21+
GhCli *ghCli
22+
MaxPreviousLinks int
23+
Verbose bool
24+
}
25+
26+
func newReporter(options reporterOptions) reporter {
2027
return reporter{
21-
ghCli: ghCli,
22-
maxPreviousLinks: maxPreviousLinks,
28+
ghCli: options.GhCli,
29+
maxPreviousLinks: options.MaxPreviousLinks,
30+
verbose: options.Verbose,
2331
}
2432
}
2533

@@ -57,6 +65,12 @@ func (r reporter) Report(ctx context.Context, issue *githubIssue, resultError fa
5765
fmt.Printf("Summary:\n%s", summary)
5866
fmt.Println("----")
5967
fmt.Println()
68+
if r.verbose {
69+
fmt.Println("---- Full Description ----")
70+
fmt.Print(description)
71+
fmt.Println("----")
72+
fmt.Println()
73+
}
6074

6175
return r.createOrUpdateIssue(ctx, nextIssue)
6276
}

dev/testsreporter/reporter_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ func TestReporterUpdateLinks(t *testing.T) {
201201
Runner: &runner,
202202
})
203203

204-
reporter := newReporter(ghCli, 5)
204+
reporter := newReporter(reporterOptions{
205+
GhCli: ghCli,
206+
MaxPreviousLinks: 5,
207+
})
205208

206209
links, newIssue, err := reporter.updateLinks(context.Background(), c.issue, c.firstBuild)
207210
require.NoError(t, err)

dev/testsreporter/testsreporter.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ type CheckOptions struct {
2626
MaxPreviousLinks int
2727
MaxTestsReported int
2828

29-
DryRun bool
29+
DryRun bool
30+
Verbose bool
3031
}
3132

3233
func Check(ctx context.Context, resultsPath string, options CheckOptions) error {
@@ -49,11 +50,15 @@ func Check(ctx context.Context, resultsPath string, options CheckOptions) error
4950
DryRun: options.DryRun,
5051
})
5152

52-
aReporter := newReporter(ghCli, options.MaxPreviousLinks)
53+
aReporter := newReporter(reporterOptions{
54+
GhCli: ghCli,
55+
MaxPreviousLinks: options.MaxPreviousLinks,
56+
Verbose: options.Verbose,
57+
})
5358

5459
if len(packageErrors) > options.MaxTestsReported {
5560
fmt.Printf("Skip creating GitHub issues, hit the maximum number (%d) of tests to be reported. Total failing tests: %d.\n", options.MaxTestsReported, len(packageErrors))
56-
packages, err := packagesFromTests(resultsPath, options)
61+
packages, err := packagesFromTests(resultsPath)
5762
if err != nil {
5863
return fmt.Errorf("failed to get packages from results files: %w", err)
5964
}
@@ -137,7 +142,7 @@ func errorsFromTests(resultsPath string, options CheckOptions) ([]*packageError,
137142
}
138143

139144
// packagesFromTests returns the sorted packages failing given the results file
140-
func packagesFromTests(resultsPath string, options CheckOptions) ([]string, error) {
145+
func packagesFromTests(resultsPath string) ([]string, error) {
141146
packages := []string{}
142147
err := filepath.Walk(resultsPath, func(path string, info os.FileInfo, err error) error {
143148
if err != nil {

magefile.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func ReportFailedTests(ctx context.Context, testResultsFolder string) error {
173173
logsDBEnabled = true
174174
}
175175

176+
verboseMode := false
177+
if v, found := os.LookupEnv("VERBOSE_MODE_ENABLED"); found && v == "true" {
178+
verboseMode = true
179+
}
180+
176181
maxIssuesString := os.Getenv("CI_MAX_TESTS_REPORTED")
177182
maxIssues := defaultMaximumTestsReported
178183
if maxIssuesString != "" {
@@ -201,6 +206,7 @@ func ReportFailedTests(ctx context.Context, testResultsFolder string) error {
201206
MaxPreviousLinks: defaultPreviousLinksNumber,
202207
MaxTestsReported: maxIssues,
203208
DryRun: dryRun,
209+
Verbose: verboseMode,
204210
}
205211
return testsreporter.Check(ctx, testResultsFolder, options)
206212
}

0 commit comments

Comments
 (0)