forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_test.go
498 lines (448 loc) · 16.6 KB
/
generic_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
/*
Copyright 2015 The Kubernetes Authors.
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 pleg
import (
"errors"
"fmt"
"reflect"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/diff"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
)
const (
testContainerRuntimeType = "fooRuntime"
)
type TestGenericPLEG struct {
pleg *GenericPLEG
runtime *containertest.FakeRuntime
clock *clock.FakeClock
}
func newTestGenericPLEG() *TestGenericPLEG {
fakeRuntime := &containertest.FakeRuntime{}
clock := clock.NewFakeClock(time.Time{})
// The channel capacity should be large enough to hold all events in a
// single test.
pleg := &GenericPLEG{
relistPeriod: time.Hour,
runtime: fakeRuntime,
eventChannel: make(chan *PodLifecycleEvent, 100),
podRecords: make(podRecords),
clock: clock,
}
return &TestGenericPLEG{pleg: pleg, runtime: fakeRuntime, clock: clock}
}
func getEventsFromChannel(ch <-chan *PodLifecycleEvent) []*PodLifecycleEvent {
events := []*PodLifecycleEvent{}
for len(ch) > 0 {
e := <-ch
events = append(events, e)
}
return events
}
func createTestContainer(ID string, state kubecontainer.ContainerState) *kubecontainer.Container {
return &kubecontainer.Container{
ID: kubecontainer.ContainerID{Type: testContainerRuntimeType, ID: ID},
State: state,
}
}
type sortableEvents []*PodLifecycleEvent
func (a sortableEvents) Len() int { return len(a) }
func (a sortableEvents) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortableEvents) Less(i, j int) bool {
if a[i].ID != a[j].ID {
return a[i].ID < a[j].ID
}
return a[i].Data.(string) < a[j].Data.(string)
}
func verifyEvents(t *testing.T, expected, actual []*PodLifecycleEvent) {
sort.Sort(sortableEvents(expected))
sort.Sort(sortableEvents(actual))
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Actual events differ from the expected; diff:\n %v", diff.ObjectDiff(expected, actual))
}
}
func TestRelisting(t *testing.T) {
testPleg := newTestGenericPLEG()
pleg, runtime := testPleg.pleg, testPleg.runtime
ch := pleg.Watch()
// The first relist should send a PodSync event to each pod.
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Containers: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateExited),
createTestContainer("c2", kubecontainer.ContainerStateRunning),
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
},
}},
{Pod: &kubecontainer.Pod{
ID: "4567",
Containers: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateExited),
},
}},
}
pleg.relist()
// Report every running/exited container if we see them for the first time.
expected := []*PodLifecycleEvent{
{ID: "1234", Type: ContainerStarted, Data: "c2"},
{ID: "4567", Type: ContainerDied, Data: "c1"},
{ID: "1234", Type: ContainerDied, Data: "c1"},
}
actual := getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
// The second relist should not send out any event because no container has
// changed.
pleg.relist()
verifyEvents(t, expected, actual)
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Containers: []*kubecontainer.Container{
createTestContainer("c2", kubecontainer.ContainerStateExited),
createTestContainer("c3", kubecontainer.ContainerStateRunning),
},
}},
{Pod: &kubecontainer.Pod{
ID: "4567",
Containers: []*kubecontainer.Container{
createTestContainer("c4", kubecontainer.ContainerStateRunning),
},
}},
}
pleg.relist()
// Only report containers that transitioned to running or exited status.
expected = []*PodLifecycleEvent{
{ID: "1234", Type: ContainerRemoved, Data: "c1"},
{ID: "1234", Type: ContainerDied, Data: "c2"},
{ID: "1234", Type: ContainerStarted, Data: "c3"},
{ID: "4567", Type: ContainerRemoved, Data: "c1"},
{ID: "4567", Type: ContainerStarted, Data: "c4"},
}
actual = getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
}
func TestDetectingContainerDeaths(t *testing.T) {
// Vary the number of relists after the container started and before the
// container died to account for the changes in pleg's internal states.
testReportMissingContainers(t, 1)
testReportMissingPods(t, 1)
testReportMissingContainers(t, 3)
testReportMissingPods(t, 3)
}
func testReportMissingContainers(t *testing.T, numRelists int) {
testPleg := newTestGenericPLEG()
pleg, runtime := testPleg.pleg, testPleg.runtime
ch := pleg.Watch()
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Containers: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateRunning),
createTestContainer("c2", kubecontainer.ContainerStateRunning),
createTestContainer("c3", kubecontainer.ContainerStateExited),
},
}},
}
// Relist and drain the events from the channel.
for i := 0; i < numRelists; i++ {
pleg.relist()
getEventsFromChannel(ch)
}
// Container c2 was stopped and removed between relists. We should report
// the event. The exited container c3 was garbage collected (i.e., removed)
// between relists. We should ignore that event.
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Containers: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateRunning),
},
}},
}
pleg.relist()
expected := []*PodLifecycleEvent{
{ID: "1234", Type: ContainerDied, Data: "c2"},
{ID: "1234", Type: ContainerRemoved, Data: "c2"},
{ID: "1234", Type: ContainerRemoved, Data: "c3"},
}
actual := getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
}
func testReportMissingPods(t *testing.T, numRelists int) {
testPleg := newTestGenericPLEG()
pleg, runtime := testPleg.pleg, testPleg.runtime
ch := pleg.Watch()
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Containers: []*kubecontainer.Container{
createTestContainer("c2", kubecontainer.ContainerStateRunning),
},
}},
}
// Relist and drain the events from the channel.
for i := 0; i < numRelists; i++ {
pleg.relist()
getEventsFromChannel(ch)
}
// Container c2 was stopped and removed between relists. We should report
// the event.
runtime.AllPodList = []*containertest.FakePod{}
pleg.relist()
expected := []*PodLifecycleEvent{
{ID: "1234", Type: ContainerDied, Data: "c2"},
{ID: "1234", Type: ContainerRemoved, Data: "c2"},
}
actual := getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
}
func newTestGenericPLEGWithRuntimeMock() (*GenericPLEG, *containertest.Mock) {
runtimeMock := &containertest.Mock{}
pleg := &GenericPLEG{
relistPeriod: time.Hour,
runtime: runtimeMock,
eventChannel: make(chan *PodLifecycleEvent, 100),
podRecords: make(podRecords),
cache: kubecontainer.NewCache(),
clock: clock.RealClock{},
}
return pleg, runtimeMock
}
func createTestPodsStatusesAndEvents(num int) ([]*kubecontainer.Pod, []*kubecontainer.PodStatus, []*PodLifecycleEvent) {
var pods []*kubecontainer.Pod
var statuses []*kubecontainer.PodStatus
var events []*PodLifecycleEvent
for i := 0; i < num; i++ {
id := types.UID(fmt.Sprintf("test-pod-%d", i))
cState := kubecontainer.ContainerStateRunning
container := createTestContainer(fmt.Sprintf("c%d", i), cState)
pod := &kubecontainer.Pod{
ID: id,
Containers: []*kubecontainer.Container{container},
}
status := &kubecontainer.PodStatus{
ID: id,
ContainerStatuses: []*kubecontainer.ContainerStatus{{ID: container.ID, State: cState}},
}
event := &PodLifecycleEvent{ID: pod.ID, Type: ContainerStarted, Data: container.ID.ID}
pods = append(pods, pod)
statuses = append(statuses, status)
events = append(events, event)
}
return pods, statuses, events
}
func TestRelistWithCache(t *testing.T) {
pleg, runtimeMock := newTestGenericPLEGWithRuntimeMock()
ch := pleg.Watch()
pods, statuses, events := createTestPodsStatusesAndEvents(2)
runtimeMock.On("GetPods", true).Return(pods, nil)
runtimeMock.On("GetPodStatus", pods[0].ID, "", "").Return(statuses[0], nil).Once()
// Inject an error when querying runtime for the pod status for pods[1].
statusErr := fmt.Errorf("unable to get status")
runtimeMock.On("GetPodStatus", pods[1].ID, "", "").Return(&kubecontainer.PodStatus{}, statusErr).Once()
pleg.relist()
actualEvents := getEventsFromChannel(ch)
cases := []struct {
pod *kubecontainer.Pod
status *kubecontainer.PodStatus
error error
}{
{pod: pods[0], status: statuses[0], error: nil},
{pod: pods[1], status: &kubecontainer.PodStatus{}, error: statusErr},
}
for i, c := range cases {
testStr := fmt.Sprintf("test[%d]", i)
actualStatus, actualErr := pleg.cache.Get(c.pod.ID)
assert.Equal(t, c.status, actualStatus, testStr)
assert.Equal(t, c.error, actualErr, testStr)
}
// pleg should not generate any event for pods[1] because of the error.
assert.Exactly(t, []*PodLifecycleEvent{events[0]}, actualEvents)
// Return normal status for pods[1].
runtimeMock.On("GetPodStatus", pods[1].ID, "", "").Return(statuses[1], nil).Once()
pleg.relist()
actualEvents = getEventsFromChannel(ch)
cases = []struct {
pod *kubecontainer.Pod
status *kubecontainer.PodStatus
error error
}{
{pod: pods[0], status: statuses[0], error: nil},
{pod: pods[1], status: statuses[1], error: nil},
}
for i, c := range cases {
testStr := fmt.Sprintf("test[%d]", i)
actualStatus, actualErr := pleg.cache.Get(c.pod.ID)
assert.Equal(t, c.status, actualStatus, testStr)
assert.Equal(t, c.error, actualErr, testStr)
}
// Now that we are able to query status for pods[1], pleg should generate an event.
assert.Exactly(t, []*PodLifecycleEvent{events[1]}, actualEvents)
}
func TestRemoveCacheEntry(t *testing.T) {
pleg, runtimeMock := newTestGenericPLEGWithRuntimeMock()
pods, statuses, _ := createTestPodsStatusesAndEvents(1)
runtimeMock.On("GetPods", true).Return(pods, nil).Once()
runtimeMock.On("GetPodStatus", pods[0].ID, "", "").Return(statuses[0], nil).Once()
// Does a relist to populate the cache.
pleg.relist()
// Delete the pod from runtime. Verify that the cache entry has been
// removed after relisting.
runtimeMock.On("GetPods", true).Return([]*kubecontainer.Pod{}, nil).Once()
pleg.relist()
actualStatus, actualErr := pleg.cache.Get(pods[0].ID)
assert.Equal(t, &kubecontainer.PodStatus{ID: pods[0].ID}, actualStatus)
assert.Equal(t, nil, actualErr)
}
func TestHealthy(t *testing.T) {
testPleg := newTestGenericPLEG()
pleg, _, clock := testPleg.pleg, testPleg.runtime, testPleg.clock
ok, _ := pleg.Healthy()
assert.True(t, ok, "pleg should be healthy")
// Advance the clock without any relisting.
clock.Step(time.Minute * 10)
ok, _ = pleg.Healthy()
assert.False(t, ok, "pleg should be unhealthy")
// Relist and than advance the time by 1 minute. pleg should be healthy
// because this is within the allowed limit.
pleg.relist()
clock.Step(time.Minute * 1)
ok, _ = pleg.Healthy()
assert.True(t, ok, "pleg should be healthy")
}
func TestRelistWithReinspection(t *testing.T) {
pleg, runtimeMock := newTestGenericPLEGWithRuntimeMock()
ch := pleg.Watch()
infraContainer := createTestContainer("infra", kubecontainer.ContainerStateRunning)
podID := types.UID("test-pod")
pods := []*kubecontainer.Pod{{
ID: podID,
Containers: []*kubecontainer.Container{infraContainer},
}}
runtimeMock.On("GetPods", true).Return(pods, nil).Once()
goodStatus := &kubecontainer.PodStatus{
ID: podID,
ContainerStatuses: []*kubecontainer.ContainerStatus{{ID: infraContainer.ID, State: infraContainer.State}},
}
runtimeMock.On("GetPodStatus", podID, "", "").Return(goodStatus, nil).Once()
goodEvent := &PodLifecycleEvent{ID: podID, Type: ContainerStarted, Data: infraContainer.ID.ID}
// listing 1 - everything ok, infra container set up for pod
pleg.relist()
actualEvents := getEventsFromChannel(ch)
actualStatus, actualErr := pleg.cache.Get(podID)
assert.Equal(t, goodStatus, actualStatus)
assert.Equal(t, nil, actualErr)
assert.Exactly(t, []*PodLifecycleEvent{goodEvent}, actualEvents)
// listing 2 - pretend runtime was in the middle of creating the non-infra container for the pod
// and return an error during inspection
transientContainer := createTestContainer("transient", kubecontainer.ContainerStateUnknown)
podsWithTransientContainer := []*kubecontainer.Pod{{
ID: podID,
Containers: []*kubecontainer.Container{infraContainer, transientContainer},
}}
runtimeMock.On("GetPods", true).Return(podsWithTransientContainer, nil).Once()
badStatus := &kubecontainer.PodStatus{
ID: podID,
ContainerStatuses: []*kubecontainer.ContainerStatus{},
}
runtimeMock.On("GetPodStatus", podID, "", "").Return(badStatus, errors.New("inspection error")).Once()
pleg.relist()
actualEvents = getEventsFromChannel(ch)
actualStatus, actualErr = pleg.cache.Get(podID)
assert.Equal(t, badStatus, actualStatus)
assert.Equal(t, errors.New("inspection error"), actualErr)
assert.Exactly(t, []*PodLifecycleEvent{}, actualEvents)
// listing 3 - pretend the transient container has now disappeared, leaving just the infra
// container. Make sure the pod is reinspected for its status and the cache is updated.
runtimeMock.On("GetPods", true).Return(pods, nil).Once()
runtimeMock.On("GetPodStatus", podID, "", "").Return(goodStatus, nil).Once()
pleg.relist()
actualEvents = getEventsFromChannel(ch)
actualStatus, actualErr = pleg.cache.Get(podID)
assert.Equal(t, goodStatus, actualStatus)
assert.Equal(t, nil, actualErr)
// no events are expected because relist #1 set the old pod record which has the infra container
// running. relist #2 had the inspection error and therefore didn't modify either old or new.
// relist #3 forced the reinspection of the pod to retrieve its status, but because the list of
// containers was the same as relist #1, nothing "changed", so there are no new events.
assert.Exactly(t, []*PodLifecycleEvent{}, actualEvents)
}
// Test detecting sandbox state changes.
func TestRelistingWithSandboxes(t *testing.T) {
testPleg := newTestGenericPLEG()
pleg, runtime := testPleg.pleg, testPleg.runtime
ch := pleg.Watch()
// The first relist should send a PodSync event to each pod.
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Sandboxes: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateExited),
createTestContainer("c2", kubecontainer.ContainerStateRunning),
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
},
}},
{Pod: &kubecontainer.Pod{
ID: "4567",
Sandboxes: []*kubecontainer.Container{
createTestContainer("c1", kubecontainer.ContainerStateExited),
},
}},
}
pleg.relist()
// Report every running/exited container if we see them for the first time.
expected := []*PodLifecycleEvent{
{ID: "1234", Type: ContainerStarted, Data: "c2"},
{ID: "4567", Type: ContainerDied, Data: "c1"},
{ID: "1234", Type: ContainerDied, Data: "c1"},
}
actual := getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
// The second relist should not send out any event because no container has
// changed.
pleg.relist()
verifyEvents(t, expected, actual)
runtime.AllPodList = []*containertest.FakePod{
{Pod: &kubecontainer.Pod{
ID: "1234",
Sandboxes: []*kubecontainer.Container{
createTestContainer("c2", kubecontainer.ContainerStateExited),
createTestContainer("c3", kubecontainer.ContainerStateRunning),
},
}},
{Pod: &kubecontainer.Pod{
ID: "4567",
Sandboxes: []*kubecontainer.Container{
createTestContainer("c4", kubecontainer.ContainerStateRunning),
},
}},
}
pleg.relist()
// Only report containers that transitioned to running or exited status.
expected = []*PodLifecycleEvent{
{ID: "1234", Type: ContainerRemoved, Data: "c1"},
{ID: "1234", Type: ContainerDied, Data: "c2"},
{ID: "1234", Type: ContainerStarted, Data: "c3"},
{ID: "4567", Type: ContainerRemoved, Data: "c1"},
{ID: "4567", Type: ContainerStarted, Data: "c4"},
}
actual = getEventsFromChannel(ch)
verifyEvents(t, expected, actual)
}