Debian Codegraph @

pkg/hns/netconf_windows_test.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package hns
15
16 import (
17 "encoding/json"
18 "net"
19
20 "github.com/Microsoft/hcsshim/hcn"
21 . "github.com/onsi/ginkgo/v2"
22 . "github.com/onsi/gomega"
23 )
24
25 var _ = Describe("NetConf", func() {
26 Describe("ApplyLoopbackDSRPolicy", func() {
27 Context("via v1 api", func() {
28 var n NetConf
29 BeforeEach(func() {
30 n = NetConf{}
31 })
32
33 It("filter out duplicated IP", func() {
34 // mock duplicated IP
35 ip := net.ParseIP("172.16.0.12")
36 n.ApplyLoopbackDSRPolicy(&ip)
37 n.ApplyLoopbackDSRPolicy(&ip)
38
39 // only one policy
40 addlArgs := n.Policies
41 Expect(addlArgs).Should(HaveLen(1))
42
43 // normal type judgement
44 policy := addlArgs[0]
45 Expect(policy.Name).Should(Equal("EndpointPolicy"))
46 value := make(map[string]interface{})
47 json.Unmarshal(policy.Value, &value)
48 Expect(value).Should(HaveKey("Type"))
49 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
50 Expect(value).Should(HaveKey("Destinations"))
51
52 // and only one item
53 destinationList := value["Destinations"].([]interface{})
54 Expect(destinationList).Should(HaveLen(1))
55 Expect(destinationList[0].(string)).Should(Equal("172.16.0.12"))
56 })
57
58 It("append different IP", func() {
59 // mock different IP
60 ip1 := net.ParseIP("172.16.0.12")
61 n.ApplyLoopbackDSRPolicy(&ip1)
62 ip2 := net.ParseIP("172.16.0.13")
63 n.ApplyLoopbackDSRPolicy(&ip2)
64
65 // will be two policies
66 addlArgs := n.Policies
67 Expect(addlArgs).Should(HaveLen(2))
68
69 // normal type judgement
70 policy := addlArgs[1] // pick second item
71 Expect(policy.Name).Should(Equal("EndpointPolicy"))
72 value := make(map[string]interface{})
73 json.Unmarshal(policy.Value, &value)
74 Expect(value).Should(HaveKey("Type"))
75 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
76 Expect(value).Should(HaveKey("Destinations"))
77
78 // only one item
79 destinationList := value["Destinations"].([]interface{})
80 Expect(destinationList).Should(HaveLen(1))
81 Expect(destinationList[0].(string)).Should(Equal("172.16.0.13"))
82 })
83 })
84
85 Context("via v2 api", func() {
86 var n NetConf
87 BeforeEach(func() {
88 n = NetConf{ApiVersion: 2}
89 })
90
91 It("filter out duplicated IP", func() {
92 // mock duplicated IP
93 ip := net.ParseIP("172.16.0.12")
94 n.ApplyLoopbackDSRPolicy(&ip)
95 n.ApplyLoopbackDSRPolicy(&ip)
96
97 // only one policy
98 addlArgs := n.Policies
99 Expect(addlArgs).Should(HaveLen(1))
100
101 // normal type judgement
102 policy := addlArgs[0]
103 Expect(policy.Name).Should(Equal("EndpointPolicy"))
104 value := make(map[string]interface{})
105 json.Unmarshal(policy.Value, &value)
106 Expect(value).Should(HaveKey("Type"))
107 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
108 Expect(value).Should(HaveKey("Settings"))
109
110 // and only one item
111 settings := value["Settings"].(map[string]interface{})
112 destinationList := settings["Destinations"].([]interface{})
113 Expect(destinationList).Should(HaveLen(1))
114 Expect(destinationList[0].(string)).Should(Equal("172.16.0.12"))
115 })
116
117 It("append different IP", func() {
118 // mock different IP
119 ip1 := net.ParseIP("172.16.0.12")
120 n.ApplyLoopbackDSRPolicy(&ip1)
121 ip2 := net.ParseIP("172.16.0.13")
122 n.ApplyLoopbackDSRPolicy(&ip2)
123
124 // will be two policies
125 addlArgs := n.Policies
126 Expect(addlArgs).Should(HaveLen(2))
127
128 // normal type judgement
129 policy := addlArgs[1] // pick second item
130 Expect(policy.Name).Should(Equal("EndpointPolicy"))
131 value := make(map[string]interface{})
132 json.Unmarshal(policy.Value, &value)
133 Expect(value).Should(HaveKey("Type"))
134 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
135 Expect(value).Should(HaveKey("Settings"))
136
137 // only one item
138 settings := value["Settings"].(map[string]interface{})
139 destinationList := settings["Destinations"].([]interface{})
140 Expect(destinationList).Should(HaveLen(1))
141 Expect(destinationList[0].(string)).Should(Equal("172.16.0.13"))
142 })
143 })
144 })
145
146 Describe("ApplyOutBoundNATPolicy", func() {
147 Context("via v1 api", func() {
148 var n NetConf
149 BeforeEach(func() {
150 n = NetConf{}
151 })
152
153 It("append different IP", func() {
154 // mock different IP
155 n.ApplyOutboundNatPolicy("192.168.0.0/16")
156 n.ApplyOutboundNatPolicy("10.244.0.0/16")
157
158 // will be two policies
159 addlArgs := n.Policies
160 Expect(addlArgs).Should(HaveLen(2))
161
162 // normal type judgement
163 policy := addlArgs[1] // pick second item
164 Expect(policy.Name).Should(Equal("EndpointPolicy"))
165 value := make(map[string]interface{})
166 json.Unmarshal(policy.Value, &value)
167 Expect(value).Should(HaveKey("Type"))
168 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
169 Expect(value).Should(HaveKey("ExceptionList"))
170
171 // but get two items
172 exceptionList := value["ExceptionList"].([]interface{})
173 Expect(exceptionList).Should(HaveLen(1))
174 Expect(exceptionList[0].(string)).Should(Equal("10.244.0.0/16"))
175 })
176
177 It("append a new one if there is not an exception OutBoundNAT policy", func() {
178 // mock different OutBoundNAT routes
179 n.Policies = []Policy{
180 {
181 Name: "EndpointPolicy",
182 Value: bprintf(`{"Type": "OutBoundNAT", "OtherList": []}`),
183 },
184 }
185 n.ApplyOutboundNatPolicy("10.244.0.0/16")
186
187 // has two policies
188 addlArgs := n.Policies
189 Expect(addlArgs).Should(HaveLen(2))
190
191 // normal type judgement
192 policy := addlArgs[0]
193 Expect(policy.Name).Should(Equal("EndpointPolicy"))
194 value := make(map[string]interface{})
195 json.Unmarshal(policy.Value, &value)
196 Expect(value).Should(HaveKey("Type"))
197 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
198 Expect(value).Should(HaveKey("OtherList"))
199 policy = addlArgs[1]
200 value = make(map[string]interface{})
201 json.Unmarshal(policy.Value, &value)
202 Expect(value).Should(HaveKey("Type"))
203 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
204 Expect(value).Should(HaveKey("ExceptionList"))
205
206 // only get one item
207 exceptionList := value["ExceptionList"].([]interface{})
208 Expect(exceptionList).Should(HaveLen(1))
209 Expect(exceptionList[0].(string)).Should(Equal("10.244.0.0/16"))
210 })
211
212 It("nothing to do if CIDR is blank", func() {
213 // mock different OutBoundNAT routes
214 n.Policies = []Policy{
215 {
216 Name: "EndpointPolicy",
217 Value: bprintf(`{"Type": "OutBoundNAT", "ExceptionList": []}`),
218 },
219 }
220 n.ApplyOutboundNatPolicy("")
221
222 // only one policy
223 addlArgs := n.Policies
224 Expect(addlArgs).Should(HaveLen(1))
225
226 // normal type judgement
227 policy := addlArgs[0]
228 Expect(policy.Name).Should(Equal("EndpointPolicy"))
229 value := make(map[string]interface{})
230 json.Unmarshal(policy.Value, &value)
231 Expect(value).Should(HaveKey("Type"))
232 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
233 Expect(value).Should(HaveKey("ExceptionList"))
234
235 // empty list
236 Expect(value["ExceptionList"]).ShouldNot(BeNil())
237 Expect(value["ExceptionList"]).Should(HaveLen(0))
238 })
239 })
240
241 Context("via v2 api", func() {
242 var n NetConf
243 BeforeEach(func() {
244 n = NetConf{ApiVersion: 2}
245 })
246
247 It("append different IP", func() {
248 // mock different IP
249 n.ApplyOutboundNatPolicy("192.168.0.0/16")
250 n.ApplyOutboundNatPolicy("10.244.0.0/16")
251
252 // will be two policies
253 addlArgs := n.Policies
254 Expect(addlArgs).Should(HaveLen(2))
255
256 // normal type judgement
257 policy := addlArgs[1] // pick second item
258 Expect(policy.Name).Should(Equal("EndpointPolicy"))
259 value := make(map[string]interface{})
260 json.Unmarshal(policy.Value, &value)
261 Expect(value).Should(HaveKey("Type"))
262 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
263 Expect(value).Should(HaveKey("Settings"))
264
265 // but get two items
266 settings := value["Settings"].(map[string]interface{})
267 exceptionList := settings["Exceptions"].([]interface{})
268 Expect(exceptionList).Should(HaveLen(1))
269 Expect(exceptionList[0].(string)).Should(Equal("10.244.0.0/16"))
270 })
271
272 It("append a new one if there is not an exception OutBoundNAT policy", func() {
273 // mock different OutBoundNAT routes
274 n.Policies = []Policy{
275 {
276 Name: "EndpointPolicy",
277 Value: bprintf(`{"Type": "OutBoundNAT", "Settings": {"Others": []}}`),
278 },
279 }
280 n.ApplyOutboundNatPolicy("10.244.0.0/16")
281
282 // has two policies
283 addlArgs := n.Policies
284 Expect(addlArgs).Should(HaveLen(2))
285
286 // normal type judgement
287 policy := addlArgs[0]
288 Expect(policy.Name).Should(Equal("EndpointPolicy"))
289 value := make(map[string]interface{})
290 json.Unmarshal(policy.Value, &value)
291 Expect(value).Should(HaveKey("Type"))
292 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
293 Expect(value).Should(HaveKey("Settings"))
294 Expect(value["Settings"]).Should(HaveKey("Others"))
295 policy = addlArgs[1]
296 value = make(map[string]interface{})
297 json.Unmarshal(policy.Value, &value)
298 Expect(value).Should(HaveKey("Type"))
299 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
300 Expect(value).Should(HaveKey("Settings"))
301
302 // only get one item
303 settings := value["Settings"].(map[string]interface{})
304 exceptionList := settings["Exceptions"].([]interface{})
305 Expect(exceptionList).Should(HaveLen(1))
306 Expect(exceptionList[0].(string)).Should(Equal("10.244.0.0/16"))
307 })
308
309 It("nothing to do if CIDR is blank", func() {
310 // mock different OutBoundNAT routes
311 n.Policies = []Policy{
312 {
313 Name: "EndpointPolicy",
314 Value: bprintf(`{"Type": "OutBoundNAT", "Settings": {"Exceptions": []}}`),
315 },
316 }
317 n.ApplyOutboundNatPolicy("")
318
319 // only one policy
320 addlArgs := n.Policies
321 Expect(addlArgs).Should(HaveLen(1))
322
323 // normal type judgement
324 policy := addlArgs[0]
325 Expect(policy.Name).Should(Equal("EndpointPolicy"))
326 value := make(map[string]interface{})
327 json.Unmarshal(policy.Value, &value)
328 Expect(value).Should(HaveKey("Type"))
329 Expect(value["Type"]).Should(Equal("OutBoundNAT"))
330 Expect(value).Should(HaveKey("Settings"))
331
332 // empty list
333 settings := value["Settings"].(map[string]interface{})
334 Expect(settings["Exceptions"]).ShouldNot(BeNil())
335 Expect(settings["Exceptions"]).Should(HaveLen(0))
336 })
337 })
338 })
339
340 Describe("ApplyDefaultPAPolicy", func() {
341 Context("via v1 api", func() {
342 var n NetConf
343 BeforeEach(func() {
344 n = NetConf{}
345 })
346
347 It("append different IP", func() {
348 // mock different IP
349 n.ApplyDefaultPAPolicy("192.168.0.1")
350 n.ApplyDefaultPAPolicy("192.168.0.2")
351
352 // will be two policies
353 addlArgs := n.Policies
354 Expect(addlArgs).Should(HaveLen(2))
355
356 // normal type judgement
357 policy := addlArgs[1] // judge second item
358 Expect(policy.Name).Should(Equal("EndpointPolicy"))
359 value := make(map[string]interface{})
360 json.Unmarshal(policy.Value, &value)
361 Expect(value).Should(HaveKey("Type"))
362 Expect(value["Type"]).Should(Equal("PA"))
363
364 // compare with second item
365 paAddress := value["PA"].(string)
366 Expect(paAddress).Should(Equal("192.168.0.2"))
367 })
368
369 It("nothing to do if IP is blank", func() {
370 // mock different policy
371 n.Policies = []Policy{
372 {
373 Name: "EndpointPolicy",
374 Value: bprintf(`{"Type": "OutBoundNAT", "Exceptions": ["192.168.0.0/16"]}`),
375 },
376 }
377 n.ApplyDefaultPAPolicy("")
378
379 // nothing
380 addlArgs := n.Policies
381 Expect(addlArgs).Should(HaveLen(1))
382 })
383 })
384
385 Context("via v2 api", func() {
386 var n NetConf
387 BeforeEach(func() {
388 n = NetConf{ApiVersion: 2}
389 })
390
391 It("append different IP", func() {
392 // mock different IP
393 n.ApplyDefaultPAPolicy("192.168.0.1")
394 n.ApplyDefaultPAPolicy("192.168.0.2")
395
396 // will be two policies
397 addlArgs := n.Policies
398 Expect(addlArgs).Should(HaveLen(2))
399
400 // normal type judgement
401 policy := addlArgs[1] // judge second item
402 Expect(policy.Name).Should(Equal("EndpointPolicy"))
403 value := make(map[string]interface{})
404 json.Unmarshal(policy.Value, &value)
405 Expect(value).Should(HaveKey("Type"))
406 Expect(value["Type"]).Should(Equal("ProviderAddress"))
407 Expect(value).Should(HaveKey("Settings"))
408
409 // compare with second item
410 settings := value["Settings"].(map[string]interface{})
411 paAddress := settings["ProviderAddress"].(string)
412 Expect(paAddress).Should(Equal("192.168.0.2"))
413 })
414
415 It("nothing to do if IP is blank", func() {
416 // mock different policy
417 n.Policies = []Policy{
418 {
419 Name: "EndpointPolicy",
420 Value: bprintf(`{"Type": "OutBoundNAT", "Settings": {"Exceptions": ["192.168.0.0/16"]}}`),
421 },
422 }
423 n.ApplyDefaultPAPolicy("")
424
425 // nothing
426 addlArgs := n.Policies
427 Expect(addlArgs).Should(HaveLen(1))
428 })
429 })
430 })
431
432 Describe("ApplyPortMappingPolicy", func() {
433 Context("via v1 api", func() {
434 var n NetConf
435 BeforeEach(func() {
436 n = NetConf{}
437 })
438
439 It("nothing to do if input is empty", func() {
440 n.ApplyPortMappingPolicy(nil)
441 Expect(n.Policies).Should(BeNil())
442
443 n.ApplyPortMappingPolicy([]PortMapEntry{})
444 Expect(n.Policies).Should(BeNil())
445 })
446
447 It("create one NAT policy", func() {
448 // mock different IP
449 n.ApplyPortMappingPolicy([]PortMapEntry{
450 {
451 ContainerPort: 80,
452 HostPort: 8080,
453 Protocol: "TCP",
454 HostIP: "192.168.1.2",
455 },
456 })
457
458 // only one item
459 addlArgs := n.Policies
460 Expect(addlArgs).Should(HaveLen(1))
461
462 // normal type judgement
463 policy := addlArgs[0]
464 Expect(policy.Name).Should(Equal("EndpointPolicy"))
465 value := make(map[string]interface{})
466 json.Unmarshal(policy.Value, &value)
467 Expect(value).Should(HaveKey("Type"))
468 Expect(value["Type"]).Should(Equal("NAT"))
469
470 // compare all values
471 Expect(value).Should(HaveKey("InternalPort"))
472 Expect(value["InternalPort"]).Should(Equal(float64(80)))
473 Expect(value).Should(HaveKey("ExternalPort"))
474 Expect(value["ExternalPort"]).Should(Equal(float64(8080)))
475 Expect(value).Should(HaveKey("Protocol"))
476 Expect(value["Protocol"]).Should(Equal("TCP"))
477 })
478 })
479
480 Context("via v2 api", func() {
481 var n NetConf
482 BeforeEach(func() {
483 n = NetConf{ApiVersion: 2}
484 })
485
486 It("nothing to do if input is empty", func() {
487 n.ApplyPortMappingPolicy(nil)
488 Expect(n.Policies).Should(BeNil())
489
490 n.ApplyPortMappingPolicy([]PortMapEntry{})
491 Expect(n.Policies).Should(BeNil())
492 })
493
494 It("creates one NAT policy", func() {
495 // mock different IP
496 n.ApplyPortMappingPolicy([]PortMapEntry{
497 {
498 ContainerPort: 80,
499 HostPort: 8080,
500 Protocol: "TCP",
501 HostIP: "192.168.1.2",
502 },
503 })
504
505 // only one item
506 addlArgs := n.Policies
507 Expect(addlArgs).Should(HaveLen(1))
508
509 // normal type judgement
510 policy := addlArgs[0]
511 Expect(policy.Name).Should(Equal("EndpointPolicy"))
512 value := make(map[string]interface{})
513 json.Unmarshal(policy.Value, &value)
514 Expect(value).Should(HaveKey("Type"))
515 Expect(value["Type"]).Should(Equal("PortMapping"))
516 Expect(value).Should(HaveKey("Settings"))
517
518 // compare all values
519 settings := value["Settings"].(map[string]interface{})
520 Expect(settings).Should(HaveKey("InternalPort"))
521 Expect(settings["InternalPort"]).Should(Equal(float64(80)))
522 Expect(settings).Should(HaveKey("ExternalPort"))
523 Expect(settings["ExternalPort"]).Should(Equal(float64(8080)))
524 Expect(settings).Should(HaveKey("Protocol"))
525 Expect(settings["Protocol"]).Should(Equal(float64(6)))
526 Expect(settings).Should(HaveKey("VIP"))
527 Expect(settings["VIP"]).Should(Equal("192.168.1.2"))
528 })
529 })
530 })
531
532 Describe("GetXEndpointPolicies", func() {
533 Context("via v1 api", func() {
534 var n NetConf
535 BeforeEach(func() {
536 n = NetConf{}
537 })
538
539 It("GetHNSEndpointPolicies", func() {
540 // mock different policies
541 n.Policies = []Policy{
542 {
543 Name: "EndpointPolicy",
544 Value: []byte(`{"Type": "OutBoundNAT", "ExceptionList": [ "192.168.1.2" ]}`),
545 },
546 {
547 Name: "someOtherType",
548 Value: []byte(`{"someOtherKey": "someOtherValue"}`),
549 },
550 }
551
552 // only one valid item
553 result := n.GetHNSEndpointPolicies()
554 Expect(len(result)).To(Equal(1))
555
556 // normal type judgement
557 policy := make(map[string]interface{})
558 err := json.Unmarshal(result[0], &policy)
559 Expect(err).ToNot(HaveOccurred())
560 Expect(policy).Should(HaveKey("Type"))
561 Expect(policy["Type"]).To(Equal("OutBoundNAT"))
562 Expect(policy).Should(HaveKey("ExceptionList"))
563 Expect(policy["ExceptionList"]).To(ContainElement("192.168.1.2"))
564 })
565 })
566
567 Context("via v2 api", func() {
568 var n NetConf
569 BeforeEach(func() {
570 n = NetConf{ApiVersion: 2}
571 })
572
573 It("GetHostComputeEndpointPolicies", func() {
574 // mock different policies
575 n.Policies = []Policy{
576 {
577 Name: "EndpointPolicy",
578 Value: []byte(`{"Type": "OutBoundNAT", "Settings": {"Exceptions": [ "192.168.1.2" ]}}`),
579 },
580 {
581 Name: "someOtherType",
582 Value: []byte(`{"someOtherKey": "someOtherValue"}`),
583 },
584 }
585
586 // only one valid item
587 result := n.GetHostComputeEndpointPolicies()
588 Expect(len(result)).To(Equal(1))
589
590 // normal type judgement
591 policy := result[0]
592 Expect(policy.Type).Should(Equal(hcn.OutBoundNAT))
593 settings := make(map[string]interface{})
594 err := json.Unmarshal(policy.Settings, &settings)
595 Expect(err).ToNot(HaveOccurred())
596 Expect(settings["Exceptions"]).To(ContainElement("192.168.1.2"))
597 })
598 })
599 })
600 })