Skip to content

Commit

Permalink
kola run: Accept a list of glob patterns
Browse files Browse the repository at this point in the history
Glob patterns are matching like the shell expands a glob pattern.
This does not allow to test against two names that don't share a
substring. On the shell this is solved by having two glob arguments.
Allow multiple glob patterns to be specified as arguments to handle
the cases where the tests don't share a substring but we want to run
exactly those tests.
  • Loading branch information
pothos committed Mar 27, 2020
1 parent 04566af commit 84d8999
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
24 changes: 13 additions & 11 deletions cmd/kola/kola.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
}

cmdRun = &cobra.Command{
Use: "run [glob pattern]",
Use: "run [glob pattern...]",
Short: "Run kola tests by category",
Long: `Run all kola tests (default) or related groups.
Expand All @@ -59,7 +59,7 @@ will be ignored.
}

cmdList = &cobra.Command{
Use: "list [glob pattern, only for --filter, defaults to '*']",
Use: "list [glob pattern..., only for --filter, defaults to '*']",
Short: "List kola test names",
Run: runList,
}
Expand Down Expand Up @@ -110,11 +110,11 @@ func runRun(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Extra arguments specified. Usage: 'kola run [glob pattern]'\n")
os.Exit(2)
}
var pattern string
if len(args) == 1 {
pattern = args[0]
var patterns []string
if len(args) >= 1 {
patterns = args
} else {
pattern = "*" // run all tests by default
patterns = []string{"*"} // run all tests by default
}

var err error
Expand All @@ -134,7 +134,7 @@ func runRun(cmd *cobra.Command, args []string) {
} else {
sshKeys = nil
}
runErr := kola.RunTests(pattern, kolaPlatform, outputDir, &sshKeys, runRemove)
runErr := kola.RunTests(patterns, kolaPlatform, outputDir, &sshKeys, runRemove)

// needs to be after RunTests() because harness empties the directory
if err := writeProps(); err != nil {
Expand Down Expand Up @@ -275,12 +275,14 @@ func runList(cmd *cobra.Command, args []string) {
tests := register.Tests

if listFilter {
pattern := "*"
if len(args) == 1 {
pattern = args[0]
var patterns []string
if len(args) >= 1 {
patterns = args
} else {
patterns = []string{"*"} // run all tests by default
}
var err error
tests, err = kola.FilterTests(register.Tests, pattern, kolaPlatform, semver.Version{})
tests, err = kola.FilterTests(register.Tests, patterns, kolaPlatform, semver.Version{})
if err != nil {
fmt.Fprintf(os.Stderr, "filtering error: %v\n", err)
os.Exit(1)
Expand Down
40 changes: 28 additions & 12 deletions kola/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func NewFlight(pltfrm string) (flight platform.Flight, err error) {
return
}

func FilterTests(tests map[string]*register.Test, pattern, pltfrm string, version semver.Version) (map[string]*register.Test, error) {
func FilterTests(tests map[string]*register.Test, patterns []string, pltfrm string, version semver.Version) (map[string]*register.Test, error) {
r := make(map[string]*register.Test)

checkPlatforms := []string{pltfrm}
Expand All @@ -199,16 +199,25 @@ func FilterTests(tests map[string]*register.Test, pattern, pltfrm string, versio
}

for name, t := range tests {
match, err := filepath.Match(pattern, t.Name)
if err != nil {
return nil, err
for _, pattern := range patterns {
match, err := filepath.Match(pattern, t.Name)
if err != nil {
return nil, err
}
if !match {
continue
}
}
if !match {
continue
patternNotName := true
for _, pattern := range patterns {
if t.Name == pattern {
patternNotName = false
break
}
}

// Check the test's min and end versions when running more than one test
if t.Name != pattern && versionOutsideRange(version, t.MinVersion, t.EndVersion) {
if patternNotName && versionOutsideRange(version, t.MinVersion, t.EndVersion) {
continue
}

Expand Down Expand Up @@ -290,12 +299,12 @@ func versionOutsideRange(version, minVersion, endVersion semver.Version) bool {
}

// RunTests is a harness for running multiple tests in parallel. Filters
// tests based on a glob pattern and by platform. Has access to all
// tests based on glob patterns and by platform. Has access to all
// tests either registered in this package or by imported packages that
// register tests in their init() function.
// outputDir is where various test logs and data will be written for
// analysis after the test run. If it already exists it will be erased!
func RunTests(pattern, pltfrm, outputDir string, sshKeys *[]agent.Key, remove bool) error {
func RunTests(patterns []string, pltfrm, outputDir string, sshKeys *[]agent.Key, remove bool) error {
var versionStr string

// Avoid incurring cost of starting machine in getClusterSemver when
Expand All @@ -304,14 +313,21 @@ func RunTests(pattern, pltfrm, outputDir string, sshKeys *[]agent.Key, remove bo
// 2) glob is an exact match which means minVersion will be ignored
// either way
// 3) the provided torcx flag is wrong
tests, err := FilterTests(register.Tests, pattern, pltfrm, semver.Version{})
tests, err := FilterTests(register.Tests, patterns, pltfrm, semver.Version{})
if err != nil {
plog.Fatal(err)
}

skipGetVersion := true
for name, t := range tests {
if name != pattern && (t.MinVersion != semver.Version{} || t.EndVersion != semver.Version{}) {
patternNotName := true
for _, pattern := range patterns {
if name == pattern {
patternNotName = false
break
}
}
if patternNotName && (t.MinVersion != semver.Version{} || t.EndVersion != semver.Version{}) {
skipGetVersion = false
break
}
Expand Down Expand Up @@ -348,7 +364,7 @@ func RunTests(pattern, pltfrm, outputDir string, sshKeys *[]agent.Key, remove bo
versionStr = version.String()

// one more filter pass now that we know real version
tests, err = FilterTests(tests, pattern, pltfrm, *version)
tests, err = FilterTests(tests, patterns, pltfrm, *version)
if err != nil {
plog.Fatal(err)
}
Expand Down

0 comments on commit 84d8999

Please sign in to comment.