Skip to content

Commit 404e04e

Browse files
authored
Remove logs from flakeguard all test results (#1453)
1 parent 18e4bb3 commit 404e04e

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

.github/workflows/generate-go-docs.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
generate_docs_new_pr:
1212
if: ${{ contains(github.event.pull_request.labels.*.name, 'generate_go_docs') }}
1313
runs-on: ubuntu-latest
14+
environment: integration
1415
permissions:
1516
id-token: write
1617
contents: read
@@ -55,6 +56,8 @@ jobs:
5556
- 'k8s-test-runner/**/*.go'
5657
framework:
5758
- 'framework/**/*.go'
59+
tools/flakeguard:
60+
- 'tools/flakeguard/**/*.go'
5861
# later add tools here or, if possible, make this filter dynamic so that it's created based on the go modules in the repository
5962
6063
- name: Find all go modules in the repository and filter the ones that changed

tools/flakeguard/cmd/aggregate_results.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ var AggregateResultsCmd = &cobra.Command{
8787
fmt.Println("Test results mapped to code owners successfully.")
8888
}
8989

90-
// Save the aggregated report to the output directory
91-
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
92-
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
93-
return fmt.Errorf("error saving aggregated test report: %w", err)
94-
}
95-
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)
96-
97-
// Filter failed tests (PassRatio < maxPassRatio and not skipped)
98-
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
99-
s.Suffix = " Filtering failed tests..."
100-
s.Start()
101-
10290
failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool {
10391
return !tr.Skipped && tr.PassRatio < maxPassRatio
10492
})
@@ -141,6 +129,19 @@ var AggregateResultsCmd = &cobra.Command{
141129
fmt.Println("No failed tests found. Skipping generation of failed tests reports.")
142130
}
143131

132+
// Remove logs from test results for the aggregated report
133+
for i := range aggregatedReport.Results {
134+
aggregatedReport.Results[i].Outputs = nil
135+
aggregatedReport.Results[i].PackageOutputs = nil
136+
}
137+
138+
// Save the aggregated report to the output directory
139+
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
140+
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
141+
return fmt.Errorf("error saving aggregated test report: %w", err)
142+
}
143+
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)
144+
144145
// Generate all-tests-summary.json
145146
if summaryFileName != "" {
146147
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)

tools/flakeguard/reports/io.go

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

33
import (
4+
"bufio"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -100,10 +101,37 @@ func SaveReportNoLogs(fs FileSystem, filePath string, report TestReport) error {
100101
return fs.WriteFile(filePath, data, 0644)
101102
}
102103

104+
// SaveReport saves a TestReport to a specified file path in JSON format.
105+
// It ensures the file is created or truncated and handles any errors during
106+
// file operations, providing a reliable way to persist test results.
103107
func SaveReport(fs FileSystem, filePath string, report TestReport) error {
104-
data, err := json.MarshalIndent(report, "", " ")
108+
// Open the file with truncation mode
109+
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
105110
if err != nil {
106-
return fmt.Errorf("error marshaling outputs: %v", err)
111+
return fmt.Errorf("error opening file: %v", err)
107112
}
108-
return fs.WriteFile(filePath, data, 0644)
113+
defer func() {
114+
if cerr := file.Close(); cerr != nil {
115+
err = fmt.Errorf("error closing file: %v", cerr)
116+
}
117+
}()
118+
119+
// Use a buffered writer for better performance
120+
bufferedWriter := bufio.NewWriter(file)
121+
defer func() {
122+
if err := bufferedWriter.Flush(); err != nil {
123+
fmt.Printf("error flushing buffer: %v\n", err)
124+
}
125+
}()
126+
127+
// Create a JSON encoder with the buffered writer
128+
encoder := json.NewEncoder(bufferedWriter)
129+
encoder.SetIndent("", " ")
130+
131+
// Encode the report
132+
if err := encoder.Encode(report); err != nil {
133+
return fmt.Errorf("error encoding JSON: %v", err)
134+
}
135+
136+
return nil
109137
}

0 commit comments

Comments
 (0)