1 package agent_test
2
3 import (
4 "testing"
5
6 "github.com/datawire/ambassador/v2/pkg/agent"
7 "github.com/datawire/ambassador/v2/pkg/kates"
8 "github.com/stretchr/testify/assert"
9 v1 "k8s.io/api/core/v1"
10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11 )
12
13 func TestCoreStore(t *testing.T) {
14 type testCases struct {
15 name string
16 getPods func() []*kates.Pod
17 expectedPods int
18 getConfigMaps func() []*kates.ConfigMap
19 expectedConfigMaps int
20 getDeployments func() []*kates.Deployment
21 expectedDeployments int
22 getEndpoints func() []*kates.Endpoints
23 expectedEndpoints int
24 }
25 cases := []*testCases{
26 {
27 name: "will add running endpoints to state of the world",
28 getEndpoints: func() []*kates.Endpoints {
29 return []*kates.Endpoints{
30 {
31 TypeMeta: metav1.TypeMeta{
32 Kind: "Endpoints",
33 APIVersion: "v1",
34 },
35 ObjectMeta: metav1.ObjectMeta{
36 Name: "some-endpoint",
37 Namespace: "default",
38 },
39 },
40 }
41 },
42 expectedEndpoints: 1,
43 },
44 {
45 name: "will add running pods to state of the world",
46 getPods: func() []*kates.Pod {
47 return []*kates.Pod{
48 {
49 TypeMeta: metav1.TypeMeta{
50 Kind: "Pod",
51 APIVersion: "v1",
52 },
53 ObjectMeta: metav1.ObjectMeta{
54 Name: "some-pod",
55 Namespace: "default",
56 },
57 Status: v1.PodStatus{
58 Phase: v1.PodRunning,
59 },
60 },
61 }
62 },
63 expectedPods: 1,
64 },
65 {
66 name: "will ensure no duplicate pods are added",
67 getPods: func() []*kates.Pod {
68 return []*kates.Pod{
69 {
70 TypeMeta: metav1.TypeMeta{
71 Kind: "Pod",
72 APIVersion: "v1",
73 },
74 ObjectMeta: metav1.ObjectMeta{
75 Name: "some-pod",
76 Namespace: "default",
77 },
78 Status: v1.PodStatus{
79 Phase: v1.PodRunning,
80 },
81 },
82 {
83 TypeMeta: metav1.TypeMeta{
84 Kind: "Pod",
85 APIVersion: "v1",
86 },
87 ObjectMeta: metav1.ObjectMeta{
88 Name: "some-pod",
89 Namespace: "default",
90 },
91 Status: v1.PodStatus{
92 Phase: v1.PodRunning,
93 },
94 },
95 }
96 },
97 expectedPods: 1,
98 },
99 {
100 name: "will exclude kube-system pods from state of the world",
101 getPods: func() []*kates.Pod {
102 return []*kates.Pod{
103 {
104 TypeMeta: metav1.TypeMeta{
105 Kind: "Pod",
106 APIVersion: "v1",
107 },
108 ObjectMeta: metav1.ObjectMeta{
109 Name: "some-pod",
110 Namespace: "kube-system",
111 },
112 Status: v1.PodStatus{
113 Phase: v1.PodRunning,
114 },
115 },
116 }
117 },
118 expectedPods: 0,
119 },
120 {
121 name: "will exclude non running pods from state of the world",
122 getPods: func() []*kates.Pod {
123 return []*kates.Pod{
124 {
125 TypeMeta: metav1.TypeMeta{
126 Kind: "Pod",
127 APIVersion: "v1",
128 },
129 ObjectMeta: metav1.ObjectMeta{
130 Name: "some-pod",
131 Namespace: "defautl",
132 },
133 Status: v1.PodStatus{
134 Phase: v1.PodSucceeded,
135 },
136 },
137 }
138 },
139 expectedPods: 0,
140 },
141 {
142 name: "will send pods in failed state",
143 getPods: func() []*kates.Pod {
144 return []*kates.Pod{
145 {
146 TypeMeta: metav1.TypeMeta{
147 Kind: "Pod",
148 APIVersion: "v1",
149 },
150 ObjectMeta: metav1.ObjectMeta{
151 Name: "some-pod",
152 Namespace: "defautl",
153 },
154 Status: v1.PodStatus{
155 Phase: v1.PodFailed,
156 },
157 },
158 }
159 },
160 expectedPods: 1,
161 },
162 {
163 name: "will add configmaps to the configmapStore successfully",
164 getConfigMaps: func() []*kates.ConfigMap {
165 return []*kates.ConfigMap{
166 {
167 TypeMeta: metav1.TypeMeta{
168 Kind: "ConfigMap",
169 APIVersion: "",
170 },
171 ObjectMeta: metav1.ObjectMeta{
172 Name: "some-config-map",
173 Namespace: "default",
174 },
175 },
176 }
177 },
178 expectedConfigMaps: 1,
179 },
180 {
181 name: "will ensure no duplicated configmaps are added",
182 getConfigMaps: func() []*kates.ConfigMap {
183 return []*kates.ConfigMap{
184 {
185 TypeMeta: metav1.TypeMeta{
186 Kind: "ConfigMap",
187 APIVersion: "",
188 },
189 ObjectMeta: metav1.ObjectMeta{
190 Name: "some-config-map",
191 Namespace: "default",
192 },
193 },
194 {
195 TypeMeta: metav1.TypeMeta{
196 Kind: "ConfigMap",
197 APIVersion: "",
198 },
199 ObjectMeta: metav1.ObjectMeta{
200 Name: "some-config-map",
201 Namespace: "default",
202 },
203 },
204 }
205 },
206 expectedConfigMaps: 1,
207 },
208 {
209 name: "will exclude configmaps from kube-system",
210 getConfigMaps: func() []*kates.ConfigMap {
211 return []*kates.ConfigMap{
212 {
213 TypeMeta: metav1.TypeMeta{
214 Kind: "ConfigMap",
215 APIVersion: "",
216 },
217 ObjectMeta: metav1.ObjectMeta{
218 Name: "some-config-map",
219 Namespace: "kube-system",
220 },
221 },
222 }
223 },
224 expectedConfigMaps: 0,
225 },
226 {
227 name: "will add Deployments to state of the world",
228 getDeployments: func() []*kates.Deployment {
229 return []*kates.Deployment{
230 {
231 TypeMeta: metav1.TypeMeta{
232 Kind: "Deployment",
233 APIVersion: "apps/v1",
234 },
235 ObjectMeta: metav1.ObjectMeta{
236 Name: "some-deployment",
237 Namespace: "default",
238 },
239 },
240 }
241 },
242 expectedDeployments: 1,
243 },
244 {
245 name: "will ensure no duplicated deployments are added",
246 getDeployments: func() []*kates.Deployment {
247 return []*kates.Deployment{
248 {
249 TypeMeta: metav1.TypeMeta{
250 Kind: "Deployment",
251 APIVersion: "apps/v1",
252 },
253 ObjectMeta: metav1.ObjectMeta{
254 Name: "some-deployment",
255 Namespace: "default",
256 },
257 },
258 {
259 TypeMeta: metav1.TypeMeta{
260 Kind: "Deployment",
261 APIVersion: "apps/v1",
262 },
263 ObjectMeta: metav1.ObjectMeta{
264 Name: "some-deployment",
265 Namespace: "default",
266 },
267 },
268 }
269 },
270 expectedDeployments: 1,
271 },
272 {
273 name: "will exclude kube-system Deployments from state of the world",
274 getDeployments: func() []*kates.Deployment {
275 return []*kates.Deployment{
276 {
277 TypeMeta: metav1.TypeMeta{
278 Kind: "Deployment",
279 APIVersion: "apps/v1",
280 },
281 ObjectMeta: metav1.ObjectMeta{
282 Name: "some-deployment",
283 Namespace: "kube-system",
284 },
285 },
286 }
287 },
288 expectedDeployments: 0,
289 },
290 }
291 for _, c := range cases {
292 c := c
293 t.Run(c.name, func(t *testing.T) {
294 t.Parallel()
295 if c.getEndpoints != nil {
296 endpoints := c.getEndpoints()
297 endpointStore := agent.NewEndpointsStore(endpoints)
298 epSOW := endpointStore.StateOfWorld()
299 assert.Equal(t, c.expectedEndpoints, len(epSOW))
300 }
301 if c.getPods != nil {
302 pods := c.getPods()
303 podStore := agent.NewPodStore(pods)
304 podSOTW := podStore.StateOfWorld()
305 if c.expectedPods != len(podSOTW) {
306 t.Errorf("error: expected %d pods but found %d", c.expectedPods, len(podSOTW))
307 }
308 }
309 if c.getConfigMaps != nil {
310 cms := c.getConfigMaps()
311 cmStore := agent.NewConfigMapStore(cms)
312 cmSOTW := cmStore.StateOfWorld()
313 if c.expectedConfigMaps != len(cmSOTW) {
314 t.Errorf("error: expected %d configmaps found %d", c.expectedConfigMaps, len(cmSOTW))
315 }
316 }
317 if c.getDeployments != nil {
318 dep := c.getDeployments()
319 depStore := agent.NewDeploymentStore(dep)
320 depSOTW := depStore.StateOfWorld()
321 if c.expectedDeployments != len(depSOTW) {
322 t.Errorf("error: expected %d deployments found %d", c.expectedDeployments, len(depSOTW))
323 }
324 }
325 })
326 }
327 }
328
View as plain text