Skip to content

Commit

Permalink
Remove logs from flakeguard all test results (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl authored Dec 9, 2024
1 parent 18e4bb3 commit 404e04e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/generate-go-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
generate_docs_new_pr:
if: ${{ contains(github.event.pull_request.labels.*.name, 'generate_go_docs') }}
runs-on: ubuntu-latest
environment: integration
permissions:
id-token: write
contents: read
Expand Down Expand Up @@ -55,6 +56,8 @@ jobs:
- 'k8s-test-runner/**/*.go'
framework:
- 'framework/**/*.go'
tools/flakeguard:
- 'tools/flakeguard/**/*.go'
# later add tools here or, if possible, make this filter dynamic so that it's created based on the go modules in the repository
- name: Find all go modules in the repository and filter the ones that changed
Expand Down
25 changes: 13 additions & 12 deletions tools/flakeguard/cmd/aggregate_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ var AggregateResultsCmd = &cobra.Command{
fmt.Println("Test results mapped to code owners successfully.")
}

// Save the aggregated report to the output directory
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
return fmt.Errorf("error saving aggregated test report: %w", err)
}
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)

// Filter failed tests (PassRatio < maxPassRatio and not skipped)
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Suffix = " Filtering failed tests..."
s.Start()

failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool {
return !tr.Skipped && tr.PassRatio < maxPassRatio
})
Expand Down Expand Up @@ -141,6 +129,19 @@ var AggregateResultsCmd = &cobra.Command{
fmt.Println("No failed tests found. Skipping generation of failed tests reports.")
}

// Remove logs from test results for the aggregated report
for i := range aggregatedReport.Results {
aggregatedReport.Results[i].Outputs = nil
aggregatedReport.Results[i].PackageOutputs = nil
}

// Save the aggregated report to the output directory
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
return fmt.Errorf("error saving aggregated test report: %w", err)
}
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)

// Generate all-tests-summary.json
if summaryFileName != "" {
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
Expand Down
34 changes: 31 additions & 3 deletions tools/flakeguard/reports/io.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reports

import (
"bufio"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -100,10 +101,37 @@ func SaveReportNoLogs(fs FileSystem, filePath string, report TestReport) error {
return fs.WriteFile(filePath, data, 0644)
}

// SaveReport saves a TestReport to a specified file path in JSON format.
// It ensures the file is created or truncated and handles any errors during
// file operations, providing a reliable way to persist test results.
func SaveReport(fs FileSystem, filePath string, report TestReport) error {
data, err := json.MarshalIndent(report, "", " ")
// Open the file with truncation mode
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error marshaling outputs: %v", err)
return fmt.Errorf("error opening file: %v", err)
}
return fs.WriteFile(filePath, data, 0644)
defer func() {
if cerr := file.Close(); cerr != nil {
err = fmt.Errorf("error closing file: %v", cerr)
}
}()

// Use a buffered writer for better performance
bufferedWriter := bufio.NewWriter(file)
defer func() {
if err := bufferedWriter.Flush(); err != nil {
fmt.Printf("error flushing buffer: %v\n", err)
}
}()

// Create a JSON encoder with the buffered writer
encoder := json.NewEncoder(bufferedWriter)
encoder.SetIndent("", " ")

// Encode the report
if err := encoder.Encode(report); err != nil {
return fmt.Errorf("error encoding JSON: %v", err)
}

return nil
}

0 comments on commit 404e04e

Please sign in to comment.