forked from upspin/upspin
/
testenv.go
398 lines (341 loc) · 9.67 KB
/
testenv.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package testenv provides a declarative environment for creating a complete Upspin test tree.
// See testenv_test.go for an example on how to use it.
package testenv
import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"strings"
"upspin.io/bind"
"upspin.io/client"
"upspin.io/config"
"upspin.io/errors"
"upspin.io/factotum"
"upspin.io/log"
"upspin.io/test/servermux"
"upspin.io/test/testutil"
"upspin.io/upbox"
"upspin.io/upspin"
"upspin.io/user"
// Implementations that are instantiated explicitly by New.
dirserver_inprocess "upspin.io/dir/inprocess"
dirserver_server "upspin.io/dir/server"
keyserver "upspin.io/key/inprocess"
storeserver "upspin.io/store/inprocess"
// Transports that are selected implicitly by bind.
_ "upspin.io/dir/remote"
_ "upspin.io/key/remote"
_ "upspin.io/store/remote"
)
// The servers that "remote" tests will work against.
const (
TestKeyServer = "key.test.upspin.io:443"
TestStoreServer = "store.test.upspin.io:443"
TestDirServer = "dir.test.upspin.io:443"
TestServerName = "dir-server@upspin.io"
)
// Setup is a configuration structure that contains a directory tree and other optional flags.
type Setup struct {
// OwnerName is the name of the user that runs the tests.
OwnerName upspin.UserName
// Kind is what kind of servers to use, "inprocess", "server", or "remote".
Kind string
// UpBox specifies whether to use upbox to run dirserver,
// storeserver, and keyserver processes separate to the test process.
// If false, the test server instances are run inside the test process.
UpBox bool
// Cache specifies whether to run a cacheserver for the owner.
// This option applies only when UpBox is true.
Cache bool
// Packing is the desired packing for the tree.
Packing upspin.Packing
// Verbose indicates whether we should print verbose debug messages.
Verbose bool
// Cleanup, if present, is run at Exit to clean up any test state necessary.
// It may return an error, which is returned by Exit.
Cleanup func(e *Env) error
}
// Env is the test environment. It contains a client which is the main piece that tests should use.
type Env struct {
// Client is the client tests should use for reaching the newly-created Tree.
Client upspin.Client
// Config is the configuration used when creating the client.
Config upspin.Config
// Setup contains the original setup options.
Setup *Setup
keyServer upspin.KeyServer
storeServer upspin.StoreServer
dirServer upspin.DirServer
schema *upbox.Schema
tmpDir string
exitCalled bool
}
var (
storeServerMux *servermux.Mux
dirServerMux *servermux.Mux
)
func init() {
var store upspin.StoreServer
storeServerMux, store = servermux.NewStore()
bind.RegisterStoreServer(upspin.InProcess, store)
var dir upspin.DirServer
dirServerMux, dir = servermux.NewDir()
bind.RegisterDirServer(upspin.InProcess, dir)
bind.RegisterKeyServer(upspin.InProcess, keyserver.New())
}
func randomEndpoint(prefix string) upspin.Endpoint {
b := make([]byte, 64)
rand.Read(b)
return upspin.Endpoint{
Transport: upspin.InProcess,
NetAddr: upspin.NetAddr(fmt.Sprintf("%s-%x", prefix, b)),
}
}
const upboxYAML = `
users:
- name: %[1]q
- name: %[2]q
cache: %[3]t
servers:
- name: keyserver
user: %[1]q
- name: storeserver
user: %[1]q
- name: dirserver
user: %[1]q
flags:
kind: %[4]s
domain: example.com
`
// New creates a new Env for testing.
func New(setup *Setup) (*Env, error) {
const op errors.Op = "testenv.New"
env := &Env{
Setup: setup,
}
cfg := config.New()
// All tests use the same keyserver, so that users of different
// DirServers can still interact with each other.
cfg = config.SetKeyEndpoint(cfg, upspin.Endpoint{Transport: upspin.InProcess})
switch setup.Kind {
case "inprocess", "server":
if setup.UpBox {
// Use upbox.
yaml := fmt.Sprintf(upboxYAML,
TestServerName,
setup.OwnerName,
setup.Cache,
setup.Kind,
)
schema, err := upbox.SchemaFromYAML(yaml)
if err != nil {
return nil, err
}
if err := schema.Start(); err != nil {
return nil, err
}
env.schema = schema
cfg, err = config.FromFile(schema.Config(string(TestServerName)))
if err != nil {
env.cleanup()
return nil, err
}
env.Config = cfg
break
}
// Test either the dir/inprocess or dir/server implementations
// entire in-memory and offline.
// Set endpoints.
storeEndpoint := randomEndpoint("store")
cfg = config.SetStoreEndpoint(cfg, storeEndpoint)
dirEndpoint := randomEndpoint("dir")
cfg = config.SetDirEndpoint(cfg, dirEndpoint)
// Set up a StoreServer instance. Just use the inprocess
// version for offline tests; the store/server implementation
// isn't interesting when run offline.
env.storeServer = storeserver.New()
storeServerMux.Register(storeEndpoint, env.storeServer)
// Set up user and factotum.
cfg = config.SetUserName(cfg, TestServerName)
f, err := factotum.NewFromDir(testutil.Repo("key", "testdata", TestServerName[:strings.Index(TestServerName, "@")]))
if err != nil {
return nil, errors.E(op, err)
}
cfg = config.SetFactotum(cfg, f)
// Set up DirServer instance.
switch setup.Kind {
case "inprocess":
env.dirServer = dirserver_inprocess.New(cfg)
case "server":
// Create temporary directory for DirServer storage.
logDir, err := ioutil.TempDir("", "testenv-dirserver")
if err != nil {
return nil, errors.E(op, err)
}
env.tmpDir = logDir
env.dirServer, err = dirserver_server.New(cfg, "logDir="+logDir)
if err != nil {
env.cleanup()
return nil, errors.E(op, err)
}
}
dirServerMux.Register(dirEndpoint, env.dirServer)
env.Config = cfg
case "remote":
if setup.UpBox {
return nil, errors.E(op, "UpBox set with incompatible Kind (remote)")
}
cfg = config.SetKeyEndpoint(cfg, upspin.Endpoint{
Transport: upspin.Remote,
NetAddr: TestKeyServer,
})
cfg = config.SetStoreEndpoint(cfg, upspin.Endpoint{
Transport: upspin.Remote,
NetAddr: TestStoreServer,
})
cfg = config.SetDirEndpoint(cfg, upspin.Endpoint{
Transport: upspin.Remote,
NetAddr: TestDirServer,
})
env.Config = cfg
default:
return nil, errors.E(op, errors.Errorf("bad kind %q", setup.Kind))
}
cfg, err := env.NewUser(setup.OwnerName)
if err != nil {
env.cleanup()
return nil, errors.E(op, err)
}
env.Config = cfg
if err := makeRootIfNotExist(env.Config); err != nil {
env.cleanup()
return nil, errors.E(op, err)
}
env.Client = client.New(cfg)
return env, nil
}
// Exit indicates the end of the test environment. It must only be called once. If Setup.Cleanup exists it is called.
func (e *Env) Exit() error {
const op errors.Op = "testenv.Exit"
if e.exitCalled {
return errors.E(op, errors.Invalid, "exit already called")
}
e.exitCalled = true
var firstErr error
check := func(err error) {
if err == nil {
return
}
if firstErr == nil {
firstErr = err
}
log.Debug.Println(op, err)
}
if e.Setup.Cleanup != nil {
check(e.Setup.Cleanup(e))
}
if e.dirServer != nil {
e.dirServer.Close()
}
if e.storeServer != nil {
e.storeServer.Close()
}
if e.keyServer != nil {
e.keyServer.Close()
}
check(e.cleanup())
return firstErr
}
func (e *Env) cleanup() error {
var err error
if e.tmpDir != "" {
err = os.RemoveAll(e.tmpDir)
e.tmpDir = ""
}
if e.schema != nil {
s := e.schema
e.schema = nil
err2 := s.Stop()
if err == nil {
err = err2
}
}
return err
}
// NewUser creates a new client for a user. The new user will not
// have a root created. Callers should use the client to make a root directory if
// necessary.
func (e *Env) NewUser(userName upspin.UserName) (upspin.Config, error) {
const op errors.Op = "testenv.NewUser"
if e.Setup.UpBox {
switch userName {
case e.Setup.OwnerName, TestServerName:
return config.FromFile(e.schema.Config(string(userName)))
}
}
cfg := config.SetUserName(e.Config, userName)
cfg = config.SetValue(cfg, "cache", "")
cfg = config.SetPacking(cfg, e.Setup.Packing)
// Set up a factotum for the user.
user, _, _, err := user.Parse(userName)
if err != nil {
return nil, errors.E(op, err)
}
var secrets string
if e.Setup.Kind == "remote" {
secrets = testutil.Repo("key", "testdata", "remote", string(user))
} else {
secrets = testutil.Repo("key", "testdata", string(user))
}
f, err := factotum.NewFromDir(secrets)
if err != nil {
return nil, errors.E(op, userName, err)
}
cfg = config.SetFactotum(cfg, f)
// Don't register users with the test cluster key server;
// our test users should be already registered there.
if e.Setup.Kind != "remote" {
// Register the user with the key server.
err = registerUserWithKeyServer(e.Config, cfg)
if err != nil {
return nil, errors.E(op, err)
}
}
return cfg, nil
}
// registerUserWithKeyServer registers userName's config with the inProcess keyServer.
func registerUserWithKeyServer(server upspin.Config, user upspin.Config) error {
key, err := bind.KeyServer(server, server.KeyEndpoint())
if err != nil {
return err
}
// Install the registered user.
u := &upspin.User{
Name: user.UserName(),
Dirs: []upspin.Endpoint{user.DirEndpoint()},
Stores: []upspin.Endpoint{user.StoreEndpoint()},
PublicKey: user.Factotum().PublicKey(),
}
return key.Put(u)
}
func makeRootIfNotExist(cfg upspin.Config) error {
path := upspin.PathName(cfg.UserName()) + "/"
dir, err := bind.DirServer(cfg, cfg.DirEndpoint())
if err != nil {
return err
}
entry := &upspin.DirEntry{
Name: path,
SignedName: path,
Attr: upspin.AttrDirectory,
}
_, err = dir.Put(entry)
if errors.Is(errors.Exist, err) {
return nil
}
return err
}