Debian Codegraph @

pkg/server/backend_manager_test.go

raw
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 "reflect"
22 "testing"
23
24 "go.uber.org/mock/gomock"
25 "google.golang.org/grpc/metadata"
26
27 agentmock "sigs.k8s.io/apiserver-network-proxy/proto/agent/mocks"
28 )
29
30 func mockAgentConn(ctrl *gomock.Controller, agentID string, agentIdentifiers []string) *agentmock.MockAgentService_ConnectServer {
31 agentConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
32 agentConnMD := metadata.MD{
33 ":authority": []string{"127.0.0.1:8091"},
34 "agentid": []string{agentID},
35 "agentidentifiers": agentIdentifiers,
36 "content-type": []string{"application/grpc"},
37 "user-agent": []string{"grpc-go/1.42.0"},
38 }
39 agentConnCtx := metadata.NewIncomingContext(context.Background(), agentConnMD)
40 agentConn.EXPECT().Context().Return(agentConnCtx).AnyTimes()
41 return agentConn
42 }
43
44 func TestNewBackend(t *testing.T) {
45 ctrl := gomock.NewController(t)
46 defer ctrl.Finish()
47
48 testCases := []struct {
49 desc string
50 ids []string
51 idents []string
52 wantErr bool
53 }{
54 {
55 desc: "no agentID",
56 wantErr: true,
57 },
58 {
59 desc: "multiple agentID",
60 ids: []string{"agent-id", "agent-id"},
61 wantErr: true,
62 },
63 {
64 desc: "multiple identifiers",
65 ids: []string{"agent-id"},
66 idents: []string{"host=localhost", "host=localhost"},
67 wantErr: true,
68 },
69 {
70 desc: "invalid identifiers",
71 ids: []string{"agent-id"},
72 idents: []string{";"},
73 wantErr: true,
74 },
75 {
76 desc: "success",
77 ids: []string{"agent-id"},
78 },
79 {
80 desc: "success with identifiers",
81 ids: []string{"agent-id"},
82 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"},
83 },
84 }
85
86 for _, tc := range testCases {
87 t.Run(tc.desc, func(t *testing.T) {
88
89 agentConn := agentmock.NewMockAgentService_ConnectServer(ctrl)
90 agentConnMD := metadata.MD{
91 ":authority": []string{"127.0.0.1:8091"},
92 "agentid": tc.ids,
93 "agentidentifiers": tc.idents,
94 "content-type": []string{"application/grpc"},
95 "user-agent": []string{"grpc-go/1.42.0"},
96 }
97 agentConnCtx := metadata.NewIncomingContext(context.Background(), agentConnMD)
98 agentConn.EXPECT().Context().Return(agentConnCtx).AnyTimes()
99
100 _, err := NewBackend(agentConn)
101 if gotErr := (err != nil); gotErr != tc.wantErr {
102 t.Errorf("NewBackend got err %q; wantErr = %t", err, tc.wantErr)
103 }
104 })
105 }
106 }
107
108 func TestDefaultBackendManager_AddRemoveBackends(t *testing.T) {
109 ctrl := gomock.NewController(t)
110 defer ctrl.Finish()
111
112 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{}))
113 backend12, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{}))
114 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{}))
115 backend22, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{}))
116 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{}))
117
118 p := NewDefaultBackendManager()
119
120 p.AddBackend(backend1)
121 p.RemoveBackend(backend1)
122 expectedBackends := make(map[string][]*Backend)
123 expectedAgentIDs := []string{}
124 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
125 t.Errorf("expected %v, got %v", e, a)
126 }
127 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
128 t.Errorf("expected %v, got %v", e, a)
129 }
130
131 p = NewDefaultBackendManager()
132 p.AddBackend(backend1)
133 p.AddBackend(backend12)
134 // Adding the same connection again should be a no-op.
135 p.AddBackend(backend12)
136 p.AddBackend(backend2)
137 p.AddBackend(backend22)
138 p.AddBackend(backend3)
139 p.RemoveBackend(backend22)
140 p.RemoveBackend(backend2)
141 p.RemoveBackend(backend1)
142 expectedBackends = map[string][]*Backend{
143 "agent1": {backend12},
144 "agent3": {backend3},
145 }
146 expectedAgentIDs = []string{"agent1", "agent3"}
147 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
148 t.Errorf("expected %v, got %v", e, a)
149 }
150 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
151 t.Errorf("expected %v, got %v", e, a)
152 }
153 }
154
155 func TestDefaultRouteBackendManager_AddRemoveBackends(t *testing.T) {
156 ctrl := gomock.NewController(t)
157 defer ctrl.Finish()
158
159 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"default-route=true"}))
160 backend12, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"default-route=true"}))
161 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
162 backend22, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
163 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"default-route=true"}))
164
165 p := NewDefaultRouteBackendManager()
166
167 p.AddBackend(backend1)
168 p.RemoveBackend(backend1)
169 expectedBackends := make(map[string][]*Backend)
170 expectedAgentIDs := []string{}
171 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
172 t.Errorf("expected %v, got %v", e, a)
173 }
174 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
175 t.Errorf("expected %v, got %v", e, a)
176 }
177
178 p = NewDefaultRouteBackendManager()
179 p.AddBackend(backend1)
180 p.AddBackend(backend12)
181 // Adding the same connection again should be a no-op.
182 p.AddBackend(backend12)
183 p.AddBackend(backend2)
184 p.AddBackend(backend22)
185 p.AddBackend(backend3)
186 p.RemoveBackend(backend22)
187 p.RemoveBackend(backend2)
188 p.RemoveBackend(backend1)
189
190 expectedBackends = map[string][]*Backend{
191 "agent1": {backend12},
192 "agent3": {backend3},
193 }
194 expectedAgentIDs = []string{"agent1", "agent3"}
195
196 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
197 t.Errorf("expected %v, got %v", e, a)
198 }
199 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
200 t.Errorf("expected %v, got %v", e, a)
201 }
202 }
203
204 func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
205 ctrl := gomock.NewController(t)
206 defer ctrl.Finish()
207
208 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
209 // backend2 has no desthost relevant identifiers
210 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"default-route=true"}))
211 // TODO: if backend3 is given conflicting identifiers with backend1, the wrong thing happens in RemoveBackend.
212 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"host=node2.mydomain.com&ipv4=5.6.7.8&ipv6=::"}))
213
214 p := NewDestHostBackendManager()
215
216 p.AddBackend(backend1)
217 p.RemoveBackend(backend1)
218 expectedBackends := make(map[string][]*Backend)
219 expectedAgentIDs := []string{}
220 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
221 t.Errorf("expected %v, got %v", e, a)
222 }
223 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
224 t.Errorf("expected %v, got %v", e, a)
225 }
226
227 p = NewDestHostBackendManager()
228 p.AddBackend(backend1)
229
230 expectedBackends = map[string][]*Backend{
231 "localhost": {backend1},
232 "1.2.3.4": {backend1},
233 "9878::7675:1292:9183:7562": {backend1},
234 "node1.mydomain.com": {backend1},
235 }
236 expectedAgentIDs = []string{
237 "1.2.3.4",
238 "9878::7675:1292:9183:7562",
239 "localhost",
240 "node1.mydomain.com",
241 }
242
243 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
244 t.Errorf("expected %v, got %v", e, a)
245 }
246 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
247 t.Errorf("expected %v, got %v", e, a)
248 }
249
250 p.AddBackend(backend2)
251 p.AddBackend(backend3)
252
253 expectedBackends = map[string][]*Backend{
254 "localhost": {backend1},
255 "node1.mydomain.com": {backend1},
256 "node2.mydomain.com": {backend3},
257 "1.2.3.4": {backend1},
258 "5.6.7.8": {backend3},
259 "9878::7675:1292:9183:7562": {backend1},
260 "::": {backend3},
261 }
262 expectedAgentIDs = []string{
263 "1.2.3.4",
264 "9878::7675:1292:9183:7562",
265 "localhost",
266 "node1.mydomain.com",
267 "5.6.7.8",
268 "::",
269 "node2.mydomain.com",
270 }
271
272 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
273 t.Errorf("expected %v, got %v", e, a)
274 }
275 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
276 t.Errorf("expected %v, got %v", e, a)
277 }
278
279 p.RemoveBackend(backend2)
280 p.RemoveBackend(backend1)
281
282 expectedBackends = map[string][]*Backend{
283 "node2.mydomain.com": {backend3},
284 "5.6.7.8": {backend3},
285 "::": {backend3},
286 }
287 expectedAgentIDs = []string{
288 "node2.mydomain.com",
289 "::",
290 "5.6.7.8",
291 }
292
293 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
294 t.Errorf("expected %v, got %v", e, a)
295 }
296 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
297 t.Errorf("expected %v, got %v", e, a)
298 }
299
300 p.RemoveBackend(backend3)
301 expectedBackends = map[string][]*Backend{}
302 expectedAgentIDs = []string{}
303
304 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
305 t.Errorf("expected %v, got %v", e, a)
306 }
307 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
308 t.Errorf("expected %v, got %v", e, a)
309 }
310 }
311
312 func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
313 ctrl := gomock.NewController(t)
314 defer ctrl.Finish()
315
316 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
317 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"host=localhost&host=node1.mydomain.com&ipv4=1.2.3.4&ipv6=9878::7675:1292:9183:7562"}))
318 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"host=localhost&host=node2.mydomain.com&ipv4=5.6.7.8&ipv6=::"}))
319
320 p := NewDestHostBackendManager()
321
322 p.AddBackend(backend1)
323 p.AddBackend(backend2)
324 p.AddBackend(backend3)
325
326 expectedBackends := map[string][]*Backend{
327 "localhost": {backend1, backend2, backend3},
328 "1.2.3.4": {backend1, backend2},
329 "5.6.7.8": {backend3},
330 "9878::7675:1292:9183:7562": {backend1, backend2},
331 "::": {backend3},
332 "node1.mydomain.com": {backend1, backend2},
333 "node2.mydomain.com": {backend3},
334 }
335 expectedAgentIDs := []string{
336 "1.2.3.4",
337 "9878::7675:1292:9183:7562",
338 "localhost",
339 "node1.mydomain.com",
340 "5.6.7.8",
341 "::",
342 "node2.mydomain.com",
343 }
344
345 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
346 t.Errorf("expected %v, got %v", e, a)
347 }
348 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
349 t.Errorf("expected %v, got %v", e, a)
350 }
351
352 p.RemoveBackend(backend1)
353 p.RemoveBackend(backend3)
354
355 expectedBackends = map[string][]*Backend{
356 "localhost": {backend2},
357 "1.2.3.4": {backend2},
358 "9878::7675:1292:9183:7562": {backend2},
359 "node1.mydomain.com": {backend2},
360 }
361 expectedAgentIDs = []string{
362 "1.2.3.4",
363 "9878::7675:1292:9183:7562",
364 "localhost",
365 "node1.mydomain.com",
366 }
367
368 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
369 t.Errorf("expected %v, got %v", e, a)
370 }
371 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
372 t.Errorf("expected %v, got %v", e, a)
373 }
374
375 p.RemoveBackend(backend2)
376 expectedBackends = map[string][]*Backend{}
377 expectedAgentIDs = []string{}
378
379 if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
380 t.Errorf("expected %v, got %v", e, a)
381 }
382 if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
383 t.Errorf("expected %v, got %v", e, a)
384 }
385 }
386
387 func TestDefaultBackendManager_GetRandomBackend_DrainingFallback(t *testing.T) {
388 ctrl := gomock.NewController(t)
389 defer ctrl.Finish()
390
391 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{}))
392 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{}))
393 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{}))
394
395 p := NewDefaultBackendManager()
396
397 // Test 1: Empty backend manager returns ErrNotFound
398 _, err := p.Backend(context.Background())
399 if _, ok := err.(*ErrNotFound); !ok {
400 t.Errorf("expected ErrNotFound, got %v", err)
401 }
402
403 // Add backends
404 p.AddBackend(backend1)
405 p.AddBackend(backend2)
406 p.AddBackend(backend3)
407
408 // Test 2: Non-draining backends are returned
409 b, err := p.Backend(context.Background())
410 if err != nil {
411 t.Errorf("unexpected error: %v", err)
412 }
413 if b.IsDraining() {
414 t.Errorf("expected non-draining backend, got draining")
415 }
416
417 // Test 3: When some backends are draining, non-draining ones are prioritized
418 backend1.SetDraining()
419
420 // Call multiple times to ensure we never get the draining backend
421 for i := 0; i < 20; i++ {
422 b, err = p.Backend(context.Background())
423 if err != nil {
424 t.Errorf("unexpected error: %v", err)
425 }
426 if b == backend1 {
427 t.Errorf("expected non-draining backend, got draining backend1")
428 }
429 }
430
431 // Test 4: When all backends are draining, fallback to a draining backend
432 backend2.SetDraining()
433 backend3.SetDraining()
434
435 b, err = p.Backend(context.Background())
436 if err != nil {
437 t.Errorf("expected fallback to draining backend, got error: %v", err)
438 }
439 if b == nil {
440 t.Error("expected a backend, got nil")
441 }
442 if !b.IsDraining() {
443 t.Error("expected draining backend as fallback")
444 }
445 }
446
447 func TestDestHostBackendManager_Backend_DrainingFallback(t *testing.T) {
448 ctrl := gomock.NewController(t)
449 defer ctrl.Finish()
450
451 backend1, _ := NewBackend(mockAgentConn(ctrl, "agent1", []string{"host=localhost"}))
452 backend2, _ := NewBackend(mockAgentConn(ctrl, "agent2", []string{"host=localhost"}))
453 backend3, _ := NewBackend(mockAgentConn(ctrl, "agent3", []string{"host=otherhost"}))
454
455 p := NewDestHostBackendManager()
456
457 // Add backends
458 p.AddBackend(backend1)
459 p.AddBackend(backend2)
460 p.AddBackend(backend3)
461
462 ctx := context.WithValue(context.Background(), destHostKey, "localhost")
463
464 // Test 1: Non-draining backends are returned
465 b, err := p.Backend(ctx)
466 if err != nil {
467 t.Errorf("unexpected error: %v", err)
468 }
469 if b.IsDraining() {
470 t.Errorf("expected non-draining backend, got draining")
471 }
472
473 // Test 2: When some backends for destHost are draining, non-draining ones are prioritized
474 backend1.SetDraining()
475
476 b, err = p.Backend(ctx)
477 if err != nil {
478 t.Errorf("unexpected error: %v", err)
479 }
480 if b != backend2 {
481 t.Errorf("expected backend2 (non-draining), got different backend")
482 }
483
484 // Test 3: When all backends for destHost are draining, fallback to a draining backend
485 backend2.SetDraining()
486
487 b, err = p.Backend(ctx)
488 if err != nil {
489 t.Errorf("expected fallback to draining backend, got error: %v", err)
490 }
491 if b == nil {
492 t.Error("expected a backend, got nil")
493 }
494 if !b.IsDraining() {
495 t.Error("expected draining backend as fallback")
496 }
497 // Verify we got one of the localhost backends, not otherhost
498 if b != backend1 && b != backend2 {
499 t.Error("expected fallback to be one of the localhost backends")
500 }
501
502 // Test 4: Different destHost still works independently
503 ctx2 := context.WithValue(context.Background(), destHostKey, "otherhost")
504 b, err = p.Backend(ctx2)
505 if err != nil {
506 t.Errorf("unexpected error: %v", err)
507 }
508 if b != backend3 {
509 t.Errorf("expected backend3 for otherhost")
510 }
511 if b.IsDraining() {
512 t.Error("expected non-draining backend for otherhost")
513 }
514 }