Skip to content

Commit e8ce79c

Browse files
committed
Save test outputs and logs to separate file
1 parent dc3d5a0 commit e8ce79c

File tree

3 files changed

+99
-33
lines changed

3 files changed

+99
-33
lines changed

tools/flakeguard/cmd/aggregate.go

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

33
import (
4-
"fmt"
54
"log"
65

76
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
@@ -13,27 +12,24 @@ var AggregateAllCmd = &cobra.Command{
1312
Short: "Aggregate all test results and output them to a file",
1413
Run: func(cmd *cobra.Command, args []string) {
1514
resultsFolderPath, _ := cmd.Flags().GetString("results-path")
16-
outputPath, _ := cmd.Flags().GetString("output-json")
15+
outputResultsPath, _ := cmd.Flags().GetString("output-results")
16+
outputLogsPath, _ := cmd.Flags().GetString("output-logs")
1717

1818
// Aggregate all test results
1919
allResults, err := reports.AggregateTestResults(resultsFolderPath)
2020
if err != nil {
2121
log.Fatalf("Error aggregating results: %v", err)
2222
}
2323

24-
// Output all results to JSON file
25-
if outputPath != "" && len(allResults) > 0 {
26-
if err := saveResults(outputPath, allResults); err != nil {
27-
log.Fatalf("Error writing aggregated results to file: %v", err)
28-
}
29-
fmt.Printf("Aggregated test results saved to %s\n", outputPath)
30-
} else {
31-
fmt.Println("No test results found.")
24+
// Output all results to JSON files
25+
if len(allResults) > 0 {
26+
reports.SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath, allResults)
3227
}
3328
},
3429
}
3530

3631
func init() {
3732
AggregateAllCmd.Flags().String("results-path", "testresult/", "Path to the folder containing JSON test result files")
38-
AggregateAllCmd.Flags().String("output-json", "all_tests.json", "Path to output the aggregated test results in JSON format")
33+
AggregateFailedCmd.Flags().String("output-results", "failed_tests.json", "Path to output the filtered failed test results in JSON format")
34+
AggregateFailedCmd.Flags().String("output-logs", "failed_logs.json", "Path to output the filtered failed test logs in JSON format")
3935
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package cmd
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"log"
7-
"os"
85

96
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
107
"github.com/spf13/cobra"
@@ -15,7 +12,8 @@ var AggregateFailedCmd = &cobra.Command{
1512
Short: "Aggregate all test results, then filter and output only failed tests based on a threshold",
1613
Run: func(cmd *cobra.Command, args []string) {
1714
resultsFolderPath, _ := cmd.Flags().GetString("results-path")
18-
outputPath, _ := cmd.Flags().GetString("output-json")
15+
outputResultsPath, _ := cmd.Flags().GetString("output-results")
16+
outputLogsPath, _ := cmd.Flags().GetString("output-logs")
1917
threshold, _ := cmd.Flags().GetFloat64("threshold")
2018
minPassRatio, _ := cmd.Flags().GetFloat64("min-pass-ratio")
2119

@@ -33,30 +31,17 @@ var AggregateFailedCmd = &cobra.Command{
3331
}
3432
}
3533

36-
// Output failed results to JSON file
37-
if outputPath != "" && len(failedResults) > 0 {
38-
if err := saveResults(outputPath, failedResults); err != nil {
39-
log.Fatalf("Error writing failed results to file: %v", err)
40-
}
41-
fmt.Printf("Filtered failed test results saved to %s\n", outputPath)
42-
} else {
43-
fmt.Println("No failed tests found based on the specified threshold and min pass ratio.")
34+
// Output results to JSON files
35+
if len(failedResults) > 0 {
36+
reports.SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath, failedResults)
4437
}
4538
},
4639
}
4740

4841
func init() {
4942
AggregateFailedCmd.Flags().String("results-path", "testresult/", "Path to the folder containing JSON test result files")
50-
AggregateFailedCmd.Flags().String("output-json", "failed_tests.json", "Path to output the filtered failed test results in JSON format")
43+
AggregateFailedCmd.Flags().String("output-results", "failed_tests.json", "Path to output the filtered failed test results in JSON format")
44+
AggregateFailedCmd.Flags().String("output-logs", "failed_logs.json", "Path to output the filtered failed test logs in JSON format")
5145
AggregateFailedCmd.Flags().Float64("threshold", 0.8, "Threshold for considering a test as failed")
5246
AggregateFailedCmd.Flags().Float64("min-pass-ratio", 0.001, "Minimum pass ratio for considering a test as flaky. Used to distinguish between tests that are truly flaky (with inconsistent results) and those that are consistently failing.")
5347
}
54-
55-
// Helper function to save results to JSON file
56-
func saveResults(filePath string, results []reports.TestResult) error {
57-
data, err := json.MarshalIndent(results, "", " ")
58-
if err != nil {
59-
return fmt.Errorf("error marshaling results: %v", err)
60-
}
61-
return os.WriteFile(filePath, data, 0644)
62-
}

tools/flakeguard/reports/reports.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"log"
78
"os"
89
"path/filepath"
910
"sort"
@@ -139,3 +140,87 @@ func PrintTests(tests []TestResult, w io.Writer) {
139140
fmt.Fprintf(w, "Outputs:\n%s\n", strings.Join(test.Outputs, ""))
140141
}
141142
}
143+
144+
// Helper function to save filtered results and logs to specified paths
145+
func SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath string, failedResults []TestResult) {
146+
if outputResultsPath != "" {
147+
if err := saveResults(outputResultsPath, failedResults); err != nil {
148+
log.Fatalf("Error writing failed results to file: %v", err)
149+
}
150+
fmt.Printf("Filtered failed test results saved to %s\n", outputResultsPath)
151+
} else {
152+
fmt.Println("No failed tests found based on the specified threshold and min pass ratio.")
153+
}
154+
155+
if outputLogsPath != "" {
156+
if err := saveTestOutputs(outputLogsPath, failedResults); err != nil {
157+
log.Fatalf("Error writing failed logs to file: %v", err)
158+
}
159+
fmt.Printf("Filtered failed test logs saved to %s\n", outputLogsPath)
160+
}
161+
}
162+
163+
// Helper function to save results to JSON file
164+
func saveResults(filePath string, results []TestResult) error {
165+
// Define a struct type without Outputs and PackageOutputs
166+
type filteredTestResult struct {
167+
TestName string
168+
TestPackage string
169+
Panicked bool
170+
PackagePanicked bool
171+
PassRatio float64
172+
PassRatioPercentage string
173+
Skipped bool
174+
Runs int
175+
Durations []float64
176+
}
177+
178+
var filteredResults []filteredTestResult
179+
for _, r := range results {
180+
filteredResults = append(filteredResults, filteredTestResult{
181+
TestName: r.TestName,
182+
TestPackage: r.TestPackage,
183+
Panicked: r.Panicked,
184+
PackagePanicked: r.PackagePanicked,
185+
PassRatio: r.PassRatio,
186+
PassRatioPercentage: r.PassRatioPercentage,
187+
Skipped: r.Skipped,
188+
Runs: r.Runs,
189+
Durations: r.Durations,
190+
})
191+
}
192+
193+
data, err := json.MarshalIndent(filteredResults, "", " ")
194+
if err != nil {
195+
return fmt.Errorf("error marshaling results: %v", err)
196+
}
197+
return os.WriteFile(filePath, data, 0644)
198+
}
199+
200+
// Helper function to save test names, packages, and outputs to JSON file
201+
func saveTestOutputs(filePath string, results []TestResult) error {
202+
// Define a struct type with only the required fields
203+
type outputOnlyResult struct {
204+
TestName string
205+
TestPackage string
206+
Outputs []string
207+
PackageOutputs []string
208+
}
209+
210+
// Convert results to the filtered struct
211+
var outputResults []outputOnlyResult
212+
for _, r := range results {
213+
outputResults = append(outputResults, outputOnlyResult{
214+
TestName: r.TestName,
215+
TestPackage: r.TestPackage,
216+
Outputs: r.Outputs,
217+
PackageOutputs: r.PackageOutputs,
218+
})
219+
}
220+
221+
data, err := json.MarshalIndent(outputResults, "", " ")
222+
if err != nil {
223+
return fmt.Errorf("error marshaling outputs: %v", err)
224+
}
225+
return os.WriteFile(filePath, data, 0644)
226+
}

0 commit comments

Comments
 (0)