forked from moby/libnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnetwork_test.go
2329 lines (1989 loc) · 50.5 KB
/
libnetwork_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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package libnetwork_test
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/plugins"
"github.com/docker/docker/pkg/reexec"
"github.com/docker/libnetwork"
"github.com/docker/libnetwork/config"
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/ipamapi"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/options"
"github.com/docker/libnetwork/osl"
"github.com/docker/libnetwork/testutils"
"github.com/docker/libnetwork/types"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
const (
bridgeNetType = "bridge"
)
var controller libnetwork.NetworkController
func TestMain(m *testing.M) {
if reexec.Init() {
return
}
if err := createController(); err != nil {
log.Errorf("Error creating controller: %v", err)
os.Exit(1)
}
//libnetwork.SetTestDataStore(controller, datastore.NewCustomDataStore(datastore.NewMockStore()))
x := m.Run()
controller.Stop()
os.Exit(x)
}
func createController() error {
var err error
// Cleanup local datastore file
os.Remove(datastore.DefaultScopes("")[datastore.LocalScope].Client.Address)
option := options.Generic{
"EnableIPForwarding": true,
}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = option
cfgOptions, err := libnetwork.OptionBoltdbWithRandomDBFile()
if err != nil {
return err
}
controller, err = libnetwork.New(append(cfgOptions, config.OptionDriverConfig(bridgeNetType, genericOption))...)
if err != nil {
return err
}
return nil
}
func createTestNetwork(networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) {
return controller.NewNetwork(networkType, networkName,
libnetwork.NetworkOptionGeneric(netOption),
libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs))
}
func getEmptyGenericOption() map[string]interface{} {
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = options.Generic{}
return genericOption
}
func getPortMapping() []types.PortBinding {
return []types.PortBinding{
{Proto: types.TCP, Port: uint16(230), HostPort: uint16(23000)},
{Proto: types.UDP, Port: uint16(200), HostPort: uint16(22000)},
{Proto: types.TCP, Port: uint16(120), HostPort: uint16(12000)},
{Proto: types.TCP, Port: uint16(320), HostPort: uint16(32000), HostPortEnd: uint16(32999)},
{Proto: types.UDP, Port: uint16(420), HostPort: uint16(42000), HostPortEnd: uint16(42001)},
}
}
func TestNull(t *testing.T) {
cnt, err := controller.NewSandbox("null_container",
libnetwork.OptionHostname("test"),
libnetwork.OptionDomainname("docker.io"),
libnetwork.OptionExtraHost("web", "192.168.0.1"))
if err != nil {
t.Fatal(err)
}
network, err := createTestNetwork("null", "testnull", options.Generic{}, nil, nil)
if err != nil {
t.Fatal(err)
}
ep, err := network.CreateEndpoint("testep")
if err != nil {
t.Fatal(err)
}
err = ep.Join(cnt)
if err != nil {
t.Fatal(err)
}
err = ep.Leave(cnt)
if err != nil {
t.Fatal(err)
}
if err := ep.Delete(); err != nil {
t.Fatal(err)
}
if err := cnt.Delete(); err != nil {
t.Fatal(err)
}
// host type is special network. Cannot be removed.
err = network.Delete()
if err == nil {
t.Fatal(err)
}
if _, ok := err.(types.ForbiddenError); !ok {
t.Fatalf("Unexpected error type")
}
}
func TestHost(t *testing.T) {
sbx1, err := controller.NewSandbox("host_c1",
libnetwork.OptionHostname("test1"),
libnetwork.OptionDomainname("docker.io"),
libnetwork.OptionExtraHost("web", "192.168.0.1"),
libnetwork.OptionUseDefaultSandbox())
if err != nil {
t.Fatal(err)
}
defer func() {
if err := sbx1.Delete(); err != nil {
t.Fatal(err)
}
}()
sbx2, err := controller.NewSandbox("host_c2",
libnetwork.OptionHostname("test2"),
libnetwork.OptionDomainname("docker.io"),
libnetwork.OptionExtraHost("web", "192.168.0.1"),
libnetwork.OptionUseDefaultSandbox())
if err != nil {
t.Fatal(err)
}
defer func() {
if err := sbx2.Delete(); err != nil {
t.Fatal(err)
}
}()
network, err := createTestNetwork("host", "testhost", options.Generic{}, nil, nil)
if err != nil {
t.Fatal(err)
}
ep1, err := network.CreateEndpoint("testep1")
if err != nil {
t.Fatal(err)
}
if err := ep1.Join(sbx1); err != nil {
t.Fatal(err)
}
ep2, err := network.CreateEndpoint("testep2")
if err != nil {
t.Fatal(err)
}
if err := ep2.Join(sbx2); err != nil {
t.Fatal(err)
}
if err := ep1.Leave(sbx1); err != nil {
t.Fatal(err)
}
if err := ep2.Leave(sbx2); err != nil {
t.Fatal(err)
}
if err := ep1.Delete(); err != nil {
t.Fatal(err)
}
if err := ep2.Delete(); err != nil {
t.Fatal(err)
}
// Try to create another host endpoint and join/leave that.
cnt3, err := controller.NewSandbox("host_c3",
libnetwork.OptionHostname("test3"),
libnetwork.OptionDomainname("docker.io"),
libnetwork.OptionExtraHost("web", "192.168.0.1"),
libnetwork.OptionUseDefaultSandbox())
if err != nil {
t.Fatal(err)
}
defer func() {
if err := cnt3.Delete(); err != nil {
t.Fatal(err)
}
}()
ep3, err := network.CreateEndpoint("testep3")
if err != nil {
t.Fatal(err)
}
if err := ep3.Join(sbx2); err != nil {
t.Fatal(err)
}
if err := ep3.Leave(sbx2); err != nil {
t.Fatal(err)
}
if err := ep3.Delete(); err != nil {
t.Fatal(err)
}
// host type is special network. Cannot be removed.
err = network.Delete()
if err == nil {
t.Fatal(err)
}
if _, ok := err.(types.ForbiddenError); !ok {
t.Fatalf("Unexpected error type")
}
}
func TestBridge(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testnetwork",
"EnableIPv6": true,
"EnableICC": true,
"EnableIPMasquerade": true,
},
}
ipamV4ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "192.168.100.0/24", Gateway: "192.168.100.1"}}
ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe90::/64", Gateway: "fe90::22"}}
network, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, ipamV4ConfList, ipamV6ConfList)
if err != nil {
t.Fatal(err)
}
ep, err := network.CreateEndpoint("testep", libnetwork.CreateOptionPortMapping(getPortMapping()))
if err != nil {
t.Fatal(err)
}
epInfo, err := ep.DriverInfo()
if err != nil {
t.Fatal(err)
}
pmd, ok := epInfo[netlabel.PortMap]
if !ok {
t.Fatalf("Could not find expected info in endpoint data")
}
pm, ok := pmd.([]types.PortBinding)
if !ok {
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
}
if len(pm) != 5 {
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
}
if err := ep.Delete(); err != nil {
t.Fatal(err)
}
if err := network.Delete(); err != nil {
t.Fatal(err)
}
}
// Testing IPV6 from MAC address
func TestBridgeIpv6FromMac(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testipv6mac",
"EnableIPv6": true,
"EnableICC": true,
"EnableIPMasquerade": true,
},
}
ipamV4ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "192.168.100.0/24", Gateway: "192.168.100.1"}}
ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe90::/64", Gateway: "fe90::22"}}
network, err := controller.NewNetwork(bridgeNetType, "testipv6mac",
libnetwork.NetworkOptionGeneric(netOption),
libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4ConfList, ipamV6ConfList),
libnetwork.NetworkOptionDeferIPv6Alloc(true))
if err != nil {
t.Fatal(err)
}
mac := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
epOption := options.Generic{netlabel.MacAddress: mac}
ep, err := network.CreateEndpoint("testep", libnetwork.EndpointOptionGeneric(epOption))
if err != nil {
t.Fatal(err)
}
iface := ep.Info().Iface()
if !bytes.Equal(iface.MacAddress(), mac) {
t.Fatalf("Unexpected mac address: %v", iface.MacAddress())
}
ip, expIP, _ := net.ParseCIDR("fe90::aabb:ccdd:eeff/64")
expIP.IP = ip
if !types.CompareIPNet(expIP, iface.AddressIPv6()) {
t.Fatalf("Expected %v. Got: %v", expIP, iface.AddressIPv6())
}
if err := ep.Delete(); err != nil {
t.Fatal(err)
}
if err := network.Delete(); err != nil {
t.Fatal(err)
}
}
func TestUnknownDriver(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{}, nil, nil)
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(types.NotFoundError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
func TestNilRemoteDriver(t *testing.T) {
_, err := controller.NewNetwork("framerelay", "dummy",
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(types.NotFoundError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
func TestNetworkName(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testnetwork",
},
}
_, err := createTestNetwork(bridgeNetType, "", netOption, nil, nil)
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
t.Fatalf("Expected to fail with ErrInvalidName error. Got %v", err)
}
networkName := "testnetwork"
n, err := createTestNetwork(bridgeNetType, networkName, netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := n.Delete(); err != nil {
t.Fatal(err)
}
}()
if n.Name() != networkName {
t.Fatalf("Expected network name %s, got %s", networkName, n.Name())
}
}
func TestNetworkType(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testnetwork",
},
}
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := n.Delete(); err != nil {
t.Fatal(err)
}
}()
if n.Type() != bridgeNetType {
t.Fatalf("Expected network type %s, got %s", bridgeNetType, n.Type())
}
}
func TestNetworkID(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testnetwork",
},
}
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := n.Delete(); err != nil {
t.Fatal(err)
}
}()
if n.ID() == "" {
t.Fatal("Expected non-empty network id")
}
}
func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
"BridgeName": "testnetwork",
}
option := options.Generic{
netlabel.GenericData: netOption,
}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, nil, nil)
if err != nil {
t.Fatal(err)
}
ep, err := network.CreateEndpoint("testep")
if err != nil {
t.Fatal(err)
}
err = network.Delete()
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(*libnetwork.ActiveEndpointsError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
// Done testing. Now cleanup.
if err := ep.Delete(); err != nil {
t.Fatal(err)
}
if err := network.Delete(); err != nil {
t.Fatal(err)
}
}
func TestUnknownNetwork(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
"BridgeName": "testnetwork",
}
option := options.Generic{
netlabel.GenericData: netOption,
}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, nil, nil)
if err != nil {
t.Fatal(err)
}
err = network.Delete()
if err != nil {
t.Fatal(err)
}
err = network.Delete()
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(*libnetwork.UnknownNetworkError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
func TestUnknownEndpoint(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
"BridgeName": "testnetwork",
}
option := options.Generic{
netlabel.GenericData: netOption,
}
ipamV4ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "192.168.100.0/24"}}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, ipamV4ConfList, nil)
if err != nil {
t.Fatal(err)
}
_, err = network.CreateEndpoint("")
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
t.Fatalf("Expected to fail with ErrInvalidName error. Actual error: %v", err)
}
ep, err := network.CreateEndpoint("testep")
if err != nil {
t.Fatal(err)
}
err = ep.Delete()
if err != nil {
t.Fatal(err)
}
// Done testing. Now cleanup
if err := network.Delete(); err != nil {
t.Fatal(err)
}
}
func TestNetworkEndpointsWalkers(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
// Create network 1 and add 2 endpoint: ep11, ep12
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "network1",
},
}
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := net1.Delete(); err != nil {
t.Fatal(err)
}
}()
ep11, err := net1.CreateEndpoint("ep11")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ep11.Delete(); err != nil {
t.Fatal(err)
}
}()
ep12, err := net1.CreateEndpoint("ep12")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ep12.Delete(); err != nil {
t.Fatal(err)
}
}()
// Test list methods on net1
epList1 := net1.Endpoints()
if len(epList1) != 2 {
t.Fatalf("Endpoints() returned wrong number of elements: %d instead of 2", len(epList1))
}
// endpoint order is not guaranteed
for _, e := range epList1 {
if e != ep11 && e != ep12 {
t.Fatal("Endpoints() did not return all the expected elements")
}
}
// Test Endpoint Walk method
var epName string
var epWanted libnetwork.Endpoint
wlk := func(ep libnetwork.Endpoint) bool {
if ep.Name() == epName {
epWanted = ep
return true
}
return false
}
// Look for ep1 on network1
epName = "ep11"
net1.WalkEndpoints(wlk)
if epWanted == nil {
t.Fatal(err)
}
if ep11 != epWanted {
t.Fatal(err)
}
current := len(controller.Networks())
// Create network 2
netOption = options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "network2",
},
}
net2, err := createTestNetwork(bridgeNetType, "network2", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := net2.Delete(); err != nil {
t.Fatal(err)
}
}()
// Test Networks method
if len(controller.Networks()) != current+1 {
t.Fatalf("Did not find the expected number of networks")
}
// Test Network Walk method
var netName string
var netWanted libnetwork.Network
nwWlk := func(nw libnetwork.Network) bool {
if nw.Name() == netName {
netWanted = nw
return true
}
return false
}
// Look for network named "network1" and "network2"
netName = "network1"
controller.WalkNetworks(nwWlk)
if netWanted == nil {
t.Fatal(err)
}
if net1.ID() != netWanted.ID() {
t.Fatal(err)
}
netName = "network2"
controller.WalkNetworks(nwWlk)
if netWanted == nil {
t.Fatal(err)
}
if net2.ID() != netWanted.ID() {
t.Fatal(err)
}
}
func TestDuplicateEndpoint(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "testnetwork",
},
}
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := n.Delete(); err != nil {
t.Fatal(err)
}
}()
ep, err := n.CreateEndpoint("ep1")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ep.Delete(); err != nil {
t.Fatal(err)
}
}()
ep2, err := n.CreateEndpoint("ep1")
defer func() {
// Cleanup ep2 as well, else network cleanup might fail for failure cases
if ep2 != nil {
if err := ep2.Delete(); err != nil {
t.Fatal(err)
}
}
}()
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(types.ForbiddenError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
func TestControllerQuery(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
// Create network 1
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "network1",
},
}
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := net1.Delete(); err != nil {
t.Fatal(err)
}
}()
// Create network 2
netOption = options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "network2",
},
}
net2, err := createTestNetwork(bridgeNetType, "network2", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := net2.Delete(); err != nil {
t.Fatal(err)
}
}()
_, err = controller.NetworkByName("")
if err == nil {
t.Fatalf("NetworkByName() succeeded with invalid target name")
}
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
t.Fatalf("Expected NetworkByName() to fail with ErrInvalidName error. Got: %v", err)
}
_, err = controller.NetworkByID("")
if err == nil {
t.Fatalf("NetworkByID() succeeded with invalid target id")
}
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
}
g, err := controller.NetworkByID("network1")
if err == nil {
t.Fatalf("Unexpected success for NetworkByID(): %v", g)
}
if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
}
g, err = controller.NetworkByName("network1")
if err != nil {
t.Fatalf("Unexpected failure for NetworkByName(): %v", err)
}
if g == nil {
t.Fatalf("NetworkByName() did not find the network")
}
if g != net1 {
t.Fatalf("NetworkByName() returned the wrong network")
}
g, err = controller.NetworkByID(net1.ID())
if err != nil {
t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
}
if net1.ID() != g.ID() {
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
}
g, err = controller.NetworkByName("network2")
if err != nil {
t.Fatalf("Unexpected failure for NetworkByName(): %v", err)
}
if g == nil {
t.Fatalf("NetworkByName() did not find the network")
}
if g != net2 {
t.Fatalf("NetworkByName() returned the wrong network")
}
g, err = controller.NetworkByID(net2.ID())
if err != nil {
t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
}
if net2.ID() != g.ID() {
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
}
}
func TestNetworkQuery(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
// Create network 1 and add 2 endpoint: ep11, ep12
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": "network1",
},
}
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := net1.Delete(); err != nil {
t.Fatal(err)
}
}()
ep11, err := net1.CreateEndpoint("ep11")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ep11.Delete(); err != nil {
t.Fatal(err)
}
}()
ep12, err := net1.CreateEndpoint("ep12")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ep12.Delete(); err != nil {
t.Fatal(err)
}
}()
e, err := net1.EndpointByName("ep11")
if err != nil {
t.Fatal(err)
}
if ep11 != e {
t.Fatalf("EndpointByName() returned %v instead of %v", e, ep11)
}
e, err = net1.EndpointByName("")
if err == nil {
t.Fatalf("EndpointByName() succeeded with invalid target name")
}
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
t.Fatalf("Expected EndpointByName() to fail with ErrInvalidName error. Got: %v", err)
}
e, err = net1.EndpointByName("IamNotAnEndpoint")
if err == nil {
t.Fatalf("EndpointByName() succeeded with unknown target name")
}
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
t.Fatal(err)
}
if e != nil {
t.Fatalf("EndpointByName(): expected nil, got %v", e)
}
e, err = net1.EndpointByID(ep12.ID())
if err != nil {
t.Fatal(err)
}
if ep12.ID() != e.ID() {
t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
}
e, err = net1.EndpointByID("")
if err == nil {
t.Fatalf("EndpointByID() succeeded with invalid target id")
}
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
t.Fatalf("EndpointByID() failed with unexpected error: %v", err)
}
}
const containerID = "valid_c"
func checkSandbox(t *testing.T, info libnetwork.EndpointInfo) {
origns, err := netns.Get()
if err != nil {
t.Fatalf("Could not get the current netns: %v", err)
}
defer origns.Close()
key := info.Sandbox().Key()
f, err := os.OpenFile(key, os.O_RDONLY, 0)
if err != nil {
t.Fatalf("Failed to open network namespace path %q: %v", key, err)
}
defer f.Close()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
nsFD := f.Fd()
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
t.Fatalf("Setting to the namespace pointed to by the sandbox %s failed: %v", key, err)
}
defer netns.Set(origns)
_, err = netlink.LinkByName("eth0")
if err != nil {
t.Fatalf("Could not find the interface eth0 inside the sandbox: %v", err)
}
_, err = netlink.LinkByName("eth1")
if err != nil {
t.Fatalf("Could not find the interface eth1 inside the sandbox: %v", err)
}
}