Skip to content

Commit 50da735

Browse files
committed
zerolog
1 parent 74a4a66 commit 50da735

File tree

16 files changed

+336
-189
lines changed

16 files changed

+336
-189
lines changed

tools/flakeguard/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ example:
1515
- 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
1616
- 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
1717
- 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
18+
go run . aggregate-results --results-path ./example_results --output-path ./example_results
1919

2020
.PHONY: example_flaky_panic
2121
example_flaky_panic:
@@ -24,7 +24,7 @@ example_flaky_panic:
2424
- 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
2525
- 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
2626
- 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
27+
go run . aggregate-results --results-path ./example_results --output-path ./example_results
2828

2929
.PHONY: example_timeout
3030
example_timeout:
@@ -33,4 +33,4 @@ example_timeout:
3333
- 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
3434
- 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
3535
- 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
36+
go run . aggregate-results --results-path ./example_results --output-path ./example_results

tools/flakeguard/cmd/aggregate_results.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/briandowns/spinner"
10+
"github.com/rs/zerolog/log"
1011
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
1112
"github.com/spf13/cobra"
1213
)
@@ -28,6 +29,9 @@ var AggregateResultsCmd = &cobra.Command{
2829
headSHA, _ := cmd.Flags().GetString("head-sha")
2930
baseSHA, _ := cmd.Flags().GetString("base-sha")
3031
githubWorkflowName, _ := cmd.Flags().GetString("github-workflow-name")
32+
reportID, _ := cmd.Flags().GetString("report-id")
33+
splunkURL, _ := cmd.Flags().GetString("splunk-url")
34+
splunkToken, _ := cmd.Flags().GetString("splunk-token")
3135

3236
// Ensure the output directory exists
3337
if err := fs.MkdirAll(outputDir, 0755); err != nil {
@@ -40,13 +44,14 @@ var AggregateResultsCmd = &cobra.Command{
4044
s.Start()
4145

4246
// Load test reports from JSON files and aggregate them
43-
aggregatedReport, err := reports.LoadAndAggregate(resultsPath)
47+
aggregatedReport, err := reports.LoadAndAggregate(resultsPath, reports.WithReportID(reportID), reports.WithSplunk(splunkURL, splunkToken))
4448
if err != nil {
4549
s.Stop()
50+
fmt.Println()
4651
return fmt.Errorf("error loading test reports: %w", err)
4752
}
4853
s.Stop()
49-
fmt.Println("Test reports aggregated successfully.")
54+
fmt.Println()
5055

5156
// Add metadata to the aggregated report
5257
aggregatedReport.HeadSHA = headSHA
@@ -59,7 +64,7 @@ var AggregateResultsCmd = &cobra.Command{
5964
return fmt.Errorf("error aggregating test reports: %w", err)
6065
}
6166
s.Stop()
62-
fmt.Println("Test reports aggregated successfully.")
67+
log.Info().Msg("Successfully loaded and aggregated test reports")
6368

6469
// Start spinner for mapping test results to paths
6570
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
@@ -73,7 +78,7 @@ var AggregateResultsCmd = &cobra.Command{
7378
return fmt.Errorf("error mapping test results to paths: %w", err)
7479
}
7580
s.Stop()
76-
fmt.Println("Test results mapped to paths successfully.")
81+
log.Info().Msg("Successfully mapped paths to test results")
7782

7883
// Map test results to code owners if codeOwnersPath is provided
7984
if codeOwnersPath != "" {
@@ -87,7 +92,7 @@ var AggregateResultsCmd = &cobra.Command{
8792
return fmt.Errorf("error mapping test results to code owners: %w", err)
8893
}
8994
s.Stop()
90-
fmt.Println("Test results mapped to code owners successfully.")
95+
log.Info().Msg("Successfully mapped code owners to test results")
9196
}
9297

9398
failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool {
@@ -97,7 +102,7 @@ var AggregateResultsCmd = &cobra.Command{
97102

98103
// Check if there are any failed tests
99104
if len(failedTests) > 0 {
100-
fmt.Printf("Found %d failed test(s).\n", len(failedTests))
105+
log.Info().Int("count", len(failedTests)).Msg("Found failed tests")
101106

102107
// Create a new report for failed tests with logs
103108
failedReportWithLogs := &reports.TestReport{
@@ -117,7 +122,7 @@ var AggregateResultsCmd = &cobra.Command{
117122
if err := reports.SaveReport(fs, failedTestsReportWithLogsPath, *failedReportWithLogs); err != nil {
118123
return fmt.Errorf("error saving failed tests report with logs: %w", err)
119124
}
120-
fmt.Printf("Failed tests report with logs saved to %s\n", failedTestsReportWithLogsPath)
125+
log.Debug().Str("path", failedTestsReportWithLogsPath).Msg("Failed tests report with logs saved")
121126

122127
// Remove logs from test results for the report without logs
123128
for i := range failedReportWithLogs.Results {
@@ -131,9 +136,9 @@ var AggregateResultsCmd = &cobra.Command{
131136
if err := reports.SaveReport(fs, failedTestsReportNoLogsPath, *failedReportWithLogs); err != nil {
132137
return fmt.Errorf("error saving failed tests report without logs: %w", err)
133138
}
134-
fmt.Printf("Failed tests report without logs saved to %s\n", failedTestsReportNoLogsPath)
139+
log.Info().Str("path", failedTestsReportNoLogsPath).Msg("Failed tests report without logs saved")
135140
} else {
136-
fmt.Println("No failed tests found. Skipping generation of failed tests reports.")
141+
log.Info().Msg("No failed tests found. Skipping generation of failed tests reports")
137142
}
138143

139144
// Remove logs from test results for the aggregated report
@@ -148,7 +153,7 @@ var AggregateResultsCmd = &cobra.Command{
148153
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
149154
return fmt.Errorf("error saving aggregated test report: %w", err)
150155
}
151-
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)
156+
log.Info().Str("path", aggregatedReportPath).Msg("Aggregated test report saved")
152157

153158
// Generate all-tests-summary.json
154159
if summaryFileName != "" {
@@ -163,11 +168,10 @@ var AggregateResultsCmd = &cobra.Command{
163168
return fmt.Errorf("error generating summary json: %w", err)
164169
}
165170
s.Stop()
166-
fmt.Printf("Summary generated at %s\n", summaryFilePath)
171+
log.Info().Str("path", summaryFilePath).Msg("Summary generated")
167172
}
168173

169-
fmt.Println("Aggregation complete.")
170-
174+
log.Info().Str("path", outputDir).Msg("Aggregation complete")
171175
return nil
172176
},
173177
}
@@ -183,8 +187,13 @@ func init() {
183187
AggregateResultsCmd.Flags().String("head-sha", "", "Head commit SHA for the test report")
184188
AggregateResultsCmd.Flags().String("base-sha", "", "Base commit SHA for the test report")
185189
AggregateResultsCmd.Flags().String("github-workflow-name", "", "GitHub workflow name for the test report")
190+
AggregateResultsCmd.Flags().String("report-id", "", "Optional identifier for the test report. Will be generated if not provided")
191+
AggregateResultsCmd.Flags().String("splunk-url", "", "Optional url to simultaneously send the test results to splunk")
192+
AggregateResultsCmd.Flags().String("splunk-token", "", "Optional Splunk HEC token to simultaneously send the test results to splunk")
186193

187-
AggregateResultsCmd.MarkFlagRequired("results-path")
194+
if err := AggregateResultsCmd.MarkFlagRequired("results-path"); err != nil {
195+
log.Fatal().Err(err).Msg("Error marking flag as required")
196+
}
188197
}
189198

190199
// New function to generate all-tests-summary.json

tools/flakeguard/cmd/check_test_owners.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"log"
64
"path/filepath"
75

6+
"github.com/rs/zerolog/log"
87
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/codeowners"
98
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
109
"github.com/spf13/cobra"
@@ -25,21 +24,21 @@ var CheckTestOwnersCmd = &cobra.Command{
2524
// Scan project for test functions
2625
testFileMap, err := reports.ScanTestFiles(projectPath)
2726
if err != nil {
28-
log.Fatalf("Error scanning test files: %v", err)
27+
log.Fatal().Err(err).Msg("Error scanning test files")
2928
}
3029

3130
// Parse CODEOWNERS file
3231
codeOwnerPatterns, err := codeowners.Parse(codeownersPath)
3332
if err != nil {
34-
log.Fatalf("Error parsing CODEOWNERS file: %v", err)
33+
log.Fatal().Err(err).Msg("Error parsing CODEOWNERS file")
3534
}
3635

3736
// Check for tests without code owners
3837
testsWithoutOwners := make(map[string]string) // Map of test names to their relative paths
3938
for testName, filePath := range testFileMap {
4039
relFilePath, err := filepath.Rel(projectPath, filePath)
4140
if err != nil {
42-
fmt.Printf("Error getting relative path for test %s: %v\n", testName, err)
41+
log.Error().Err(err).Msgf("Error getting relative path for test %s", testName)
4342
continue
4443
}
4544
// Convert to Unix-style path for matching
@@ -60,19 +59,18 @@ var CheckTestOwnersCmd = &cobra.Command{
6059
percentageWithoutOwners := float64(totalWithoutOwners) / float64(totalTests) * 100
6160

6261
// Report results
63-
fmt.Printf("Total Test functions found: %d\n", totalTests)
64-
fmt.Printf("Test functions with owners: %d (%.2f%%)\n", totalWithOwners, percentageWithOwners)
65-
fmt.Printf("Test functions without owners: %d (%.2f%%)\n", totalWithoutOwners, percentageWithoutOwners)
62+
log.Info().Msgf("Total Test functions found: %d", totalTests)
63+
log.Info().Msgf("Test functions with owners: %d (%.2f%%)", totalWithOwners, percentageWithOwners)
64+
log.Info().Msgf("Test functions without owners: %d (%.2f%%)", totalWithoutOwners, percentageWithoutOwners)
6665

6766
if printTestFunctions {
68-
6967
if totalWithoutOwners > 0 {
70-
fmt.Println("\nTest functions without owners:")
68+
log.Debug().Msg("Test functions without owners:")
7169
for testName, relPath := range testsWithoutOwners {
72-
fmt.Printf("- %s (%s)\n", testName, relPath)
70+
log.Debug().Msgf("- %s (%s)", testName, relPath)
7371
}
7472
} else {
75-
fmt.Println("All Test functions have code owners!")
73+
log.Debug().Msg("All Test functions have code owners!")
7674
}
7775
}
7876

tools/flakeguard/cmd/find.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package cmd
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
"log"
75

6+
"github.com/rs/zerolog/log"
87
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/git"
98
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/golang"
109
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/utils"
@@ -32,26 +31,26 @@ var FindTestsCmd = &cobra.Command{
3231
if findByTestFilesDiff {
3332
changedTestFiles, err := git.FindChangedFiles(projectPath, baseRef, "grep '_test\\.go$'")
3433
if err != nil {
35-
log.Fatalf("Error finding changed test files: %v", err)
34+
log.Fatal().Err(err).Msg("Error finding changed test files")
3635
}
3736
if onlyShowChangedTestFiles {
3837
outputResults(changedTestFiles, jsonOutput)
3938
return
4039
}
4140
if verbose {
42-
fmt.Println("Changed test files:", changedTestFiles)
41+
log.Debug().Strs("files", changedTestFiles).Msg("Found changed test files")
4342
}
4443
changedTestPkgs, err = golang.GetFilePackages(changedTestFiles)
4544
if err != nil {
46-
log.Fatalf("Error getting package names for test files: %v", err)
45+
log.Fatal().Err(err).Msg("Error getting package names for test files")
4746
}
4847
}
4948

5049
// Find all affected test packages
5150
var affectedTestPkgs []string
5251
if findByAffected {
5352
if verbose {
54-
fmt.Println("Finding affected packages...")
53+
log.Debug().Msg("Finding affected packages...")
5554
}
5655
affectedTestPkgs = findAffectedPackages(baseRef, projectPath, excludes, levels)
5756
}
@@ -63,7 +62,7 @@ var FindTestsCmd = &cobra.Command{
6362
// Filter out packages that do not have tests
6463
if filterEmptyTests {
6564
if verbose {
66-
fmt.Println("Filtering packages without tests...")
65+
log.Debug().Msg("Filtering packages without tests...")
6766
}
6867
testPkgs = golang.FilterPackagesWithTests(testPkgs)
6968
}
@@ -85,40 +84,40 @@ func init() {
8584
FindTestsCmd.Flags().Bool("only-show-changed-test-files", false, "Only show the changed test files and exit")
8685

8786
if err := FindTestsCmd.MarkFlagRequired("base-ref"); err != nil {
88-
fmt.Println("Error marking base-ref as required:", err)
87+
log.Fatal().Err(err).Msg("Error marking base-ref as required")
8988
}
9089
}
9190

9291
func findAffectedPackages(baseRef, projectPath string, excludes []string, levels int) []string {
9392
goList, err := golang.GoList()
9493
if err != nil {
95-
log.Fatalf("Error getting go list: %v\nStdErr: %s", err, goList.Stderr.String())
94+
log.Fatal().Err(err).Msg("Error getting go list")
9695
}
9796
gitDiff, err := git.Diff(baseRef)
9897
if err != nil {
99-
log.Fatalf("Error getting the git diff: %v\nStdErr: %s", err, gitDiff.Stderr.String())
98+
log.Fatal().Err(err).Msg("Error getting the git diff")
10099
}
101100
gitModDiff, err := git.ModDiff(baseRef, projectPath)
102101
if err != nil {
103-
log.Fatalf("Error getting the git mod diff: %v\nStdErr: %s", err, gitModDiff.Stderr.String())
102+
log.Fatal().Err(err).Msg("Error getting the git mod diff")
104103
}
105104

106105
packages, err := golang.ParsePackages(goList.Stdout)
107106
if err != nil {
108-
log.Fatalf("Error parsing packages: %v", err)
107+
log.Fatal().Err(err).Msg("Error parsing packages")
109108
}
110109

111110
fileMap := golang.GetGoFileMap(packages, true)
112111

113112
var changedPackages []string
114113
changedPackages, err = git.GetChangedGoPackagesFromDiff(gitDiff.Stdout, projectPath, excludes, fileMap)
115114
if err != nil {
116-
log.Fatalf("Error getting changed packages: %v", err)
115+
log.Fatal().Err(err).Msg("Error getting changed packages")
117116
}
118117

119118
changedModPackages, err := git.GetGoModChangesFromDiff(gitModDiff.Stdout)
120119
if err != nil {
121-
log.Fatalf("Error getting go.mod changes: %v", err)
120+
log.Fatal().Err(err).Msg("Error getting go.mod changes")
122121
}
123122

124123
depMap := golang.GetGoDepMap(packages)
@@ -156,12 +155,12 @@ func outputResults(packages []string, jsonOutput bool) {
156155
if jsonOutput {
157156
data, err := json.Marshal(packages)
158157
if err != nil {
159-
log.Fatalf("Error marshaling test files to JSON: %v", err)
158+
log.Fatal().Err(err).Msg("Error marshaling test packages to JSON")
160159
}
161-
fmt.Println(string(data))
160+
log.Debug().Str("output", string(data)).Msg("JSON")
162161
} else {
163162
for _, pkg := range packages {
164-
fmt.Print(pkg, " ")
163+
log.Debug().Str("package", pkg).Msg("Test package")
165164
}
166165
}
167166
}

0 commit comments

Comments
 (0)