-
Notifications
You must be signed in to change notification settings - Fork 148
/
upgrader_test.go
550 lines (442 loc) · 9.79 KB
/
upgrader_test.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package tableflip
import (
"bytes"
"context"
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
"syscall"
"testing"
"time"
)
type testUpgrader struct {
*Upgrader
procs chan *testProcess
}
func newTestUpgrader(opts Options) *testUpgrader {
env, procs := testEnv()
u, err := newUpgrader(env, opts)
if err != nil {
panic(err)
}
err = u.Ready()
if err != nil {
panic(err)
}
return &testUpgrader{
Upgrader: u,
procs: procs,
}
}
func (tu *testUpgrader) upgradeProc(t *testing.T) (*testProcess, <-chan error) {
t.Helper()
ch := make(chan error, 1)
go func() {
for {
err := tu.Upgrade()
if err != errNotReady {
ch <- err
return
}
}
}()
select {
case err := <-ch:
t.Fatal("Upgrade failed:", err)
return nil, nil
case proc := <-tu.procs:
return proc, ch
}
}
var names = []string{"zaphod", "beeblebrox"}
func TestMain(m *testing.M) {
upg, err := New(Options{})
if errors.Is(err, ErrNotSupported) {
fmt.Fprintln(os.Stderr, "Skipping tests, OS is not supported")
os.Exit(0)
}
if err != nil {
panic(err)
}
if upg.parent == nil {
// Execute test suite if there is no parent.
os.Exit(m.Run())
}
if err := childProcess(upg); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
type childState struct {
PID int
}
// Used by Benchmark and TestUpgraderOnOS
func childProcess(upg *Upgrader) error {
if !upg.HasParent() {
return errors.New("Upgrader doesn't recognize parent")
}
wState, err := upg.Fds.File("wState")
if err != nil {
return err
}
if wState != nil {
state := &childState{
PID: os.Getpid(),
}
if err := gob.NewEncoder(wState).Encode(state); err != nil {
return err
}
wState.Close()
}
for _, name := range names {
file, err := upg.Fds.File(name)
if err != nil {
return fmt.Errorf("can't get file %s: %s", name, err)
}
if file == nil {
continue
}
if _, err := io.WriteString(file, name); err != nil {
return fmt.Errorf("can't write to %s: %s", name, err)
}
file.Close()
}
rExit, err := upg.Fds.File("rExit")
if err != nil {
return err
}
// Ready closes all inherited but unused files.
if err := upg.Ready(); err != nil {
return fmt.Errorf("can't signal ready: %s", err)
}
// Block until the parent is done with us. Returning an
// error here won't make the parent fail, so don't bother.
if rExit != nil {
var b [1]byte
rExit.Read(b[:])
}
return nil
}
func TestUpgraderOnOS(t *testing.T) {
u, err := newUpgrader(stdEnv, Options{})
if err != nil {
t.Fatal("Can't create Upgrader:", err)
}
defer u.Stop()
pipe := func() (r, w *os.File) {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
return r, w
}
addPipe := func(name string, file *os.File) {
t.Helper()
if err := u.Fds.AddFile(name, file); err != nil {
t.Fatal(err)
}
file.Close()
}
rState, wState := pipe()
defer rState.Close()
addPipe("wState", wState)
rExit, wExit := pipe()
defer wExit.Close()
addPipe("rExit", rExit)
var readers []*os.File
defer func() {
for _, r := range readers {
r.Close()
}
}()
for _, name := range names {
r, w := pipe()
addPipe(name, w)
readers = append(readers, r)
}
if err := u.Upgrade(); err == nil {
t.Error("Upgrade before Ready should return an error")
}
if err := u.Ready(); err != nil {
t.Fatal("Ready failed:", err)
}
for {
if err := u.Upgrade(); err == nil {
break
} else if err != errNotReady {
t.Fatal("Upgrade failed:", err)
}
}
// Tell child it's OK to exit now.
wExit.Close()
// Close copies of write pipes, so that
// reads below return EOF.
u.Stop()
var state childState
if err := gob.NewDecoder(rState).Decode(&state); err != nil {
t.Fatal("Can't decode state from child:", err)
}
if state.PID == os.Getpid() {
t.Error("Child did not execute in new process")
}
for i, name := range names {
nameBytes, err := ioutil.ReadAll(readers[i])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(nameBytes, []byte(name)) {
t.Fatalf("File %s has name %s in child", name, string(nameBytes))
}
}
}
func TestUpgraderCleanExit(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
proc, errs := u.upgradeProc(t)
proc.exit(nil)
if err := <-errs; err == nil {
t.Error("Expected Upgrade to return error when new child exits clean")
}
}
func TestUpgraderUncleanExit(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
proc, errs := u.upgradeProc(t)
proc.exit(errors.New("some error"))
if err := <-errs; err == nil {
t.Error("Expected Upgrade to return error when new child exits unclean")
}
}
func TestUpgraderTimeout(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{
UpgradeTimeout: 10 * time.Millisecond,
})
defer u.Stop()
new, errs := u.upgradeProc(t)
if sig := new.recvSignal(nil); sig != os.Kill {
t.Error("Expected os.Kill, got", sig)
}
if err := <-errs; err == nil {
t.Error("Expected Upgrade to return error when new child times out")
}
}
func TestUpgraderListenConfig(t *testing.T) {
t.Parallel()
var listenConfigUsed bool
u := newTestUpgrader(Options{
ListenConfig: &net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
listenConfigUsed = true
return nil
},
},
})
defer u.Stop()
new, _ := u.upgradeProc(t)
go new.recvSignal(nil)
_, err := u.Listen("tcp", ":0")
if err != nil {
t.Errorf("Unexpected error from listen: %v", err)
}
if !listenConfigUsed {
t.Error("Expected ListenConfig to be called during Listen")
}
new.exit(nil)
}
func TestUpgraderConcurrentUpgrade(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
new, _ := u.upgradeProc(t)
go new.recvSignal(nil)
if err := u.Upgrade(); err == nil {
t.Error("Expected Upgrade to refuse concurrent upgrade")
}
new.exit(nil)
}
func TestHasParent(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
if u.HasParent() {
t.Fatal("First process cannot have a parent")
}
}
func TestUpgraderWaitForParent(t *testing.T) {
t.Parallel()
env, procs := testEnv()
child, err := startChild(env, nil)
if err != nil {
t.Fatal(err)
}
proc := <-procs
u, err := newUpgrader(&proc.env, Options{})
if err != nil {
t.Fatal(err)
}
defer u.Stop()
if err := u.Ready(); err != nil {
t.Fatal(err)
}
exited := make(chan error, 1)
go func() {
exited <- u.WaitForParent(context.Background())
}()
select {
case <-exited:
t.Fatal("Returned before parent exited")
case <-time.After(time.Second):
}
readyFile := <-child.ready
if err := readyFile.Close(); err != nil {
t.Fatal(err)
}
if err := <-exited; err != nil {
t.Fatal("Unexpected error:", err)
}
}
func TestUpgraderReady(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
new, errs := u.upgradeProc(t)
_, exited, err := new.notify()
if err != nil {
t.Fatal("Can't notify Upgrader:", err)
}
if err := <-errs; err != nil {
t.Fatal("Expected Upgrade to return nil when child is ready")
}
select {
case <-u.Exit():
default:
t.Error("Expected Exit() to be closed when upgrade is done")
}
// Simulate the process exiting
file := <-u.exitFd
file.file.Close()
select {
case err := <-exited:
if err != nil {
t.Error("exit error", err)
}
case <-time.After(time.Second):
t.Error("Child wasn't notified of parent exiting")
}
}
func TestUpgraderShutdownCancelsUpgrade(t *testing.T) {
t.Parallel()
u := newTestUpgrader(Options{})
defer u.Stop()
new, errs := u.upgradeProc(t)
go new.recvSignal(nil)
u.Stop()
if err := <-errs; err == nil {
t.Error("Upgrade doesn't return an error when Stopp()ed")
}
if err := u.Upgrade(); err == nil {
t.Error("Upgrade doesn't return an error after Stop()")
}
}
func TestReadyWritesPIDFile(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "tableflip")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
file := dir + "/pid"
u := newTestUpgrader(Options{
PIDFile: file,
})
defer u.Stop()
if err := u.Ready(); err != nil {
t.Fatal("Ready returned error:", err)
}
fh, err := os.Open(file)
if err != nil {
t.Fatal("PID file doesn't exist:", err)
}
defer fh.Close()
var pid int
if _, err := fmt.Fscan(fh, &pid); err != nil {
t.Fatal("Can't read PID:", err)
}
if pid != os.Getpid() {
t.Error("PID doesn't match")
}
}
func TestWritePidFileWithoutPath(t *testing.T) {
pidFile := "tableflip-test.pid"
err := writePIDFile(pidFile)
if err != nil {
t.Fatal("Could not write pidfile:", err)
}
defer os.Remove(pidFile)
// lets see if we are able to read the file back
fh, err := os.Open(pidFile)
if err != nil {
t.Fatal("PID file doesn't exist:", err)
}
defer fh.Close()
// just to be sure: check the pid for correctness
// if something failed at a previous run we could be reading
// a bogus pidfile
var pid int
if _, err := fmt.Fscan(fh, &pid); err != nil {
t.Fatal("Can't read PID:", err)
}
if pid != os.Getpid() {
t.Error("PID doesn't match")
}
}
func BenchmarkUpgrade(b *testing.B) {
for _, n := range []int{4, 400, 4000} {
b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) {
fds := newFds(nil, nil)
for i := 0; i < n; i += 2 {
r, w, err := os.Pipe()
if err != nil {
b.Fatal(err)
}
err = fds.AddFile(strconv.Itoa(n), r)
if err != nil {
b.Fatal(err)
}
r.Close()
err = fds.AddFile(strconv.Itoa(n), w)
if err != nil {
b.Fatal(err)
}
w.Close()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u, err := newUpgrader(stdEnv, Options{})
if err != nil {
b.Fatal("Can't create Upgrader:", err)
}
if err := u.Ready(); err != nil {
b.Fatal("Can't call Ready:", err)
}
u.Fds = fds
if err := u.Upgrade(); err != nil {
b.Fatal(err)
}
}
b.StopTimer()
for _, f := range fds.used {
f.Close()
}
})
}
}