Skip to content

Commit 86104d8

Browse files
authored
Merge branch 'main' into tt-1806-labelling-readme
2 parents c747939 + 5c4a2d3 commit 86104d8

File tree

12 files changed

+1178
-518
lines changed

12 files changed

+1178
-518
lines changed

tools/flakeguard/.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
tmp_test_flaky_state
1+
tmp_test_flaky*
22
test_results_*.json
3-
debug_outputs/
3+
debug_outputs
4+
example_results/
5+
example*.json
6+
example*.md

tools/flakeguard/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,30 @@ test:
77
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
88
set -euo pipefail
99
go list ./... | grep -v 'example_test_package' | xargs go test -json -cover -coverprofile unit-test-coverage.out -v 2>&1 | tee /tmp/gotest.log | gotestfmt
10+
11+
.PHONY: example
12+
example:
13+
rm -rf example_results
14+
mkdir -p example_results
15+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=Panic,Timeout --max-pass-ratio=1 --race=false --output-json=example_results/example_run_1.json
16+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=Panic,Timeout --max-pass-ratio=1 --race=false --output-json=example_results/example_run_2.json
17+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=Panic,Timeout --max-pass-ratio=1 --race=false --output-json=example_results/example_run_3.json
18+
go run . aggregate-results --results-path ./example_results --output-results ./example_results/all_tests_example.json
19+
20+
.PHONY: example_flaky_panic
21+
example_flaky_panic:
22+
rm -rf example_results
23+
mkdir -p example_results
24+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=TestPanic --max-pass-ratio=1 --race=false --output-json=example_results/example_run_1.json
25+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=TestPanic --max-pass-ratio=1 --race=false --output-json=example_results/example_run_2.json
26+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=TestPanic --max-pass-ratio=1 --race=false --output-json=example_results/example_run_3.json
27+
go run . aggregate-results --results-path ./example_results --output-results ./example_results/all_tests_example.json
28+
29+
.PHONY: example_timeout
30+
example_timeout:
31+
rm -rf example_results
32+
mkdir -p example_results
33+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --select-tests=TestTimeout --timeout=1s --max-pass-ratio=1 --race=false --output-json=example_results/example_run_1.json
34+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --select-tests=TestTimeout --timeout=1s --max-pass-ratio=1 --race=false --output-json=example_results/example_run_2.json
35+
- go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --select-tests=TestTimeout --timeout=1s --max-pass-ratio=1 --race=false --output-json=example_results/example_run_3.json
36+
go run . aggregate-results --results-path ./example_results --output-results ./example_results/all_tests_example.json

tools/flakeguard/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ Run with `--help` to see all flags for the commands.
3030
### JSON Output
3131

3232
Both `find` and `run` commands support JSON output `--json`, making it easy to integrate Flakeguard with CI/CD pipelines and reporting tools.
33+
34+
### Example Run
35+
36+
You can find example usage and see outputs with:
37+
38+
```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
42+
```

tools/flakeguard/cmd/aggregate_results.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ var (
1818
var AggregateResultsCmd = &cobra.Command{
1919
Use: "aggregate-results",
2020
Short: "Aggregate test results and optionally filter failed tests based on a threshold",
21-
Run: func(cmd *cobra.Command, args []string) {
22-
// Aggregate all test results
23-
allResults, err := reports.AggregateTestResults(resultsFolderPath)
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
allReport, err := reports.AggregateTestResults(resultsFolderPath)
2423
if err != nil {
2524
log.Fatalf("Error aggregating results: %v", err)
2625
}
@@ -29,25 +28,27 @@ var AggregateResultsCmd = &cobra.Command{
2928

3029
if filterFailed {
3130
// Filter to only include tests that failed below the threshold
32-
for _, result := range allResults {
31+
for _, result := range allReport.Results {
3332
if result.PassRatio < maxPassRatio && !result.Skipped {
3433
resultsToSave = append(resultsToSave, result)
3534
}
3635
}
3736
} else {
38-
resultsToSave = allResults
37+
resultsToSave = allReport.Results
3938
}
39+
allReport.Results = resultsToSave
4040

4141
// Output results to JSON files
4242
if len(resultsToSave) > 0 {
43-
reports.SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath, resultsToSave)
43+
return reports.SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath, allReport)
4444
}
45+
return nil
4546
},
4647
}
4748

4849
func init() {
4950
AggregateResultsCmd.Flags().StringVarP(&resultsFolderPath, "results-path", "p", "", "Path to the folder containing JSON test result files")
50-
AggregateResultsCmd.Flags().StringVarP(&outputResultsPath, "output-results", "o", "./results.json", "Path to output the aggregated or filtered test results in JSON format")
51+
AggregateResultsCmd.Flags().StringVarP(&outputResultsPath, "output-results", "o", "./results", "Path to output the aggregated or filtered test results in JSON and markdown format")
5152
AggregateResultsCmd.Flags().StringVarP(&outputLogsPath, "output-logs", "l", "", "Path to output the filtered test logs in JSON format")
5253
AggregateResultsCmd.Flags().Float64VarP(&maxPassRatio, "max-pass-ratio", "m", 1.0, "The maximum (non-inclusive) pass ratio threshold for a test to be considered a failure. Any tests below this pass rate will be considered flaky.")
5354
AggregateResultsCmd.Flags().BoolVarP(&filterFailed, "filter-failed", "f", false, "If true, filter and output only failed tests based on the max-pass-ratio threshold")

tools/flakeguard/cmd/run.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ var RunTestsCmd = &cobra.Command{
2121
testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
2222
testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
2323
runCount, _ := cmd.Flags().GetInt("run-count")
24+
timeout, _ := cmd.Flags().GetDuration("timeout")
25+
tags, _ := cmd.Flags().GetStringArray("tags")
2426
useRace, _ := cmd.Flags().GetBool("race")
2527
outputPath, _ := cmd.Flags().GetString("output-json")
2628
maxPassRatio, _ := cmd.Flags().GetFloat64("max-pass-ratio")
2729
skipTests, _ := cmd.Flags().GetStringSlice("skip-tests")
30+
selectTests, _ := cmd.Flags().GetStringSlice("select-tests")
2831
printFailedTests, _ := cmd.Flags().GetBool("print-failed-tests")
2932
useShuffle, _ := cmd.Flags().GetBool("shuffle")
3033
shuffleSeed, _ := cmd.Flags().GetString("shuffle-seed")
@@ -49,37 +52,31 @@ var RunTestsCmd = &cobra.Command{
4952
ProjectPath: projectPath,
5053
Verbose: true,
5154
RunCount: runCount,
55+
Timeout: timeout,
56+
Tags: tags,
5257
UseRace: useRace,
5358
SkipTests: skipTests,
59+
SelectTests: selectTests,
5460
SelectedTestPackages: testPackages,
5561
UseShuffle: useShuffle,
5662
ShuffleSeed: shuffleSeed,
5763
}
5864

59-
testResults, err := runner.RunTests()
65+
testReport, err := runner.RunTests()
6066
if err != nil {
6167
fmt.Printf("Error running tests: %v\n", err)
6268
os.Exit(1)
6369
}
6470

65-
passedTests := reports.FilterPassedTests(testResults, maxPassRatio)
66-
failedTests := reports.FilterFailedTests(testResults, maxPassRatio)
67-
skippedTests := reports.FilterSkippedTests(testResults)
68-
flakyTests := reports.FilterFlakyTests(testResults, maxPassRatio)
69-
7071
// Print all failed tests including flaky tests
71-
if len(failedTests) > 0 && printFailedTests {
72-
fmt.Printf("Maximum threshold for flaky tests: %.2f\n", maxPassRatio)
72+
if printFailedTests {
7373
fmt.Printf("PassRatio threshold for flaky tests: %.2f\n", maxPassRatio)
74-
fmt.Printf("%d failed tests:\n", len(failedTests))
75-
reports.PrintTests(failedTests, os.Stdout)
74+
reports.PrintTests(os.Stdout, testReport.Results, maxPassRatio)
7675
}
7776

78-
fmt.Printf("Summary: %d passed, %d skipped, %d failed, %d flaky\n", len(passedTests), len(skippedTests), len(failedTests), len(flakyTests))
79-
8077
// Save the test results in JSON format
81-
if outputPath != "" && len(testResults) > 0 {
82-
jsonData, err := json.MarshalIndent(testResults, "", " ")
78+
if outputPath != "" && len(testReport.Results) > 0 {
79+
jsonData, err := json.MarshalIndent(testReport, "", " ")
8380
if err != nil {
8481
log.Fatalf("Error marshaling test results to JSON: %v", err)
8582
}
@@ -89,10 +86,11 @@ var RunTestsCmd = &cobra.Command{
8986
fmt.Printf("All test results saved to %s\n", outputPath)
9087
}
9188

89+
flakyTests := reports.FilterFlakyTests(testReport.Results, maxPassRatio)
9290
if len(flakyTests) > 0 {
9391
// Exit with error code if there are flaky tests
9492
os.Exit(1)
95-
} else if len(testResults) == 0 {
93+
} else if len(testReport.Results) == 0 {
9694
fmt.Printf("No tests were run for the specified packages.\n")
9795
}
9896
},
@@ -104,12 +102,15 @@ func init() {
104102
RunTestsCmd.Flags().StringSlice("test-packages", nil, "Comma-separated list of test packages to run")
105103
RunTestsCmd.Flags().Bool("run-all-packages", false, "Run all test packages in the project. This flag overrides --test-packages and --test-packages-json")
106104
RunTestsCmd.Flags().IntP("run-count", "c", 1, "Number of times to run the tests")
105+
RunTestsCmd.Flags().Duration("timeout", 0, "Passed on to the 'go test' command as the -timeout flag")
106+
RunTestsCmd.Flags().StringArray("tags", nil, "Passed on to the 'go test' command as the -tags flag")
107107
RunTestsCmd.Flags().Bool("race", false, "Enable the race detector")
108108
RunTestsCmd.Flags().Bool("shuffle", false, "Enable test shuffling")
109109
RunTestsCmd.Flags().String("shuffle-seed", "", "Set seed for test shuffling. Must be used with --shuffle")
110110
RunTestsCmd.Flags().Bool("fail-fast", false, "Stop on the first test failure")
111111
RunTestsCmd.Flags().String("output-json", "", "Path to output the test results in JSON format")
112112
RunTestsCmd.Flags().StringSlice("skip-tests", nil, "Comma-separated list of test names to skip from running")
113+
RunTestsCmd.Flags().StringSlice("select-tests", nil, "Comma-separated list of test names to specifically run")
113114
RunTestsCmd.Flags().Bool("print-failed-tests", true, "Print failed test results to the console")
114115
RunTestsCmd.Flags().Float64("max-pass-ratio", 1.0, "The maximum (non-inclusive) pass ratio threshold for a test to be considered a failure. Any tests below this pass rate will be considered flaky.")
115116
}

tools/flakeguard/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard
22

33
go 1.21.9
44

5-
require github.com/spf13/cobra v1.8.1
5+
require (
6+
github.com/spf13/cobra v1.8.1
7+
golang.org/x/text v0.20.0
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect

tools/flakeguard/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1212
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1313
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1414
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15+
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
16+
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
1517
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1618
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1719
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)