Skip to content

Commit 21f02c1

Browse files
committed
fix
1 parent 6aba80f commit 21f02c1

File tree

4 files changed

+31
-73
lines changed

4 files changed

+31
-73
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9-
"path/filepath"
109

1110
"github.com/rs/zerolog/log"
1211
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
@@ -62,13 +61,6 @@ var RunTestsCmd = &cobra.Command{
6261
os.Exit(ErrorExitCode)
6362
}
6463

65-
outputDir := filepath.Dir(outputPath)
66-
initialDirSize, err := getDirSize(outputDir)
67-
if err != nil {
68-
log.Error().Err(err).Str("path", outputDir).Msg("Error getting initial directory size")
69-
// intentionally don't exit here, as we can still proceed with the run
70-
}
71-
7264
// Check if project dependencies are correctly set up
7365
if err := checkDependencies(projectPath); err != nil {
7466
log.Error().Err(err).Msg("Error checking project dependencies")
@@ -112,26 +104,27 @@ var RunTestsCmd = &cobra.Command{
112104

113105
// Run the tests
114106
var (
115-
testReport *reports.TestReport
107+
firstReport *reports.TestReport
116108
)
117109

110+
var err error
118111
if len(testCmdStrings) > 0 {
119-
testReport, err = testRunner.RunTestCmd(testCmdStrings)
112+
firstReport, err = testRunner.RunTestCmd(testCmdStrings)
120113
if err != nil {
121114
log.Fatal().Err(err).Msg("Error running custom test command")
122115
os.Exit(ErrorExitCode)
123116
}
124117
} else {
125-
testReport, err = testRunner.RunTestPackages(testPackages)
118+
firstReport, err = testRunner.RunTestPackages(testPackages)
126119
if err != nil {
127120
log.Fatal().Err(err).Msg("Error running test packages")
128121
os.Exit(ErrorExitCode)
129122
}
130123
}
131124

132125
// Save the test results in JSON format
133-
if outputPath != "" && len(testReport.Results) > 0 {
134-
jsonData, err := json.MarshalIndent(testReport, "", " ")
126+
if outputPath != "" && len(firstReport.Results) > 0 {
127+
jsonData, err := json.MarshalIndent(firstReport, "", " ")
135128
if err != nil {
136129
log.Error().Err(err).Msg("Error marshaling test results to JSON")
137130
os.Exit(ErrorExitCode)
@@ -143,14 +136,17 @@ var RunTestsCmd = &cobra.Command{
143136
log.Info().Str("path", outputPath).Msg("Test results saved")
144137
}
145138

146-
if len(testReport.Results) == 0 {
139+
if len(firstReport.Results) == 0 {
147140
log.Warn().Msg("No tests were run for the specified packages")
148141
return
149142
}
150143

144+
fmt.Printf("\nFlakeguard Initial Summary:\n")
145+
reports.RenderResults(os.Stdout, firstReport, false, false)
146+
151147
// Rerun failed tests
152148
if rerunFailed > 0 {
153-
rerunReport, err := testRunner.RerunFailedTests(testReport.Results)
149+
rerunReport, err := testRunner.RerunFailedTests(firstReport.Results)
154150
if err != nil {
155151
log.Fatal().Err(err).Msg("Error rerunning failed tests")
156152
os.Exit(ErrorExitCode)
@@ -160,59 +156,37 @@ var RunTestsCmd = &cobra.Command{
160156
failedAfterRerun := reports.FilterTests(rerunReport.Results, func(tr reports.TestResult) bool {
161157
return !tr.Skipped && tr.Successes == 0
162158
})
159+
fmt.Printf("\nFlakeguard Rerun Summary:\n")
160+
reports.RenderResults(os.Stdout, rerunReport, false, false)
161+
163162
if len(failedAfterRerun) > 0 {
164163
log.Error().
165164
Int("tests", len(failedAfterRerun)).
166165
Int("reruns", rerunFailed).
167166
Msg("Tests still failing after reruns with 0 successes")
168167
os.Exit(ErrorExitCode)
168+
} else {
169+
log.Info().Msg("Failed tests passed at least once after reruns")
170+
os.Exit(0)
169171
}
170172

171173
// TODO: save rerun test report to JSON file
172174
}
173175

174176
// Filter flaky tests using FilterTests
175-
flakyTests := reports.FilterTests(testReport.Results, func(tr reports.TestResult) bool {
177+
flakyTests := reports.FilterTests(firstReport.Results, func(tr reports.TestResult) bool {
176178
return !tr.Skipped && tr.PassRatio < passRatioThreshold
177179
})
178180

179-
finalDirSize, err := getDirSize(outputDir)
180-
if err != nil {
181-
log.Error().Err(err).Str("path", outputDir).Msg("Error getting final directory size")
182-
// intentionally don't exit here, as we can still proceed with the run
183-
}
184-
diskSpaceUsed := byteCountSI(finalDirSize - initialDirSize)
185-
186-
// Report with more detailed information about reruns
187-
if rerunFailed > 0 {
188-
log.Info().
189-
Int("initial runs", runCount).
190-
Int("reruns for failed tests", rerunFailed).
191-
Str("disk space used", diskSpaceUsed).
192-
Msg("Test execution completed")
193-
} else {
194-
log.Info().
195-
Int("runs", runCount).
196-
Str("disk space used", diskSpaceUsed).
197-
Msg("Test execution completed")
198-
}
199-
200181
if len(flakyTests) > 0 {
201182
log.Info().
202183
Int("count", len(flakyTests)).
203184
Str("stability threshold", fmt.Sprintf("%.0f%%", passRatioThreshold*100)).
204185
Msg("Found flaky tests")
186+
os.Exit(FlakyTestsExitCode)
205187
} else {
206188
log.Info().Msg("All tests passed stability requirements")
207189
}
208-
209-
fmt.Printf("\nFlakeguard Summary\n")
210-
reports.RenderResults(os.Stdout, testReport, false, false)
211-
212-
if len(flakyTests) > 0 {
213-
// Exit with error code if there are flaky tests
214-
os.Exit(FlakyTestsExitCode)
215-
}
216190
},
217191
}
218192

tools/flakeguard/reports/presentation.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func renderSummaryTable(w io.Writer, summary *SummaryData, markdown bool, collap
231231

232232
func renderTestResultsTable(w io.Writer, table [][]string, collapsible bool) {
233233
if len(table) <= 1 {
234-
fmt.Fprintln(w, "No tests found under the specified pass ratio threshold.")
235234
return
236235
}
237236
printTable(w, table, collapsible)

tools/flakeguard/runner/example_test_package/example_tests_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,21 @@ func TestTimeout(t *testing.T) {
218218
func TestRandomFlaky(t *testing.T) {
219219
t.Parallel()
220220

221+
time.Sleep(100 * time.Millisecond)
222+
221223
// Seed random number generator with current time
222-
r := rand.New(rand.NewSource(time.Now().UnixNano()))
224+
seed := time.Now().UnixNano()
225+
t.Logf("Using seed: %d", seed)
226+
r := rand.New(rand.NewSource(seed))
223227

224228
// Generate a random number between 0 and 1
225229
randomValue := r.Float64()
226230

227231
t.Logf("Random value generated: %f", randomValue)
228232

229-
// Fail the test approximately 90% of the time
230-
if randomValue < 0.9 {
231-
t.Fatal("This test randomly failed (90% probability)")
233+
if randomValue < 0.5 {
234+
t.Fatal("This test randomly failed")
232235
}
233236

234-
t.Log("This test randomly passed (90% probability)")
237+
t.Log("This test randomly passed")
235238
}

tools/flakeguard/runner/runner.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,8 @@ func (r *Runner) RerunFailedTests(results []reports.TestResult) (*reports.TestRe
739739
// Group failing tests by package for more efficient reruns
740740
failingTestsByPackage := make(map[string][]string)
741741
for _, tr := range results {
742-
if !tr.Skipped && tr.PassRatio < 1 {
742+
if !tr.Skipped {
743+
// if !tr.Skipped && tr.PassRatio < 1 { //TODO uncomment
743744
if _, exists := failingTestsByPackage[tr.TestPackage]; !exists {
744745
failingTestsByPackage[tr.TestPackage] = []string{}
745746
}
@@ -754,7 +755,7 @@ func (r *Runner) RerunFailedTests(results []reports.TestResult) (*reports.TestRe
754755
var rerunJsonFilePaths []string
755756

756757
// Rerun each failing test package up to RerunFailed times
757-
for i := 0; i < r.RerunFailed; i++ {
758+
for i := range r.RerunFailed {
758759
for pkg, tests := range failingTestsByPackage {
759760
// Build regex pattern to match all failing tests in this package
760761
testPattern := fmt.Sprintf("^(%s)$", strings.Join(tests, "|"))
@@ -805,26 +806,7 @@ func (r *Runner) RerunFailedTests(results []reports.TestResult) (*reports.TestRe
805806
Results: rerunResults,
806807
MaxPassRatio: r.MaxPassRatio,
807808
}
809+
report.GenerateSummaryData()
808810

809811
return report, nil
810812
}
811-
812-
// buildGoTestCommandForTest builds a `go test` command specifically
813-
// for one failing test, using TestPackage and TestName in the -run argument.
814-
func (r *Runner) buildGoTestCommandForTest(t reports.TestResult) []string {
815-
cmd := []string{
816-
"go", "test",
817-
t.TestPackage,
818-
"-run", fmt.Sprintf("^%s$", t.TestName), // Run exactly this test
819-
"-json", // Example flag, adjust as needed
820-
}
821-
822-
// Add verbosity if requested
823-
if r.Verbose {
824-
cmd = append(cmd, "-v")
825-
}
826-
827-
// Add any additional flags or args required by your setup here
828-
829-
return cmd
830-
}

0 commit comments

Comments
 (0)