github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_network_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"os"
    13  	"strings"
    14  
    15  	"github.com/docker/docker/api/types"
    16  	"github.com/docker/docker/api/types/versions/v1p20"
    17  	"github.com/docker/docker/pkg/integration/checker"
    18  	"github.com/docker/libnetwork/driverapi"
    19  	remoteapi "github.com/docker/libnetwork/drivers/remote/api"
    20  	"github.com/docker/libnetwork/ipamapi"
    21  	remoteipam "github.com/docker/libnetwork/ipams/remote/api"
    22  	"github.com/docker/libnetwork/netlabel"
    23  	"github.com/go-check/check"
    24  	"github.com/vishvananda/netlink"
    25  )
    26  
    27  const dummyNetworkDriver = "dummy-network-driver"
    28  const dummyIpamDriver = "dummy-ipam-driver"
    29  
    30  var remoteDriverNetworkRequest remoteapi.CreateNetworkRequest
    31  
    32  func init() {
    33  	check.Suite(&DockerNetworkSuite{
    34  		ds: &DockerSuite{},
    35  	})
    36  }
    37  
    38  type DockerNetworkSuite struct {
    39  	server *httptest.Server
    40  	ds     *DockerSuite
    41  	d      *Daemon
    42  }
    43  
    44  func (s *DockerNetworkSuite) SetUpTest(c *check.C) {
    45  	s.d = NewDaemon(c)
    46  }
    47  
    48  func (s *DockerNetworkSuite) TearDownTest(c *check.C) {
    49  	s.d.Stop()
    50  	s.ds.TearDownTest(c)
    51  }
    52  
    53  func (s *DockerNetworkSuite) SetUpSuite(c *check.C) {
    54  	mux := http.NewServeMux()
    55  	s.server = httptest.NewServer(mux)
    56  	c.Assert(s.server, check.NotNil, check.Commentf("Failed to start a HTTP Server"))
    57  
    58  	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
    59  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    60  		fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
    61  	})
    62  
    63  	// Network driver implementation
    64  	mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
    65  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    66  		fmt.Fprintf(w, `{"Scope":"local"}`)
    67  	})
    68  
    69  	mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
    70  		err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
    71  		if err != nil {
    72  			http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
    73  			return
    74  		}
    75  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    76  		fmt.Fprintf(w, "null")
    77  	})
    78  
    79  	mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
    80  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    81  		fmt.Fprintf(w, "null")
    82  	})
    83  
    84  	mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
    85  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    86  		fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
    87  	})
    88  
    89  	mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
    90  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    91  
    92  		veth := &netlink.Veth{
    93  			LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0"}
    94  		if err := netlink.LinkAdd(veth); err != nil {
    95  			fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
    96  		} else {
    97  			fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
    98  		}
    99  	})
   100  
   101  	mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   102  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   103  		fmt.Fprintf(w, "null")
   104  	})
   105  
   106  	mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   107  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   108  		if link, err := netlink.LinkByName("cnt0"); err == nil {
   109  			netlink.LinkDel(link)
   110  		}
   111  		fmt.Fprintf(w, "null")
   112  	})
   113  
   114  	// Ipam Driver implementation
   115  	var (
   116  		poolRequest       remoteipam.RequestPoolRequest
   117  		poolReleaseReq    remoteipam.ReleasePoolRequest
   118  		addressRequest    remoteipam.RequestAddressRequest
   119  		addressReleaseReq remoteipam.ReleaseAddressRequest
   120  		lAS               = "localAS"
   121  		gAS               = "globalAS"
   122  		pool              = "172.28.0.0/16"
   123  		poolID            = lAS + "/" + pool
   124  		gw                = "172.28.255.254/16"
   125  	)
   126  
   127  	mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   128  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   129  		fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
   130  	})
   131  
   132  	mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   133  		err := json.NewDecoder(r.Body).Decode(&poolRequest)
   134  		if err != nil {
   135  			http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
   136  			return
   137  		}
   138  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   139  		if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
   140  			fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
   141  		} else if poolRequest.Pool != "" && poolRequest.Pool != pool {
   142  			fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
   143  		} else {
   144  			fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
   145  		}
   146  	})
   147  
   148  	mux.HandleFunc(fmt.Sprintf("/%s.RequestAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   149  		err := json.NewDecoder(r.Body).Decode(&addressRequest)
   150  		if err != nil {
   151  			http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
   152  			return
   153  		}
   154  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   155  		// make sure libnetwork is now querying on the expected pool id
   156  		if addressRequest.PoolID != poolID {
   157  			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
   158  		} else if addressRequest.Address != "" {
   159  			fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
   160  		} else {
   161  			fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
   162  		}
   163  	})
   164  
   165  	mux.HandleFunc(fmt.Sprintf("/%s.ReleaseAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   166  		err := json.NewDecoder(r.Body).Decode(&addressReleaseReq)
   167  		if err != nil {
   168  			http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
   169  			return
   170  		}
   171  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   172  		// make sure libnetwork is now asking to release the expected address fro mthe expected poolid
   173  		if addressRequest.PoolID != poolID {
   174  			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
   175  		} else if addressReleaseReq.Address != gw {
   176  			fmt.Fprintf(w, `{"Error":"unknown address"}`)
   177  		} else {
   178  			fmt.Fprintf(w, "null")
   179  		}
   180  	})
   181  
   182  	mux.HandleFunc(fmt.Sprintf("/%s.ReleasePool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
   183  		err := json.NewDecoder(r.Body).Decode(&poolReleaseReq)
   184  		if err != nil {
   185  			http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
   186  			return
   187  		}
   188  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
   189  		// make sure libnetwork is now asking to release the expected poolid
   190  		if addressRequest.PoolID != poolID {
   191  			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
   192  		} else {
   193  			fmt.Fprintf(w, "null")
   194  		}
   195  	})
   196  
   197  	err := os.MkdirAll("/etc/docker/plugins", 0755)
   198  	c.Assert(err, checker.IsNil)
   199  
   200  	fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", dummyNetworkDriver)
   201  	err = ioutil.WriteFile(fileName, []byte(s.server.URL), 0644)
   202  	c.Assert(err, checker.IsNil)
   203  
   204  	ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", dummyIpamDriver)
   205  	err = ioutil.WriteFile(ipamFileName, []byte(s.server.URL), 0644)
   206  	c.Assert(err, checker.IsNil)
   207  }
   208  
   209  func (s *DockerNetworkSuite) TearDownSuite(c *check.C) {
   210  	if s.server == nil {
   211  		return
   212  	}
   213  
   214  	s.server.Close()
   215  
   216  	err := os.RemoveAll("/etc/docker/plugins")
   217  	c.Assert(err, checker.IsNil)
   218  }
   219  
   220  func assertNwIsAvailable(c *check.C, name string) {
   221  	if !isNwPresent(c, name) {
   222  		c.Fatalf("Network %s not found in network ls o/p", name)
   223  	}
   224  }
   225  
   226  func assertNwNotAvailable(c *check.C, name string) {
   227  	if isNwPresent(c, name) {
   228  		c.Fatalf("Found network %s in network ls o/p", name)
   229  	}
   230  }
   231  
   232  func isNwPresent(c *check.C, name string) bool {
   233  	out, _ := dockerCmd(c, "network", "ls")
   234  	lines := strings.Split(out, "\n")
   235  	for i := 1; i < len(lines)-1; i++ {
   236  		if strings.Contains(lines[i], name) {
   237  			return true
   238  		}
   239  	}
   240  	return false
   241  }
   242  
   243  func getNwResource(c *check.C, name string) *types.NetworkResource {
   244  	out, _ := dockerCmd(c, "network", "inspect", name)
   245  	nr := []types.NetworkResource{}
   246  	err := json.Unmarshal([]byte(out), &nr)
   247  	c.Assert(err, check.IsNil)
   248  	return &nr[0]
   249  }
   250  
   251  func (s *DockerNetworkSuite) TestDockerNetworkLsDefault(c *check.C) {
   252  	defaults := []string{"bridge", "host", "none"}
   253  	for _, nn := range defaults {
   254  		assertNwIsAvailable(c, nn)
   255  	}
   256  }
   257  
   258  func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {
   259  	dockerCmd(c, "network", "create", "test")
   260  	assertNwIsAvailable(c, "test")
   261  
   262  	dockerCmd(c, "network", "rm", "test")
   263  	assertNwNotAvailable(c, "test")
   264  }
   265  
   266  func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
   267  	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
   268  	networkResources := []types.NetworkResource{}
   269  	err := json.Unmarshal([]byte(out), &networkResources)
   270  	c.Assert(err, check.IsNil)
   271  	c.Assert(networkResources, checker.HasLen, 2)
   272  
   273  	// Should print an error, return an exitCode 1 *but* should print the host network
   274  	out, exitCode, err := dockerCmdWithError("network", "inspect", "host", "nonexistent")
   275  	c.Assert(err, checker.NotNil)
   276  	c.Assert(exitCode, checker.Equals, 1)
   277  	c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
   278  	networkResources = []types.NetworkResource{}
   279  	inspectOut := strings.SplitN(out, "\n", 2)[1]
   280  	err = json.Unmarshal([]byte(inspectOut), &networkResources)
   281  	c.Assert(networkResources, checker.HasLen, 1)
   282  
   283  	// Should print an error and return an exitCode, nothing else
   284  	out, exitCode, err = dockerCmdWithError("network", "inspect", "nonexistent")
   285  	c.Assert(err, checker.NotNil)
   286  	c.Assert(exitCode, checker.Equals, 1)
   287  	c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
   288  }
   289  
   290  func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
   291  	dockerCmd(c, "network", "create", "test")
   292  	assertNwIsAvailable(c, "test")
   293  	nr := getNwResource(c, "test")
   294  
   295  	c.Assert(nr.Name, checker.Equals, "test")
   296  	c.Assert(len(nr.Containers), checker.Equals, 0)
   297  
   298  	// run a container
   299  	out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
   300  	c.Assert(waitRun("test"), check.IsNil)
   301  	containerID := strings.TrimSpace(out)
   302  
   303  	// connect the container to the test network
   304  	dockerCmd(c, "network", "connect", "test", containerID)
   305  
   306  	// inspect the network to make sure container is connected
   307  	nr = getNetworkResource(c, nr.ID)
   308  	c.Assert(len(nr.Containers), checker.Equals, 1)
   309  	c.Assert(nr.Containers[containerID], check.NotNil)
   310  
   311  	// check if container IP matches network inspect
   312  	ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
   313  	c.Assert(err, check.IsNil)
   314  	containerIP := findContainerIP(c, "test", "test")
   315  	c.Assert(ip.String(), checker.Equals, containerIP)
   316  
   317  	// disconnect container from the network
   318  	dockerCmd(c, "network", "disconnect", "test", containerID)
   319  	nr = getNwResource(c, "test")
   320  	c.Assert(nr.Name, checker.Equals, "test")
   321  	c.Assert(len(nr.Containers), checker.Equals, 0)
   322  
   323  	// check if network connect fails for inactive containers
   324  	dockerCmd(c, "stop", containerID)
   325  	_, _, err = dockerCmdWithError("network", "connect", "test", containerID)
   326  	c.Assert(err, check.NotNil)
   327  
   328  	dockerCmd(c, "network", "rm", "test")
   329  	assertNwNotAvailable(c, "test")
   330  }
   331  
   332  func (s *DockerNetworkSuite) TestDockerNetworkIpamMultipleNetworks(c *check.C) {
   333  	// test0 bridge network
   334  	dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test1")
   335  	assertNwIsAvailable(c, "test1")
   336  
   337  	// test2 bridge network does not overlap
   338  	dockerCmd(c, "network", "create", "--subnet=192.169.0.0/16", "test2")
   339  	assertNwIsAvailable(c, "test2")
   340  
   341  	// for networks w/o ipam specified, docker will choose proper non-overlapping subnets
   342  	dockerCmd(c, "network", "create", "test3")
   343  	assertNwIsAvailable(c, "test3")
   344  	dockerCmd(c, "network", "create", "test4")
   345  	assertNwIsAvailable(c, "test4")
   346  	dockerCmd(c, "network", "create", "test5")
   347  	assertNwIsAvailable(c, "test5")
   348  
   349  	// test network with multiple subnets
   350  	// bridge network doesnt support multiple subnets. hence, use a dummy driver that supports
   351  
   352  	dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16", "test6")
   353  	assertNwIsAvailable(c, "test6")
   354  
   355  	// test network with multiple subnets with valid ipam combinations
   356  	// also check same subnet across networks when the driver supports it.
   357  	dockerCmd(c, "network", "create", "-d", dummyNetworkDriver,
   358  		"--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16",
   359  		"--gateway=192.168.0.100", "--gateway=192.170.0.100",
   360  		"--ip-range=192.168.1.0/24",
   361  		"--aux-address", "a=192.168.1.5", "--aux-address", "b=192.168.1.6",
   362  		"--aux-address", "a=192.170.1.5", "--aux-address", "b=192.170.1.6",
   363  		"test7")
   364  	assertNwIsAvailable(c, "test7")
   365  
   366  	// cleanup
   367  	for i := 1; i < 8; i++ {
   368  		dockerCmd(c, "network", "rm", fmt.Sprintf("test%d", i))
   369  	}
   370  }
   371  
   372  func (s *DockerNetworkSuite) TestDockerNetworkCustomIpam(c *check.C) {
   373  	// Create a bridge network using custom ipam driver
   374  	dockerCmd(c, "network", "create", "--ipam-driver", dummyIpamDriver, "br0")
   375  	assertNwIsAvailable(c, "br0")
   376  
   377  	// Verify expected network ipam fields are there
   378  	nr := getNetworkResource(c, "br0")
   379  	c.Assert(nr.Driver, checker.Equals, "bridge")
   380  	c.Assert(nr.IPAM.Driver, checker.Equals, dummyIpamDriver)
   381  
   382  	// remove network and exercise remote ipam driver
   383  	dockerCmd(c, "network", "rm", "br0")
   384  	assertNwNotAvailable(c, "br0")
   385  }
   386  
   387  func (s *DockerNetworkSuite) TestDockerNetworkInspect(c *check.C) {
   388  	// if unspecified, network gateway will be selected from inside preferred pool
   389  	dockerCmd(c, "network", "create", "--driver=bridge", "--subnet=172.28.0.0/16", "--ip-range=172.28.5.0/24", "--gateway=172.28.5.254", "br0")
   390  	assertNwIsAvailable(c, "br0")
   391  
   392  	nr := getNetworkResource(c, "br0")
   393  	c.Assert(nr.Driver, checker.Equals, "bridge")
   394  	c.Assert(nr.Scope, checker.Equals, "local")
   395  	c.Assert(nr.IPAM.Driver, checker.Equals, "default")
   396  	c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
   397  	c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16")
   398  	c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24")
   399  	c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254")
   400  	dockerCmd(c, "network", "rm", "br0")
   401  }
   402  
   403  func (s *DockerNetworkSuite) TestDockerNetworkIpamInvalidCombinations(c *check.C) {
   404  	// network with ip-range out of subnet range
   405  	_, _, err := dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--ip-range=192.170.0.0/16", "test")
   406  	c.Assert(err, check.NotNil)
   407  
   408  	// network with multiple gateways for a single subnet
   409  	_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--gateway=192.168.0.1", "--gateway=192.168.0.2", "test")
   410  	c.Assert(err, check.NotNil)
   411  
   412  	// Multiple overlaping subnets in the same network must fail
   413  	_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--subnet=192.168.1.0/16", "test")
   414  	c.Assert(err, check.NotNil)
   415  
   416  	// overlapping subnets across networks must fail
   417  	// create a valid test0 network
   418  	dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test0")
   419  	assertNwIsAvailable(c, "test0")
   420  	// create an overlapping test1 network
   421  	_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.128.0/17", "test1")
   422  	c.Assert(err, check.NotNil)
   423  	dockerCmd(c, "network", "rm", "test0")
   424  }
   425  
   426  func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
   427  	dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
   428  	assertNwIsAvailable(c, "testopt")
   429  	gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
   430  	c.Assert(gopts, checker.NotNil)
   431  	opts, ok := gopts.(map[string]interface{})
   432  	c.Assert(ok, checker.Equals, true)
   433  	c.Assert(opts["opt1"], checker.Equals, "drv1")
   434  	c.Assert(opts["opt2"], checker.Equals, "drv2")
   435  	dockerCmd(c, "network", "rm", "testopt")
   436  
   437  }
   438  
   439  func (s *DockerDaemonSuite) TestDockerNetworkNoDiscoveryDefaultBridgeNetwork(c *check.C) {
   440  	// On default bridge network built-in service discovery should not happen
   441  	hostsFile := "/etc/hosts"
   442  	bridgeName := "external-bridge"
   443  	bridgeIP := "192.169.255.254/24"
   444  	out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
   445  	c.Assert(err, check.IsNil, check.Commentf(out))
   446  	defer deleteInterface(c, bridgeName)
   447  
   448  	err = s.d.StartWithBusybox("--bridge", bridgeName)
   449  	c.Assert(err, check.IsNil)
   450  	defer s.d.Restart()
   451  
   452  	// run two containers and store first container's etc/hosts content
   453  	out, err = s.d.Cmd("run", "-d", "busybox", "top")
   454  	c.Assert(err, check.IsNil)
   455  	cid1 := strings.TrimSpace(out)
   456  	defer s.d.Cmd("stop", cid1)
   457  
   458  	hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
   459  	c.Assert(err, checker.IsNil)
   460  
   461  	out, err = s.d.Cmd("run", "-d", "--name", "container2", "busybox", "top")
   462  	c.Assert(err, check.IsNil)
   463  	cid2 := strings.TrimSpace(out)
   464  
   465  	// verify first container's etc/hosts file has not changed after spawning the second named container
   466  	hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
   467  	c.Assert(err, checker.IsNil)
   468  	c.Assert(string(hosts), checker.Equals, string(hostsPost),
   469  		check.Commentf("Unexpected %s change on second container creation", hostsFile))
   470  
   471  	// stop container 2 and verify first container's etc/hosts has not changed
   472  	_, err = s.d.Cmd("stop", cid2)
   473  	c.Assert(err, check.IsNil)
   474  
   475  	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
   476  	c.Assert(err, checker.IsNil)
   477  	c.Assert(string(hosts), checker.Equals, string(hostsPost),
   478  		check.Commentf("Unexpected %s change on second container creation", hostsFile))
   479  
   480  	// but discovery is on when connecting to non default bridge network
   481  	network := "anotherbridge"
   482  	out, err = s.d.Cmd("network", "create", network)
   483  	c.Assert(err, check.IsNil, check.Commentf(out))
   484  	defer s.d.Cmd("network", "rm", network)
   485  
   486  	out, err = s.d.Cmd("network", "connect", network, cid1)
   487  	c.Assert(err, check.IsNil, check.Commentf(out))
   488  
   489  	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
   490  	c.Assert(err, checker.IsNil)
   491  	c.Assert(string(hosts), checker.Equals, string(hostsPost),
   492  		check.Commentf("Unexpected %s change on second network connection", hostsFile))
   493  
   494  	cName := "container3"
   495  	out, err = s.d.Cmd("run", "-d", "--net", network, "--name", cName, "busybox", "top")
   496  	c.Assert(err, check.IsNil, check.Commentf(out))
   497  	cid3 := strings.TrimSpace(out)
   498  	defer s.d.Cmd("stop", cid3)
   499  
   500  	// container1 etc/hosts file should contain an entry for the third container
   501  	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
   502  	c.Assert(err, checker.IsNil)
   503  	c.Assert(string(hostsPost), checker.Contains, cName,
   504  		check.Commentf("Container 1  %s file does not contain entries for named container %q: %s", hostsFile, cName, string(hostsPost)))
   505  
   506  	// on container3 disconnect, first container's etc/hosts should go back to original form
   507  	out, err = s.d.Cmd("network", "disconnect", network, cid3)
   508  	c.Assert(err, check.IsNil, check.Commentf(out))
   509  
   510  	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
   511  	c.Assert(err, checker.IsNil)
   512  	c.Assert(string(hosts), checker.Equals, string(hostsPost),
   513  		check.Commentf("Unexpected %s content after disconnecting from second network", hostsFile))
   514  }
   515  
   516  func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
   517  	hostsFile := "/etc/hosts"
   518  	cstmBridgeNw := "custom-bridge-nw"
   519  
   520  	dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
   521  	assertNwIsAvailable(c, cstmBridgeNw)
   522  
   523  	// run two anonymous containers and store their etc/hosts content
   524  	out, _ := dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
   525  	cid1 := strings.TrimSpace(out)
   526  
   527  	hosts1, err := readContainerFileWithExec(cid1, hostsFile)
   528  	c.Assert(err, checker.IsNil)
   529  
   530  	out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
   531  	cid2 := strings.TrimSpace(out)
   532  
   533  	hosts2, err := readContainerFileWithExec(cid2, hostsFile)
   534  	c.Assert(err, checker.IsNil)
   535  
   536  	// verify first container etc/hosts file has not changed
   537  	hosts1post, err := readContainerFileWithExec(cid1, hostsFile)
   538  	c.Assert(err, checker.IsNil)
   539  	c.Assert(string(hosts1), checker.Equals, string(hosts1post),
   540  		check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
   541  
   542  	// start a named container
   543  	cName := "AnyName"
   544  	out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
   545  	cid3 := strings.TrimSpace(out)
   546  
   547  	// verify etc/hosts file for first two containers contains the named container entry
   548  	hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
   549  	c.Assert(err, checker.IsNil)
   550  	c.Assert(string(hosts1post), checker.Contains, cName,
   551  		check.Commentf("Container 1  %s file does not contain entries for named container %q: %s", hostsFile, cName, string(hosts1post)))
   552  
   553  	hosts2post, err := readContainerFileWithExec(cid2, hostsFile)
   554  	c.Assert(err, checker.IsNil)
   555  	c.Assert(string(hosts2post), checker.Contains, cName,
   556  		check.Commentf("Container 2  %s file does not contain entries for named container %q: %s", hostsFile, cName, string(hosts2post)))
   557  
   558  	// Stop named container and verify first two containers' etc/hosts entries are back to original
   559  	dockerCmd(c, "stop", cid3)
   560  	hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
   561  	c.Assert(err, checker.IsNil)
   562  	c.Assert(string(hosts1), checker.Equals, string(hosts1post),
   563  		check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
   564  
   565  	hosts2post, err = readContainerFileWithExec(cid2, hostsFile)
   566  	c.Assert(err, checker.IsNil)
   567  	c.Assert(string(hosts2), checker.Equals, string(hosts2post),
   568  		check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
   569  }
   570  
   571  func (s *DockerNetworkSuite) TestDockerNetworkLinkOndefaultNetworkOnly(c *check.C) {
   572  	// Link feature must work only on default network, and not across networks
   573  	cnt1 := "container1"
   574  	cnt2 := "container2"
   575  	network := "anotherbridge"
   576  
   577  	// Run first container on default network
   578  	dockerCmd(c, "run", "-d", "--name", cnt1, "busybox", "top")
   579  
   580  	// Create another network and run the second container on it
   581  	dockerCmd(c, "network", "create", network)
   582  	assertNwIsAvailable(c, network)
   583  	dockerCmd(c, "run", "-d", "--net", network, "--name", cnt2, "busybox", "top")
   584  
   585  	// Try launching a container on default network, linking to the first container. Must succeed
   586  	dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt1, cnt1), "busybox", "top")
   587  
   588  	// Try launching a container on default network, linking to the second container. Must fail
   589  	_, _, err := dockerCmdWithError("run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
   590  	c.Assert(err, checker.NotNil)
   591  
   592  	// Connect second container to default network. Now a container on default network can link to it
   593  	dockerCmd(c, "network", "connect", "bridge", cnt2)
   594  	dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
   595  }
   596  
   597  func (s *DockerNetworkSuite) TestDockerNetworkOverlayPortMapping(c *check.C) {
   598  	// Verify exposed ports are present in ps output when running a container on
   599  	// a network managed by a driver which does not provide the default gateway
   600  	// for the container
   601  	nwn := "ov"
   602  	ctn := "bb"
   603  	port1 := 80
   604  	port2 := 443
   605  	expose1 := fmt.Sprintf("--expose=%d", port1)
   606  	expose2 := fmt.Sprintf("--expose=%d", port2)
   607  
   608  	dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
   609  	assertNwIsAvailable(c, nwn)
   610  
   611  	dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, expose1, expose2, "busybox", "top")
   612  
   613  	// Check docker ps o/p for last created container reports the unpublished ports
   614  	unpPort1 := fmt.Sprintf("%d/tcp", port1)
   615  	unpPort2 := fmt.Sprintf("%d/tcp", port2)
   616  	out, _ := dockerCmd(c, "ps", "-n=1")
   617  	// Missing unpublished ports in docker ps output
   618  	c.Assert(out, checker.Contains, unpPort1)
   619  	// Missing unpublished ports in docker ps output
   620  	c.Assert(out, checker.Contains, unpPort2)
   621  }
   622  
   623  func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
   624  	// Verify endpoint MAC address is correctly populated in container's network settings
   625  	nwn := "ov"
   626  	ctn := "bb"
   627  
   628  	dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
   629  	assertNwIsAvailable(c, nwn)
   630  
   631  	dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, "busybox", "top")
   632  
   633  	mac, err := inspectField(ctn, "NetworkSettings.Networks."+nwn+".MacAddress")
   634  	c.Assert(err, checker.IsNil)
   635  	c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
   636  }
   637  
   638  func (s *DockerSuite) TestInspectApiMultipeNetworks(c *check.C) {
   639  	dockerCmd(c, "network", "create", "mybridge1")
   640  	dockerCmd(c, "network", "create", "mybridge2")
   641  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
   642  	id := strings.TrimSpace(out)
   643  	c.Assert(waitRun(id), check.IsNil)
   644  
   645  	dockerCmd(c, "network", "connect", "mybridge1", id)
   646  	dockerCmd(c, "network", "connect", "mybridge2", id)
   647  
   648  	body := getInspectBody(c, "v1.20", id)
   649  	var inspect120 v1p20.ContainerJSON
   650  	err := json.Unmarshal(body, &inspect120)
   651  	c.Assert(err, checker.IsNil)
   652  
   653  	versionedIP := inspect120.NetworkSettings.IPAddress
   654  
   655  	body = getInspectBody(c, "v1.21", id)
   656  	var inspect121 types.ContainerJSON
   657  	err = json.Unmarshal(body, &inspect121)
   658  	c.Assert(err, checker.IsNil)
   659  	c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
   660  
   661  	bridge := inspect121.NetworkSettings.Networks["bridge"]
   662  	c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
   663  	c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
   664  }
   665  
   666  func connectContainerToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
   667  	// Run a container on the default network
   668  	out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
   669  	c.Assert(err, checker.IsNil, check.Commentf(out))
   670  
   671  	// Attach the container to other three networks
   672  	for _, nw := range nws {
   673  		out, err = d.Cmd("network", "create", nw)
   674  		c.Assert(err, checker.IsNil, check.Commentf(out))
   675  		out, err = d.Cmd("network", "connect", nw, cName)
   676  		c.Assert(err, checker.IsNil, check.Commentf(out))
   677  	}
   678  }
   679  
   680  func verifyContainerIsConnectedToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
   681  	// Verify container is connected to all three networks
   682  	for _, nw := range nws {
   683  		out, err := d.Cmd("inspect", "-f", fmt.Sprintf("{{.NetworkSettings.Networks.%s}}", nw), cName)
   684  		c.Assert(err, checker.IsNil, check.Commentf(out))
   685  		c.Assert(out, checker.Not(checker.Equals), "<no value>\n")
   686  	}
   687  }
   688  
   689  func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksGracefulDaemonRestart(c *check.C) {
   690  	cName := "bb"
   691  	nwList := []string{"nw1", "nw2", "nw3"}
   692  
   693  	s.d.Start()
   694  
   695  	connectContainerToNetworks(c, s.d, cName, nwList)
   696  	verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
   697  
   698  	// Reload daemon
   699  	s.d.Restart()
   700  
   701  	_, err := s.d.Cmd("start", cName)
   702  	c.Assert(err, checker.IsNil)
   703  
   704  	verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
   705  }
   706  
   707  func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRestart(c *check.C) {
   708  	cName := "cc"
   709  	nwList := []string{"nw1", "nw2", "nw3"}
   710  
   711  	s.d.Start()
   712  
   713  	connectContainerToNetworks(c, s.d, cName, nwList)
   714  	verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
   715  
   716  	// Kill daemon and restart
   717  	if err := s.d.cmd.Process.Kill(); err != nil {
   718  		c.Fatal(err)
   719  	}
   720  	s.d.Restart()
   721  
   722  	// Restart container
   723  	_, err := s.d.Cmd("start", cName)
   724  	c.Assert(err, checker.IsNil)
   725  
   726  	verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
   727  }