This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundup.go
145 lines (132 loc) · 2.98 KB
/
roundup.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
package main
import (
"exec"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strconv"
"strings"
)
const (
jLo = 1
jHi = 16
jEnv = "ROUNDUP_CONCURRENCY"
)
var testCaseRegexp = regexp.MustCompile("it_[a-zA-Z0-9_]*")
type TaskResult struct {
task string
err os.Error
output string
}
func printUsage() {
fmt.Printf(`roundup [options] testplan
Options:
-jN - concurrency level. For example, -j4 means parallel execution with 4 threads.
`)
}
func execute(testPlan, task string) (res *TaskResult) {
res = new(TaskResult)
res.task = task
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("set -e; . %s; %s", testPlan, task))
cmd.Dir, _ = path.Split(testPlan)
data, err := cmd.CombinedOutput()
res.err = err
res.output = string(data)
return
}
func worker(index int, testPlan string, taskCh chan string, resCh chan *TaskResult, doneCh chan bool) {
for task := range taskCh {
resCh <- execute(testPlan, task)
}
doneCh <- true
}
func resultWorker(resCh chan *TaskResult, doneResCh chan []*TaskResult) {
var all []*TaskResult
for r := range resCh {
if r.err == nil {
fmt.Printf("%s\tPASS\n", r.task)
} else {
fmt.Printf("%s\n", r.output)
fmt.Printf("%s\tFAILED\n", r.task)
}
all = append(all, r)
}
doneResCh <- all
}
func summarize(res []*TaskResult) bool {
passed := 0
for _, r := range res {
if r.err == nil {
passed++
}
}
fmt.Printf("\nTests: %3d | Passed: %3d | Failed: %3d |\n", len(res), passed, len(res)-passed)
return passed == len(res)
}
func main() {
var err os.Error
if len(os.Args) == 1 {
printUsage()
os.Exit(1)
}
var j = jLo
if os.Getenv(jEnv) != "" {
j, err = strconv.Atoi(os.Getenv(jEnv))
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %s environment variable is set, "+
"but has non-numeric value %s\n",
jEnv, os.Getenv(jEnv))
j = 1
}
}
if strings.HasPrefix(os.Args[1], "-j") {
j, err = strconv.Atoi(os.Args[1][2:])
if err != nil {
fmt.Fprintf(os.Stderr, "roundup: Couldn't parse -j option. "+
"Valid values are like -j2, -j4, -j16, etc\n")
os.Exit(1)
}
if len(os.Args) == 2 {
printUsage()
os.Exit(1)
}
}
if j < jLo || j > jHi {
fmt.Fprintf(os.Stderr, "roundup: %d is invalid value for concurrency level. "+
"Concurrency levels from %d to %d are only supported\n", j, jLo, jHi)
os.Exit(1)
}
testPlan := os.Args[len(os.Args)-1]
data, err := ioutil.ReadFile(testPlan)
if err != nil {
fmt.Fprintf(os.Stderr, "roundup: couldn't read %s: %v", testPlan, err)
os.Exit(1)
}
taskCh := make(chan string)
resCh := make(chan *TaskResult)
doneCh := make(chan bool)
doneResCh := make(chan []*TaskResult)
for i := 0; i < j; i++ {
go worker(i, testPlan, taskCh, resCh, doneCh)
}
go resultWorker(resCh, doneResCh)
for _, line := range strings.Split(string(data), "\n") {
m := testCaseRegexp.FindString(line)
if m != "" {
taskCh <- m
}
}
close(taskCh)
for i := 0; i < j; i++ {
<-doneCh
}
close(doneCh)
close(resCh)
res := <-doneResCh
close(doneResCh)
if !summarize(res) {
os.Exit(2)
}
}