Browse code

Add tests for internal network

Signed-off-by: Chun Chen <ramichen@tencent.com>

Chun Chen authored on 2015/12/22 10:31:50
Showing 6 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"strings"
9 9
 
10 10
 	"github.com/docker/libnetwork"
11
+	"github.com/docker/libnetwork/netlabel"
11 12
 	"github.com/docker/libnetwork/types"
12 13
 	"github.com/gorilla/mux"
13 14
 )
... ...
@@ -279,6 +280,11 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b
279 279
 	processCreateDefaults(c, &create)
280 280
 
281 281
 	options := []libnetwork.NetworkOption{}
282
+	if len(create.NetworkOpts) > 0 {
283
+		if _, ok := create.NetworkOpts[netlabel.Internal]; ok {
284
+			options = append(options, libnetwork.NetworkOptionInternalNetwork())
285
+		}
286
+	}
282 287
 	if len(create.DriverOpts) > 0 {
283 288
 		options = append(options, libnetwork.NetworkOptionDriverOpts(create.DriverOpts))
284 289
 	}
... ...
@@ -37,6 +37,7 @@ type networkCreate struct {
37 37
 	Name        string            `json:"name"`
38 38
 	NetworkType string            `json:"network_type"`
39 39
 	DriverOpts  map[string]string `json:"driver_opts"`
40
+	NetworkOpts map[string]string `json:"network_opts"`
40 41
 }
41 42
 
42 43
 // endpointCreate represents the body of the "create endpoint" http request message
... ...
@@ -9,6 +9,7 @@ import (
9 9
 
10 10
 	flag "github.com/docker/docker/pkg/mflag"
11 11
 	"github.com/docker/docker/pkg/stringid"
12
+	"github.com/docker/libnetwork/netlabel"
12 13
 )
13 14
 
14 15
 type command struct {
... ...
@@ -41,15 +42,19 @@ func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
41 41
 func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
42 42
 	cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
43 43
 	flDriver := cmd.String([]string{"d", "-driver"}, "", "Driver to manage the Network")
44
+	flInternal := cmd.Bool([]string{"-internal"}, false, "Config the network to be internal")
44 45
 	cmd.Require(flag.Exact, 1)
45 46
 	err := cmd.ParseFlags(args, true)
46 47
 	if err != nil {
47 48
 		return err
48 49
 	}
49
-
50
+	networkOpts := make(map[string]string)
51
+	if *flInternal {
52
+		networkOpts[netlabel.Internal] = "true"
53
+	}
50 54
 	// Construct network create request body
51 55
 	var driverOpts []string
52
-	nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts}
56
+	nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts, NetworkOpts: networkOpts}
53 57
 	obj, _, err := readBody(cli.call("POST", "/networks", nc, nil))
54 58
 	if err != nil {
55 59
 		return err
... ...
@@ -34,9 +34,10 @@ type SandboxResource struct {
34 34
 
35 35
 // networkCreate is the expected body of the "create network" http request message
36 36
 type networkCreate struct {
37
-	Name        string   `json:"name"`
38
-	NetworkType string   `json:"network_type"`
39
-	DriverOpts  []string `json:"driver_opts"`
37
+	Name        string            `json:"name"`
38
+	NetworkType string            `json:"network_type"`
39
+	DriverOpts  []string          `json:"driver_opts"`
40
+	NetworkOpts map[string]string `json:"network_opts"`
40 41
 }
41 42
 
42 43
 // serviceCreate represents the body of the "publish service" http request message
... ...
@@ -280,7 +280,11 @@ function test_overlay() {
280 280
     end=3
281 281
     # Setup overlay network and connect containers ot it
282 282
     if [ -z "${2}" -o "${2}" != "skip_add" ]; then
283
-	dnet_cmd $(inst_id2port 1) network create -d overlay multihost
283
+        if [ -z "${2}" -o "${2}" != "internal" ]; then
284
+	    dnet_cmd $(inst_id2port 1) network create -d overlay multihost
285
+	else
286
+	    dnet_cmd $(inst_id2port 1) network create -d overlay --internal multihost
287
+	fi
284 288
     fi
285 289
 
286 290
     for i in `seq ${start} ${end}`;
... ...
@@ -292,8 +296,13 @@ function test_overlay() {
292 292
     # Now test connectivity between all the containers using service names
293 293
     for i in `seq ${start} ${end}`;
294 294
     do
295
-	runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
296
-	     "ping -c 1 www.google.com"
295
+	if [ -z "${2}" -o "${2}" != "internal" ]; then
296
+	    runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
297
+	        "ping -c 1 www.google.com"
298
+	else
299
+	    default_route=`runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) "ip route | grep default"`
300
+	    [ "$default_route" = "" ]
301
+	fi
297 302
 	for j in `seq ${start} ${end}`;
298 303
 	do
299 304
 	    if [ "$i" -eq "$j" ]; then
... ...
@@ -29,3 +29,8 @@ load helpers
29 29
     wait_for_dnet $(inst_id2port 3) dnet-3-consul
30 30
     test_overlay consul skip_add
31 31
 }
32
+
33
+@test "Test overlay network internal network with consul" {
34
+    skip_for_circleci
35
+    test_overlay consul internal
36
+}
32 37
\ No newline at end of file