forked from perkeep/perkeep
/
world.go
297 lines (266 loc) · 7.86 KB
/
world.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
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/osutil"
)
// World defines an integration test world.
//
// It's used to run the actual Camlistore binaries (camlistored,
// camput, camget, camtool, etc) together in large tests, including
// building them, finding them, and wiring them up in an isolated way.
type World struct {
camRoot string // typically $GOPATH[0]/src/camlistore.org
tempDir string
listener net.Listener // randomly chosen 127.0.0.1 port for the server
port int
server *exec.Cmd
isRunning int32 // state of the camlistored server. Access with sync/atomic only.
serverErr error
cammount *os.Process
}
// NewWorld returns a new test world.
// It requires that GOPATH is set to find the "camlistore.org" root.
func NewWorld() (*World, error) {
if os.Getenv("GOPATH") == "" {
return nil, errors.New("GOPATH environment variable isn't set; required to run Camlistore integration tests")
}
root, err := osutil.GoPackagePath("camlistore.org")
if err == os.ErrNotExist {
return nil, errors.New("Directory \"camlistore.org\" not found under GOPATH/src; can't run Camlistore integration tests.")
}
if err != nil {
return nil, fmt.Errorf("Error searching for \"camlistore.org\" under GOPATH: %v", err)
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
return &World{
camRoot: root,
listener: ln,
port: ln.Addr().(*net.TCPAddr).Port,
}, nil
}
func (w *World) Addr() string {
return w.listener.Addr().String()
}
// Start builds the Camlistore binaries and starts a server.
func (w *World) Start() error {
var err error
w.tempDir, err = ioutil.TempDir("", "camlistore-test-")
if err != nil {
return err
}
// Build.
{
cmd := exec.Command("go", "run", "make.go")
cmd.Dir = w.camRoot
log.Print("Running make.go to build camlistore binaries for testing...")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Error building world: %v, %s", err, string(out))
}
log.Print("Ran make.go.")
}
// Start camlistored.
{
w.server = exec.Command(
filepath.Join(w.camRoot, "bin", "camlistored"),
"--openbrowser=false",
"--configfile="+filepath.Join(w.camRoot, "pkg", "test", "testdata", "server-config.json"),
"--listen=FD:3",
"--pollparent=true",
)
var buf bytes.Buffer
w.server.Stdout = &buf
w.server.Stderr = &buf
w.server.Dir = w.tempDir
w.server.Env = append(os.Environ(),
"CAMLI_DEBUG=1",
"CAMLI_ROOT="+w.tempDir,
"CAMLI_SECRET_RING="+filepath.Join(w.camRoot, filepath.FromSlash("pkg/jsonsign/testdata/test-secring.gpg")),
"CAMLI_BASE_URL=http://127.0.0.1:"+strconv.Itoa(w.port),
)
listenerFD, err := w.listener.(*net.TCPListener).File()
if err != nil {
return err
}
w.server.ExtraFiles = []*os.File{listenerFD}
if err := w.server.Start(); err != nil {
w.serverErr = fmt.Errorf("starting camlistored: %v", err)
return w.serverErr
}
atomic.StoreInt32(&w.isRunning, 1)
waitc := make(chan error, 1)
go func() {
err := w.server.Wait()
w.serverErr = fmt.Errorf("%v: %s", err, buf.String())
atomic.StoreInt32(&w.isRunning, 0)
waitc <- w.serverErr
}()
upc := make(chan bool)
timeoutc := make(chan bool)
go func() {
for i := 0; i < 100; i++ {
res, err := http.Get("http://127.0.0.1:" + strconv.Itoa(w.port))
if err == nil {
res.Body.Close()
upc <- true
return
}
time.Sleep(50 * time.Millisecond)
}
w.serverErr = errors.New(buf.String())
atomic.StoreInt32(&w.isRunning, 0)
timeoutc <- true
}()
select {
case <-waitc:
return fmt.Errorf("server exited: %v", w.serverErr)
case <-timeoutc:
return fmt.Errorf("server never became reachable: %v", w.serverErr)
case <-upc:
if err := w.Ping(); err != nil {
return err
}
// Success.
}
}
return nil
}
// Ping returns an error if the world's camlistored is not running.
func (w *World) Ping() error {
if atomic.LoadInt32(&w.isRunning) != 1 {
return fmt.Errorf("camlistored not running: %v", w.serverErr)
}
return nil
}
func (w *World) Stop() {
if w == nil {
return
}
w.server.Process.Kill()
if d := w.tempDir; d != "" {
os.RemoveAll(d)
}
}
func (w *World) NewPermanode(t *testing.T) blob.Ref {
if err := w.Ping(); err != nil {
t.Fatal(err)
}
out := MustRunCmd(t, w.Cmd("camput", "permanode"))
br, ok := blob.Parse(strings.TrimSpace(out))
if !ok {
t.Fatalf("Expected permanode in camput stdout; got %q", out)
}
return br
}
func (w *World) Cmd(binary string, args ...string) *exec.Cmd {
return w.CmdWithEnv(binary, os.Environ(), args...)
}
func (w *World) CmdWithEnv(binary string, env []string, args ...string) *exec.Cmd {
cmd := exec.Command(filepath.Join(w.camRoot, "bin", binary), args...)
switch binary {
case "camget", "camput", "camtool", "cammount":
clientConfigDir := filepath.Join(w.camRoot, "config", "dev-client-dir")
cmd.Env = append([]string{
"CAMLI_CONFIG_DIR=" + clientConfigDir,
// Respected by env expansions in config/dev-client-dir/client-config.json:
"CAMLI_SERVER=" + w.ServerBaseURL(),
"CAMLI_SECRET_RING=" + w.SecretRingFile(),
"CAMLI_KEYID=" + w.ClientIdentity(),
"CAMLI_AUTH=userpass:testuser:passTestWorld",
}, env...)
default:
panic("Unknown binary " + binary)
}
return cmd
}
func (w *World) ServerBaseURL() string {
return fmt.Sprintf("http://127.0.0.1:%d", w.port)
}
var theWorld *World
// GetWorld returns (creating if necessary) a test singleton world.
// It calls Fatal on the provided test if there are problems.
func GetWorld(t *testing.T) *World {
w := theWorld
if w == nil {
var err error
w, err = NewWorld()
if err != nil {
t.Fatalf("Error finding test world: %v", err)
}
err = w.Start()
if err != nil {
t.Fatalf("Error starting test world: %v", err)
}
theWorld = w
}
return w
}
// GetWorldMaybe returns the current World. It might be nil.
func GetWorldMaybe(t *testing.T) *World {
return theWorld
}
// RunCmd runs c (which is assumed to be something short-lived, like a
// camput or camget command), capturing its stdout for return, and
// also capturing its stderr, just in the case of errors.
// If there's an error, the return error fully describes the command and
// all output.
func RunCmd(c *exec.Cmd) (output string, err error) {
var stdout, stderr bytes.Buffer
c.Stderr = &stderr
c.Stdout = &stdout
err = c.Run()
if err != nil {
return "", fmt.Errorf("Error running command %+v: Stdout:\n%s\nStderrr:\n%s\n", c, stdout.String(), stderr.String())
}
return stdout.String(), nil
}
// MustRunCmd wraps RunCmd, failing t if RunCmd returns an error.
func MustRunCmd(t *testing.T, c *exec.Cmd) string {
out, err := RunCmd(c)
if err != nil {
t.Fatal(err)
}
return out
}
// ClientIdentity returns the GPG identity to use in World tests, suitable
// for setting in CAMLI_CLIENT_IDENTITY.
func (w *World) ClientIdentity() string {
return "26F5ABDA"
}
// SecretRingFile returns the GnuPG secret ring, suitable for setting
// in CAMLI_SECRET_RING.
func (w *World) SecretRingFile() string {
return filepath.Join(w.camRoot, "pkg", "jsonsign", "testdata", "test-secring.gpg")
}
// SearchHandlerPath returns the path to the search handler, with trailing slash.
func (w *World) SearchHandlerPath() string { return "/my-search/" }