Skip to content

Commit 35998cd

Browse files
craig[bot]herkolategan
andcommitted
Merge #160007
160007: roachtest: add regex to list operations r=srosenberg a=herkolategan Previously, listing operation(s) would always list all operations. This change allows passing regex to the command to get an idea of what would execute if you were to pass the same regex to the run command. Epic: None Release note: None Co-authored-by: Herko Lategan <[email protected]>
2 parents 6bc52cb + 280559e commit 35998cd

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

pkg/cmd/roachtest/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Check --parallelism, --run-forever and --wait-before-next-execution flags`,
236236
rootCmd.AddCommand(runOperationCmd)
237237

238238
var listOperationCmd = &cobra.Command{
239-
Use: "list-operations",
239+
Use: "list-operations [regex...]",
240240
Short: "list all operation names",
241241
Long: `List all available operations that can be run with the run-operation command.
242242
@@ -245,12 +245,13 @@ This command lists the names of all registered operations.
245245
Example:
246246
247247
roachtest list-operations
248+
roachtest list-operations node-kill/.*m$
248249
`,
249250
RunE: func(cmd *cobra.Command, args []string) error {
250251
r := makeTestRegistry()
251252
operations.RegisterOperations(&r)
252253

253-
ops := r.AllOperations()
254+
ops := r.FilteredOperations(registry.MergeRegEx(args))
254255
for _, op := range ops {
255256
fmt.Printf("%s\n", op.Name)
256257
}
@@ -459,12 +460,7 @@ func opsToRun(r testRegistryImpl, filter string) ([]registry.OperationSpec, erro
459460
if err != nil {
460461
return nil, err
461462
}
462-
var filteredOps []registry.OperationSpec
463-
for _, opSpec := range r.AllOperations() {
464-
if regex.MatchString(opSpec.Name) {
465-
filteredOps = append(filteredOps, opSpec)
466-
}
467-
}
463+
filteredOps := r.FilteredOperations(regex)
468464
if len(filteredOps) == 0 {
469465
return nil, errors.New("no matching operations to run")
470466
}

pkg/cmd/roachtest/registry/filter.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,8 @@ func OnlyBenchmarks() TestFilterOption {
5757
// NewTestFilter initializes a new filter. The strings are interpreted as
5858
// regular expressions (which are joined with |).
5959
func NewTestFilter(regexps []string, options ...TestFilterOption) (*TestFilter, error) {
60-
makeRE := func(strs []string) *regexp.Regexp {
61-
switch len(strs) {
62-
case 0:
63-
return regexp.MustCompile(`.`)
64-
case 1:
65-
return regexp.MustCompile(strs[0])
66-
default:
67-
for i := range strs {
68-
strs[i] = "(" + strs[i] + ")"
69-
}
70-
return regexp.MustCompile(strings.Join(strs, "|"))
71-
}
72-
}
73-
7460
tf := &TestFilter{
75-
Name: makeRE(regexps),
61+
Name: MergeRegEx(regexps),
7662
}
7763
for _, o := range options {
7864
o(tf)
@@ -89,6 +75,21 @@ func NewTestFilter(regexps []string, options ...TestFilterOption) (*TestFilter,
8975
return tf, nil
9076
}
9177

78+
// MergeRegEx merges the given strings into a single regexp.
79+
func MergeRegEx(strs []string) *regexp.Regexp {
80+
switch len(strs) {
81+
case 0:
82+
return regexp.MustCompile(`.`)
83+
case 1:
84+
return regexp.MustCompile(strs[0])
85+
default:
86+
for i := range strs {
87+
strs[i] = "(" + strs[i] + ")"
88+
}
89+
return regexp.MustCompile(strings.Join(strs, "|"))
90+
}
91+
}
92+
9293
// MatchFailReason describes the reason(s) a filter did not match the test.
9394
type MatchFailReason struct {
9495
// If true, the filter requires a benchmark and the test is not one.

pkg/cmd/roachtest/test_registry.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,14 @@ func (r testRegistryImpl) AllOperations() []registry.OperationSpec {
180180
})
181181
return ops
182182
}
183+
184+
// FilteredOperations returns operations filtered by the given regex.
185+
func (r testRegistryImpl) FilteredOperations(regex *regexp.Regexp) []registry.OperationSpec {
186+
var filteredOps []registry.OperationSpec
187+
for _, opSpec := range r.AllOperations() {
188+
if regex.MatchString(opSpec.Name) {
189+
filteredOps = append(filteredOps, opSpec)
190+
}
191+
}
192+
return filteredOps
193+
}

0 commit comments

Comments
 (0)