Skip to content

Commit 6b98721

Browse files
committed
Fix runner tests
1 parent c81a88b commit 6b98721

File tree

4 files changed

+27
-125
lines changed

4 files changed

+27
-125
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ var RunTestsCmd = &cobra.Command{
127127
OmitOutputsOnSuccess: omitOutputsOnSuccess,
128128
MaxPassRatio: passRatioThreshold, // Use the calculated threshold
129129
IgnoreParentFailuresOnSubtests: ignoreParentFailuresOnSubtests,
130-
RerunCount: rerunFailedCount,
131130
FailFast: failFast,
132131
}
133132

@@ -185,7 +184,7 @@ var RunTestsCmd = &cobra.Command{
185184
flushSummaryAndExit(0)
186185
}
187186

188-
rerunResults, rerunJsonOutputPaths, err := testRunner.RerunFailedTests(failedTests)
187+
rerunResults, rerunJsonOutputPaths, err := testRunner.RerunFailedTests(failedTests, rerunFailedCount)
189188
if err != nil {
190189
log.Fatal().Err(err).Msg("Error rerunning failed tests")
191190
flushSummaryAndExit(ErrorExitCode)

tools/flakeguard/runner/example_test_package/example_tests_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package exampletestpackage
22

33
import (
44
"log"
5-
"math/rand"
65
"os"
76
"sync"
87
"testing"
@@ -213,24 +212,3 @@ func TestTimeout(t *testing.T) {
213212
time.Sleep(time.Until(deadline))
214213
t.Logf("This test should have timed out")
215214
}
216-
217-
// TestRandomFlaky is a truly random flaky test that will fail approximately 50% of the time
218-
func TestRandomFlaky(t *testing.T) {
219-
t.Parallel()
220-
221-
// Seed random number generator with current time
222-
seed := time.Now().UnixNano()
223-
t.Logf("Using seed: %d", seed)
224-
r := rand.New(rand.NewSource(seed))
225-
226-
// Generate a random number between 0 and 1
227-
randomValue := r.Float64()
228-
229-
t.Logf("Random value generated: %f", randomValue)
230-
231-
if randomValue < 0.5 {
232-
t.Fatal("This test randomly failed")
233-
}
234-
235-
t.Log("This test randomly passed")
236-
}

tools/flakeguard/runner/runner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ type Runner struct {
3030
ProjectPath string // Path to the Go project directory.
3131
Verbose bool // If true, provides detailed logging.
3232
RunCount int // Number of times to run the tests.
33-
RerunCount int // Number of additional runs for tests that initially fail.
3433
GoTestCountFlag *int // Run go test with -count flag.
3534
GoTestRaceFlag bool // Run go test with -race flag.
3635
GoTestTimeoutFlag string // Run go test with -timeout flag
@@ -658,7 +657,7 @@ func parseSubTest(testName string) (parentTestName, subTestName string) {
658657
return parts[0], parts[1]
659658
}
660659

661-
func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) ([]reports.TestResult, []string, error) {
660+
func (r *Runner) RerunFailedTests(failedTests []reports.TestResult, rerunCount int) ([]reports.TestResult, []string, error) {
662661
// Group the provided failed tests by package for more efficient reruns
663662
failingTestsByPackage := make(map[string][]string)
664663
for _, tr := range failedTests {
@@ -672,7 +671,7 @@ func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) ([]reports.T
672671
var rerunJsonOutputPaths []string
673672

674673
// Rerun each failing test package up to RerunCount times
675-
for i := range r.RerunCount {
674+
for i := range rerunCount {
676675
for pkg, tests := range failingTestsByPackage {
677676
// Build regex pattern to match all failing tests in this package
678677
testPattern := fmt.Sprintf("^(%s)$", strings.Join(tests, "|"))
@@ -710,7 +709,7 @@ func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) ([]reports.T
710709
}
711710

712711
// Parse all rerun results at once with a consistent prefix
713-
rerunResults, err := r.parseTestResults(rerunJsonOutputPaths, "rerun", r.RerunCount)
712+
rerunResults, err := r.parseTestResults(rerunJsonOutputPaths, "rerun", rerunCount)
714713
if err != nil {
715714
return nil, rerunJsonOutputPaths, fmt.Errorf("failed to parse rerun results: %w", err)
716715
}

tools/flakeguard/runner/runner_test.go

Lines changed: 23 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
12+
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/utils"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415
)
@@ -42,15 +43,15 @@ type expectedTestResult struct {
4243
func TestPrettyProjectPath(t *testing.T) {
4344
t.Parallel()
4445

45-
prettyPath, err := prettyProjectPath("./")
46+
prettyPath, err := utils.GetGoProjectName("./")
4647
require.NoError(t, err)
4748
assert.Equal(t, "github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard", prettyPath)
4849
}
4950

5051
func TestRun(t *testing.T) {
5152
var (
5253
zeroRuns = 0
53-
oneRun = 1
54+
oneCount = 1
5455
successPassRate = 1.0
5556
failPassRate = 0.0
5657
)
@@ -184,6 +185,7 @@ func TestRun(t *testing.T) {
184185
Verbose: true,
185186
RunCount: defaultTestRunCount,
186187
GoTestRaceFlag: false,
188+
GoTestCountFlag: &oneCount,
187189
SkipTests: []string{},
188190
SelectTests: []string{"TestFlakyPanic"},
189191
FailFast: false,
@@ -241,11 +243,11 @@ func TestRun(t *testing.T) {
241243
},
242244
expectedTests: map[string]*expectedTestResult{
243245
"TestFail": {
244-
exactRuns: &oneRun,
246+
exactRuns: &oneCount,
245247
allFailures: true,
246248
},
247249
"TestPass": {
248-
exactRuns: &oneRun,
250+
exactRuns: &oneCount,
249251
allSuccesses: true,
250252
},
251253
},
@@ -254,7 +256,7 @@ func TestRun(t *testing.T) {
254256

255257
for _, tc := range testCases {
256258
t.Run(tc.name, func(t *testing.T) {
257-
testReport, err := tc.runner.RunTestPackages([]string{flakyTestPackagePath})
259+
testResults, err := tc.runner.RunTestPackages([]string{flakyTestPackagePath})
258260
require.NoError(t, err)
259261

260262
t.Cleanup(func() {
@@ -267,7 +269,7 @@ func TestRun(t *testing.T) {
267269
}
268270
saniTName := strings.ReplaceAll(t.Name(), "/", "_")
269271
resultsFileName := filepath.Join(debugDir, fmt.Sprintf("test_results_%s.json", saniTName))
270-
jsonResults, err := json.Marshal(testReport)
272+
jsonResults, err := json.Marshal(testResults)
271273
if err != nil {
272274
t.Logf("error marshalling test report: %v", err)
273275
return
@@ -287,16 +289,8 @@ func TestRun(t *testing.T) {
287289
}
288290
})
289291

290-
if tc.runner.FailFast {
291-
require.Equal(t, 1, testReport.SummaryData.TestRunCount, "unexpected number of unique tests run")
292-
} else {
293-
require.Equal(t, tc.runner.RunCount, testReport.SummaryData.TestRunCount, "unexpected number of test runs")
294-
}
295-
296-
require.Equal(t, tc.runner.GoTestRaceFlag, testReport.RaceDetection, "unexpected race usage")
297-
298-
assert.Equal(t, len(tc.expectedTests), len(testReport.Results), "unexpected number of test results")
299-
for _, result := range testReport.Results {
292+
assert.Equal(t, len(tc.expectedTests), len(testResults), "unexpected number of test results")
293+
for _, result := range testResults {
300294
t.Run(fmt.Sprintf("checking results of %s", result.TestName), func(t *testing.T) {
301295
require.NotNil(t, result, "test result was nil")
302296
expected, ok := tc.expectedTests[result.TestName]
@@ -514,15 +508,15 @@ func TestFailedOutputs(t *testing.T) {
514508
CollectRawOutput: true,
515509
}
516510

517-
testReport, err := runner.RunTestPackages([]string{flakyTestPackagePath})
511+
testResults, err := runner.RunTestPackages([]string{flakyTestPackagePath})
518512
require.NoError(t, err, "running tests should not produce an unexpected error")
519513

520-
require.Equal(t, 1, testReport.SummaryData.TotalRuns, "unexpected number of test runs")
514+
require.Equal(t, 1, len(testResults), "unexpected number of test runs")
521515

522516
var testFailResult *reports.TestResult
523-
for i := range testReport.Results {
524-
if testReport.Results[i].TestName == "TestFail" {
525-
testFailResult = &testReport.Results[i]
517+
for i := range testResults {
518+
if testResults[i].TestName == "TestFail" {
519+
testFailResult = &testResults[i]
526520
break
527521
}
528522
}
@@ -537,68 +531,6 @@ func TestFailedOutputs(t *testing.T) {
537531
}
538532
}
539533

540-
func TestRerunFailed(t *testing.T) {
541-
t.Parallel()
542-
543-
// Configure a runner that will rerun failed tests
544-
runner := Runner{
545-
ProjectPath: "./",
546-
Verbose: true,
547-
RunCount: 2, // Run tests twice initially
548-
RerunCount: 3, // Rerun failing tests 3 more times
549-
SelectTests: []string{"TestFail"}, // This test is known to always fail
550-
CollectRawOutput: true,
551-
}
552-
553-
testReport, err := runner.RunTestPackages([]string{flakyTestPackagePath})
554-
require.NoError(t, err, "running tests should not produce an unexpected error")
555-
556-
// Verify we have the expected number of results
557-
require.Equal(t, 1, len(testReport.Results), "expected exactly one test result")
558-
559-
// Find the TestFail result
560-
var testFailResult *reports.TestResult
561-
for i := range testReport.Results {
562-
if testReport.Results[i].TestName == "TestFail" {
563-
testFailResult = &testReport.Results[i]
564-
break
565-
}
566-
}
567-
require.NotNil(t, testFailResult, "expected TestFail result not found in report")
568-
569-
// TestFail should have run 5 times (2 initial + 3 reruns)
570-
require.Equal(t, 5, testFailResult.Runs, "TestFail should have run 5 times (2 initial + 3 reruns)")
571-
572-
// Verify that we have outputs from failed runs
573-
require.NotEmpty(t, testFailResult.FailedOutputs, "expected failed outputs for TestFail")
574-
require.Empty(t, testFailResult.PassedOutputs, "TestFail should have no passed outputs")
575-
576-
// We should have outputs from both run and rerun prefixes
577-
hasRunOutput := false
578-
hasRerunOutput := false
579-
580-
// Check if we have outputs from both original runs and reruns
581-
for runID := range testFailResult.FailedOutputs {
582-
t.Logf("Found failed outputs for run %s", runID)
583-
if strings.HasPrefix(runID, "run") {
584-
hasRunOutput = true
585-
} else if strings.HasPrefix(runID, "rerun") {
586-
hasRerunOutput = true
587-
}
588-
}
589-
590-
require.True(t, hasRunOutput, "expected outputs from initial runs")
591-
require.True(t, hasRerunOutput, "expected outputs from reruns")
592-
593-
// Check that the PassRatio is 0 (since TestFail always fails)
594-
require.Equal(t, 0.0, testFailResult.PassRatio, "TestFail should have a pass ratio of 0.0")
595-
596-
// Verify that the test ran exactly 5 times and all were failures
597-
require.Equal(t, 5, testFailResult.Failures, "TestFail should have failed 5 times")
598-
require.Equal(t, 0, testFailResult.Successes, "TestFail should have passed 0 times")
599-
require.Equal(t, 5, testFailResult.Successes+testFailResult.Failures, "total of successes and failures should equal 5")
600-
}
601-
602534
func TestSkippedTests(t *testing.T) {
603535
t.Parallel()
604536

@@ -610,17 +542,13 @@ func TestSkippedTests(t *testing.T) {
610542
CollectRawOutput: true,
611543
}
612544

613-
testReport, err := runner.RunTestPackages([]string{flakyTestPackagePath})
545+
testResults, err := runner.RunTestPackages([]string{flakyTestPackagePath})
614546
require.NoError(t, err, "running tests should not produce an unexpected error")
615547

616-
require.Equal(t, 0, testReport.SummaryData.TotalRuns, "unexpected number of test runs")
617-
require.Equal(t, 1, len(testReport.Results), "unexpected number of test results")
618-
require.Equal(t, 0, testReport.SummaryData.TestRunCount, "unexpected test run count")
619-
620548
var testSkipResult *reports.TestResult
621-
for i := range testReport.Results {
622-
if testReport.Results[i].TestName == "TestSkipped" {
623-
testSkipResult = &testReport.Results[i]
549+
for i := range testResults {
550+
if testResults[i].TestName == "TestSkipped" {
551+
testSkipResult = &testResults[i]
624552
break
625553
}
626554
}
@@ -645,15 +573,13 @@ func TestOmitOutputsOnSuccess(t *testing.T) {
645573
OmitOutputsOnSuccess: true,
646574
}
647575

648-
testReport, err := runner.RunTestPackages([]string{flakyTestPackagePath})
576+
testResults, err := runner.RunTestPackages([]string{flakyTestPackagePath})
649577
require.NoError(t, err, "running tests should not produce an unexpected error")
650578

651-
require.Equal(t, 1, testReport.SummaryData.TotalRuns, "unexpected number of test runs")
652-
653579
var testPassResult *reports.TestResult
654-
for i := range testReport.Results {
655-
if testReport.Results[i].TestName == "TestPass" {
656-
testPassResult = &testReport.Results[i]
580+
for i := range testResults {
581+
if testResults[i].TestName == "TestPass" {
582+
testPassResult = &testResults[i]
657583
break
658584
}
659585
}

0 commit comments

Comments
 (0)