瀏覽代碼

Add tests for internal network

Signed-off-by: Chun Chen <ramichen@tencent.com>
Chun Chen 9 年之前
父節點
當前提交
59e1e42ce7

+ 6 - 0
libnetwork/api/api.go

@@ -8,6 +8,7 @@ import (
 	"strings"
 
 	"github.com/docker/libnetwork"
+	"github.com/docker/libnetwork/netlabel"
 	"github.com/docker/libnetwork/types"
 	"github.com/gorilla/mux"
 )
@@ -279,6 +280,11 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b
 	processCreateDefaults(c, &create)
 
 	options := []libnetwork.NetworkOption{}
+	if len(create.NetworkOpts) > 0 {
+		if _, ok := create.NetworkOpts[netlabel.Internal]; ok {
+			options = append(options, libnetwork.NetworkOptionInternalNetwork())
+		}
+	}
 	if len(create.DriverOpts) > 0 {
 		options = append(options, libnetwork.NetworkOptionDriverOpts(create.DriverOpts))
 	}

+ 1 - 0
libnetwork/api/types.go

@@ -37,6 +37,7 @@ type networkCreate struct {
 	Name        string            `json:"name"`
 	NetworkType string            `json:"network_type"`
 	DriverOpts  map[string]string `json:"driver_opts"`
+	NetworkOpts map[string]string `json:"network_opts"`
 }
 
 // endpointCreate represents the body of the "create endpoint" http request message

+ 7 - 2
libnetwork/client/network.go

@@ -9,6 +9,7 @@ import (
 
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/stringid"
+	"github.com/docker/libnetwork/netlabel"
 )
 
 type command struct {
@@ -41,15 +42,19 @@ func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
 func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
 	cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
 	flDriver := cmd.String([]string{"d", "-driver"}, "", "Driver to manage the Network")
+	flInternal := cmd.Bool([]string{"-internal"}, false, "Config the network to be internal")
 	cmd.Require(flag.Exact, 1)
 	err := cmd.ParseFlags(args, true)
 	if err != nil {
 		return err
 	}
-
+	networkOpts := make(map[string]string)
+	if *flInternal {
+		networkOpts[netlabel.Internal] = "true"
+	}
 	// Construct network create request body
 	var driverOpts []string
-	nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts}
+	nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts, NetworkOpts: networkOpts}
 	obj, _, err := readBody(cli.call("POST", "/networks", nc, nil))
 	if err != nil {
 		return err

+ 4 - 3
libnetwork/client/types.go

@@ -34,9 +34,10 @@ type SandboxResource struct {
 
 // networkCreate is the expected body of the "create network" http request message
 type networkCreate struct {
-	Name        string   `json:"name"`
-	NetworkType string   `json:"network_type"`
-	DriverOpts  []string `json:"driver_opts"`
+	Name        string            `json:"name"`
+	NetworkType string            `json:"network_type"`
+	DriverOpts  []string          `json:"driver_opts"`
+	NetworkOpts map[string]string `json:"network_opts"`
 }
 
 // serviceCreate represents the body of the "publish service" http request message

+ 12 - 3
libnetwork/test/integration/dnet/helpers.bash

@@ -280,7 +280,11 @@ function test_overlay() {
     end=3
     # Setup overlay network and connect containers ot it
     if [ -z "${2}" -o "${2}" != "skip_add" ]; then
-	dnet_cmd $(inst_id2port 1) network create -d overlay multihost
+        if [ -z "${2}" -o "${2}" != "internal" ]; then
+	    dnet_cmd $(inst_id2port 1) network create -d overlay multihost
+	else
+	    dnet_cmd $(inst_id2port 1) network create -d overlay --internal multihost
+	fi
     fi
 
     for i in `seq ${start} ${end}`;
@@ -292,8 +296,13 @@ function test_overlay() {
     # Now test connectivity between all the containers using service names
     for i in `seq ${start} ${end}`;
     do
-	runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
-	     "ping -c 1 www.google.com"
+	if [ -z "${2}" -o "${2}" != "internal" ]; then
+	    runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
+	        "ping -c 1 www.google.com"
+	else
+	    default_route=`runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) "ip route | grep default"`
+	    [ "$default_route" = "" ]
+	fi
 	for j in `seq ${start} ${end}`;
 	do
 	    if [ "$i" -eq "$j" ]; then

+ 5 - 0
libnetwork/test/integration/dnet/overlay-consul.bats

@@ -29,3 +29,8 @@ load helpers
     wait_for_dnet $(inst_id2port 3) dnet-3-consul
     test_overlay consul skip_add
 }
+
+@test "Test overlay network internal network with consul" {
+    skip_for_circleci
+    test_overlay consul internal
+}