1
16
17 package config
18
19 import (
20 "testing"
21
22 "k8s.io/api/core/v1"
23 apiequality "k8s.io/apimachinery/pkg/api/equality"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/runtime"
26 "k8s.io/apimachinery/pkg/watch"
27 "k8s.io/client-go/tools/cache"
28 kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
29 )
30
31 type fakePodLW struct {
32 listResp runtime.Object
33 watchResp watch.Interface
34 }
35
36 func (lw fakePodLW) List(options metav1.ListOptions) (runtime.Object, error) {
37 return lw.listResp, nil
38 }
39
40 func (lw fakePodLW) Watch(options metav1.ListOptions) (watch.Interface, error) {
41 return lw.watchResp, nil
42 }
43
44 var _ cache.ListerWatcher = fakePodLW{}
45
46 func TestNewSourceApiserver_UpdatesAndMultiplePods(t *testing.T) {
47 pod1v1 := &v1.Pod{
48 ObjectMeta: metav1.ObjectMeta{Name: "p"},
49 Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/one"}}}}
50 pod1v2 := &v1.Pod{
51 ObjectMeta: metav1.ObjectMeta{Name: "p"},
52 Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/two"}}}}
53 pod2 := &v1.Pod{
54 ObjectMeta: metav1.ObjectMeta{Name: "q"},
55 Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/blah"}}}}
56
57
58 fakeWatch := watch.NewFake()
59 lw := fakePodLW{
60 listResp: &v1.PodList{Items: []v1.Pod{*pod1v1}},
61 watchResp: fakeWatch,
62 }
63
64 ch := make(chan interface{})
65
66 newSourceApiserverFromLW(lw, ch)
67
68 got, ok := <-ch
69 if !ok {
70 t.Errorf("Unable to read from channel when expected")
71 }
72 update := got.(kubetypes.PodUpdate)
73 expected := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v1)
74 if !apiequality.Semantic.DeepEqual(expected, update) {
75 t.Errorf("Expected %#v; Got %#v", expected, update)
76 }
77
78
79 fakeWatch.Add(pod2)
80 got, ok = <-ch
81 if !ok {
82 t.Errorf("Unable to read from channel when expected")
83 }
84 update = got.(kubetypes.PodUpdate)
85
86 expectedA := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v1, pod2)
87 expectedB := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2, pod1v1)
88
89 if !apiequality.Semantic.DeepEqual(expectedA, update) && !apiequality.Semantic.DeepEqual(expectedB, update) {
90 t.Errorf("Expected %#v or %#v, Got %#v", expectedA, expectedB, update)
91 }
92
93
94 fakeWatch.Modify(pod1v2)
95 got, ok = <-ch
96 if !ok {
97 t.Errorf("Unable to read from channel when expected")
98 }
99 update = got.(kubetypes.PodUpdate)
100 expectedA = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v2, pod2)
101 expectedB = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2, pod1v2)
102
103 if !apiequality.Semantic.DeepEqual(expectedA, update) && !apiequality.Semantic.DeepEqual(expectedB, update) {
104 t.Errorf("Expected %#v or %#v, Got %#v", expectedA, expectedB, update)
105 }
106
107
108 fakeWatch.Delete(pod1v2)
109 got, ok = <-ch
110 if !ok {
111 t.Errorf("Unable to read from channel when expected")
112 }
113 update = got.(kubetypes.PodUpdate)
114 expected = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2)
115 if !apiequality.Semantic.DeepEqual(expected, update) {
116 t.Errorf("Expected %#v, Got %#v", expected, update)
117 }
118
119
120 fakeWatch.Delete(pod2)
121 got, ok = <-ch
122 if !ok {
123 t.Errorf("Unable to read from channel when expected")
124 }
125 update = got.(kubetypes.PodUpdate)
126 expected = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource)
127 if !apiequality.Semantic.DeepEqual(expected, update) {
128 t.Errorf("Expected %#v, Got %#v", expected, update)
129 }
130 }
131
132 func TestNewSourceApiserver_TwoNamespacesSameName(t *testing.T) {
133 pod1 := v1.Pod{
134 ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "one"},
135 Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/one"}}}}
136 pod2 := v1.Pod{
137 ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "two"},
138 Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/blah"}}}}
139
140
141 fakeWatch := watch.NewFake()
142 lw := fakePodLW{
143 listResp: &v1.PodList{Items: []v1.Pod{pod1, pod2}},
144 watchResp: fakeWatch,
145 }
146
147 ch := make(chan interface{})
148
149 newSourceApiserverFromLW(lw, ch)
150
151 got, ok := <-ch
152 if !ok {
153 t.Errorf("Unable to read from channel when expected")
154 }
155 update := got.(kubetypes.PodUpdate)
156
157 if !(len(update.Pods) == 2) {
158 t.Errorf("Expected %d, Got %d", 2, len(update.Pods))
159 }
160
161
162 fakeWatch.Delete(&pod1)
163 got, ok = <-ch
164 if !ok {
165 t.Errorf("Unable to read from channel when expected")
166 }
167 update = got.(kubetypes.PodUpdate)
168 if !(len(update.Pods) == 1) {
169 t.Errorf("Expected %d, Got %d", 1, len(update.Pods))
170 }
171 }
172
173 func TestNewSourceApiserverInitialEmptySendsEmptyPodUpdate(t *testing.T) {
174
175 fakeWatch := watch.NewFake()
176 lw := fakePodLW{
177 listResp: &v1.PodList{Items: []v1.Pod{}},
178 watchResp: fakeWatch,
179 }
180
181 ch := make(chan interface{})
182
183 newSourceApiserverFromLW(lw, ch)
184
185 got, ok := <-ch
186 if !ok {
187 t.Errorf("Unable to read from channel when expected")
188 }
189 update := got.(kubetypes.PodUpdate)
190 expected := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource)
191 if !apiequality.Semantic.DeepEqual(expected, update) {
192 t.Errorf("Expected %#v; Got %#v", expected, update)
193 }
194 }
195
View as plain text