forked from zeebo/goci
/
runner.go
212 lines (184 loc) · 4.54 KB
/
runner.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
package main
import (
"bytes"
"fmt"
"github.com/zeebo/goci/app/rpc"
"github.com/zeebo/goci/app/rpc/client"
"github.com/zeebo/goci/environ"
"github.com/zeebo/goci/tarball"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type LocalWorld interface {
Create(string, os.FileMode) (io.WriteCloser, error)
TempDir(string) (string, error)
Make(environ.Command) environ.Proc
RemoveAll(string) error
}
var World LocalWorld = environ.New()
//responder is a type that knows about the test environment and can post
//responses to the test manager.
type responder struct {
url string
id string
index int
test rpc.RunTest
}
//createResponse creates a TestResponse with the given error and output strings
func (r *responder) createResponse(output string, typ rpc.OutputType) *rpc.TestResponse {
return &rpc.TestResponse{
ID: r.id,
Output: rpc.Output{
ImportPath: r.test.ImportPath,
Config: r.test.Config,
Output: output,
Type: typ,
},
}
}
//post sends the TestResponse to the TestManager
func (r *responder) post(args *rpc.TestResponse) {
cl := client.New(r.url, http.DefaultClient, client.JsonCodec)
log.Printf("Posting response[%s]: %+v", r.url, args)
cl.Call("Runner.Post", args, new(rpc.None))
}
//bail is a helper function to post an error
func (r *responder) bail(e interface{}) {
resp := r.createResponse(fmt.Sprint(e), rpc.OutputError)
r.post(resp)
}
//success is a helper function to post an output
func (r *responder) success(e interface{}) {
resp := r.createResponse(fmt.Sprint(e), rpc.OutputSuccess)
r.post(resp)
}
//loadTest loads the test field of the responder, returning any errors.
func (r *responder) loadTest() (err error) {
cl := client.New(r.url, http.DefaultClient, client.JsonCodec)
args := &rpc.TestRequest{
ID: r.id,
Index: r.index,
}
err = cl.Call("Runner.Request", args, &r.test)
log.Printf("Handling request[%s]: %+v", r.url, r.test)
return
}
//timeout runs the given proc with a timeout, and returns if the process
//finished in the duration specified.
func timeout(r *responder, p environ.Proc, dur time.Duration) (ok bool) {
done := make(chan bool, 1)
if err := p.Start(); err != nil {
r.bail("error starting command")
return
}
defer p.Kill()
//start a race
go func() {
p.Wait()
done <- true
}()
go func() {
<-time.After(dur)
done <- false
}()
//see who won
ok = <-done
return
}
//turn off all the log flags
func init() {
log.SetFlags(0)
}
func main() {
//make sure we have the right number of arguments
if len(os.Args) != 4 {
log.Print("usage: runner <url to rpc> <test ID> <test index>")
return
}
//set up the responder
r := new(responder)
r.url, r.id = os.Args[1], os.Args[2]
//parse the index
s64, err := strconv.ParseInt(os.Args[3], 10, 32)
if err != nil {
log.Fatal(err)
}
r.index = int(s64)
//load the test description
if err := r.loadTest(); err != nil {
log.Fatal(err)
}
//now we can post results back.
//create a temporary directory for the sources
sdir, err := World.TempDir("src")
if err != nil {
r.bail(err)
return
}
defer World.RemoveAll(sdir)
//download the sources
sr, err := http.Get(r.test.SourceURL)
if err != nil || sr.StatusCode != http.StatusOK {
r.bail(fmt.Sprintf("%d: %v", sr.StatusCode, err))
return
}
//extract them into the directory
if err := tarball.Extract(sr.Body, sdir); err != nil {
r.bail(err)
return
}
//close the body
sr.Body.Close()
//create the directory for the binary
bdir, err := World.TempDir("bin")
if err != nil {
r.bail(err)
return
}
defer World.RemoveAll(bdir)
//create the binary file
binFile := filepath.Join(bdir, "binary")
bw, err := World.Create(binFile, 0777)
if err != nil {
r.bail(err)
return
}
//download the binary
br, err := http.Get(r.test.BinaryURL)
if err != nil || br.StatusCode != http.StatusOK {
r.bail(fmt.Sprintf("%d: %v", br.StatusCode, err))
return
}
//copy the binary data in and close the file
io.Copy(bw, br.Body)
bw.Close()
br.Body.Close()
//create the command
env := []string{
//copy in some basic env vars if we have them
fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
fmt.Sprintf("GOROOT=%s", os.Getenv("GOROOT")),
fmt.Sprintf("GOPATH=%s", os.Getenv("GOPATH")),
}
var buf bytes.Buffer
cmd := environ.Command{
W: &buf,
Dir: sdir,
Env: env,
Path: binFile,
Args: []string{binFile, "-test.v"},
}
proc := World.Make(cmd)
//only allow the test to run for one minute
finished := timeout(r, proc, time.Minute)
if finished {
r.success(buf.String())
} else {
r.bail("test lasted more than 1 minute")
}
}