-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmachine_test.go
252 lines (224 loc) · 6.5 KB
/
machine_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
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigmachine
import (
"bytes"
"context"
"encoding/gob"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"runtime"
"testing"
"time"
"github.com/grailbio/base/errors"
"github.com/grailbio/bigmachine/rpc"
)
var fakeDigest = digester.FromString("fake binary")
type fakeSupervisor struct {
Args []string
Environ []string
Image []byte
LastKeepalive time.Time
Hung bool
Execd bool
}
func (s *fakeSupervisor) Setenv(ctx context.Context, env []string, _ *struct{}) error {
s.Environ = env
return nil
}
func (s *fakeSupervisor) Setargs(ctx context.Context, args []string, _ *struct{}) error {
s.Args = args
return nil
}
func (s *fakeSupervisor) Setbinary(ctx context.Context, binary io.Reader, _ *struct{}) (err error) {
s.Image, err = ioutil.ReadAll(binary)
return err
}
func (s *fakeSupervisor) GetBinary(ctx context.Context, _ struct{}, rc *io.ReadCloser) error {
if s.Image == nil {
return errors.E(errors.Invalid, "no binary set")
}
*rc = ioutil.NopCloser(bytes.NewReader(s.Image))
return nil
}
func (s *fakeSupervisor) Exec(ctx context.Context, exec io.Reader, _ *struct{}) error {
s.Execd = true
return nil
}
func (s *fakeSupervisor) Tail(ctx context.Context, fd int, rc *io.ReadCloser) error {
return errors.New("not supported")
}
func (s *fakeSupervisor) Ping(ctx context.Context, seq int, replyseq *int) error {
*replyseq = seq
return nil
}
func (s *fakeSupervisor) Info(ctx context.Context, _ struct{}, info *Info) error {
info.Goos = runtime.GOOS
info.Goarch = runtime.GOARCH
info.Digest = fakeDigest
return nil
}
func (s *fakeSupervisor) Keepalive(ctx context.Context, next time.Duration, reply *keepaliveReply) error {
if s.Hung {
<-ctx.Done()
return ctx.Err()
}
s.LastKeepalive = time.Now()
reply.Next = next
reply.Healthy = true
return nil
}
func (s *fakeSupervisor) Hang(ctx context.Context, _ struct{}, _ *struct{}) error {
<-ctx.Done()
return ctx.Err()
}
func (s *fakeSupervisor) Register(ctx context.Context, svc service, _ *struct{}) error {
// Tests only require that we Init services (if needed), so we don't do any
// actual registration.
return maybeInit(svc.Instance, nil)
}
func newTestMachine(t *testing.T, params ...Param) (m *Machine, supervisor *fakeSupervisor, shutdown func()) {
t.Helper()
supervisor = new(fakeSupervisor)
srv := rpc.NewServer()
if err := srv.Register("Supervisor", supervisor); err != nil {
t.Fatal(err)
}
httpsrv := httptest.NewServer(srv)
client, err := rpc.NewClient(func() *http.Client { return httpsrv.Client() }, "/")
if err != nil {
httpsrv.Close()
t.Fatal(err)
}
m = &Machine{
Addr: httpsrv.URL,
client: client,
owner: true,
keepalivePeriod: time.Minute,
keepaliveTimeout: 2 * time.Minute,
keepaliveRpcTimeout: 10 * time.Second,
tailDone: make(chan struct{}),
}
for _, param := range params {
param.applyParam(m)
}
m.start(nil)
return m, supervisor, func() {
m.Cancel()
select {
case <-m.Wait(Stopped):
case <-time.After(time.Second):
t.Log("failed to stop server after 1 second")
}
httpsrv.Close()
}
}
func TestMachineBootup(t *testing.T) {
m, supervisor, shutdown := newTestMachine(t)
defer shutdown()
<-m.Wait(Running)
if got, want := m.State(), Running; got != want {
t.Errorf("got %v, want %v", got, want)
}
r, err := binary()
if err != nil {
t.Fatal(err)
}
image, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(supervisor.Image, image) {
t.Error("image does not match")
}
if time.Since(supervisor.LastKeepalive) > time.Minute {
t.Errorf("failed to maintain keepalive")
}
}
func TestMachineEnv(t *testing.T) {
m, supervisor, shutdown := newTestMachine(t, Environ{"test=yes"})
defer shutdown()
<-m.Wait(Running)
if got, want := len(supervisor.Environ), 1; got != want {
t.Fatalf("got %v, want %v", got, want)
}
if got, want := supervisor.Environ[0], "test=yes"; got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestCallTimeout(t *testing.T) {
m, _, shutdown := newTestMachine(t)
defer shutdown()
const timeout = 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err := m.Call(ctx, "Supervisor.Hang", struct{}{}, nil)
if got, want := err, context.DeadlineExceeded; got != want {
t.Errorf("got %v, want %v", got, want)
}
cancel()
}
func TestMachineContext(t *testing.T) {
log.SetFlags(log.Llongfile)
m, supervisor, shutdown := newTestMachine(t)
defer shutdown()
supervisor.Hung = true
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
err := m.Call(ctx, "Supervisor.Hang", struct{}{}, nil)
if got, want := err, context.Canceled; got != want {
t.Errorf("got %v, want %v", got, want)
}
}
// serviceGobUnregistered is a service that is not registered with gob, so
// attempts to register it will fail.
type serviceGobUnregistered struct{}
// TestServiceGobUnregisteredFastFail verifies that we fail fast when a service
// is not gob-encodable.
func TestServiceGobUnregisteredFastFail(t *testing.T) {
m, _, shutdown := newTestMachine(t, Services{"GobUnregistered": serviceGobUnregistered{}})
defer shutdown()
select {
case <-m.Wait(Running):
if m.State() == Running {
t.Fatalf("machine is running with broken service")
}
case <-time.After(2 * time.Minute):
// If our test environment causes this to falsely fail, we almost
// surely have lots of other problems, as this should otherwise fail
// almost instantly.
t.Fatalf("took too long to fail")
}
}
// serviceInitPanic is a service that panics in Init, indicating that the
// service is fatally broken.
type serviceInitPanic struct{}
func (serviceInitPanic) Init(b *B) error {
panic("")
}
func init() {
gob.Register(serviceInitPanic{})
}
// TestBadServiceFastFail verifies that we fail fast when a service panics in
// its Init.
func TestServiceInitPanicFastFail(t *testing.T) {
m, _, shutdown := newTestMachine(t, Services{"InitPanic": serviceInitPanic{}})
defer shutdown()
select {
case <-m.Wait(Running):
if m.State() == Running {
t.Fatalf("machine is running with broken service")
}
case <-time.After(2 * time.Minute):
// If our test environment causes this to falsely fail, we almost
// surely have lots of other problems, as this should otherwise fail
// almost instantly.
t.Fatalf("took too long to fail")
}
}