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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
// +build linux
package netavark_test
// The tests have to be run as root.
// For each test there will be two network namespaces created,
// netNSTest and netNSContainer. Each test must be run inside
// netNSTest to prevent leakage in the host netns, therefore
// it should use the following structure:
// It("test name", func() {
// runTest(func() {
// // add test logic here
// })
// })
import (
"io/ioutil"
"net"
"os"
"strconv"
"sync"
"time"
"github.com/containernetworking/plugins/pkg/ns"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/containers/podman/v3/libpod/network/types"
"github.com/containers/podman/v3/libpod/network/util"
"github.com/containers/podman/v3/pkg/netns"
"github.com/containers/podman/v3/pkg/rootless"
"github.com/containers/storage/pkg/stringid"
)
var _ = Describe("run netavark", func() {
var (
libpodNet types.ContainerNetwork
confDir string
netNSTest ns.NetNS
netNSContainer ns.NetNS
)
// runTest is a helper function to run a test. It ensures that each test
// is run in its own netns. It also creates a mountns to mount a tmpfs to /var/lib/cni.
runTest := func(run func()) {
netNSTest.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
// we have to setup the loopback adapter in this netns to use port forwarding
link, err := netlink.LinkByName("lo")
Expect(err).To(BeNil(), "Failed to get loopback adapter")
err = netlink.LinkSetUp(link)
Expect(err).To(BeNil(), "Failed to set loopback adapter up")
run()
return nil
})
}
BeforeEach(func() {
if _, ok := os.LookupEnv("NETAVARK_BINARY"); !ok {
Skip("NETAVARK_BINARY not set skip run tests")
}
// set the logrus settings
logrus.SetLevel(logrus.TraceLevel)
// disable extra quotes so we can easily copy the netavark command
logrus.SetFormatter(&logrus.TextFormatter{DisableQuote: true})
logrus.SetOutput(os.Stderr)
// The tests need root privileges.
// Technically we could work around that by using user namespaces and
// the rootless cni code but this is to much work to get it right for a unit test.
if rootless.IsRootless() {
Skip("this test needs to be run as root")
}
var err error
confDir, err = ioutil.TempDir("", "podman_netavark_test")
if err != nil {
Fail("Failed to create tmpdir")
}
netNSTest, err = netns.NewNS()
if err != nil {
Fail("Failed to create netns")
}
netNSContainer, err = netns.NewNS()
if err != nil {
Fail("Failed to create netns")
}
})
JustBeforeEach(func() {
var err error
libpodNet, err = getNetworkInterface(confDir, false)
if err != nil {
Fail("Failed to create NewCNINetworkInterface")
}
})
AfterEach(func() {
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetLevel(logrus.InfoLevel)
os.RemoveAll(confDir)
netns.UnmountNS(netNSTest)
netNSTest.Close()
netns.UnmountNS(netNSContainer)
netNSContainer.Close()
})
It("test basic setup", func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
opts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: "someID",
ContainerName: "someName",
Networks: map[string]types.PerNetworkOptions{
defNet: {
InterfaceName: intName,
},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), opts)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
ip := res[defNet].Interfaces[intName].Networks[0].Subnet.IP
Expect(ip.String()).To(ContainSubstring("10.88.0."))
gw := res[defNet].Interfaces[intName].Networks[0].Gateway
util.NormalizeIP(&gw)
Expect(gw.String()).To(Equal("10.88.0.1"))
macAddress := res[defNet].Interfaces[intName].MacAddress
Expect(macAddress).To(HaveLen(6))
// default network has no dns
Expect(res[defNet].DNSServerIPs).To(BeEmpty())
Expect(res[defNet].DNSSearchDomains).To(BeEmpty())
// check in the container namespace if the settings are applied
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
i, err := net.InterfaceByName(intName)
Expect(err).To(BeNil())
Expect(i.Name).To(Equal(intName))
Expect(i.HardwareAddr).To(Equal(net.HardwareAddr(macAddress)))
addrs, err := i.Addrs()
Expect(err).To(BeNil())
subnet := &net.IPNet{
IP: ip,
Mask: net.CIDRMask(16, 32),
}
Expect(addrs).To(ContainElements(EqualSubnet(subnet)))
// check loopback adapter
i, err = net.InterfaceByName("lo")
Expect(err).To(BeNil())
Expect(i.Name).To(Equal("lo"))
Expect(i.Flags & net.FlagLoopback).To(Equal(net.FlagLoopback))
Expect(i.Flags&net.FlagUp).To(Equal(net.FlagUp), "Loopback adapter should be up")
return nil
})
Expect(err).To(BeNil())
// default bridge name
bridgeName := "podman0"
// check settings on the host side
i, err := net.InterfaceByName(bridgeName)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(bridgeName))
addrs, err := i.Addrs()
Expect(err).ToNot(HaveOccurred())
// test that the gateway ip is assigned to the interface
subnet := &net.IPNet{
IP: gw,
Mask: net.CIDRMask(16, 32),
}
Expect(addrs).To(ContainElements(EqualSubnet(subnet)))
wg := &sync.WaitGroup{}
expected := stringid.GenerateNonCryptoID()
// now check ip connectivity
err = netNSContainer.Do(func(_ ns.NetNS) error {
wg.Add(1)
runNetListener(wg, "tcp", "0.0.0.0", 5000, expected)
return nil
})
Expect(err).ToNot(HaveOccurred())
conn, err := net.Dial("tcp", ip.String()+":5000")
Expect(err).To(BeNil())
_, err = conn.Write([]byte(expected))
Expect(err).To(BeNil())
conn.Close()
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(opts))
Expect(err).ToNot(HaveOccurred())
wg.Wait()
})
})
It("setup two containers", func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
setupOpts1 := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
defNet: {InterfaceName: intName},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), setupOpts1)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
ip1 := res[defNet].Interfaces[intName].Networks[0].Subnet.IP
Expect(ip1.String()).To(ContainSubstring("10.88.0."))
Expect(res[defNet].Interfaces[intName].MacAddress).To(HaveLen(6))
setupOpts2 := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
defNet: {InterfaceName: intName},
},
},
}
netNSContainer2, err := netns.NewNS()
Expect(err).ToNot(HaveOccurred())
defer netns.UnmountNS(netNSContainer2)
defer netNSContainer2.Close()
res, err = libpodNet.Setup(netNSContainer2.Path(), setupOpts2)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
ip2 := res[defNet].Interfaces[intName].Networks[0].Subnet.IP
Expect(ip2.String()).To(ContainSubstring("10.88.0."))
Expect(res[defNet].Interfaces[intName].MacAddress).To(HaveLen(6))
Expect(ip1.Equal(ip2)).To(BeFalse(), "IP1 %s should not be equal to IP2 %s", ip1.String(), ip2.String())
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts1))
Expect(err).ToNot(HaveOccurred())
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts2))
Expect(err).ToNot(HaveOccurred())
})
})
It("setup dualstack network", func() {
runTest(func() {
s1, _ := types.ParseCIDR("10.0.0.1/24")
s2, _ := types.ParseCIDR("fd10:88:a::/64")
network, err := libpodNet.NetworkCreate(types.Network{
Subnets: []types.Subnet{
{Subnet: s1}, {Subnet: s2},
},
})
Expect(err).ToNot(HaveOccurred())
netName := network.Name
intName := "eth0"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
netName: {InterfaceName: intName},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(netName))
Expect(res[netName].Interfaces).To(HaveKey(intName))
Expect(res[netName].Interfaces[intName].Networks).To(HaveLen(2))
ip1 := res[netName].Interfaces[intName].Networks[0].Subnet.IP
Expect(ip1.String()).To(ContainSubstring("10.0.0."))
gw1 := res[netName].Interfaces[intName].Networks[0].Gateway
Expect(gw1.String()).To(Equal("10.0.0.1"))
ip2 := res[netName].Interfaces[intName].Networks[1].Subnet.IP
Expect(ip2.String()).To(ContainSubstring("fd10:88:a::"))
gw2 := res[netName].Interfaces[intName].Networks[0].Gateway
Expect(gw2.String()).To(Equal("fd10:88:a::1"))
Expect(res[netName].Interfaces[intName].MacAddress).To(HaveLen(6))
// check in the container namespace if the settings are applied
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
i, err := net.InterfaceByName(intName)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(intName))
addrs, err := i.Addrs()
Expect(err).ToNot(HaveOccurred())
subnet1 := s1.IPNet
subnet1.IP = ip1
subnet2 := s2.IPNet
subnet2.IP = ip2
Expect(addrs).To(ContainElements(EqualSubnet(&subnet1), EqualSubnet(&subnet2)))
// check loopback adapter
i, err = net.InterfaceByName("lo")
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal("lo"))
Expect(i.Flags & net.FlagLoopback).To(Equal(net.FlagLoopback))
Expect(i.Flags&net.FlagUp).To(Equal(net.FlagUp), "Loopback adapter should be up")
return nil
})
Expect(err).ToNot(HaveOccurred())
bridgeName := network.NetworkInterface
// check settings on the host side
i, err := net.InterfaceByName(bridgeName)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(bridgeName))
addrs, err := i.Addrs()
Expect(err).ToNot(HaveOccurred())
// test that the gateway ip is assigned to the interface
subnet1 := s1.IPNet
subnet1.IP = gw1
subnet2 := s2.IPNet
subnet2.IP = gw2
Expect(addrs).To(ContainElements(EqualSubnet(&subnet1), EqualSubnet(&subnet2)))
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
Expect(err).ToNot(HaveOccurred())
})
})
It("setup two networks", func() {
runTest(func() {
s1, _ := types.ParseCIDR("10.0.0.1/24")
network1, err := libpodNet.NetworkCreate(types.Network{
Subnets: []types.Subnet{
{Subnet: s1},
},
})
Expect(err).ToNot(HaveOccurred())
netName1 := network1.Name
intName1 := "eth0"
s2, _ := types.ParseCIDR("10.1.0.0/24")
network2, err := libpodNet.NetworkCreate(types.Network{
Subnets: []types.Subnet{
{Subnet: s2},
},
})
Expect(err).ToNot(HaveOccurred())
netName2 := network2.Name
intName2 := "eth1"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
netName1: {InterfaceName: intName1},
netName2: {InterfaceName: intName2},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(2))
Expect(res).To(HaveKey(netName1))
Expect(res).To(HaveKey(netName2))
Expect(res[netName1].Interfaces).To(HaveKey(intName1))
Expect(res[netName2].Interfaces).To(HaveKey(intName2))
Expect(res[netName1].Interfaces[intName1].Networks).To(HaveLen(1))
ip1 := res[netName1].Interfaces[intName1].Networks[0].Subnet.IP
Expect(ip1.String()).To(ContainSubstring("10.0.0."))
gw1 := res[netName1].Interfaces[intName1].Networks[0].Gateway
Expect(gw1.String()).To(Equal("10.0.0.1"))
ip2 := res[netName2].Interfaces[intName2].Networks[0].Subnet.IP
Expect(ip2.String()).To(ContainSubstring("10.1.0."))
gw2 := res[netName2].Interfaces[intName2].Networks[0].Gateway
Expect(gw2.String()).To(Equal("10.1.0.1"))
mac1 := res[netName1].Interfaces[intName1].MacAddress
Expect(mac1).To(HaveLen(6))
mac2 := res[netName2].Interfaces[intName2].MacAddress
Expect(mac2).To(HaveLen(6))
// check in the container namespace if the settings are applied
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
i, err := net.InterfaceByName(intName1)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(intName1))
addrs, err := i.Addrs()
Expect(err).ToNot(HaveOccurred())
subnet1 := s1.IPNet
subnet1.IP = ip1
Expect(addrs).To(ContainElements(EqualSubnet(&subnet1)))
i, err = net.InterfaceByName(intName2)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(intName2))
addrs, err = i.Addrs()
Expect(err).ToNot(HaveOccurred())
subnet2 := s2.IPNet
subnet2.IP = ip2
Expect(addrs).To(ContainElements(EqualSubnet(&subnet2)))
// check loopback adapter
i, err = net.InterfaceByName("lo")
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal("lo"))
Expect(i.Flags & net.FlagLoopback).To(Equal(net.FlagLoopback))
Expect(i.Flags&net.FlagUp).To(Equal(net.FlagUp), "Loopback adapter should be up")
return nil
})
Expect(err).ToNot(HaveOccurred())
bridgeName1 := network1.NetworkInterface
// check settings on the host side
i, err := net.InterfaceByName(bridgeName1)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(bridgeName1))
addrs, err := i.Addrs()
Expect(err).ToNot(HaveOccurred())
// test that the gateway ip is assigned to the interface
subnet1 := s1.IPNet
subnet1.IP = gw1
Expect(addrs).To(ContainElements(EqualSubnet(&subnet1)))
bridgeName2 := network2.NetworkInterface
// check settings on the host side
i, err = net.InterfaceByName(bridgeName2)
Expect(err).ToNot(HaveOccurred())
Expect(i.Name).To(Equal(bridgeName2))
addrs, err = i.Addrs()
Expect(err).ToNot(HaveOccurred())
// test that the gateway ip is assigned to the interface
subnet2 := s2.IPNet
subnet2.IP = gw2
Expect(addrs).To(ContainElements(EqualSubnet(&subnet2)))
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
Expect(err).ToNot(HaveOccurred())
})
})
for _, proto := range []string{"tcp", "udp"} {
// copy proto to extra var to keep correct references in the goroutines
protocol := proto
It("run with exposed ports protocol "+protocol, func() {
runTest(func() {
testdata := stringid.GenerateNonCryptoID()
defNet := types.DefaultNetworkName
intName := "eth0"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
PortMappings: []types.PortMapping{{
Protocol: protocol,
HostIP: "127.0.0.1",
HostPort: 5000,
ContainerPort: 5000,
}},
Networks: map[string]types.PerNetworkOptions{
defNet: {InterfaceName: intName},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).To(BeNil())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
Expect(res[defNet].Interfaces[intName].Networks[0].Subnet.IP.String()).To(ContainSubstring("10.88.0."))
Expect(res[defNet].Interfaces[intName].MacAddress).To(HaveLen(6))
// default network has no dns
Expect(res[defNet].DNSServerIPs).To(BeEmpty())
Expect(res[defNet].DNSSearchDomains).To(BeEmpty())
var wg sync.WaitGroup
wg.Add(1)
// start a listener in the container ns
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
runNetListener(&wg, protocol, "0.0.0.0", 5000, testdata)
return nil
})
Expect(err).To(BeNil())
conn, err := net.Dial(protocol, "127.0.0.1:5000")
Expect(err).To(BeNil())
_, err = conn.Write([]byte(testdata))
Expect(err).To(BeNil())
conn.Close()
// wait for the listener to finish
wg.Wait()
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
Expect(err).To(BeNil())
})
})
It("run with range ports protocol "+protocol, func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
PortMappings: []types.PortMapping{{
Protocol: protocol,
HostIP: "127.0.0.1",
HostPort: 5001,
ContainerPort: 5000,
Range: 3,
}},
Networks: map[string]types.PerNetworkOptions{
defNet: {InterfaceName: intName},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).To(BeNil())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
containerIP := res[defNet].Interfaces[intName].Networks[0].Subnet.IP.String()
Expect(containerIP).To(ContainSubstring("10.88.0."))
Expect(res[defNet].Interfaces[intName].MacAddress).To(HaveLen(6))
// default network has no dns
Expect(res[defNet].DNSServerIPs).To(BeEmpty())
Expect(res[defNet].DNSSearchDomains).To(BeEmpty())
// loop over all ports
for p := 5001; p < 5004; p++ {
port := p
var wg sync.WaitGroup
wg.Add(1)
testdata := stringid.GenerateNonCryptoID()
// start a listener in the container ns
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
runNetListener(&wg, protocol, containerIP, port-1, testdata)
return nil
})
Expect(err).To(BeNil())
conn, err := net.Dial(protocol, net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
Expect(err).To(BeNil())
_, err = conn.Write([]byte(testdata))
Expect(err).To(BeNil())
conn.Close()
// wait for the listener to finish
wg.Wait()
}
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
Expect(err).To(BeNil())
})
})
}
It("simple teardown", func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
opts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: "someID",
ContainerName: "someName",
Networks: map[string]types.PerNetworkOptions{
defNet: {
InterfaceName: intName,
},
},
},
}
res, err := libpodNet.Setup(netNSContainer.Path(), opts)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res).To(HaveKey(defNet))
Expect(res[defNet].Interfaces).To(HaveKey(intName))
Expect(res[defNet].Interfaces[intName].Networks).To(HaveLen(1))
ip := res[defNet].Interfaces[intName].Networks[0].Subnet.IP
Expect(ip.String()).To(ContainSubstring("10.88.0."))
gw := res[defNet].Interfaces[intName].Networks[0].Gateway
Expect(gw.String()).To(Equal("10.88.0.1"))
macAddress := res[defNet].Interfaces[intName].MacAddress
Expect(macAddress).To(HaveLen(6))
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(opts))
Expect(err).ToNot(HaveOccurred())
err = netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
// check that the container interface is removed
_, err := net.InterfaceByName(intName)
Expect(err).To(HaveOccurred())
return nil
})
Expect(err).ToNot(HaveOccurred())
// default bridge name
bridgeName := "podman0"
// check that bridge interface was removed
_, err = net.InterfaceByName(bridgeName)
Expect(err).To(HaveOccurred())
})
})
It("test netavark error", func() {
runTest(func() {
intName := "eth0"
err := netNSContainer.Do(func(_ ns.NetNS) error {
defer GinkgoRecover()
attr := netlink.NewLinkAttrs()
attr.Name = "eth0"
err := netlink.LinkAdd(&netlink.Bridge{LinkAttrs: attr})
Expect(err).ToNot(HaveOccurred())
return nil
})
Expect(err).ToNot(HaveOccurred())
defNet := types.DefaultNetworkName
opts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: "someID",
ContainerName: "someName",
Networks: map[string]types.PerNetworkOptions{
defNet: {
InterfaceName: intName,
},
},
},
}
_, err = libpodNet.Setup(netNSContainer.Path(), opts)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("interface eth0 already exists on container namespace"))
})
})
})
func runNetListener(wg *sync.WaitGroup, protocol, ip string, port int, expectedData string) {
switch protocol {
case "tcp":
ln, err := net.Listen(protocol, net.JoinHostPort(ip, strconv.Itoa(port)))
Expect(err).To(BeNil())
// make sure to read in a separate goroutine to not block
go func() {
defer GinkgoRecover()
defer wg.Done()
defer ln.Close()
conn, err := ln.Accept()
Expect(err).To(BeNil())
defer conn.Close()
conn.SetDeadline(time.Now().Add(1 * time.Second))
data, err := ioutil.ReadAll(conn)
Expect(err).To(BeNil())
Expect(string(data)).To(Equal(expectedData))
}()
case "udp":
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.ParseIP(ip),
Port: port,
})
Expect(err).To(BeNil())
conn.SetDeadline(time.Now().Add(1 * time.Second))
go func() {
defer GinkgoRecover()
defer wg.Done()
defer conn.Close()
data := make([]byte, len(expectedData))
i, err := conn.Read(data)
Expect(err).To(BeNil())
Expect(i).To(Equal(len(expectedData)))
Expect(string(data)).To(Equal(expectedData))
}()
default:
Fail("unsupported protocol")
}
}
|