-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlifecycle_test.go
More file actions
446 lines (393 loc) · 13.3 KB
/
lifecycle_test.go
File metadata and controls
446 lines (393 loc) · 13.3 KB
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
// Copyright (c) HashiCorp, envoyAdminPort.
// SPDX-License-Identifier: MPL-2.0
package consuldp
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)
var (
envoyAdminPort = 19000
envoyAdminAddr = "127.0.0.1"
)
// TestLifecycleServerClosed tests that the lifecycle manager properly starts up
// and shuts down when the context passed into it is cancelled.
func TestLifecycleServerClosed(t *testing.T) {
cfg := Config{
Envoy: &EnvoyConfig{
AdminBindAddress: envoyAdminAddr,
AdminBindPort: envoyAdminPort,
},
}
m := NewLifecycleConfig(&cfg, &mockProxy{})
ctx, cancel := context.WithCancel(context.Background())
_ = m.startLifecycleManager(ctx)
require.Equal(t, m.running, true)
cancel()
require.Eventually(t, func() bool {
return !m.running
}, time.Second*2, time.Second)
}
// TestLifecycleServer_Startup the graceful startup functionality of the dataplane
// using different grace period and simulated startup duration configurations.
func TestLifecycleServer_Startup(t *testing.T) {
cases := map[string]struct {
startupGracePeriodSeconds int
gracefulStartupPath string
gracefulPort int
proxyStartupDelaySeconds int
}{
"startup grace period with default path, no startup time": {
startupGracePeriodSeconds: 5,
},
"startup time with default path, no grace period": {
proxyStartupDelaySeconds: 5,
},
"startup time and grace period with default path, grace period > startup time": {
startupGracePeriodSeconds: 10,
proxyStartupDelaySeconds: 5,
},
"startup time and grace period with default path, grace period < startup time": {
startupGracePeriodSeconds: 5,
proxyStartupDelaySeconds: 10,
},
"startup time and grace period with custom path, grace period < startup time": {
startupGracePeriodSeconds: 5,
proxyStartupDelaySeconds: 10,
gracefulStartupPath: "/custom_startup",
},
}
for name, c := range cases {
c := c
log.Printf("config = %v", c)
t.Run(name, func(t *testing.T) {
// Add a small margin of error for assertions checking expected
// behavior within the shutdown grace period window.
cfg := Config{
Envoy: &EnvoyConfig{
AdminBindAddress: envoyAdminAddr,
AdminBindPort: envoyAdminPort,
GracefulPort: c.gracefulPort,
GracefulStartupPath: c.gracefulStartupPath,
StartupGracePeriodSeconds: c.startupGracePeriodSeconds,
},
}
m := NewLifecycleConfig(&cfg, &mockProxy{
startupDelaySeconds: c.proxyStartupDelaySeconds,
})
require.NotNil(t, m)
require.NotNil(t, m.proxy)
require.NotNil(t, m.errorExitCh)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := m.startLifecycleManager(ctx)
require.NoError(t, err)
// Have consul-dataplane's lifecycle server start on an open port
// and figure out what port was used so we can make requests to it.
// Conveniently, this seems to wait until the server is ready for requests.
portCh := make(chan int, 1)
if c.gracefulPort == 0 {
m.lifecycleServer.Addr = "127.0.0.1:0"
}
m.lifecycleServer.BaseContext = func(l net.Listener) context.Context {
portCh <- l.Addr().(*net.TCPAddr).Port
return context.Background()
}
var port int
select {
case port = <-portCh:
case <-time.After(5 * time.Second):
}
// Check lifecycle server graceful port configuration
if c.gracefulPort != 0 {
require.Equal(t, c.gracefulPort, port, "failed to set lifecycle server port")
} else {
require.NotEqual(t, 0, port, "failed to figure out lifecycle server port")
}
log.Printf("port = %v\n", port)
// Check lifecycle server graceful startup path configuration
if c.gracefulStartupPath != "" {
require.Equal(t, m.gracefulStartupPath, c.gracefulStartupPath, "failed to set lifecycle server graceful startup HTTP endpoint path")
}
startupURL := fmt.Sprintf("http://127.0.0.1:%d%s", port, m.gracefulStartupPath)
// Start the mock proxy.
go func() {
fmt.Print("starting go func")
err := m.proxy.Run(ctx)
require.NoError(t, err)
fmt.Print("proxy should be running")
}()
start := time.Now()
log.Printf("sending startup check request to %s\n", startupURL)
resp, err := http.Get(startupURL)
require.NoError(t, err)
require.True(t, resp.StatusCode == 200)
duration := time.Since(start)
var expectedTime int
if c.proxyStartupDelaySeconds < c.startupGracePeriodSeconds {
expectedTime = c.proxyStartupDelaySeconds
} else {
expectedTime = c.startupGracePeriodSeconds
}
require.True(t, duration.Seconds()-float64(time.Duration(expectedTime)) < 1)
require.NoError(t, err)
require.NotNil(t, resp)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NotNil(t, body)
})
}
}
// TestLifecycleServer_Shutdown the graceful shutdown functionality of the dataplane
// with different grace period and listener draining configurations.
func TestLifecycleServer_Shutdown(t *testing.T) {
cases := map[string]struct {
shutdownDrainListenersEnabled bool
shutdownGracePeriodSeconds int
gracefulShutdownPath string
gracefulPort int
}{
"connection draining disabled without shutdown grace period": {
// All inbound and outbound connections are terminated immediately.
},
"connection draining enabled without shutdown grace period": {
// This should immediately send "Connection: close" to inbound HTTP1
// connections, GOAWAY to inbound HTTP2, and terminate connections on
// request completion. Outbound connections should start being rejected
// immediately.
shutdownDrainListenersEnabled: true,
},
"connection draining disabled with shutdown grace period": {
// This should immediately terminate any open inbound connections.
// Outbound connections should be allowed until the grace period has
// elapsed.
shutdownGracePeriodSeconds: 5,
},
"connection draining enabled with shutdown grace period": {
// This should immediately send "Connection: close" to inbound HTTP1
// connections, GOAWAY to inbound HTTP2, and terminate connections on
// request completion.
// Outbound connections should be allowed until the grace period has
// elapsed, then any remaining open connections should be closed and new
// outbound connections should start being rejected until pod termination.
shutdownDrainListenersEnabled: true,
shutdownGracePeriodSeconds: 5,
},
"custom graceful shutdown path and port": {
shutdownDrainListenersEnabled: true,
shutdownGracePeriodSeconds: 5,
gracefulShutdownPath: "/quit-nicely",
gracefulPort: 23108,
},
}
for name, c := range cases {
c := c
log.Printf("config = %v", c)
t.Run(name, func(t *testing.T) {
// Add a small margin of error for assertions checking expected
// behavior within the shutdown grace period window.
shutdownTimeout := time.Duration((c.shutdownGracePeriodSeconds + 5)) * time.Second
cfg := Config{
Envoy: &EnvoyConfig{
AdminBindAddress: envoyAdminAddr,
AdminBindPort: envoyAdminPort,
ShutdownDrainListenersEnabled: c.shutdownDrainListenersEnabled,
ShutdownGracePeriodSeconds: c.shutdownGracePeriodSeconds,
GracefulShutdownPath: c.gracefulShutdownPath,
GracefulPort: c.gracefulPort,
},
}
m := NewLifecycleConfig(&cfg, &mockProxy{})
require.NotNil(t, m)
require.NotNil(t, m.proxy)
require.NotNil(t, m.errorExitCh)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := m.startLifecycleManager(ctx)
require.NoError(t, err)
// Have consul-dataplane's lifecycle server start on an open port
// and figure out what port was used so we can make requests to it.
// Conveniently, this seems to wait until the server is ready for requests.
portCh := make(chan int, 1)
if c.gracefulPort == 0 {
m.lifecycleServer.Addr = "127.0.0.1:0"
}
m.lifecycleServer.BaseContext = func(l net.Listener) context.Context {
portCh <- l.Addr().(*net.TCPAddr).Port
return context.Background()
}
var port int
select {
case port = <-portCh:
case <-time.After(5 * time.Second):
}
// Check lifecycle server graceful port configuration
if c.gracefulPort != 0 {
require.Equal(t, c.gracefulPort, port, "failed to set lifecycle server port")
} else {
require.NotEqual(t, 0, port, "failed to figure out lifecycle server port")
}
log.Printf("port = %v\n", port)
// Check lifecycle server graceful shutdown path configuration
if c.gracefulShutdownPath != "" {
require.Equal(t, m.gracefulShutdownPath, c.gracefulShutdownPath, "failed to set lifecycle server graceful shutdown HTTP endpoint path")
}
shutdownUrl := fmt.Sprintf("http://127.0.0.1:%d%s", port, m.gracefulShutdownPath)
// Start the mock proxy.
go func() {
err := m.proxy.Run(ctx)
require.NoError(t, err)
}()
log.Printf("sending request to %s\n", shutdownUrl)
resp, err := http.Get(shutdownUrl)
// HTTP handler is not blocking, so need to wait and check mock
// client for expected method calls to proxy manager within
// expected shutdown grace period plus a small margin of error.
if c.shutdownDrainListenersEnabled {
require.Eventually(t, func() bool {
return m.proxy.(*mockProxy).drainCalled.Load() == 1
}, shutdownTimeout, time.Second, "Proxy.Drain() not called as expected")
} else {
require.Never(t, func() bool {
return m.proxy.(*mockProxy).drainCalled.Load() == 1
}, shutdownTimeout, time.Second, "Proxy.Drain() called unexpectedly")
}
require.Eventually(t, func() bool {
return m.proxy.(*mockProxy).quitCalled.Load() == 1
}, shutdownTimeout, time.Second, "Proxy.Quit() not called as expected")
// Expect that proxy is not forcefully killed as part of graceful shutdown.
require.Never(t, func() bool {
return m.proxy.(*mockProxy).killCalled.Load() == 1
}, shutdownTimeout, time.Second, "Proxy.Kill() called unexpectedly")
require.NoError(t, err)
require.NotNil(t, resp)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NotNil(t, body)
})
}
}
type mockProxy struct {
runCalled atomic.Int32
drainCalled atomic.Int32
quitCalled atomic.Int32
killCalled atomic.Int32
isReady atomic.Bool
startupDelaySeconds int
dumpConfigErr error
drainErr error
quitErr error
}
func (p *mockProxy) Run(ctx context.Context) error {
p.runCalled.Add(1)
time.Sleep(time.Duration(p.startupDelaySeconds) * time.Second)
p.isReady.Store(true)
return nil
}
func (p *mockProxy) Drain() error {
p.drainCalled.Add(1)
return p.drainErr
}
func (p *mockProxy) Quit() error {
p.quitCalled.Add(1)
return p.quitErr
}
func (p *mockProxy) Kill() error {
p.killCalled.Add(1)
return nil
}
func (p *mockProxy) DumpConfig() error {
return p.dumpConfigErr
}
func (p *mockProxy) Ready() (bool, error) {
return p.isReady.Load(), nil
}
func TestGracefulShutdown_DumpConfigError_DoesNotKillProxy(t *testing.T) {
m := &lifecycleConfig{
shutdownDrainListenersEnabled: true,
shutdownGracePeriodSeconds: 3,
dumpEnvoyConfigOnExitEnabled: true,
proxy: &mockProxy{
dumpConfigErr: errors.New("connection refused"),
},
errorExitCh: make(chan struct{}, 1),
mu: sync.Mutex{},
logger: hclog.NewNullLogger(),
}
done := make(chan struct{})
go func() {
m.gracefulShutdown()
close(done)
}()
select {
case <-m.errorExitCh:
require.Fail(t, "BUG: errorExitCh closed due to DumpConfig error — "+
"this causes Run() to call Quit()+Kill() immediately, killing Envoy at 0ms")
case <-time.After(1 * time.Second):
t.Log("OK: errorExitCh not closed, graceful shutdown continues")
}
<-done
}
func TestGracefulShutdown_DrainError_DoesNotKillProxy(t *testing.T) {
m := &lifecycleConfig{
shutdownDrainListenersEnabled: true,
shutdownGracePeriodSeconds: 2,
dumpEnvoyConfigOnExitEnabled: false,
proxy: &mockProxy{
drainErr: errors.New("connection refused"),
},
errorExitCh: make(chan struct{}, 1),
mu: sync.Mutex{},
logger: hclog.NewNullLogger(),
}
done := make(chan struct{})
go func() {
m.gracefulShutdown()
close(done)
}()
select {
case <-m.errorExitCh:
require.Fail(t, "BUG: errorExitCh closed due to Drain error — "+
"this kills Envoy immediately instead of waiting for grace period")
case <-time.After(1 * time.Second):
t.Log("OK: errorExitCh not closed, graceful shutdown continues")
}
<-done
}
func TestGracefulShutdown_QuitError_DoesNotKillProxy(t *testing.T) {
m := &lifecycleConfig{
shutdownDrainListenersEnabled: false,
shutdownGracePeriodSeconds: 1,
dumpEnvoyConfigOnExitEnabled: false,
proxy: &mockProxy{
quitErr: errors.New("connection refused"),
},
errorExitCh: make(chan struct{}, 1),
mu: sync.Mutex{},
logger: hclog.NewNullLogger(),
}
done := make(chan struct{})
go func() {
m.gracefulShutdown()
close(done)
}()
select {
case <-m.errorExitCh:
require.Fail(t, "BUG: errorExitCh closed due to Quit error — "+
"this triggers a second Quit()+Kill() from Run() select loop")
case <-done:
t.Log("OK: gracefulShutdown completed without closing errorExitCh")
case <-time.After(5 * time.Second):
require.Fail(t, "Timeout")
}
}