Skip to content

Commit 436e956

Browse files
committed
Add reporting to example run
1 parent 103764e commit 436e956

File tree

9 files changed

+110
-63
lines changed

9 files changed

+110
-63
lines changed

tools/flakeguard/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ example:
4141
--splunk-url "https://splunk.example.com" \
4242
--splunk-token "splunk-token" \
4343
--splunk-event "example-splunk-event"
44+
GITHUB_TOKEN="EXAMPLE_GITHUB_TOKEN" go run . generate-report \
45+
--aggregated-results-path ./example_results/all-test-results.json \
46+
--output-path ./example_results \
47+
--github-repository "smartcontractkit/chainlink-testing-framework" \
48+
--github-run-id "1" \
49+
--failed-tests-artifact-name "failed-test-results-with-logs.json" \
50+
--base-branch "exampleBaseBranch" \
51+
--current-branch "exampleCurrentBranch" \
52+
--current-commit-sha "abc" \
53+
--repo-url "https://github.com/smartcontractkit/chainlink-testing-framework" \
54+
--action-run-id "1" \
55+
--max-pass-ratio "1.0"
4456

4557
.PHONY: example_flaky_panic
4658
example_flaky_panic:
@@ -61,6 +73,18 @@ example_flaky_panic:
6173
--splunk-url "https://splunk.example.com" \
6274
--splunk-token "splunk-token" \
6375
--splunk-event "example-splunk-event"
76+
GITHUB_TOKEN="EXAMPLE_GITHUB_TOKEN" go run . generate-report \
77+
--aggregated-results-path ./example_results/all-test-results.json \
78+
--output-path ./example_results \
79+
--github-repository "smartcontractkit/chainlink-testing-framework" \
80+
--github-run-id "1" \
81+
--failed-tests-artifact-name "failed-test-results-with-logs.json" \
82+
--base-branch "exampleBaseBranch" \
83+
--current-branch "exampleCurrentBranch" \
84+
--current-commit-sha "abc" \
85+
--repo-url "https://github.com/smartcontractkit/chainlink-testing-framework" \
86+
--action-run-id "1" \
87+
--max-pass-ratio "1.0"
6488

6589
.PHONY: example_timeout
6690
example_timeout:
@@ -81,3 +105,15 @@ example_timeout:
81105
--splunk-url "https://splunk.example.com" \
82106
--splunk-token "splunk-token" \
83107
--splunk-event "example-splunk-event"
108+
GITHUB_TOKEN="EXAMPLE_GITHUB_TOKEN" go run . generate-report \
109+
--aggregated-results-path ./example_results/all-test-results.json \
110+
--output-path ./example_results \
111+
--github-repository "smartcontractkit/chainlink-testing-framework" \
112+
--github-run-id "1" \
113+
--failed-tests-artifact-name "failed-test-results-with-logs.json" \
114+
--base-branch "exampleBaseBranch" \
115+
--current-branch "exampleCurrentBranch" \
116+
--current-commit-sha "abc" \
117+
--repo-url "https://github.com/smartcontractkit/chainlink-testing-framework" \
118+
--action-run-id "1" \
119+
--max-pass-ratio "1.0"

tools/flakeguard/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Both `find` and `run` commands support JSON output `--json`, making it easy to i
3636
You can find example usage and see outputs with:
3737

3838
```sh
39-
make example # Run an example flow of running tests and aggregating results
40-
make example_panic # Run example flow with panicking tests
41-
ls example_results # See results of each run and aggregation
39+
make example # Run an example flow of running tests, aggregating results, and reporting them to GitHub
40+
make example_flaky_panic # Run example flow with flaky and panicking tests
41+
make example_timeout # Run example flow tests the timeout
42+
ls example_results # See results of each run and aggregation
4243
```

tools/flakeguard/cmd/generate_report.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"golang.org/x/oauth2"
1818
)
1919

20+
const exampleGitHubToken = "EXAMPLE_GITHUB_TOKEN" //nolint:gosec
21+
2022
var GenerateReportCmd = &cobra.Command{
2123
Use: "generate-report",
2224
Short: "Generate test reports from aggregated results that can be posted to GitHub",
@@ -191,6 +193,9 @@ func init() {
191193
}
192194

193195
func fetchArtifactLink(githubToken, githubRepo string, githubRunID int64, artifactName string) (string, error) {
196+
if githubToken == exampleGitHubToken {
197+
return "https://example-artifact-link.com", nil
198+
}
194199
ctx := context.Background()
195200
ts := oauth2.StaticTokenSource(
196201
&oauth2.Token{AccessToken: githubToken},

tools/flakeguard/reports/data.go

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,61 @@ type TestReport struct {
2626
MaxPassRatio float64 `json:"max_pass_ratio,omitempty"`
2727
}
2828

29+
// GenerateSummaryData generates a summary of a report's test results
30+
func (testReport *TestReport) GenerateSummaryData() {
31+
var runs, mostRuns, passes, fails, skips, panickedTests, racedTests, flakyTests, skippedTests int
32+
33+
for _, result := range testReport.Results {
34+
runs += result.Runs
35+
if result.Runs > mostRuns {
36+
mostRuns = result.Runs
37+
}
38+
passes += result.Successes
39+
fails += result.Failures
40+
skips += result.Skips
41+
42+
// Count tests that were entirely skipped
43+
if result.Runs == 0 && result.Skipped {
44+
skippedTests++
45+
}
46+
47+
if result.Panic {
48+
panickedTests++
49+
flakyTests++
50+
} else if result.Race {
51+
racedTests++
52+
flakyTests++
53+
} else if !result.Skipped && result.Runs > 0 && result.PassRatio < testReport.MaxPassRatio {
54+
flakyTests++
55+
}
56+
}
57+
58+
// Calculate the raw pass ratio
59+
passRatio := passRatio(passes, runs)
60+
61+
// Calculate the raw flake ratio
62+
totalTests := len(testReport.Results)
63+
flakeRatio := flakeRatio(flakyTests, totalTests)
64+
65+
passRatioStr := formatRatio(passRatio)
66+
flakeTestRatioStr := formatRatio(flakeRatio)
67+
68+
testReport.SummaryData = &SummaryData{
69+
UniqueTestsRun: totalTests,
70+
TestRunCount: mostRuns,
71+
PanickedTests: panickedTests,
72+
RacedTests: racedTests,
73+
FlakyTests: flakyTests,
74+
FlakyTestPercent: flakeTestRatioStr,
75+
76+
TotalRuns: runs,
77+
PassedRuns: passes,
78+
FailedRuns: fails,
79+
SkippedRuns: skips,
80+
PassPercent: passRatioStr,
81+
}
82+
}
83+
2984
// TestResult contains the results and outputs of a single test
3085
type TestResult struct {
3186
// ReportID is the ID of the report this test result belongs to
@@ -129,61 +184,6 @@ type SplunkTestResultEvent struct {
129184

130185
// Data Processing Functions
131186

132-
// GenerateSummaryData generates a summary of a report's test results
133-
func GenerateSummaryData(testReport *TestReport) {
134-
var runs, mostRuns, passes, fails, skips, panickedTests, racedTests, flakyTests, skippedTests int
135-
136-
for _, result := range testReport.Results {
137-
runs += result.Runs
138-
if result.Runs > mostRuns {
139-
mostRuns = result.Runs
140-
}
141-
passes += result.Successes
142-
fails += result.Failures
143-
skips += result.Skips
144-
145-
// Count tests that were entirely skipped
146-
if result.Runs == 0 && result.Skipped {
147-
skippedTests++
148-
}
149-
150-
if result.Panic {
151-
panickedTests++
152-
flakyTests++
153-
} else if result.Race {
154-
racedTests++
155-
flakyTests++
156-
} else if !result.Skipped && result.Runs > 0 && result.PassRatio < testReport.MaxPassRatio {
157-
flakyTests++
158-
}
159-
}
160-
161-
// Calculate the raw pass ratio
162-
passRatio := passRatio(passes, runs)
163-
164-
// Calculate the raw flake ratio
165-
totalTests := len(testReport.Results)
166-
flakeRatio := flakeRatio(flakyTests, totalTests)
167-
168-
passRatioStr := formatRatio(passRatio)
169-
flakeTestRatioStr := formatRatio(flakeRatio)
170-
171-
testReport.SummaryData = &SummaryData{
172-
UniqueTestsRun: totalTests,
173-
TestRunCount: mostRuns,
174-
PanickedTests: panickedTests,
175-
RacedTests: racedTests,
176-
FlakyTests: flakyTests,
177-
FlakyTestPercent: flakeTestRatioStr,
178-
179-
TotalRuns: runs,
180-
PassedRuns: passes,
181-
FailedRuns: fails,
182-
SkippedRuns: skips,
183-
PassPercent: passRatioStr,
184-
}
185-
}
186-
187187
func FilterResults(report *TestReport, maxPassRatio float64) *TestReport {
188188
filteredResults := FilterTests(report.Results, func(tr TestResult) bool {
189189
return !tr.Skipped && tr.PassRatio < maxPassRatio

tools/flakeguard/reports/data_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestGenerateSummaryData(t *testing.T) {
205205

206206
for _, tc := range tests {
207207
t.Run(tc.name, func(t *testing.T) {
208-
GenerateSummaryData(tc.testReport)
208+
tc.testReport.GenerateSummaryData()
209209
assert.Equal(t, tc.expected, tc.testReport.SummaryData, "Summary data does not match expected")
210210
})
211211
}

tools/flakeguard/reports/io.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,13 @@ func aggregate(reportChan <-chan *TestReport, errChan <-chan error, opts *aggreg
491491

492492
sortTestResults(aggregatedResults)
493493
fullReport.Results = aggregatedResults
494-
GenerateSummaryData(fullReport)
494+
fullReport.GenerateSummaryData()
495495

496496
if sendToSplunk {
497497
err = sendDataToSplunk(opts, *fullReport)
498+
if err != nil {
499+
return fullReport, fmt.Errorf("error sending data to Splunk: %w", err)
500+
}
498501
}
499502
return fullReport, err
500503
}
@@ -630,13 +633,15 @@ func sendDataToSplunk(opts *aggregateOptions, report TestReport) error {
630633
Int("successfully sent", successfulResultsSent).
631634
Int("total results", len(results)).
632635
Errs("errors", splunkErrs).
636+
Str("report id", report.ID).
633637
Str("duration", time.Since(start).String()).
634638
Msg("Errors occurred while sending test results to Splunk")
635639
} else {
636640
log.Debug().
637641
Int("successfully sent", successfulResultsSent).
638642
Int("total results", len(results)).
639643
Str("duration", time.Since(start).String()).
644+
Str("report id", report.ID).
640645
Msg("All results sent successfully to Splunk")
641646
}
642647

tools/flakeguard/reports/presentation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ func GenerateGitHubSummaryMarkdown(w io.Writer, testReport *TestReport, maxPassR
9090

9191
if testReport.SummaryData.FlakyTests > 0 {
9292
fmt.Fprintln(w, "## Found Flaky Tests :x:")
93-
fmt.Fprintln(w)
9493
} else {
9594
fmt.Fprintln(w, "## No Flakes Found :white_check_mark:")
9695
}
96+
fmt.Fprintln(w)
9797

9898
RenderResults(w, testReport, true, false)
9999

tools/flakeguard/reports/presentation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func TestRenderResults(t *testing.T) {
241241
for _, tc := range testcases {
242242
t.Run(tc.name, func(t *testing.T) {
243243
// Generate the summary data
244-
GenerateSummaryData(tc.testReport)
244+
tc.testReport.GenerateSummaryData()
245245

246246
var buf bytes.Buffer
247247
RenderResults(&buf, tc.testReport, false, false)

tools/flakeguard/runner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (r *Runner) RunTests() (*reports.TestReport, error) {
8484
Results: results,
8585
MaxPassRatio: r.MaxPassRatio,
8686
}
87-
reports.GenerateSummaryData(report)
87+
report.GenerateSummaryData()
8888
return report, nil
8989
}
9090

0 commit comments

Comments
 (0)