Skip to content

Commit f619786

Browse files
committed
ci-stress: prevent stressing integration tests like lint_test
These should not be stressed even if the code is updated. They cannot be `bazel test`'ed like normal tests. We use similar queries elsewhere to filter out `integration` tests (for example, `build/teamcity/cockroach/ci/tests-ibm-cloud-linux-s390x/unit_tests_impl.sh`). Release justification: Non-production code changes Release note: none Epic: none
1 parent 97f748a commit f619786

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

pkg/cmd/ci-stress/main.go

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,75 @@ func getPkgToTests(diff string) map[string][]string {
117117
return ret
118118
}
119119

120-
func runTests(ctx context.Context, pkgToTests map[string][]string, extraBazelArgs []string) error {
121-
var testPackages []string
120+
// selectAppropriateTests constructs bazel test targets from pkgToTests,
121+
// then uses `bazel query` to exclude any targets tagged with
122+
// "integration". Returns the surviving test targets and a sorted,
123+
// deduplicated list of test names to stress.
124+
func selectAppropriateTests(
125+
ctx context.Context, pkgToTests map[string][]string,
126+
) (testTargets []string, tests []string, err error) {
127+
var candidates []string
122128
for pkg := range pkgToTests {
123-
testPackages = append(testPackages, fmt.Sprintf("//%s:%s_test", pkg, filepath.Base(pkg)))
129+
targetName := strings.ReplaceAll(filepath.Base(pkg), ".", "_")
130+
candidates = append(candidates, fmt.Sprintf("//%s:%s_test", pkg, targetName))
131+
}
132+
if len(candidates) == 0 {
133+
return nil, nil, nil
134+
}
135+
targetSet := strings.Join(candidates, " ")
136+
query := fmt.Sprintf(
137+
"set(%s) except attr(\"tags\", \"[\\[ ]integration[,\\]]\", set(%s))",
138+
targetSet, targetSet,
139+
)
140+
cmd := exec.CommandContext(ctx, "bazel", "query", query)
141+
outputBytes, err := cmd.Output()
142+
if err != nil {
143+
var exitErr *exec.ExitError
144+
if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {
145+
return nil, nil, fmt.Errorf("unable to filter integration tests: bazel query: %w\nstderr: %s", err, exitErr.Stderr)
146+
}
147+
return nil, nil, fmt.Errorf("unable to filter integration tests: bazel query: %w", err)
148+
}
149+
output := strings.TrimSpace(string(outputBytes))
150+
if output == "" {
151+
return nil, nil, nil
152+
}
153+
testTargets = strings.Split(output, "\n")
154+
155+
// Collect tests only from packages that survived filtering.
156+
survivingPkgs := make(map[string]bool, len(testTargets))
157+
for _, target := range testTargets {
158+
pkg := strings.TrimPrefix(target[:strings.IndexByte(target, ':')], "//")
159+
survivingPkgs[pkg] = true
124160
}
125161
allTests := make(map[string]struct{})
126-
for _, tests := range pkgToTests {
127-
for _, test := range tests {
162+
for pkg, pkgTests := range pkgToTests {
163+
if !survivingPkgs[pkg] {
164+
continue
165+
}
166+
for _, test := range pkgTests {
128167
allTests[test] = struct{}{}
129168
}
130169
}
131-
allTestsSlice := make([]string, 0, len(allTests))
170+
tests = make([]string, 0, len(allTests))
132171
for test := range allTests {
133-
allTestsSlice = append(allTestsSlice, test)
172+
tests = append(tests, test)
134173
}
135-
slices.Sort(allTestsSlice)
136-
testFilter := strings.Join(allTestsSlice, "|")
137-
testFilter = "^(" + testFilter + ")$"
174+
slices.Sort(tests)
175+
return testTargets, tests, nil
176+
}
177+
178+
func runTests(ctx context.Context, pkgToTests map[string][]string, extraBazelArgs []string) error {
179+
testTargets, tests, err := selectAppropriateTests(ctx, pkgToTests)
180+
if err != nil {
181+
return err
182+
}
183+
if len(testTargets) == 0 {
184+
fmt.Println("no non-integration test targets to stress, exiting")
185+
return nil
186+
}
187+
188+
testFilter := "^(" + strings.Join(tests, "|") + ")$"
138189
runsPerTest := 25
139190
// Run each test multiple times. Calculate the number of times based on
140191
// whether this is --race or not.
@@ -145,7 +196,7 @@ func runTests(ctx context.Context, pkgToTests map[string][]string, extraBazelArg
145196
}
146197
}
147198
bazelArgs := []string{"test", "--test_filter", testFilter, "--runs_per_test", strconv.Itoa(runsPerTest)}
148-
bazelArgs = append(bazelArgs, testPackages...)
199+
bazelArgs = append(bazelArgs, testTargets...)
149200
bazelArgs = append(bazelArgs, extraBazelArgs...)
150201
fmt.Printf("running `bazel` with args %+v\n", bazelArgs)
151202
cmd := exec.CommandContext(ctx, "bazel", bazelArgs...)

0 commit comments

Comments
 (0)