Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kola run: Accept a list of glob patterns, kola list: Platform/distro filters with glob patterns #73

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions cmd/kola/kola.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"golang.org/x/crypto/ssh/agent"

"github.com/coreos/go-semver/semver"
"github.com/coreos/pkg/capnslog"
"github.com/spf13/cobra"

Expand All @@ -45,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 @@ -58,12 +59,13 @@ will be ignored.
}

cmdList = &cobra.Command{
Use: "list",
Use: "list [glob pattern..., only for --filter, defaults to '*']",
Short: "List kola test names",
Run: runList,
}

listJSON bool
listJSON bool
listFilter bool

runRemove bool
runSetSSHKeys bool
Expand All @@ -75,6 +77,7 @@ func init() {
root.AddCommand(cmdList)

cmdList.Flags().BoolVar(&listJSON, "json", false, "format output in JSON")
cmdList.Flags().BoolVar(&listFilter, "filter", false, "Filter by --platform and --distro, required for glob patterns, uses '*' as pattern if no pattern is specified")

cmdRun.Flags().BoolVarP(&runRemove, "remove", "r", true, "remove instances after test exits (--remove=false will keep them)")
cmdRun.Flags().BoolVarP(&runSetSSHKeys, "keys", "k", false, "add SSH keys from --key options")
Expand Down Expand Up @@ -107,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 @@ -131,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 @@ -269,8 +272,26 @@ func writeProps() error {
}

func runList(cmd *cobra.Command, args []string) {
tests := register.Tests

if listFilter {
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, patterns, kolaPlatform, semver.Version{})
if err != nil {
fmt.Fprintf(os.Stderr, "filtering error: %v\n", err)
os.Exit(1)
}
}

var testlist []*item
for name, test := range register.Tests {

for name, test := range tests {
item := &item{
name,
test.Platforms,
Expand Down
43 changes: 32 additions & 11 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,30 @@ 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
noMatch := true
for _, pattern := range patterns {
match, err := filepath.Match(pattern, t.Name)
if err != nil {
return nil, err
}
if match {
noMatch = false
break
}
}
if !match {
if noMatch {
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 +304,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 +318,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 +369,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