| 1 | package check |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/pingcap/log" |
| 13 | "go.uber.org/zap" |
| 14 | ) |
| 15 | |
| 16 | // ----------------------------------------------------------------------- |
| 17 | // Test suite registry. |
| 18 | |
| 19 | var ( |
| 20 | allParallelSuites []interface{} |
| 21 | allSerialSuites []interface{} |
| 22 | ) |
| 23 | |
| 24 | // Suite registers the given value as a test suite to be run. Any methods |
| 25 | // starting with the Test prefix in the given value will be considered as |
| 26 | // a test method. |
| 27 | func Suite(suite interface{}) interface{} { |
| 28 | allParallelSuites = append(allParallelSuites, suite) |
| 29 | return suite |
| 30 | } |
| 31 | |
| 32 | // SerialSuites registers the given value as a test suite to be run serially. Any methods |
| 33 | // starting with the Test prefix in the given value will be considered as |
| 34 | // a test method. |
| 35 | func SerialSuites(suite interface{}) interface{} { |
| 36 | allSerialSuites = append(allSerialSuites, suite) |
| 37 | return suite |
| 38 | } |
| 39 | |
| 40 | // ----------------------------------------------------------------------- |
| 41 | // Public running interface. |
| 42 | |
| 43 | var ( |
| 44 | oldFilterFlag = flag.String("gocheck.f", "", "Regular expression selecting which tests and/or suites to run") |
| 45 | oldVerboseFlag = flag.Bool("gocheck.v", false, "Verbose mode") |
| 46 | oldStreamFlag = flag.Bool("gocheck.vv", false, "Super verbose mode (disables output caching)") |
| 47 | oldBenchFlag = flag.Bool("gocheck.b", false, "Run benchmarks") |
| 48 | oldBenchTime = flag.Duration("gocheck.btime", 1*time.Second, "approximate run time for each benchmark") |
| 49 | oldListFlag = flag.Bool("gocheck.list", false, "List the names of all tests that will be run") |
| 50 | oldWorkFlag = flag.Bool("gocheck.work", false, "Display and do not remove the test working directory") |
| 51 | |
| 52 | newFilterFlag = flag.String("check.f", "", "Regular expression selecting which tests and/or suites to run") |
| 53 | newVerboseFlag = flag.Bool("check.v", false, "Verbose mode") |
| 54 | newStreamFlag = flag.Bool("check.vv", false, "Super verbose mode (disables output caching)") |
| 55 | newBenchFlag = flag.Bool("check.b", false, "Run benchmarks") |
| 56 | newBenchTime = flag.Duration("check.btime", 1*time.Second, "approximate run time for each benchmark") |
| 57 | methodTimeout = flag.Duration("check.timeout", 0, "timeout value for each function") |
| 58 | newBenchMem = flag.Bool("check.bmem", false, "Report memory benchmarks") |
| 59 | newListFlag = flag.Bool("check.list", false, "List the names of all tests that will be run") |
| 60 | newWorkFlag = flag.Bool("check.work", false, "Display and do not remove the test working directory") |
| 61 | newExcludeFlag = flag.String("check.exclude", "", "Regular expression to exclude tests to run") |
| 62 | |
| 63 | CustomParallelSuiteFlag = flag.Bool("check.p", false, "Run suites in parallel") |
| 64 | ) |
| 65 | |
| 66 | var CustomVerboseFlag bool |
| 67 | |
| 68 | // TestingT runs all test suites registered with the Suite function, |
| 69 | // printing results to stdout, and reporting any failures back to |
| 70 | // the "testing" package. |
| 71 | func TestingT(testingT *testing.T) { |
| 72 | l := zap.NewAtomicLevel() |
| 73 | if l.UnmarshalText([]byte(os.Getenv("log_level"))) == nil { |
| 74 | log.SetLevel(l.Level()) |
| 75 | } |
| 76 | benchTime := *newBenchTime |
| 77 | if benchTime == 1*time.Second { |
| 78 | benchTime = *oldBenchTime |
| 79 | } |
| 80 | conf := &RunConf{ |
| 81 | Filter: *oldFilterFlag + *newFilterFlag, |
| 82 | Verbose: *oldVerboseFlag || *newVerboseFlag || CustomVerboseFlag, |
| 83 | Stream: *oldStreamFlag || *newStreamFlag, |
| 84 | Benchmark: *oldBenchFlag || *newBenchFlag, |
| 85 | BenchmarkTime: benchTime, |
| 86 | MethodTimeout: *methodTimeout, |
| 87 | BenchmarkMem: *newBenchMem, |
| 88 | KeepWorkDir: *oldWorkFlag || *newWorkFlag, |
| 89 | Exclude: *newExcludeFlag, |
| 90 | } |
| 91 | if *oldListFlag || *newListFlag { |
| 92 | w := bufio.NewWriter(os.Stdout) |
| 93 | for _, name := range ListAll(conf) { |
| 94 | fmt.Fprintln(w, name) |
| 95 | } |
| 96 | w.Flush() |
| 97 | return |
| 98 | } |
| 99 | result := RunAll(conf) |
| 100 | println(result.String()) |
| 101 | if !result.Passed() { |
| 102 | testingT.Fail() |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // RunAll runs all test suites registered with the Suite function, using the |
| 107 | // provided run configuration. |
| 108 | func RunAll(runConf *RunConf) *Result { |
| 109 | result := Result{} |
| 110 | if !*CustomParallelSuiteFlag { |
| 111 | // run all suites serially. |
| 112 | for _, suite := range allParallelSuites { |
| 113 | result.Add(Run(suite, runConf)) |
| 114 | } |
| 115 | |
| 116 | for _, suite := range allSerialSuites { |
| 117 | result.Add(Run(suite, runConf)) |
| 118 | } |
| 119 | return &result |
| 120 | } |
| 121 | |
| 122 | wg := sync.WaitGroup{} |
| 123 | notifyRunningSuitesCh := make(chan struct{}) |
| 124 | suiteRunners := make([]*suiteRunner, 0, len(allParallelSuites)) |
| 125 | for _, suite := range allParallelSuites { |
| 126 | suiteRunners = append(suiteRunners, parallelRun(suite, runConf, &wg, notifyRunningSuitesCh)) |
| 127 | } |
| 128 | close(notifyRunningSuitesCh) |
| 129 | wg.Wait() |
| 130 | for _, runner := range suiteRunners { |
| 131 | ret := &runner.tracker.result |
| 132 | result.Add(ret) |
| 133 | } |
| 134 | |
| 135 | for _, suite := range allSerialSuites { |
| 136 | result.Add(Run(suite, runConf)) |
| 137 | } |
| 138 | return &result |
| 139 | } |
| 140 | |
| 141 | func parallelRun(suite interface{}, runConf *RunConf, wg *sync.WaitGroup, notifyRunningSuitesCh chan struct{}) *suiteRunner { |
| 142 | runner := newSuiteRunner(suite, runConf) |
| 143 | runner.asyncRun(wg, notifyRunningSuitesCh) |
| 144 | return runner |
| 145 | } |
| 146 | |
| 147 | // Run runs the provided test suite using the provided run configuration. |
| 148 | func Run(suite interface{}, runConf *RunConf) *Result { |
| 149 | runner := newSuiteRunner(suite, runConf) |
| 150 | return runner.run() |
| 151 | } |
| 152 | |
| 153 | // ListAll returns the names of all the test functions registered with the |
| 154 | // Suite function that will be run with the provided run configuration. |
| 155 | func ListAll(runConf *RunConf) []string { |
| 156 | var names []string |
| 157 | for _, suite := range allParallelSuites { |
| 158 | names = append(names, List(suite, runConf)...) |
| 159 | } |
| 160 | |
| 161 | for _, suite := range allSerialSuites { |
| 162 | names = append(names, List(suite, runConf)...) |
| 163 | } |
| 164 | return names |
| 165 | } |
| 166 | |
| 167 | // List returns the names of the test functions in the given |
| 168 | // suite that will be run with the provided run configuration. |
| 169 | func List(suite interface{}, runConf *RunConf) []string { |
| 170 | var names []string |
| 171 | runner := newSuiteRunner(suite, runConf) |
| 172 | for _, t := range runner.tests { |
| 173 | names = append(names, t.String()) |
| 174 | } |
| 175 | return names |
| 176 | } |
| 177 | |
| 178 | // ----------------------------------------------------------------------- |
| 179 | // Result methods. |
| 180 | |
| 181 | func (r *Result) Add(other *Result) { |
| 182 | r.Succeeded += other.Succeeded |
| 183 | r.Skipped += other.Skipped |
| 184 | r.Failed += other.Failed |
| 185 | r.Panicked += other.Panicked |
| 186 | r.FixturePanicked += other.FixturePanicked |
| 187 | r.ExpectedFailures += other.ExpectedFailures |
| 188 | r.Missed += other.Missed |
| 189 | if r.WorkDir != "" && other.WorkDir != "" { |
| 190 | r.WorkDir += ":" + other.WorkDir |
| 191 | } else if other.WorkDir != "" { |
| 192 | r.WorkDir = other.WorkDir |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func (r *Result) Passed() bool { |
| 197 | return r.Failed == 0 && r.Panicked == 0 && |
| 198 | r.FixturePanicked == 0 && r.Missed == 0 && |
| 199 | r.RunError == nil |
| 200 | } |
| 201 | |
| 202 | func (r *Result) String() string { |
| 203 | if r.RunError != nil { |
| 204 | return "ERROR: " + r.RunError.Error() |
| 205 | } |
| 206 | |
| 207 | var value string |
| 208 | if r.Failed == 0 && r.Panicked == 0 && r.FixturePanicked == 0 && |
| 209 | r.Missed == 0 { |
| 210 | value = "OK: " |
| 211 | } else { |
| 212 | value = "OOPS: " |
| 213 | } |
| 214 | value += fmt.Sprintf("%d passed", r.Succeeded) |
| 215 | if r.Skipped != 0 { |
| 216 | value += fmt.Sprintf(", %d skipped", r.Skipped) |
| 217 | } |
| 218 | if r.ExpectedFailures != 0 { |
| 219 | value += fmt.Sprintf(", %d expected failures", r.ExpectedFailures) |
| 220 | } |
| 221 | if r.Failed != 0 { |
| 222 | value += fmt.Sprintf(", %d FAILED", r.Failed) |
| 223 | } |
| 224 | if r.Panicked != 0 { |
| 225 | value += fmt.Sprintf(", %d PANICKED", r.Panicked) |
| 226 | } |
| 227 | if r.Timeouted != 0 { |
| 228 | value += fmt.Sprintf(", %d TIMEOUTED", r.Timeouted) |
| 229 | } |
| 230 | if r.FixturePanicked != 0 { |
| 231 | value += fmt.Sprintf(", %d FIXTURE-PANICKED", r.FixturePanicked) |
| 232 | } |
| 233 | if r.Missed != 0 { |
| 234 | value += fmt.Sprintf(", %d MISSED", r.Missed) |
| 235 | } |
| 236 | if r.WorkDir != "" { |
| 237 | value += "\nWORK=" + r.WorkDir |
| 238 | } |
| 239 | return value |
| 240 | } |