...
1
16
17 package container_test
18
19 import (
20 "context"
21 "reflect"
22 "testing"
23 "time"
24
25 . "k8s.io/kubernetes/pkg/kubelet/container"
26 ctest "k8s.io/kubernetes/pkg/kubelet/container/testing"
27 )
28
29 func comparePods(t *testing.T, expected []*ctest.FakePod, actual []*Pod) {
30 if len(expected) != len(actual) {
31 t.Errorf("expected %d pods, got %d instead", len(expected), len(actual))
32 }
33 for i := range expected {
34 if !reflect.DeepEqual(expected[i].Pod, actual[i]) {
35 t.Errorf("expected %#v, got %#v", expected[i].Pod, actual[i])
36 }
37 }
38 }
39
40 func TestGetPods(t *testing.T) {
41 ctx := context.Background()
42 runtime := &ctest.FakeRuntime{}
43 expected := []*ctest.FakePod{{Pod: &Pod{ID: "1111"}}, {Pod: &Pod{ID: "2222"}}, {Pod: &Pod{ID: "3333"}}}
44 runtime.PodList = expected
45 cache := NewTestRuntimeCache(runtime)
46 actual, err := cache.GetPods(ctx)
47 if err != nil {
48 t.Errorf("unexpected error %v", err)
49 }
50
51 comparePods(t, expected, actual)
52 }
53
54 func TestForceUpdateIfOlder(t *testing.T) {
55 ctx := context.Background()
56 runtime := &ctest.FakeRuntime{}
57 cache := NewTestRuntimeCache(runtime)
58
59
60 oldpods := []*ctest.FakePod{{Pod: &Pod{ID: "1111"}}}
61 runtime.PodList = oldpods
62 cache.UpdateCacheWithLock()
63
64
65 newpods := []*ctest.FakePod{{Pod: &Pod{ID: "1111"}}, {Pod: &Pod{ID: "2222"}}, {Pod: &Pod{ID: "3333"}}}
66 runtime.PodList = newpods
67
68
69 cache.ForceUpdateIfOlder(ctx, time.Now().Add(-20*time.Minute))
70 actual := cache.GetCachedPods()
71 comparePods(t, oldpods, actual)
72
73
74 cache.ForceUpdateIfOlder(ctx, time.Now().Add(20*time.Second))
75 actual = cache.GetCachedPods()
76 comparePods(t, newpods, actual)
77 }
78
View as plain text