forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpod_test.go
537 lines (490 loc) · 12.5 KB
/
pod_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package plugin
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"github.com/openshift/origin/pkg/sdn/plugin/cniserver"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kapi "k8s.io/kubernetes/pkg/api"
kunversioned "k8s.io/kubernetes/pkg/api/unversioned"
kcontainer "k8s.io/kubernetes/pkg/kubelet/container"
kcontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/kubelet/network"
khostport "k8s.io/kubernetes/pkg/kubelet/network/hostport"
utiltesting "k8s.io/kubernetes/pkg/util/testing"
cnitypes "github.com/containernetworking/cni/pkg/types"
)
type operation struct {
command cniserver.CNICommand
namespace string
name string
cidr string // pod CIDR for add operation
failStr string // error string for failing the operation
request *cniserver.PodRequest // filled in automatically from other info
result *cnitypes.Result
}
type expectedPod struct {
// IP address to return for the pod's ADD operation
cidr string
added bool
updated uint
deleted bool
errors map[cniserver.CNICommand]string
}
type podTester struct {
t *testing.T
testname string
client *http.Client
// Holds list of expected pods and their IP address for the ADD operation
pods map[string]*expectedPod
}
func newPodTester(t *testing.T, testname string, socketPath string) *podTester {
client := &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
return &podTester{
t: t,
testname: testname,
client: client,
pods: make(map[string]*expectedPod),
}
}
func ptPodKey(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}
func (pt *podTester) getExpectedPod(namespace, name string, command cniserver.CNICommand) (*expectedPod, error) {
pod := pt.pods[ptPodKey(namespace, name)]
if pod == nil {
return nil, fmt.Errorf("pod not found!")
} else if failStr, ok := pod.errors[command]; ok {
return nil, fmt.Errorf(failStr)
}
return pod, nil
}
func (pt *podTester) addExpectedPod(t *testing.T, op *operation) {
pk := ptPodKey(op.namespace, op.name)
pod, ok := pt.pods[pk]
if !ok {
pod = &expectedPod{
cidr: op.cidr,
errors: make(map[cniserver.CNICommand]string),
}
pt.pods[pk] = pod
}
if op.failStr != "" {
pod.errors[op.command] = op.failStr
}
}
func fakeRunningPod(namespace, name string, ip net.IP) *runningPod {
activePod := &khostport.ActivePod{
Pod: &kapi.Pod{
TypeMeta: kunversioned.TypeMeta{
Kind: "Pod",
},
ObjectMeta: kapi.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: kapi.PodSpec{
Containers: []kapi.Container{
{
Name: "foobareasadfa",
Image: "awesome-image",
},
},
},
},
IP: ip,
}
return &runningPod{activePod: activePod, vnid: 0}
}
func (pt *podTester) setup(req *cniserver.PodRequest) (*cnitypes.Result, *runningPod, error) {
pod, err := pt.getExpectedPod(req.PodNamespace, req.PodName, req.Command)
if err != nil {
return nil, nil, err
} else if pod.added {
return nil, nil, fmt.Errorf("pod already added!")
}
pod.added = true
ip, ipnet, _ := net.ParseCIDR(pod.cidr)
result := &cnitypes.Result{
IP4: &cnitypes.IPConfig{
IP: net.IPNet{
IP: ip,
Mask: ipnet.Mask,
},
},
}
return result, fakeRunningPod(req.PodNamespace, req.PodName, ip), nil
}
func (pt *podTester) update(req *cniserver.PodRequest) (uint32, error) {
pod, err := pt.getExpectedPod(req.PodNamespace, req.PodName, req.Command)
if err != nil {
return 0, err
}
pod.updated += 1
return 0, nil
}
func (pt *podTester) teardown(req *cniserver.PodRequest) error {
pod, err := pt.getExpectedPod(req.PodNamespace, req.PodName, req.Command)
if err == nil {
pod.deleted = true
}
return err
}
type fakeHost struct {
runtime kcontainer.Runtime
}
var _ network.Host = &fakeHost{}
func newFakeHost() *fakeHost {
return &fakeHost{
runtime: &kcontainertest.FakeRuntime{
AllPodList: []*kcontainertest.FakePod{},
},
}
}
func (fnh *fakeHost) GetPodByName(name, namespace string) (*kapi.Pod, bool) {
return nil, false
}
func (fnh *fakeHost) GetKubeClient() clientset.Interface {
return nil
}
func (fnh *fakeHost) GetRuntime() kcontainer.Runtime {
return fnh.runtime
}
func (fnh *fakeHost) GetNetNS(containerID string) (string, error) {
return "", nil
}
func (fnh *fakeHost) SupportsLegacyFeatures() bool {
return false
}
type podcheck struct {
namespace string
name string
updateCount uint
}
func TestPodManager(t *testing.T) {
tmpDir, err := utiltesting.MkTmpdir("cniserver")
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)
socketPath := filepath.Join(tmpDir, "cni-server.sock")
testcases := map[string]struct {
operations []*operation
checks []*podcheck
}{
"ADD+DEL one pod": {
operations: []*operation{
{
command: cniserver.CNI_ADD,
namespace: "namespace1",
name: "pod1",
cidr: "10.1.2.4/24",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace1",
name: "pod1",
},
},
checks: []*podcheck{
{
namespace: "namespace1",
name: "pod1",
updateCount: 0,
},
},
},
"ADD+UPDATE+DEL many pod": {
operations: []*operation{
{
command: cniserver.CNI_ADD,
namespace: "namespace1",
name: "pod1",
cidr: "10.1.2.4/24",
},
{
command: cniserver.CNI_UPDATE,
namespace: "namespace1",
name: "pod1",
},
{
command: cniserver.CNI_ADD,
namespace: "namespace2",
name: "pod2",
cidr: "10.1.2.3/24",
},
{
command: cniserver.CNI_ADD,
namespace: "namespace3",
name: "pod3",
cidr: "10.1.2.2/24",
},
{
command: cniserver.CNI_UPDATE,
namespace: "namespace2",
name: "pod2",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace1",
name: "pod1",
},
{
command: cniserver.CNI_UPDATE,
namespace: "namespace2",
name: "pod2",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace3",
name: "pod3",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace2",
name: "pod2",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace2",
name: "pod2",
},
},
checks: []*podcheck{
{
namespace: "namespace1",
name: "pod1",
updateCount: 1,
},
{
namespace: "namespace2",
name: "pod2",
updateCount: 2,
},
{
namespace: "namespace3",
name: "pod3",
updateCount: 0,
},
},
},
"ADD error": {
operations: []*operation{
{
command: cniserver.CNI_ADD,
namespace: "namespace1",
name: "pod1",
cidr: "10.1.2.5/24",
failStr: "fail hard",
},
},
},
"UPDATE error": {
operations: []*operation{
{
command: cniserver.CNI_ADD,
namespace: "namespace2",
name: "pod2",
cidr: "10.1.2.5/24",
},
{
command: cniserver.CNI_UPDATE,
namespace: "namespace2",
name: "pod2",
failStr: "fail harder",
},
},
},
"DEL error": {
operations: []*operation{
{
command: cniserver.CNI_ADD,
namespace: "namespace3",
name: "pod3",
cidr: "10.1.2.5/24",
},
{
command: cniserver.CNI_DEL,
namespace: "namespace3",
name: "pod3",
failStr: "fail like a rock",
},
},
},
"unknown command": {
operations: []*operation{
{
command: cniserver.CNICommand("foobar!"),
namespace: "namespace3",
name: "pod3",
failStr: "unhandled CNI request foobar!",
},
},
},
}
for k, tc := range testcases {
podTester := newPodTester(t, k, socketPath)
podManager := newDefaultPodManager()
podManager.podHandler = podTester
_, net, _ := net.ParseCIDR("1.2.0.0/16")
podManager.Start(socketPath, newFakeHost(), "1.2.3.0/24", net)
// Add pods to our expected pod list before kicking off the
// actual pod setup to ensure we don't concurrently access
// our pod map from different goroutines
for _, op := range tc.operations {
podTester.addExpectedPod(t, op)
}
for _, op := range tc.operations {
op.request = &cniserver.PodRequest{
Command: op.command,
PodNamespace: op.namespace,
PodName: op.name,
ContainerId: "asd;lfkajsdflkajfs",
Netns: "/some/network/namespace",
Result: make(chan *cniserver.PodResult),
}
podManager.addRequest(op.request)
}
for _, op := range tc.operations {
result := podManager.waitRequest(op.request)
if op.failStr != "" {
if result.Err == nil {
t.Fatalf("[%s] unexpected %v result success", k, op)
}
if !strings.HasPrefix(fmt.Sprintf("%v", result.Err), op.failStr) {
t.Fatalf("[%s] unexpected %v error: %v", k, op, result.Err)
}
} else {
if result.Err != nil {
t.Fatalf("[%s] unexpected %v result error %v", k, op, result.Err)
}
if op.command == cniserver.CNI_ADD {
if result.Response == nil {
t.Fatalf("[%s] unexpected %v nil result response", k, op)
}
var cniResult *cnitypes.Result
if err := json.Unmarshal(result.Response, &cniResult); err != nil {
t.Fatalf("[%s] unexpected error unmarshalling CNI result '%s': %v", k, string(result.Response), err)
}
if cniResult.IP4.IP.String() != op.cidr {
t.Fatalf("[%s] expected ADD IP %s but got %s", k, op.cidr, cniResult.IP4.IP.String())
}
}
}
}
// Verify pod operations performed as requested
for _, check := range tc.checks {
pod, err := podTester.getExpectedPod(check.namespace, check.name, "")
if err != nil {
t.Fatalf("[%s] expected pod %v: %v", k, check, err)
}
if len(pod.errors) > 0 {
// expected error; don't check operations
continue
}
if !pod.added {
t.Fatalf("[%s] added pod %v not marked added", k, check)
}
if pod.updated != check.updateCount {
t.Fatalf("[%s] pod %v update count wrong, got %v", k, check, pod.updated)
}
if !pod.deleted {
t.Fatalf("[%s] expected pod %v to be deleted", k, check)
}
// Make sure it's gone from the podManager too
if podManager.getPod(&cniserver.PodRequest{
PodNamespace: check.namespace,
PodName: check.name,
}) != nil {
t.Fatalf("[%s] expected pod %v to be deleted from podManager", k, check)
}
}
}
}
// Test a direct pod update, not through the CNIServer, like the node process
// currently does due to lack of a standard CNI update command
func TestDirectPodUpdate(t *testing.T) {
tmpDir, err := utiltesting.MkTmpdir("cniserver")
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)
socketPath := filepath.Join(tmpDir, "cni-server.sock")
podTester := newPodTester(t, "update", socketPath)
podManager := newDefaultPodManager()
podManager.podHandler = podTester
_, net, _ := net.ParseCIDR("1.2.0.0/16")
podManager.Start(socketPath, newFakeHost(), "1.2.3.0/24", net)
op := &operation{
command: cniserver.CNI_UPDATE,
namespace: "foobarnamespace",
name: "foobarname",
}
podTester.addExpectedPod(t, op)
req := &cniserver.PodRequest{
Command: op.command,
PodNamespace: op.namespace,
PodName: op.name,
ContainerId: "asdfasdfasdfaf",
Result: make(chan *cniserver.PodResult),
}
// Send request and wait for the result
if _, err = podManager.handleCNIRequest(req); err != nil {
t.Fatalf("failed to update pod: %v", err)
}
}
func TestUpdateMulticastFlows(t *testing.T) {
pods := map[string]*runningPod{
"blah": {
vnid: 5,
ofport: 2,
},
"baz": {
vnid: 5,
ofport: 8,
},
"foobar": {
vnid: 5,
ofport: 7,
},
"blah2": {
vnid: 6,
ofport: 3,
},
"baz2": {
vnid: 6,
ofport: 9,
},
"bork": {
vnid: 8,
ofport: 10,
},
}
outputs := localMulticastOutputs(pods, 0)
if outputs != "" {
t.Fatalf("Unexpected outputs for vnid 0: %s", outputs)
}
outputs = localMulticastOutputs(pods, 5)
if outputs != "output:2,output:7,output:8" {
t.Fatalf("Unexpected outputs for vnid 5: %s", outputs)
}
outputs = localMulticastOutputs(pods, 6)
if outputs != "output:3,output:9" {
t.Fatalf("Unexpected outputs for vnid 6: %s", outputs)
}
outputs = localMulticastOutputs(pods, 8)
if outputs != "output:10" {
t.Fatalf("Unexpected outputs for vnid 0: %s", outputs)
}
}