forked from ETCDEVTeam/go-schroedinger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schroedinger.go
318 lines (274 loc) · 6.6 KB
/
schroedinger.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package schroedinger
import (
"bufio"
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
const commentPattern = "#"
var errCommentLine = errors.New("comment line")
var errEmptyLine = errors.New("empty line")
var trialsAllowed int
// different for windows
var goExecutablePath string
var commandPrefix []string
type test struct {
pkg string
name string
trials int
}
func (t *test) String() string {
return fmt.Sprintf("%s %s", t.pkg, t.name)
}
func init() {
goExecutablePath = getGoPath()
commandPrefix = getCommandPrefix()
}
func getGoPath() string {
return filepath.Join(runtime.GOROOT(), "bin", "go")
}
func getCommandPrefix() []string {
if runtime.GOOS == "windows" {
return []string{"cmd", "/C"}
}
return []string{"/bin/sh", "-c"}
}
func parseLinePackageTest(s string) *test {
t := &test{}
lsep := strings.Split(s, " ")
t.pkg = lsep[0]
if len(lsep) > 1 {
t.name = lsep[1]
}
t.pkg = strings.Replace(t.pkg, "/", string(filepath.Separator), -1)
return t
}
func parseMatchList(list string) []string {
// eg. "", "downloader,fetcher", "sync"
if len(list) == 0 {
return nil
}
ll := strings.Trim(list, " ")
return strings.Split(ll, ",")
}
func getNonRecursivePackageName(s string) string {
out := strings.TrimSuffix(s, string(filepath.Separator)+"...")
out = strings.TrimSuffix(out, "...")
return out
}
func handleLine(s string) (*test, error) {
var t *test
ss := strings.Trim(s, " ")
if len(ss) == 0 {
return nil, errEmptyLine
}
if strings.HasPrefix(ss, commentPattern) {
return nil, errCommentLine
}
if strings.Contains(ss, commentPattern) {
sss := strings.Split(ss, commentPattern)
ss = strings.Trim(sss[0], " ")
}
t = parseLinePackageTest(ss)
return t, nil
}
func lineMatchList(line string, whites, blacks []string) bool {
if blacks != nil && len(blacks) > 0 {
for _, m := range blacks {
if strings.Contains(line, m) {
return false
}
}
}
if whites != nil && len(whites) > 0 {
for _, m := range whites {
if !strings.Contains(line, m) {
return false
} else {
return true
}
}
}
return true
}
func collectTestsFromFile(f string) (tests []*test, err error) {
file, err := os.Open(f)
if err != nil {
return tests, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
t, e := handleLine(scanner.Text())
if e == nil {
tests = append(tests, t)
}
}
return tests, scanner.Err()
}
func filterTests(tests []*test, allowed func(*test) bool) []*test {
var out []*test
for _, t := range tests {
if allowed(t) {
out = append(out, t)
}
}
return out
}
func grepFailures(gotestout []byte) []string {
reader := bytes.NewReader(gotestout)
scanner := bufio.NewScanner(reader)
var fails []string
for scanner.Scan() {
// eg. '--- FAIL: TestFastCriticalRestarts64 (12.34s)'
text := scanner.Text()
if !strings.Contains(text, "FAIL") {
continue
}
if !strings.Contains(text, ":") {
continue
}
step1 := strings.Split(text, ":")
step2 := strings.Split(step1[1], "(")
testname := strings.Trim(step2[0], " ")
fails = append(fails, testname)
}
if e := scanner.Err(); e != nil {
log.Fatal(e)
}
return fails
}
func runTest(t *test) ([]byte, error) {
args := fmt.Sprintf("test %s", t.pkg)
if t.name != "" {
args += fmt.Sprintf(" -run %s", t.name)
}
log.Println("|", commandPrefix[0], commandPrefix[1], goExecutablePath+" "+args)
cmd := exec.Command(commandPrefix[0], commandPrefix[1], goExecutablePath+" "+args)
t.trials++
out, err := cmd.CombinedOutput()
return out, err
}
func tryIndividualTest(t *test, c chan error) {
for t.trials < trialsAllowed {
start := time.Now()
if o, e := runTest(t); e == nil {
log.Println(t)
log.Printf("- PASS (%v) %d/%d", time.Since(start), t.trials, trialsAllowed)
c <- nil
return
} else {
log.Println(t)
log.Printf("- FAIL (%v) %d/%d: %v", time.Since(start), t.trials, trialsAllowed, e)
fmt.Println()
fmt.Println(string(o))
}
}
c <- fmt.Errorf("FAIL %s %s", t.pkg, t.name)
}
// only gets to send one nil/error on the given channel
func tryPackageTest(t *test, c chan error) {
start := time.Now()
if o, e := runTest(t); e == nil {
log.Println(t)
log.Printf("- PASS (%v)", time.Since(start))
fmt.Println()
fmt.Println(string(o))
c <- nil
return
} else {
log.Println(t)
log.Printf("- FAIL (%v)", time.Since(start))
fmt.Println()
fmt.Println(string(o))
fails := grepFailures(o)
if len(fails) == 0 {
log.Fatalf("%s reported failure, but no failing tests were discovered, err=%v",
getNonRecursivePackageName(t.pkg), e)
}
var failingTests []*test
for _, f := range fails {
failingTests = append(failingTests,
&test{
pkg: getNonRecursivePackageName(t.pkg),
name: f,
trials: 1,
})
}
log.Printf("Found failing test(s) in %s: %v. Rerunning...",
getNonRecursivePackageName(t.pkg),
fails,
)
pc := make(chan error, len(failingTests))
for _, f := range failingTests {
go tryIndividualTest(f, pc)
}
for i := 0; i < len(failingTests); i++ {
if e := <-pc; e != nil {
c <- e
return
}
}
c <- nil
}
}
func tryTest(t *test, c chan error) {
if t.name != "" {
tryIndividualTest(t, c)
} else {
tryPackageTest(t, c)
}
}
func Run(testsFile, whitelistMatch, blacklistMatch string, trialsN int) {
e := run(testsFile, whitelistMatch, blacklistMatch, trialsN)
if e != nil {
log.Fatal(e)
}
}
func run(testsFile, whitelistMatch, blacklistMatch string, trialsN int) error {
if trialsN == 0 {
return fmt.Errorf("trials allowed must be >0, got: %d", trialsAllowed)
}
trialsAllowed = trialsN
whites := parseMatchList(whitelistMatch)
blacks := parseMatchList(blacklistMatch)
testsFile = filepath.Clean(testsFile)
testsFile, _ = filepath.Abs(testsFile)
allowed := func(t *test) bool {
return lineMatchList(t.pkg+" "+t.name, whites, blacks)
}
alltests, err := collectTestsFromFile(testsFile)
if err != nil {
return err
}
tests := filterTests(alltests, allowed)
log.Println("* go executable path:", goExecutablePath)
log.Println("* command prefix:", strings.Join(commandPrefix, " "))
log.Println("* tests file:", testsFile)
log.Println("* trials allowed: ", trialsAllowed)
log.Println("* blacklist: ", blacks)
log.Println("* whitelist: ", whites)
log.Printf("* running %d/%d tests", len(tests), len(alltests))
var results = make(chan error, len(tests))
allstart := time.Now()
defer func() {
log.Printf("FINISHED (%v)", time.Since(allstart))
}()
for _, t := range tests {
go tryTest(t, results)
}
for i := 0; i < len(tests); i++ {
if e := <-results; e != nil {
return e
}
}
close(results)
return nil
}