Skip to content

Commit ddfad6b

Browse files
craig[bot]herkolategan
andcommitted
Merge #148894
148894: roachtest: ensure all skipped tests are logged r=srosenberg,golgeek,DarrylWong a=herkolategan Previously, there was a bug where the last tests after the selected indexes were not being logged as skipped. This change adds the length of the specs to the list of selected indexes to ensure that the loop will skip all the tests between the selected indexes, including the tail end of the tests. Epic: None Release note: None Co-authored-by: Herko Lategan <[email protected]>
2 parents a148cb0 + 0e0375c commit ddfad6b

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

pkg/cmd/roachtest/main.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ package main
88
import (
99
"context"
1010
"fmt"
11+
"io"
1112
"math"
1213
"math/rand"
1314
"os"
1415
"os/user"
1516
"regexp"
16-
"sort"
1717
"strings"
1818
"time"
1919

@@ -25,6 +25,7 @@ import (
2525
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/testselector"
2626
"github.com/cockroachdb/cockroach/pkg/roachprod"
2727
"github.com/cockroachdb/cockroach/pkg/roachprod/config"
28+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
2829
"github.com/cockroachdb/errors"
2930
_ "github.com/lib/pq" // register postgres driver
3031
"github.com/spf13/cobra"
@@ -315,7 +316,12 @@ func testsToRun(
315316
}
316317
}
317318

318-
return selectSpecs(notSkipped, selectProbability, true, print), nil
319+
var stdout io.Writer
320+
if print {
321+
stdout = os.Stdout
322+
}
323+
rng, _ := randutil.NewPseudoRand()
324+
return selectSpecs(notSkipped, rng, selectProbability, true, stdout), nil
319325
}
320326

321327
// updateSpecForSelectiveTests is responsible for updating the test spec skip and skip details
@@ -436,24 +442,28 @@ func opsToRun(r testRegistryImpl, filter string) ([]registry.OperationSpec, erro
436442
// testRegistryImpl.AllTests().
437443
// TODO(smg260): Perhaps expose `atLeastOnePerPrefix` via CLI
438444
func selectSpecs(
439-
specs []registry.TestSpec, samplePct float64, atLeastOnePerPrefix bool, print bool,
445+
specs []registry.TestSpec,
446+
rng *rand.Rand,
447+
samplePct float64,
448+
atLeastOnePerPrefix bool,
449+
stdout io.Writer,
440450
) []registry.TestSpec {
441451
if samplePct == 1 || len(specs) == 0 {
442452
return specs
443453
}
444454

445455
var sampled []registry.TestSpec
446-
var selectedIdxs []int
456+
selectedIndexes := make(map[int]struct{})
447457

448458
prefix := strings.Split(specs[0].Name, "/")[0]
449459
prefixSelected := false
450460
prefixIdx := 0
451461

452462
// Selects one random spec from the range [start, end) and appends it to sampled.
453463
collectRandomSpecFromRange := func(start, end int) {
454-
i := start + rand.Intn(end-start)
464+
i := start + rng.Intn(end-start)
455465
sampled = append(sampled, specs[i])
456-
selectedIdxs = append(selectedIdxs, i)
466+
selectedIndexes[i] = struct{}{}
457467
}
458468
for i, s := range specs {
459469
if atLeastOnePerPrefix {
@@ -469,9 +479,9 @@ func selectSpecs(
469479
}
470480
}
471481

472-
if rand.Float64() < samplePct {
482+
if rng.Float64() < samplePct {
473483
sampled = append(sampled, s)
474-
selectedIdxs = append(selectedIdxs, i)
484+
selectedIndexes[i] = struct{}{}
475485
prefixSelected = true
476486
continue
477487
}
@@ -482,27 +492,18 @@ func selectSpecs(
482492
}
483493
}
484494

485-
p := 0
486-
// The list would already be sorted were it not for the lookback to
487-
// ensure at least one test per prefix.
488-
if atLeastOnePerPrefix {
489-
sort.Ints(selectedIdxs)
490-
}
491-
// This loop depends on an ordered list as we are essentially
492-
// skipping all values in between the selected indexes.
493-
for _, i := range selectedIdxs {
494-
for j := p; j < i; j++ {
495-
s := specs[j]
496-
if print && roachtestflags.TeamCity {
497-
fmt.Fprintf(os.Stdout, "##teamcity[testIgnored name='%s' message='excluded via sampling']\n",
495+
// Print a skip message for all tests that are not selected.
496+
for i, s := range specs {
497+
if _, ok := selectedIndexes[i]; !ok {
498+
if stdout != nil && roachtestflags.TeamCity {
499+
fmt.Fprintf(stdout, "##teamcity[testIgnored name='%s' message='excluded via sampling']\n",
498500
s.Name)
499501
}
500502

501-
if print {
502-
fmt.Fprintf(os.Stdout, "--- SKIP: %s (%s)\n\texcluded via sampling\n", s.Name, "0.00s")
503+
if stdout != nil {
504+
fmt.Fprintf(stdout, "--- SKIP: %s (%s)\n\texcluded via sampling\n", s.Name, "0.00s")
503505
}
504506
}
505-
p = i + 1
506507
}
507508

508509
return sampled

pkg/cmd/roachtest/main_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package main
77

88
import (
9+
"bytes"
910
"context"
1011
gosql "database/sql"
1112
"fmt"
@@ -483,3 +484,27 @@ func shuffleStrings(r *rand.Rand, strings []string) []string {
483484
})
484485
return strings
485486
}
487+
488+
func TestSkippedSelectSpecs(t *testing.T) {
489+
specs := []registry.TestSpec{}
490+
for i := range 5 {
491+
specs = append(specs, registry.TestSpec{Name: fmt.Sprintf("t%d", i+1)})
492+
}
493+
494+
rng := randutil.NewTestRandWithSeed(0)
495+
buf := bytes.NewBuffer(nil)
496+
_ = selectSpecs(specs, rng, 0.1, false, buf)
497+
498+
containsTestSkip := func(testName string) bool {
499+
return strings.Contains(buf.String(), fmt.Sprintf("--- SKIP: %s", testName))
500+
}
501+
502+
// With the fixed random seed above, these are the expected skipped tests.
503+
expectedSkippedTests := []string{"t1", "t2", "t3", "t5"}
504+
for _, testName := range expectedSkippedTests {
505+
assert.True(t, containsTestSkip(testName), "Expected skip message for %s not found in buffer", testName)
506+
}
507+
// With the fixed random seed above, the test t4 should not be skipped.
508+
testName := "t4"
509+
assert.False(t, containsTestSkip(testName), "Expected no skip message for %s but found in buffer", testName)
510+
}

0 commit comments

Comments
 (0)