Debian Codegraph @

pkg/server/backend_manager_test.go

raw symbols SCIP
1 /*
2 Copyright 2020 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package server
18
19 import (
20 "context"
21 "fmt"
22 "reflect"
23 "testing"
24
25 "github.com/stretchr/testify/assert"
26 "go.uber.org/mock/gomock"
27 "google.golang.org/grpc/metadata"
28
29 agentmock "sigs.k8s.io/apiserver-network-proxy/proto/agent/mocks"
30 )
31
32 func mockAgentConn(ctrl *gomock.Controller, agentID string, agentIdentifiers []string) *agentmock.MockAgentService_ConnectServer {
33 agentConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
34 agentConnMD := metadata.MD{
35 ":authority": []string{"127.0.0.1:8091"},
36 "agentid": []string{agentID},
37 "agentidentifiers": agentIdentifiers,
38 "content-type": []string{"application/grpc"},
39 "user-agent": []string{"grpc-go/1.42.0"},
40 }
41 agentConnCtx := metadata.NewIncomingContext(context.Background(), agentConnMD)
42 agentConn.EXPECT().Context().Return(agentConnCtx).AnyTimes()
43 return agentConn
44 }
45
46 func TestNewBackend(t *testing.T) {
47 ctrl := gomock.NewController(t)
48 defer ctrl.Finish()
49
50 testCases := []struct {
51 desc string
52 ids []string
53 idents []string
54 wantErr bool
55 }{
56 {
57 desc: "no agentID",
58 wantErr: true,
59 },
60 {
61 desc: "multiple agentID",
62 ids: []string{"agent-id", "agent-id"},
63 wantErr: true,
64 },
65 {
66 desc: "multiple identifiers",
67 ids: []string{"agent-id"},
68 idents: []string{"host=localhost", "host=localhost"},
69 wantErr: true,
70 },
71 {
72 desc: "invalid identifiers",
73 ids: []string{"agent-id"},
74 idents: []string{";"},
75 wantErr: true,
76 },
77 {
78 desc: "success",
79 ids: []string{"agent-id"},
80 },
81 {
82 desc: "success with identifiers",
83 ids: []string{"agent-id"},
84 idents: []string{"host=localhost&host=node1.mydomain.com&cidr=127.0.0.1/16&ipv4=1.2.3.4&ipv4=5.6.7.8&ipv6=:::::&default-route=true"},
85 },
86 }
87
88 for _, tc := range testCases {
89 t.Run(tc.desc, func(t *testing.T) {
90
91 agentConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
92 agentConnMD := metadata.MD{
93 ":authority": []string{"127.0.0.1:8091"},
94 "agentid": tc.ids,
95 "agentidentifiers": tc.idents,
96 "content-type": []string{"application/grpc"},
97 "user-agent": []string{"grpc-go/1.42.0"},
98 }
99 agentConnCtx := metadata.NewIncomingContext(context.Background(), agentConnMD)
100 agentConn.EXPECT().Context().Return(agentConnCtx).AnyTimes()
101
102 _, err := NewBackend(agentConn)
103 if gotErr := (err != nil); gotErr != tc.wantErr {
104 t.Errorf("NewBackend got err %q; wantErr = %t", err, tc.wantErr)
105 }
106 })
107 }
108 }
109
110 func TestDefaultBackendManager_AddRemoveBackends(t *testing.T) {
111 ctrl := gomock.NewController(t)
112 defer ctrl.Finish()
113
114 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{}))
115 backend12, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{}))
116 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{}))
117 backend22, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{}))
118 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{}))
119
120 p := NewDefaultBackendManager()
121
122 p.AddBackend(backend1)
123 p.RemoveBackend(backend1)
124 expectedBackends := make(map[string][]*Backend)
125 expectedAgentIDs := []string{}
126 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
127 t.Errorf("expected %v, got %v", e, a)
128 }
129 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
130 t.Errorf("expected %v, got %v", e, a)
131 }
132
133 p = NewDefaultBackendManager()
134 p.AddBackend(backend1)
135 p.AddBackend(backend12)
136 // Adding the same connection again should be a no-op.
137 p.AddBackend(backend12)
138 p.AddBackend(backend2)
139 p.AddBackend(backend22)
140 p.AddBackend(backend3)
141 p.RemoveBackend(backend22)
142 p.RemoveBackend(backend2)
143 p.RemoveBackend(backend1)
144 expectedBackends = map[string][]*Backend{
145 "agent1": {backend12},
146 "agent3": {backend3},
147 }
148 expectedAgentIDs = []string{"agent1", "agent3"}
149 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
150 t.Errorf("expected %v, got %v", e, a)
151 }
152 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
153 t.Errorf("expected %v, got %v", e, a)
154 }
155 }
156
157 func TestDefaultRouteBackendManager_AddRemoveBackends(t *testing.T) {
158 ctrl := gomock.NewController(t)
159 defer ctrl.Finish()
160
161 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"default-route=true"}))
162 backend12, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"default-route=true"}))
163 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
164 backend22, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
165 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"default-route=true"}))
166
167 p := NewDefaultRouteBackendManager()
168
169 p.AddBackend(backend1)
170 p.RemoveBackend(backend1)
171 expectedBackends := make(map[string][]*Backend)
172 expectedAgentIDs := []string{}
173 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
174 t.Errorf("expected %v, got %v", e, a)
175 }
176 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
177 t.Errorf("expected %v, got %v", e, a)
178 }
179
180 p = NewDefaultRouteBackendManager()
181 p.AddBackend(backend1)
182 p.AddBackend(backend12)
183 // Adding the same connection again should be a no-op.
184 p.AddBackend(backend12)
185 p.AddBackend(backend2)
186 p.AddBackend(backend22)
187 p.AddBackend(backend3)
188 p.RemoveBackend(backend22)
189 p.RemoveBackend(backend2)
190 p.RemoveBackend(backend1)
191
192 expectedBackends = map[string][]*Backend{
193 "agent1": {backend12},
194 "agent3": {backend3},
195 }
196 expectedAgentIDs = []string{"agent1", "agent3"}
197
198 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
199 t.Errorf("expected %v, got %v", e, a)
200 }
201 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
202 t.Errorf("expected %v, got %v", e, a)
203 }
204 }
205
206 func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
207 ctrl := gomock.NewController(t)
208 defer ctrl.Finish()
209
210 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
211 // backend2 has no desthost relevant identifiers
212 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
213 // TODO: if backend3 is given conflicting identifiers with backend1, the wrong thing happens in RemoveBackend.
214 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"host=node2.mydomain.com&ipv4=5.6.7.8&ipv6=::"}))
215
216 p := NewDestHostBackendManager()
217
218 p.AddBackend(backend1)
219 p.RemoveBackend(backend1)
220 expectedBackends := make(map[string][]*Backend)
221 expectedAgentIDs := []string{}
222 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
223 t.Errorf("expected %v, got %v", e, a)
224 }
225 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
226 t.Errorf("expected %v, got %v", e, a)
227 }
228
229 p = NewDestHostBackendManager()
230 p.AddBackend(backend1)
231
232 expectedBackends = map[string][]*Backend{
233 "localhost": {backend1},
234 "1.2.3.4": {backend1},
235 "9878::7675:1292:9183:7562": {backend1},
236 "node1.mydomain.com": {backend1},
237 }
238 expectedAgentIDs = []string{
239 "1.2.3.4",
240 "9878::7675:1292:9183:7562",
241 "localhost",
242 "node1.mydomain.com",
243 }
244
245 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
246 t.Errorf("expected %v, got %v", e, a)
247 }
248 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
249 t.Errorf("expected %v, got %v", e, a)
250 }
251
252 p.AddBackend(backend2)
253 p.AddBackend(backend3)
254
255 expectedBackends = map[string][]*Backend{
256 "localhost": {backend1},
257 "node1.mydomain.com": {backend1},
258 "node2.mydomain.com": {backend3},
259 "1.2.3.4": {backend1},
260 "5.6.7.8": {backend3},
261 "9878::7675:1292:9183:7562": {backend1},
262 "::": {backend3},
263 }
264 expectedAgentIDs = []string{
265 "1.2.3.4",
266 "9878::7675:1292:9183:7562",
267 "localhost",
268 "node1.mydomain.com",
269 "5.6.7.8",
270 "::",
271 "node2.mydomain.com",
272 }
273
274 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
275 t.Errorf("expected %v, got %v", e, a)
276 }
277 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
278 t.Errorf("expected %v, got %v", e, a)
279 }
280
281 p.RemoveBackend(backend2)
282 p.RemoveBackend(backend1)
283
284 expectedBackends = map[string][]*Backend{
285 "node2.mydomain.com": {backend3},
286 "5.6.7.8": {backend3},
287 "::": {backend3},
288 }
289 expectedAgentIDs = []string{
290 "node2.mydomain.com",
291 "::",
292 "5.6.7.8",
293 }
294
295 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
296 t.Errorf("expected %v, got %v", e, a)
297 }
298 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
299 t.Errorf("expected %v, got %v", e, a)
300 }
301
302 p.RemoveBackend(backend3)
303 expectedBackends = map[string][]*Backend{}
304 expectedAgentIDs = []string{}
305
306 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
307 t.Errorf("expected %v, got %v", e, a)
308 }
309 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
310 t.Errorf("expected %v, got %v", e, a)
311 }
312 }
313
314 func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
315 ctrl := gomock.NewController(t)
316 defer ctrl.Finish()
317
318 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
319 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
320 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"host=localhost&host=node2.mydomain.com&ipv4=5.6.7.8&ipv6=::"}))
321
322 p := NewDestHostBackendManager()
323
324 p.AddBackend(backend1)
325 p.AddBackend(backend2)
326 p.AddBackend(backend3)
327
328 expectedBackends := map[string][]*Backend{
329 "localhost": {backend1, backend2, backend3},
330 "1.2.3.4": {backend1, backend2},
331 "5.6.7.8": {backend3},
332 "9878::7675:1292:9183:7562": {backend1, backend2},
333 "::": {backend3},
334 "node1.mydomain.com": {backend1, backend2},
335 "node2.mydomain.com": {backend3},
336 }
337 expectedAgentIDs := []string{
338 "1.2.3.4",
339 "9878::7675:1292:9183:7562",
340 "localhost",
341 "node1.mydomain.com",
342 "5.6.7.8",
343 "::",
344 "node2.mydomain.com",
345 }
346
347 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
348 t.Errorf("expected %v, got %v", e, a)
349 }
350 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
351 t.Errorf("expected %v, got %v", e, a)
352 }
353
354 p.RemoveBackend(backend1)
355 p.RemoveBackend(backend3)
356
357 expectedBackends = map[string][]*Backend{
358 "localhost": {backend2},
359 "1.2.3.4": {backend2},
360 "9878::7675:1292:9183:7562": {backend2},
361 "node1.mydomain.com": {backend2},
362 }
363 expectedAgentIDs = []string{
364 "1.2.3.4",
365 "9878::7675:1292:9183:7562",
366 "localhost",
367 "node1.mydomain.com",
368 }
369
370 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
371 t.Errorf("expected %v, got %v", e, a)
372 }
373 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
374 t.Errorf("expected %v, got %v", e, a)
375 }
376
377 p.RemoveBackend(backend2)
378 expectedBackends = map[string][]*Backend{}
379 expectedAgentIDs = []string{}
380
381 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
382 t.Errorf("expected %v, got %v", e, a)
383 }
384 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
385 t.Errorf("expected %v, got %v", e, a)
386 }
387 }
388
389 func TestProxyStrategy(t *testing.T) {
390 for desc, tc := range map[string]struct {
391 input ProxyStrategy
392 want string
393 wantPanic string
394 }{
395 "default": {
396 input: ProxyStrategyDefault,
397 want: "default",
398 },
399 "destHost": {
400 input: ProxyStrategyDestHost,
401 want: "destHost",
402 },
403 "defaultRoute": {
404 input: ProxyStrategyDefaultRoute,
405 want: "defaultRoute",
406 },
407 "unrecognized": {
408 input: ProxyStrategy(0),
409 wantPanic: "unhandled ProxyStrategy: 0",
410 },
411 } {
412 t.Run(desc, func(t *testing.T) {
413 if tc.wantPanic != "" {
414 assert.PanicsWithValue(t, tc.wantPanic, func() {
415 _ = tc.input.String()
416 })
417 } else {
418 got := tc.input.String()
419 if got != tc.want {
420 t.Errorf("ProxyStrategy.String(): got %v, want %v", got, tc.want)
421 }
422 }
423 })
424 }
425 }
426
427 func TestParseProxyStrategy(t *testing.T) {
428 for desc, tc := range map[string]struct {
429 input string
430 want ProxyStrategy
431 wantErr error
432 }{
433 "empty": {
434 input: "",
435 wantErr: fmt.Errorf("unknown proxy strategy: "),
436 },
437 "unrecognized": {
438 input: "unrecognized",
439 wantErr: fmt.Errorf("unknown proxy strategy: unrecognized"),
440 },
441 "default": {
442 input: "default",
443 want: ProxyStrategyDefault,
444 },
445 "destHost": {
446 input: "destHost",
447 want: ProxyStrategyDestHost,
448 },
449 "defaultRoute": {
450 input: "defaultRoute",
451 want: ProxyStrategyDefaultRoute,
452 },
453 } {
454 t.Run(desc, func(t *testing.T) {
455 got, err := ParseProxyStrategy(tc.input)
456 assert.Equal(t, tc.wantErr, err, "ParseProxyStrategy(%s): got error %q, want %v", tc.input, err, tc.wantErr)
457 if got != tc.want {
458 t.Errorf("ParseProxyStrategy(%s): got %v, want %v", tc.input, got, tc.want)
459 }
460 })
461 }
462 }
463
464 func TestParseProxyStrategies(t *testing.T) {
465 for desc, tc := range map[string]struct {
466 input string
467 want []ProxyStrategy
468 wantErr error
469 }{
470 "empty": {
471 input: "",
472 wantErr: fmt.Errorf("proxy strategies cannot be empty"),
473 },
474 "unrecognized": {
475 input: "unrecognized",
476 wantErr: fmt.Errorf("unknown proxy strategy: unrecognized"),
477 },
478 "default": {
479 input: "default",
480 want: []ProxyStrategy{ProxyStrategyDefault},
481 },
482 "destHost": {
483 input: "destHost",
484 want: []ProxyStrategy{ProxyStrategyDestHost},
485 },
486 "defaultRoute": {
487 input: "defaultRoute",
488 want: []ProxyStrategy{ProxyStrategyDefaultRoute},
489 },
490 "duplicate": {
491 input: "destHost,defaultRoute,defaultRoute,default",
492 want: []ProxyStrategy{ProxyStrategyDestHost, ProxyStrategyDefaultRoute, ProxyStrategyDefaultRoute, ProxyStrategyDefault},
493 },
494 "multiple": {
495 input: "destHost,defaultRoute,default",
496 want: []ProxyStrategy{ProxyStrategyDestHost, ProxyStrategyDefaultRoute, ProxyStrategyDefault},
497 },
498 } {
499 t.Run(desc, func(t *testing.T) {
500 got, err := ParseProxyStrategies(tc.input)
501 assert.Equal(t, tc.wantErr, err, "ParseProxyStrategies(%s): got error %q, want %v", tc.input, err, tc.wantErr)
502 if !reflect.DeepEqual(got, tc.want) {
503 t.Errorf("ParseProxyStrategies(%s): got %v, want %v", tc.input, got, tc.want)
504 }
505 })
506 }
507 }