-
Notifications
You must be signed in to change notification settings - Fork 3
/
command.go
167 lines (142 loc) · 4.13 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package barrier
import (
"context"
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// NewCommand generates a new CLI
func NewCommand(
name string,
description string,
version string,
stash GenerateStash,
) *cobra.Command {
cobra.OnInitialize(func() {
viper.SetEnvPrefix(name)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
})
var rootCmd = &cobra.Command{
Use: name,
Short: description,
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version and exit.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
var cmdListTests = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List registered tests.",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
suites := filterSuites()
return listTests(suites)
},
}
cmdListTests.Flags().StringSliceP("id", "i", nil, "Only run tests with the given identifier")
cmdListTests.Flags().StringSliceP("tag", "t", nil, "Only run tests with the given tags")
var cmdRunTests = &cobra.Command{
Use: "test",
Aliases: []string{"run"},
Short: "Run the registered tests",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: add argument check.
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("limit"))
defer cancel()
var s interface{}
if stash != nil {
s = stash(ctx)
}
suites := filterSuites()
for _, suite := range suites {
// Store caller stash per suite
suite.stash = s
err := newTestRunner(
viper.GetString("build-id"),
suite,
viper.GetDuration("limit"),
viper.GetInt("concurrent"),
viper.GetInt("stress"),
viper.GetBool("verbose"),
viper.GetBool("skip-teardown"),
viper.GetBool("stop-on-failure"),
).run(ctx, suite)
if err != nil {
return err
}
}
return nil
},
}
// Parameters to configure suite behaviors
cmdRunTests.Flags().StringSliceP("suite", "Z", nil, "Only run suites specified")
// Parameters to configure test behaviors
cmdRunTests.Flags().BoolP("verbose", "V", false, "Show logs even on success")
cmdRunTests.Flags().DurationP("limit", "l", 20*time.Minute, "Execution time limit")
cmdRunTests.Flags().IntP("concurrent", "c", 20, "Max number of concurrent tests")
cmdRunTests.Flags().IntP("stress", "s", 1, "Number of times to run each test in parallel")
cmdRunTests.Flags().StringSliceP("id", "i", nil, "Only run tests with the given identifier")
cmdRunTests.Flags().StringSliceP("tag", "t", nil, "Only run tests with the given tags")
cmdRunTests.Flags().BoolP("match-all", "M", false, "Match all tags specified")
cmdRunTests.Flags().BoolP("skip-teardown", "S", false, "Skip teardown step")
cmdRunTests.Flags().BoolP("stop-on-failure", "X", false, "Stop on the first failed test")
rootCmd.AddCommand(
versionCmd,
cmdListTests,
cmdRunTests,
)
return rootCmd
}
// runSuite returns true if we should consider the suite for running
func runSuite(s *suiteInfo, names []string) bool {
if len(names) == 0 {
return true
}
for _, name := range names {
if name == s.Name {
return true
}
}
return false
}
// filterSuites filters the suite based on ids and/or tags
func filterSuites() []*suiteInfo {
s := []*suiteInfo{}
names := viper.GetStringSlice("suite")
for _, suite := range mainSuites.sorted() {
// Filter Suites
if !runSuite(suite, names) {
continue
}
// Filter Tests in a suite
ids := viper.GetStringSlice("id")
if len(ids) > 0 {
suite = suite.testsWithIDs(viper.GetBool("verbose"), ids)
} else {
tags := viper.GetStringSlice("tag")
if len(tags) > 0 {
suite = suite.testsWithArgs(viper.GetBool("verbose"), viper.GetBool("match-all"), tags)
}
}
if len(suite.tests) > 0 {
s = append(s, suite)
}
}
return s
}