diff --git a/pkg/cmd/roachtest/main.go b/pkg/cmd/roachtest/main.go index b4b6ec563ffb..e0a666e3034f 100644 --- a/pkg/cmd/roachtest/main.go +++ b/pkg/cmd/roachtest/main.go @@ -236,7 +236,7 @@ Check --parallelism, --run-forever and --wait-before-next-execution flags`, rootCmd.AddCommand(runOperationCmd) var listOperationCmd = &cobra.Command{ - Use: "list-operations", + Use: "list-operations [regex...]", Short: "list all operation names", Long: `List all available operations that can be run with the run-operation command. @@ -245,12 +245,13 @@ This command lists the names of all registered operations. Example: roachtest list-operations + roachtest list-operations node-kill/.*m$ `, RunE: func(cmd *cobra.Command, args []string) error { r := makeTestRegistry() operations.RegisterOperations(&r) - ops := r.AllOperations() + ops := r.FilteredOperations(registry.MergeRegEx(args)) for _, op := range ops { fmt.Printf("%s\n", op.Name) } @@ -459,12 +460,7 @@ func opsToRun(r testRegistryImpl, filter string) ([]registry.OperationSpec, erro if err != nil { return nil, err } - var filteredOps []registry.OperationSpec - for _, opSpec := range r.AllOperations() { - if regex.MatchString(opSpec.Name) { - filteredOps = append(filteredOps, opSpec) - } - } + filteredOps := r.FilteredOperations(regex) if len(filteredOps) == 0 { return nil, errors.New("no matching operations to run") } diff --git a/pkg/cmd/roachtest/registry/filter.go b/pkg/cmd/roachtest/registry/filter.go index b0f94a86e6b7..87a9371cb3d9 100644 --- a/pkg/cmd/roachtest/registry/filter.go +++ b/pkg/cmd/roachtest/registry/filter.go @@ -57,22 +57,8 @@ func OnlyBenchmarks() TestFilterOption { // NewTestFilter initializes a new filter. The strings are interpreted as // regular expressions (which are joined with |). func NewTestFilter(regexps []string, options ...TestFilterOption) (*TestFilter, error) { - makeRE := func(strs []string) *regexp.Regexp { - switch len(strs) { - case 0: - return regexp.MustCompile(`.`) - case 1: - return regexp.MustCompile(strs[0]) - default: - for i := range strs { - strs[i] = "(" + strs[i] + ")" - } - return regexp.MustCompile(strings.Join(strs, "|")) - } - } - tf := &TestFilter{ - Name: makeRE(regexps), + Name: MergeRegEx(regexps), } for _, o := range options { o(tf) @@ -89,6 +75,21 @@ func NewTestFilter(regexps []string, options ...TestFilterOption) (*TestFilter, return tf, nil } +// MergeRegEx merges the given strings into a single regexp. +func MergeRegEx(strs []string) *regexp.Regexp { + switch len(strs) { + case 0: + return regexp.MustCompile(`.`) + case 1: + return regexp.MustCompile(strs[0]) + default: + for i := range strs { + strs[i] = "(" + strs[i] + ")" + } + return regexp.MustCompile(strings.Join(strs, "|")) + } +} + // MatchFailReason describes the reason(s) a filter did not match the test. type MatchFailReason struct { // If true, the filter requires a benchmark and the test is not one. diff --git a/pkg/cmd/roachtest/test_registry.go b/pkg/cmd/roachtest/test_registry.go index 070831010366..da951b2d8e6b 100644 --- a/pkg/cmd/roachtest/test_registry.go +++ b/pkg/cmd/roachtest/test_registry.go @@ -180,3 +180,14 @@ func (r testRegistryImpl) AllOperations() []registry.OperationSpec { }) return ops } + +// FilteredOperations returns operations filtered by the given regex. +func (r testRegistryImpl) FilteredOperations(regex *regexp.Regexp) []registry.OperationSpec { + var filteredOps []registry.OperationSpec + for _, opSpec := range r.AllOperations() { + if regex.MatchString(opSpec.Name) { + filteredOps = append(filteredOps, opSpec) + } + } + return filteredOps +}