Browse code

libnetwork_test: remove in-container special case

The SetupTestOSContext calls were made conditional in
https://github.com/moby/libnetwork/pull/148 to work around limitations
in runtime.LockOSThread() which existed before Go 1.10. This workaround
is no longer necessary now that runtime.UnlockOSThread() needs to be
called an equal number of times before the goroutine is unlocked from
the OS thread.

Unfortunately some tests break when SetupTestOSContext is not skipped.
(Evidently this code path has not been exercised in a long time.) A
newly-created network namespace is very barebones: it contains a
loopback interface in the down state and little else. Even pinging
localhost does not work inside of a brand new namespace. Set the
loopback interface to up during namespace setup so that tests which
need to use the loopback interface can do so.

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2022/11/05 10:27:38
Showing 8 changed files
... ...
@@ -180,9 +180,7 @@ func getIPv4Data(t *testing.T, iface string) []driverapi.IPAMData {
180 180
 }
181 181
 
182 182
 func TestCreateFullOptions(t *testing.T) {
183
-	if !testutils.IsRunningInContainer() {
184
-		defer testutils.SetupTestOSContext(t)()
185
-	}
183
+	defer testutils.SetupTestOSContext(t)()
186 184
 	d := newDriver()
187 185
 
188 186
 	config := &configuration{
... ...
@@ -236,9 +234,7 @@ func TestCreateFullOptions(t *testing.T) {
236 236
 }
237 237
 
238 238
 func TestCreateNoConfig(t *testing.T) {
239
-	if !testutils.IsRunningInContainer() {
240
-		defer testutils.SetupTestOSContext(t)()
241
-	}
239
+	defer testutils.SetupTestOSContext(t)()
242 240
 	d := newDriver()
243 241
 
244 242
 	netconfig := &networkConfiguration{BridgeName: DefaultBridgeName}
... ...
@@ -251,9 +247,7 @@ func TestCreateNoConfig(t *testing.T) {
251 251
 }
252 252
 
253 253
 func TestCreateFullOptionsLabels(t *testing.T) {
254
-	if !testutils.IsRunningInContainer() {
255
-		defer testutils.SetupTestOSContext(t)()
256
-	}
254
+	defer testutils.SetupTestOSContext(t)()
257 255
 	d := newDriver()
258 256
 
259 257
 	config := &configuration{
... ...
@@ -359,9 +353,7 @@ func TestCreateFullOptionsLabels(t *testing.T) {
359 359
 }
360 360
 
361 361
 func TestCreate(t *testing.T) {
362
-	if !testutils.IsRunningInContainer() {
363
-		defer testutils.SetupTestOSContext(t)()
364
-	}
362
+	defer testutils.SetupTestOSContext(t)()
365 363
 
366 364
 	d := newDriver()
367 365
 
... ...
@@ -387,9 +379,7 @@ func TestCreate(t *testing.T) {
387 387
 }
388 388
 
389 389
 func TestCreateFail(t *testing.T) {
390
-	if !testutils.IsRunningInContainer() {
391
-		defer testutils.SetupTestOSContext(t)()
392
-	}
390
+	defer testutils.SetupTestOSContext(t)()
393 391
 
394 392
 	d := newDriver()
395 393
 
... ...
@@ -407,9 +397,7 @@ func TestCreateFail(t *testing.T) {
407 407
 }
408 408
 
409 409
 func TestCreateMultipleNetworks(t *testing.T) {
410
-	if !testutils.IsRunningInContainer() {
411
-		defer testutils.SetupTestOSContext(t)()
412
-	}
410
+	defer testutils.SetupTestOSContext(t)()
413 411
 
414 412
 	d := newDriver()
415 413
 
... ...
@@ -617,9 +605,7 @@ func TestQueryEndpointInfoHairpin(t *testing.T) {
617 617
 }
618 618
 
619 619
 func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) {
620
-	if !testutils.IsRunningInContainer() {
621
-		defer testutils.SetupTestOSContext(t)()
622
-	}
620
+	defer testutils.SetupTestOSContext(t)()
623 621
 	d := newDriver()
624 622
 
625 623
 	config := &configuration{
... ...
@@ -720,9 +706,7 @@ func getPortMapping() []types.PortBinding {
720 720
 }
721 721
 
722 722
 func TestLinkContainers(t *testing.T) {
723
-	if !testutils.IsRunningInContainer() {
724
-		defer testutils.SetupTestOSContext(t)()
725
-	}
723
+	defer testutils.SetupTestOSContext(t)()
726 724
 
727 725
 	d := newDriver()
728 726
 	iptable := iptables.GetIptable(iptables.IPv4)
... ...
@@ -876,9 +860,7 @@ func TestLinkContainers(t *testing.T) {
876 876
 }
877 877
 
878 878
 func TestValidateConfig(t *testing.T) {
879
-	if !testutils.IsRunningInContainer() {
880
-		defer testutils.SetupTestOSContext(t)()
881
-	}
879
+	defer testutils.SetupTestOSContext(t)()
882 880
 
883 881
 	// Test mtu
884 882
 	c := networkConfiguration{Mtu: -2}
... ...
@@ -949,9 +931,7 @@ func TestValidateConfig(t *testing.T) {
949 949
 }
950 950
 
951 951
 func TestSetDefaultGw(t *testing.T) {
952
-	if !testutils.IsRunningInContainer() {
953
-		defer testutils.SetupTestOSContext(t)()
954
-	}
952
+	defer testutils.SetupTestOSContext(t)()
955 953
 
956 954
 	d := newDriver()
957 955
 
... ...
@@ -1095,9 +1075,7 @@ func TestCreateWithExistingBridge(t *testing.T) {
1095 1095
 }
1096 1096
 
1097 1097
 func TestCreateParallel(t *testing.T) {
1098
-	if !testutils.IsRunningInContainer() {
1099
-		defer testutils.SetupTestOSContext(t)()
1100
-	}
1098
+	defer testutils.SetupTestOSContext(t)()
1101 1099
 
1102 1100
 	d := newDriver()
1103 1101
 
... ...
@@ -13,9 +13,7 @@ import (
13 13
 )
14 14
 
15 15
 func TestHostsEntries(t *testing.T) {
16
-	if !testutils.IsRunningInContainer() {
17
-		defer testutils.SetupTestOSContext(t)()
18
-	}
16
+	defer testutils.SetupTestOSContext(t)()
19 17
 
20 18
 	expectedHostsFile := `127.0.0.1	localhost
21 19
 ::1	localhost ip6-localhost ip6-loopback
... ...
@@ -562,9 +562,7 @@ func TestServiceVIPReuse(t *testing.T) {
562 562
 func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
563 563
 	skip.If(t, runtime.GOOS == "windows", "test only works on linux")
564 564
 
565
-	if !testutils.IsRunningInContainer() {
566
-		defer testutils.SetupTestOSContext(t)()
567
-	}
565
+	defer testutils.SetupTestOSContext(t)()
568 566
 
569 567
 	cfgOptions, err := OptionBoltdbWithRandomDBFile()
570 568
 	if err != nil {
... ...
@@ -62,13 +62,9 @@ func createGlobalInstance(t *testing.T) {
62 62
 		t.Fatal(err)
63 63
 	}
64 64
 
65
-	if testutils.IsRunningInContainer() {
66
-		testns = origins
67
-	} else {
68
-		testns, err = netns.New()
69
-		if err != nil {
70
-			t.Fatal(err)
71
-		}
65
+	testns, err = netns.New()
66
+	if err != nil {
67
+		t.Fatal(err)
72 68
 	}
73 69
 
74 70
 	netOption := options.Generic{
... ...
@@ -207,9 +203,7 @@ func TestHost(t *testing.T) {
207 207
 
208 208
 // Testing IPV6 from MAC address
209 209
 func TestBridgeIpv6FromMac(t *testing.T) {
210
-	if !testutils.IsRunningInContainer() {
211
-		defer testutils.SetupTestOSContext(t)()
212
-	}
210
+	defer testutils.SetupTestOSContext(t)()
213 211
 
214 212
 	netOption := options.Generic{
215 213
 		netlabel.GenericData: options.Generic{
... ...
@@ -283,9 +277,7 @@ func checkSandbox(t *testing.T, info libnetwork.EndpointInfo) {
283 283
 }
284 284
 
285 285
 func TestEndpointJoin(t *testing.T) {
286
-	if !testutils.IsRunningInContainer() {
287
-		defer testutils.SetupTestOSContext(t)()
288
-	}
286
+	defer testutils.SetupTestOSContext(t)()
289 287
 
290 288
 	// Create network 1 and add 2 endpoint: ep11, ep12
291 289
 	netOption := options.Generic{
... ...
@@ -459,9 +451,7 @@ func TestExternalKey(t *testing.T) {
459 459
 }
460 460
 
461 461
 func externalKeyTest(t *testing.T, reexec bool) {
462
-	if !testutils.IsRunningInContainer() {
463
-		defer testutils.SetupTestOSContext(t)()
464
-	}
462
+	defer testutils.SetupTestOSContext(t)()
465 463
 
466 464
 	n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
467 465
 		netlabel.GenericData: options.Generic{
... ...
@@ -621,9 +611,7 @@ func reexecSetKey(key string, containerID string, controllerID string) error {
621 621
 }
622 622
 
623 623
 func TestEnableIPv6(t *testing.T) {
624
-	if !testutils.IsRunningInContainer() {
625
-		defer testutils.SetupTestOSContext(t)()
626
-	}
624
+	defer testutils.SetupTestOSContext(t)()
627 625
 
628 626
 	tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
629 627
 	expectedResolvConf := []byte("search pommesfrites.fr\nnameserver 127.0.0.11\nnameserver 2001:4860:4860::8888\noptions ndots:0\n")
... ...
@@ -699,9 +687,7 @@ func TestEnableIPv6(t *testing.T) {
699 699
 }
700 700
 
701 701
 func TestResolvConfHost(t *testing.T) {
702
-	if !testutils.IsRunningInContainer() {
703
-		defer testutils.SetupTestOSContext(t)()
704
-	}
702
+	defer testutils.SetupTestOSContext(t)()
705 703
 
706 704
 	tmpResolvConf := []byte("search localhost.net\nnameserver 127.0.0.1\nnameserver 2001:4860:4860::8888\n")
707 705
 
... ...
@@ -775,9 +761,7 @@ func TestResolvConfHost(t *testing.T) {
775 775
 }
776 776
 
777 777
 func TestResolvConf(t *testing.T) {
778
-	if !testutils.IsRunningInContainer() {
779
-		defer testutils.SetupTestOSContext(t)()
780
-	}
778
+	defer testutils.SetupTestOSContext(t)()
781 779
 
782 780
 	tmpResolvConf1 := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
783 781
 	tmpResolvConf2 := []byte("search pommesfrites.fr\nnameserver 112.34.56.78\nnameserver 2001:4860:4860::8888\n")
... ...
@@ -1069,9 +1053,7 @@ func TestParallel2(t *testing.T) {
1069 1069
 }
1070 1070
 
1071 1071
 func TestBridge(t *testing.T) {
1072
-	if !testutils.IsRunningInContainer() {
1073
-		defer testutils.SetupTestOSContext(t)()
1074
-	}
1072
+	defer testutils.SetupTestOSContext(t)()
1075 1073
 
1076 1074
 	netOption := options.Generic{
1077 1075
 		netlabel.EnableIPv6: true,
... ...
@@ -145,9 +145,7 @@ func TestNull(t *testing.T) {
145 145
 }
146 146
 
147 147
 func TestUnknownDriver(t *testing.T) {
148
-	if !testutils.IsRunningInContainer() {
149
-		defer testutils.SetupTestOSContext(t)()
150
-	}
148
+	defer testutils.SetupTestOSContext(t)()
151 149
 
152 150
 	_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{}, nil, nil)
153 151
 	if err == nil {
... ...
@@ -172,9 +170,7 @@ func TestNilRemoteDriver(t *testing.T) {
172 172
 }
173 173
 
174 174
 func TestNetworkName(t *testing.T) {
175
-	if !testutils.IsRunningInContainer() {
176
-		defer testutils.SetupTestOSContext(t)()
177
-	}
175
+	defer testutils.SetupTestOSContext(t)()
178 176
 
179 177
 	netOption := options.Generic{
180 178
 		netlabel.GenericData: options.Generic{
... ...
@@ -208,9 +204,7 @@ func TestNetworkName(t *testing.T) {
208 208
 }
209 209
 
210 210
 func TestNetworkType(t *testing.T) {
211
-	if !testutils.IsRunningInContainer() {
212
-		defer testutils.SetupTestOSContext(t)()
213
-	}
211
+	defer testutils.SetupTestOSContext(t)()
214 212
 
215 213
 	netOption := options.Generic{
216 214
 		netlabel.GenericData: options.Generic{
... ...
@@ -234,9 +228,7 @@ func TestNetworkType(t *testing.T) {
234 234
 }
235 235
 
236 236
 func TestNetworkID(t *testing.T) {
237
-	if !testutils.IsRunningInContainer() {
238
-		defer testutils.SetupTestOSContext(t)()
239
-	}
237
+	defer testutils.SetupTestOSContext(t)()
240 238
 
241 239
 	netOption := options.Generic{
242 240
 		netlabel.GenericData: options.Generic{
... ...
@@ -260,9 +252,7 @@ func TestNetworkID(t *testing.T) {
260 260
 }
261 261
 
262 262
 func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
263
-	if !testutils.IsRunningInContainer() {
264
-		defer testutils.SetupTestOSContext(t)()
265
-	}
263
+	defer testutils.SetupTestOSContext(t)()
266 264
 
267 265
 	netOption := options.Generic{
268 266
 		"BridgeName": "testnetwork",
... ...
@@ -301,9 +291,7 @@ func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
301 301
 }
302 302
 
303 303
 func TestNetworkConfig(t *testing.T) {
304
-	if !testutils.IsRunningInContainer() {
305
-		defer testutils.SetupTestOSContext(t)()
306
-	}
304
+	defer testutils.SetupTestOSContext(t)()
307 305
 
308 306
 	// Verify config network cannot inherit another config network
309 307
 	_, err := controller.NewNetwork("bridge", "config_network0", "",
... ...
@@ -403,9 +391,7 @@ func TestNetworkConfig(t *testing.T) {
403 403
 }
404 404
 
405 405
 func TestUnknownNetwork(t *testing.T) {
406
-	if !testutils.IsRunningInContainer() {
407
-		defer testutils.SetupTestOSContext(t)()
408
-	}
406
+	defer testutils.SetupTestOSContext(t)()
409 407
 
410 408
 	netOption := options.Generic{
411 409
 		"BridgeName": "testnetwork",
... ...
@@ -435,9 +421,7 @@ func TestUnknownNetwork(t *testing.T) {
435 435
 }
436 436
 
437 437
 func TestUnknownEndpoint(t *testing.T) {
438
-	if !testutils.IsRunningInContainer() {
439
-		defer testutils.SetupTestOSContext(t)()
440
-	}
438
+	defer testutils.SetupTestOSContext(t)()
441 439
 
442 440
 	netOption := options.Generic{
443 441
 		"BridgeName": "testnetwork",
... ...
@@ -477,9 +461,7 @@ func TestUnknownEndpoint(t *testing.T) {
477 477
 }
478 478
 
479 479
 func TestNetworkEndpointsWalkers(t *testing.T) {
480
-	if !testutils.IsRunningInContainer() {
481
-		defer testutils.SetupTestOSContext(t)()
482
-	}
480
+	defer testutils.SetupTestOSContext(t)()
483 481
 
484 482
 	// Create network 1 and add 2 endpoint: ep11, ep12
485 483
 	netOption := options.Generic{
... ...
@@ -607,9 +589,7 @@ func TestNetworkEndpointsWalkers(t *testing.T) {
607 607
 }
608 608
 
609 609
 func TestDuplicateEndpoint(t *testing.T) {
610
-	if !testutils.IsRunningInContainer() {
611
-		defer testutils.SetupTestOSContext(t)()
612
-	}
610
+	defer testutils.SetupTestOSContext(t)()
613 611
 
614 612
 	netOption := options.Generic{
615 613
 		netlabel.GenericData: options.Generic{
... ...
@@ -656,9 +636,7 @@ func TestDuplicateEndpoint(t *testing.T) {
656 656
 }
657 657
 
658 658
 func TestControllerQuery(t *testing.T) {
659
-	if !testutils.IsRunningInContainer() {
660
-		defer testutils.SetupTestOSContext(t)()
661
-	}
659
+	defer testutils.SetupTestOSContext(t)()
662 660
 
663 661
 	// Create network 1
664 662
 	netOption := options.Generic{
... ...
@@ -758,9 +736,7 @@ func TestControllerQuery(t *testing.T) {
758 758
 }
759 759
 
760 760
 func TestNetworkQuery(t *testing.T) {
761
-	if !testutils.IsRunningInContainer() {
762
-		defer testutils.SetupTestOSContext(t)()
763
-	}
761
+	defer testutils.SetupTestOSContext(t)()
764 762
 
765 763
 	// Create network 1 and add 2 endpoint: ep11, ep12
766 764
 	netOption := options.Generic{
... ...
@@ -907,9 +883,7 @@ func (f *fakeSandbox) DisableService() error {
907 907
 }
908 908
 
909 909
 func TestEndpointDeleteWithActiveContainer(t *testing.T) {
910
-	if !testutils.IsRunningInContainer() {
911
-		defer testutils.SetupTestOSContext(t)()
912
-	}
910
+	defer testutils.SetupTestOSContext(t)()
913 911
 
914 912
 	n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
915 913
 		netlabel.GenericData: options.Generic{
... ...
@@ -982,9 +956,7 @@ func TestEndpointDeleteWithActiveContainer(t *testing.T) {
982 982
 }
983 983
 
984 984
 func TestEndpointMultipleJoins(t *testing.T) {
985
-	if !testutils.IsRunningInContainer() {
986
-		defer testutils.SetupTestOSContext(t)()
987
-	}
985
+	defer testutils.SetupTestOSContext(t)()
988 986
 
989 987
 	n, err := createTestNetwork(bridgeNetType, "testmultiple", options.Generic{
990 988
 		netlabel.GenericData: options.Generic{
... ...
@@ -1056,9 +1028,7 @@ func TestEndpointMultipleJoins(t *testing.T) {
1056 1056
 }
1057 1057
 
1058 1058
 func TestLeaveAll(t *testing.T) {
1059
-	if !testutils.IsRunningInContainer() {
1060
-		defer testutils.SetupTestOSContext(t)()
1061
-	}
1059
+	defer testutils.SetupTestOSContext(t)()
1062 1060
 
1063 1061
 	n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
1064 1062
 		netlabel.GenericData: options.Generic{
... ...
@@ -1121,9 +1091,7 @@ func TestLeaveAll(t *testing.T) {
1121 1121
 }
1122 1122
 
1123 1123
 func TestContainerInvalidLeave(t *testing.T) {
1124
-	if !testutils.IsRunningInContainer() {
1125
-		defer testutils.SetupTestOSContext(t)()
1126
-	}
1124
+	defer testutils.SetupTestOSContext(t)()
1127 1125
 
1128 1126
 	n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
1129 1127
 		netlabel.GenericData: options.Generic{
... ...
@@ -1187,9 +1155,7 @@ func TestContainerInvalidLeave(t *testing.T) {
1187 1187
 }
1188 1188
 
1189 1189
 func TestEndpointUpdateParent(t *testing.T) {
1190
-	if !testutils.IsRunningInContainer() {
1191
-		defer testutils.SetupTestOSContext(t)()
1192
-	}
1190
+	defer testutils.SetupTestOSContext(t)()
1193 1191
 
1194 1192
 	n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
1195 1193
 		netlabel.GenericData: options.Generic{
... ...
@@ -82,9 +82,7 @@ func TestSandboxAddEmpty(t *testing.T) {
82 82
 
83 83
 // // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order
84 84
 func TestSandboxAddMultiPrio(t *testing.T) {
85
-	if !testutils.IsRunningInContainer() {
86
-		defer testutils.SetupTestOSContext(t)()
87
-	}
85
+	defer testutils.SetupTestOSContext(t)()
88 86
 
89 87
 	opts := [][]NetworkOption{
90 88
 		{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
... ...
@@ -169,9 +167,7 @@ func TestSandboxAddMultiPrio(t *testing.T) {
169 169
 }
170 170
 
171 171
 func TestSandboxAddSamePrio(t *testing.T) {
172
-	if !testutils.IsRunningInContainer() {
173
-		defer testutils.SetupTestOSContext(t)()
174
-	}
172
+	defer testutils.SetupTestOSContext(t)()
175 173
 
176 174
 	opts := [][]NetworkOption{
177 175
 		{},
... ...
@@ -47,6 +47,17 @@ func SetupTestOSContext(t *testing.T) func() {
47 47
 	// sure to re-initialize initNs context
48 48
 	ns.Init()
49 49
 
50
+	nl := ns.NlHandle()
51
+	lo, err := nl.LinkByName("lo")
52
+	if err != nil {
53
+		restore()
54
+		t.Fatalf("Failed to get handle to loopback interface 'lo' in new netns: %v", err)
55
+	}
56
+	if err := nl.LinkSetUp(lo); err != nil {
57
+		restore()
58
+		t.Fatalf("Failed to enable loopback interface in new netns: %v", err)
59
+	}
60
+
50 61
 	return func() {
51 62
 		if err := newNS.Close(); err != nil {
52 63
 			t.Logf("Warning: netns closing failed (%v)", err)
53 64
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-package testutils
2
-
3
-import (
4
-	"os"
5
-)
6
-
7
-// IsRunningInContainer returns whether the test is running inside a container.
8
-func IsRunningInContainer() bool {
9
-	_, err := os.Stat("/.dockerenv")
10
-	return err == nil
11
-}