diff --git a/.github/workflows/generate-go-docs.yaml b/.github/workflows/generate-go-docs.yaml index 422818e71..55f6a1910 100644 --- a/.github/workflows/generate-go-docs.yaml +++ b/.github/workflows/generate-go-docs.yaml @@ -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 @@ -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 diff --git a/tools/flakeguard/cmd/aggregate_results.go b/tools/flakeguard/cmd/aggregate_results.go index cdf3d695f..ad6a98b4b 100644 --- a/tools/flakeguard/cmd/aggregate_results.go +++ b/tools/flakeguard/cmd/aggregate_results.go @@ -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 }) @@ -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) diff --git a/tools/flakeguard/reports/io.go b/tools/flakeguard/reports/io.go index e38952ff6..35eb46858 100644 --- a/tools/flakeguard/reports/io.go +++ b/tools/flakeguard/reports/io.go @@ -1,6 +1,7 @@ package reports import ( + "bufio" "encoding/json" "fmt" "io" @@ -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 }