Skip to content

Commit d66a92d

Browse files
committed
roachtest: ensure all skipped tests are logged
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
1 parent 39d519b commit d66a92d

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-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

0 commit comments

Comments
 (0)