2015-10-09 14:21:48 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2015-11-10 03:57:06 -05:00
|
|
|
"sort"
|
2015-10-09 14:21:48 -04:00
|
|
|
"strings"
|
2016-01-17 22:34:23 -05:00
|
|
|
"time"
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2015-10-13 18:44:23 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-11-08 21:19:53 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/versions/v1p20"
|
2015-10-09 14:21:48 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-10-14 21:49:27 -04:00
|
|
|
remoteapi "github.com/docker/libnetwork/drivers/remote/api"
|
2015-10-19 19:17:05 -04:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
|
|
|
remoteipam "github.com/docker/libnetwork/ipams/remote/api"
|
2015-10-14 21:49:27 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-10-09 14:21:48 -04:00
|
|
|
"github.com/go-check/check"
|
2015-10-29 12:25:29 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-10-09 14:21:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const dummyNetworkDriver = "dummy-network-driver"
|
2015-10-19 19:17:05 -04:00
|
|
|
const dummyIpamDriver = "dummy-ipam-driver"
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2015-10-14 21:49:27 -04:00
|
|
|
var remoteDriverNetworkRequest remoteapi.CreateNetworkRequest
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
func init() {
|
|
|
|
check.Suite(&DockerNetworkSuite{
|
|
|
|
ds: &DockerSuite{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type DockerNetworkSuite struct {
|
|
|
|
server *httptest.Server
|
|
|
|
ds *DockerSuite
|
|
|
|
d *Daemon
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) SetUpTest(c *check.C) {
|
|
|
|
s.d = NewDaemon(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TearDownTest(c *check.C) {
|
|
|
|
s.d.Stop()
|
|
|
|
s.ds.TearDownTest(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) SetUpSuite(c *check.C) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
s.server = httptest.NewServer(mux)
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(s.server, check.NotNil, check.Commentf("Failed to start a HTTP Server"))
|
2016-01-17 22:34:23 -05:00
|
|
|
setupRemoteNetworkDrivers(c, mux, s.server.URL, dummyNetworkDriver, dummyIpamDriver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRemoteNetworkDrivers(c *check.C, mux *http.ServeMux, url, netDrv, ipamDrv string) {
|
2015-10-09 14:21:48 -04:00
|
|
|
|
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2015-10-19 19:17:05 -04:00
|
|
|
fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
|
2015-10-09 14:21:48 -04:00
|
|
|
})
|
|
|
|
|
2015-10-19 19:17:05 -04:00
|
|
|
// Network driver implementation
|
2015-10-09 14:21:48 -04:00
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"Scope":"local"}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
2015-10-14 21:49:27 -04:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2015-10-09 14:21:48 -04:00
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
})
|
|
|
|
|
2015-10-29 12:25:29 -04:00
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
|
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0"}
|
|
|
|
if err := netlink.LinkAdd(veth); err != nil {
|
|
|
|
fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
if link, err := netlink.LinkByName("cnt0"); err == nil {
|
|
|
|
netlink.LinkDel(link)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
})
|
|
|
|
|
2015-10-19 19:17:05 -04:00
|
|
|
// Ipam Driver implementation
|
|
|
|
var (
|
|
|
|
poolRequest remoteipam.RequestPoolRequest
|
|
|
|
poolReleaseReq remoteipam.ReleasePoolRequest
|
|
|
|
addressRequest remoteipam.RequestAddressRequest
|
|
|
|
addressReleaseReq remoteipam.ReleaseAddressRequest
|
|
|
|
lAS = "localAS"
|
|
|
|
gAS = "globalAS"
|
|
|
|
pool = "172.28.0.0/16"
|
|
|
|
poolID = lAS + "/" + pool
|
|
|
|
gw = "172.28.255.254/16"
|
|
|
|
)
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&poolRequest)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
|
|
|
|
fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
|
|
|
|
} else if poolRequest.Pool != "" && poolRequest.Pool != pool {
|
|
|
|
fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.RequestAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&addressRequest)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
// make sure libnetwork is now querying on the expected pool id
|
|
|
|
if addressRequest.PoolID != poolID {
|
|
|
|
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
|
|
|
|
} else if addressRequest.Address != "" {
|
|
|
|
fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.ReleaseAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&addressReleaseReq)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2015-12-13 11:00:39 -05:00
|
|
|
// make sure libnetwork is now asking to release the expected address from the expected poolid
|
2015-10-19 19:17:05 -04:00
|
|
|
if addressRequest.PoolID != poolID {
|
|
|
|
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
|
|
|
|
} else if addressReleaseReq.Address != gw {
|
|
|
|
fmt.Fprintf(w, `{"Error":"unknown address"}`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.ReleasePool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&poolReleaseReq)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
// make sure libnetwork is now asking to release the expected poolid
|
|
|
|
if addressRequest.PoolID != poolID {
|
|
|
|
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-10-14 10:33:13 -04:00
|
|
|
err := os.MkdirAll("/etc/docker/plugins", 0755)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2016-01-17 22:34:23 -05:00
|
|
|
fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", netDrv)
|
|
|
|
err = ioutil.WriteFile(fileName, []byte(url), 0644)
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-19 19:17:05 -04:00
|
|
|
|
2016-01-17 22:34:23 -05:00
|
|
|
ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", ipamDrv)
|
|
|
|
err = ioutil.WriteFile(ipamFileName, []byte(url), 0644)
|
2015-10-19 19:17:05 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TearDownSuite(c *check.C) {
|
|
|
|
if s.server == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.server.Close()
|
|
|
|
|
2015-10-14 10:33:13 -04:00
|
|
|
err := os.RemoveAll("/etc/docker/plugins")
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertNwIsAvailable(c *check.C, name string) {
|
|
|
|
if !isNwPresent(c, name) {
|
|
|
|
c.Fatalf("Network %s not found in network ls o/p", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNwNotAvailable(c *check.C, name string) {
|
|
|
|
if isNwPresent(c, name) {
|
|
|
|
c.Fatalf("Found network %s in network ls o/p", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNwPresent(c *check.C, name string) bool {
|
|
|
|
out, _ := dockerCmd(c, "network", "ls")
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
for i := 1; i < len(lines)-1; i++ {
|
2015-10-30 03:07:17 -04:00
|
|
|
netFields := strings.Fields(lines[i])
|
|
|
|
if netFields[1] == name {
|
2015-10-09 14:21:48 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-10 03:57:06 -05:00
|
|
|
// assertNwList checks network list retrived with ls command
|
|
|
|
// equals to expected network list
|
|
|
|
// note: out should be `network ls [option]` result
|
|
|
|
func assertNwList(c *check.C, out string, expectNws []string) {
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
var nwList []string
|
|
|
|
for _, line := range lines[1 : len(lines)-1] {
|
|
|
|
netFields := strings.Fields(line)
|
|
|
|
// wrap all network name in nwList
|
|
|
|
nwList = append(nwList, netFields[1])
|
|
|
|
}
|
|
|
|
// first need to sort out and expected
|
|
|
|
sort.StringSlice(nwList).Sort()
|
|
|
|
sort.StringSlice(expectNws).Sort()
|
|
|
|
|
|
|
|
// network ls should contains all expected networks
|
|
|
|
c.Assert(nwList, checker.DeepEquals, expectNws)
|
|
|
|
}
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
func getNwResource(c *check.C, name string) *types.NetworkResource {
|
|
|
|
out, _ := dockerCmd(c, "network", "inspect", name)
|
2015-10-21 02:57:29 -04:00
|
|
|
nr := []types.NetworkResource{}
|
2015-10-09 14:21:48 -04:00
|
|
|
err := json.Unmarshal([]byte(out), &nr)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-10-21 02:57:29 -04:00
|
|
|
return &nr[0]
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkLsDefault(c *check.C) {
|
|
|
|
defaults := []string{"bridge", "host", "none"}
|
|
|
|
for _, nn := range defaults {
|
|
|
|
assertNwIsAvailable(c, nn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 21:12:02 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkCreatePredefined(c *check.C) {
|
|
|
|
predefined := []string{"bridge", "host", "none", "default"}
|
|
|
|
for _, net := range predefined {
|
|
|
|
// predefined networks can't be created again
|
|
|
|
out, _, err := dockerCmdWithError("network", "create", net)
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkRmPredefined(c *check.C) {
|
|
|
|
predefined := []string{"bridge", "host", "none", "default"}
|
|
|
|
for _, net := range predefined {
|
|
|
|
// predefined networks can't be removed
|
|
|
|
out, _, err := dockerCmdWithError("network", "rm", net)
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 03:57:06 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "network", "create", "dev")
|
|
|
|
defer func() {
|
|
|
|
dockerCmd(c, "network", "rm", "dev")
|
|
|
|
}()
|
2015-12-27 23:56:57 -05:00
|
|
|
networkID := strings.TrimSpace(out)
|
2015-11-10 03:57:06 -05:00
|
|
|
|
|
|
|
// filter with partial ID and partial name
|
|
|
|
// only show 'bridge' and 'dev' network
|
2015-12-27 23:56:57 -05:00
|
|
|
out, _ = dockerCmd(c, "network", "ls", "-f", "id="+networkID[0:5], "-f", "name=dge")
|
2015-11-10 03:57:06 -05:00
|
|
|
assertNwList(c, out, []string{"dev", "bridge"})
|
|
|
|
|
|
|
|
// only show built-in network (bridge, none, host)
|
|
|
|
out, _ = dockerCmd(c, "network", "ls", "-f", "type=builtin")
|
|
|
|
assertNwList(c, out, []string{"bridge", "none", "host"})
|
|
|
|
|
|
|
|
// only show custom networks (dev)
|
|
|
|
out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom")
|
|
|
|
assertNwList(c, out, []string{"dev"})
|
|
|
|
|
|
|
|
// show all networks with filter
|
|
|
|
// it should be equivalent of ls without option
|
|
|
|
out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom", "-f", "type=builtin")
|
|
|
|
assertNwList(c, out, []string{"dev", "bridge", "host", "none"})
|
|
|
|
}
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "test")
|
|
|
|
assertNwIsAvailable(c, "test")
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "rm", "test")
|
|
|
|
assertNwNotAvailable(c, "test")
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:44:23 -04:00
|
|
|
func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
|
|
|
|
out, _, err := dockerCmdWithError("network", "rm", "test")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:10:20 -04:00
|
|
|
func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "testDelMulti0")
|
|
|
|
assertNwIsAvailable(c, "testDelMulti0")
|
|
|
|
dockerCmd(c, "network", "create", "testDelMulti1")
|
|
|
|
assertNwIsAvailable(c, "testDelMulti1")
|
|
|
|
dockerCmd(c, "network", "create", "testDelMulti2")
|
|
|
|
assertNwIsAvailable(c, "testDelMulti2")
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
|
2015-12-27 23:56:57 -05:00
|
|
|
containerID := strings.TrimSpace(out)
|
|
|
|
waitRun(containerID)
|
2015-10-29 12:10:20 -04:00
|
|
|
|
|
|
|
// delete three networks at the same time, since testDelMulti2
|
2015-12-27 23:56:57 -05:00
|
|
|
// contains active container, its deletion should fail.
|
2015-10-29 12:10:20 -04:00
|
|
|
out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
|
|
|
|
// err should not be nil due to deleting testDelMulti2 failed.
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
// testDelMulti2 should fail due to network has active endpoints
|
|
|
|
c.Assert(out, checker.Contains, "has active endpoints")
|
|
|
|
assertNwNotAvailable(c, "testDelMulti0")
|
|
|
|
assertNwNotAvailable(c, "testDelMulti1")
|
2015-12-27 23:56:57 -05:00
|
|
|
// testDelMulti2 can't be deleted, so it should exist
|
2015-10-29 12:10:20 -04:00
|
|
|
assertNwIsAvailable(c, "testDelMulti2")
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:32:10 -05:00
|
|
|
func (s *DockerSuite) TestDockerNetworkInspect(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "network", "inspect", "host")
|
|
|
|
networkResources := []types.NetworkResource{}
|
|
|
|
err := json.Unmarshal([]byte(out), &networkResources)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(networkResources, checker.HasLen, 1)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "network", "inspect", "--format='{{ .Name }}'", "host")
|
|
|
|
c.Assert(strings.TrimSpace(out), check.Equals, "host")
|
|
|
|
}
|
|
|
|
|
2015-10-21 02:57:29 -04:00
|
|
|
func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "network", "inspect", "host", "none")
|
|
|
|
networkResources := []types.NetworkResource{}
|
|
|
|
err := json.Unmarshal([]byte(out), &networkResources)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(networkResources, checker.HasLen, 2)
|
|
|
|
|
|
|
|
// Should print an error, return an exitCode 1 *but* should print the host network
|
|
|
|
out, exitCode, err := dockerCmdWithError("network", "inspect", "host", "nonexistent")
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(exitCode, checker.Equals, 1)
|
|
|
|
c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
|
|
|
|
networkResources = []types.NetworkResource{}
|
2015-12-07 22:04:38 -05:00
|
|
|
inspectOut := strings.SplitN(out, "\nError: No such network: nonexistent\n", 2)[0]
|
2015-10-21 02:57:29 -04:00
|
|
|
err = json.Unmarshal([]byte(inspectOut), &networkResources)
|
|
|
|
c.Assert(networkResources, checker.HasLen, 1)
|
|
|
|
|
|
|
|
// Should print an error and return an exitCode, nothing else
|
|
|
|
out, exitCode, err = dockerCmdWithError("network", "inspect", "nonexistent")
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(exitCode, checker.Equals, 1)
|
|
|
|
c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
|
|
|
|
}
|
|
|
|
|
2015-11-02 11:29:58 -05:00
|
|
|
func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "brNetForInspect")
|
|
|
|
assertNwIsAvailable(c, "brNetForInspect")
|
|
|
|
defer func() {
|
|
|
|
dockerCmd(c, "network", "rm", "brNetForInspect")
|
|
|
|
assertNwNotAvailable(c, "brNetForInspect")
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "testNetInspect1", "--net", "brNetForInspect", "busybox", "top")
|
|
|
|
c.Assert(waitRun("testNetInspect1"), check.IsNil)
|
|
|
|
containerID := strings.TrimSpace(out)
|
|
|
|
defer func() {
|
|
|
|
// we don't stop container by name, because we'll rename it later
|
|
|
|
dockerCmd(c, "stop", containerID)
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
|
|
|
|
networkResources := []types.NetworkResource{}
|
|
|
|
err := json.Unmarshal([]byte(out), &networkResources)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(networkResources, checker.HasLen, 1)
|
|
|
|
container, ok := networkResources[0].Containers[containerID]
|
|
|
|
c.Assert(ok, checker.True)
|
|
|
|
c.Assert(container.Name, checker.Equals, "testNetInspect1")
|
|
|
|
|
|
|
|
// rename container and check docker inspect output update
|
|
|
|
newName := "HappyNewName"
|
|
|
|
dockerCmd(c, "rename", "testNetInspect1", newName)
|
|
|
|
|
|
|
|
// check whether network inspect works properly
|
|
|
|
out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
|
|
|
|
newNetRes := []types.NetworkResource{}
|
|
|
|
err = json.Unmarshal([]byte(out), &newNetRes)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(newNetRes, checker.HasLen, 1)
|
|
|
|
container1, ok := newNetRes[0].Containers[containerID]
|
|
|
|
c.Assert(ok, checker.True)
|
|
|
|
c.Assert(container1.Name, checker.Equals, newName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "test")
|
|
|
|
assertNwIsAvailable(c, "test")
|
|
|
|
nr := getNwResource(c, "test")
|
|
|
|
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(nr.Name, checker.Equals, "test")
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 0)
|
2015-10-09 14:21:48 -04:00
|
|
|
|
|
|
|
// run a container
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
c.Assert(waitRun("test"), check.IsNil)
|
|
|
|
containerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
// connect the container to the test network
|
|
|
|
dockerCmd(c, "network", "connect", "test", containerID)
|
|
|
|
|
|
|
|
// inspect the network to make sure container is connected
|
|
|
|
nr = getNetworkResource(c, nr.ID)
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 1)
|
2015-10-09 14:21:48 -04:00
|
|
|
c.Assert(nr.Containers[containerID], check.NotNil)
|
|
|
|
|
|
|
|
// check if container IP matches network inspect
|
|
|
|
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-10-26 08:00:49 -04:00
|
|
|
containerIP := findContainerIP(c, "test", "test")
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(ip.String(), checker.Equals, containerIP)
|
2015-10-09 14:21:48 -04:00
|
|
|
|
|
|
|
// disconnect container from the network
|
|
|
|
dockerCmd(c, "network", "disconnect", "test", containerID)
|
|
|
|
nr = getNwResource(c, "test")
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(nr.Name, checker.Equals, "test")
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 0)
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
// run another container
|
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--net", "test", "--name", "test2", "busybox", "top")
|
|
|
|
c.Assert(waitRun("test2"), check.IsNil)
|
|
|
|
containerID = strings.TrimSpace(out)
|
|
|
|
|
|
|
|
nr = getNwResource(c, "test")
|
|
|
|
c.Assert(nr.Name, checker.Equals, "test")
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 1)
|
|
|
|
|
|
|
|
// force disconnect the container to the test network
|
|
|
|
dockerCmd(c, "network", "disconnect", "-f", "test", containerID)
|
|
|
|
|
|
|
|
nr = getNwResource(c, "test")
|
|
|
|
c.Assert(nr.Name, checker.Equals, "test")
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 0)
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
dockerCmd(c, "network", "rm", "test")
|
|
|
|
assertNwNotAvailable(c, "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkIpamMultipleNetworks(c *check.C) {
|
|
|
|
// test0 bridge network
|
|
|
|
dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test1")
|
|
|
|
assertNwIsAvailable(c, "test1")
|
|
|
|
|
|
|
|
// test2 bridge network does not overlap
|
|
|
|
dockerCmd(c, "network", "create", "--subnet=192.169.0.0/16", "test2")
|
|
|
|
assertNwIsAvailable(c, "test2")
|
|
|
|
|
|
|
|
// for networks w/o ipam specified, docker will choose proper non-overlapping subnets
|
|
|
|
dockerCmd(c, "network", "create", "test3")
|
|
|
|
assertNwIsAvailable(c, "test3")
|
|
|
|
dockerCmd(c, "network", "create", "test4")
|
|
|
|
assertNwIsAvailable(c, "test4")
|
|
|
|
dockerCmd(c, "network", "create", "test5")
|
|
|
|
assertNwIsAvailable(c, "test5")
|
|
|
|
|
|
|
|
// test network with multiple subnets
|
2015-12-13 11:00:39 -05:00
|
|
|
// bridge network doesn't support multiple subnets. hence, use a dummy driver that supports
|
2015-10-09 14:21:48 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16", "test6")
|
|
|
|
assertNwIsAvailable(c, "test6")
|
|
|
|
|
|
|
|
// test network with multiple subnets with valid ipam combinations
|
|
|
|
// also check same subnet across networks when the driver supports it.
|
|
|
|
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver,
|
|
|
|
"--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16",
|
|
|
|
"--gateway=192.168.0.100", "--gateway=192.170.0.100",
|
|
|
|
"--ip-range=192.168.1.0/24",
|
|
|
|
"--aux-address", "a=192.168.1.5", "--aux-address", "b=192.168.1.6",
|
|
|
|
"--aux-address", "a=192.170.1.5", "--aux-address", "b=192.170.1.6",
|
|
|
|
"test7")
|
|
|
|
assertNwIsAvailable(c, "test7")
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
for i := 1; i < 8; i++ {
|
|
|
|
dockerCmd(c, "network", "rm", fmt.Sprintf("test%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 19:17:05 -04:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkCustomIpam(c *check.C) {
|
|
|
|
// Create a bridge network using custom ipam driver
|
|
|
|
dockerCmd(c, "network", "create", "--ipam-driver", dummyIpamDriver, "br0")
|
|
|
|
assertNwIsAvailable(c, "br0")
|
|
|
|
|
|
|
|
// Verify expected network ipam fields are there
|
|
|
|
nr := getNetworkResource(c, "br0")
|
|
|
|
c.Assert(nr.Driver, checker.Equals, "bridge")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, dummyIpamDriver)
|
|
|
|
|
|
|
|
// remove network and exercise remote ipam driver
|
|
|
|
dockerCmd(c, "network", "rm", "br0")
|
|
|
|
assertNwNotAvailable(c, "br0")
|
|
|
|
}
|
|
|
|
|
2015-10-23 15:28:39 -04:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkIpamOptions(c *check.C) {
|
|
|
|
// Create a bridge network using custom ipam driver and options
|
|
|
|
dockerCmd(c, "network", "create", "--ipam-driver", dummyIpamDriver, "--ipam-opt", "opt1=drv1", "--ipam-opt", "opt2=drv2", "br0")
|
|
|
|
assertNwIsAvailable(c, "br0")
|
|
|
|
|
|
|
|
// Verify expected network ipam options
|
|
|
|
nr := getNetworkResource(c, "br0")
|
|
|
|
opts := nr.IPAM.Options
|
|
|
|
c.Assert(opts["opt1"], checker.Equals, "drv1")
|
|
|
|
c.Assert(opts["opt2"], checker.Equals, "drv2")
|
|
|
|
}
|
|
|
|
|
2015-12-27 23:56:57 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkInspectDefault(c *check.C) {
|
|
|
|
nr := getNetworkResource(c, "none")
|
|
|
|
c.Assert(nr.Driver, checker.Equals, "null")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
|
|
|
|
|
|
|
|
nr = getNetworkResource(c, "host")
|
|
|
|
c.Assert(nr.Driver, checker.Equals, "host")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
|
|
|
|
|
|
|
|
nr = getNetworkResource(c, "bridge")
|
|
|
|
c.Assert(nr.Driver, checker.Equals, "bridge")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
|
|
|
|
c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
|
|
|
|
c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomUnspecified(c *check.C) {
|
|
|
|
// if unspecified, network subnet will be selected from inside preferred pool
|
|
|
|
dockerCmd(c, "network", "create", "test01")
|
|
|
|
assertNwIsAvailable(c, "test01")
|
|
|
|
|
|
|
|
nr := getNetworkResource(c, "test01")
|
|
|
|
c.Assert(nr.Driver, checker.Equals, "bridge")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
|
|
|
|
c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
|
|
|
|
c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "rm", "test01")
|
|
|
|
assertNwNotAvailable(c, "test01")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomSpecified(c *check.C) {
|
2015-10-09 14:21:48 -04:00
|
|
|
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")
|
|
|
|
assertNwIsAvailable(c, "br0")
|
|
|
|
|
|
|
|
nr := getNetworkResource(c, "br0")
|
2015-10-14 10:33:13 -04:00
|
|
|
c.Assert(nr.Driver, checker.Equals, "bridge")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
|
|
|
|
c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16")
|
|
|
|
c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24")
|
|
|
|
c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254")
|
2015-10-09 14:21:48 -04:00
|
|
|
dockerCmd(c, "network", "rm", "br0")
|
2015-12-27 23:56:57 -05:00
|
|
|
assertNwNotAvailable(c, "test01")
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkIpamInvalidCombinations(c *check.C) {
|
|
|
|
// network with ip-range out of subnet range
|
|
|
|
_, _, err := dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--ip-range=192.170.0.0/16", "test")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
// network with multiple gateways for a single subnet
|
|
|
|
_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--gateway=192.168.0.1", "--gateway=192.168.0.2", "test")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Multiple overlapping subnets in the same network must fail
|
2015-10-09 14:21:48 -04:00
|
|
|
_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--subnet=192.168.1.0/16", "test")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
// overlapping subnets across networks must fail
|
|
|
|
// create a valid test0 network
|
|
|
|
dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test0")
|
|
|
|
assertNwIsAvailable(c, "test0")
|
|
|
|
// create an overlapping test1 network
|
|
|
|
_, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.128.0/17", "test1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
dockerCmd(c, "network", "rm", "test0")
|
2015-12-27 23:56:57 -05:00
|
|
|
assertNwNotAvailable(c, "test0")
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
2015-10-14 21:49:27 -04:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
|
|
|
|
assertNwIsAvailable(c, "testopt")
|
|
|
|
gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
|
|
|
|
c.Assert(gopts, checker.NotNil)
|
|
|
|
opts, ok := gopts.(map[string]interface{})
|
|
|
|
c.Assert(ok, checker.Equals, true)
|
|
|
|
c.Assert(opts["opt1"], checker.Equals, "drv1")
|
|
|
|
c.Assert(opts["opt2"], checker.Equals, "drv2")
|
|
|
|
dockerCmd(c, "network", "rm", "testopt")
|
2015-12-27 23:56:57 -05:00
|
|
|
assertNwNotAvailable(c, "testopt")
|
2015-10-14 21:49:27 -04:00
|
|
|
|
|
|
|
}
|
2015-10-22 11:41:50 -04:00
|
|
|
|
2015-10-23 22:37:35 -04:00
|
|
|
func (s *DockerDaemonSuite) TestDockerNetworkNoDiscoveryDefaultBridgeNetwork(c *check.C) {
|
2015-10-29 14:51:59 -04:00
|
|
|
testRequires(c, ExecSupport)
|
2015-10-23 22:37:35 -04:00
|
|
|
// On default bridge network built-in service discovery should not happen
|
2015-10-22 11:41:50 -04:00
|
|
|
hostsFile := "/etc/hosts"
|
|
|
|
bridgeName := "external-bridge"
|
|
|
|
bridgeIP := "192.169.255.254/24"
|
|
|
|
out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
defer deleteInterface(c, bridgeName)
|
|
|
|
|
2015-10-23 22:37:35 -04:00
|
|
|
err = s.d.StartWithBusybox("--bridge", bridgeName)
|
2015-10-22 11:41:50 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer s.d.Restart()
|
|
|
|
|
|
|
|
// run two containers and store first container's etc/hosts content
|
|
|
|
out, err = s.d.Cmd("run", "-d", "busybox", "top")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
cid1 := strings.TrimSpace(out)
|
|
|
|
defer s.d.Cmd("stop", cid1)
|
|
|
|
|
|
|
|
hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-10-21 12:08:19 -04:00
|
|
|
out, err = s.d.Cmd("run", "-d", "--name", "container2", "busybox", "top")
|
2015-10-22 11:41:50 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
cid2 := strings.TrimSpace(out)
|
|
|
|
|
2015-10-21 12:08:19 -04:00
|
|
|
// verify first container's etc/hosts file has not changed after spawning the second named container
|
2015-10-22 11:41:50 -04:00
|
|
|
hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts), checker.Equals, string(hostsPost),
|
|
|
|
check.Commentf("Unexpected %s change on second container creation", hostsFile))
|
|
|
|
|
|
|
|
// stop container 2 and verify first container's etc/hosts has not changed
|
|
|
|
_, err = s.d.Cmd("stop", cid2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts), checker.Equals, string(hostsPost),
|
|
|
|
check.Commentf("Unexpected %s change on second container creation", hostsFile))
|
|
|
|
|
|
|
|
// but discovery is on when connecting to non default bridge network
|
|
|
|
network := "anotherbridge"
|
|
|
|
out, err = s.d.Cmd("network", "create", network)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
defer s.d.Cmd("network", "rm", network)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("network", "connect", network, cid1)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
2015-12-07 18:46:14 -05:00
|
|
|
hosts, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-10-22 11:41:50 -04:00
|
|
|
hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts), checker.Equals, string(hostsPost),
|
|
|
|
check.Commentf("Unexpected %s change on second network connection", hostsFile))
|
|
|
|
}
|
2015-10-21 12:08:19 -04:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
|
2015-11-25 18:38:33 -05:00
|
|
|
testRequires(c, ExecSupport, NotArm)
|
2015-10-21 12:08:19 -04:00
|
|
|
hostsFile := "/etc/hosts"
|
|
|
|
cstmBridgeNw := "custom-bridge-nw"
|
2015-11-06 08:34:49 -05:00
|
|
|
cstmBridgeNw1 := "custom-bridge-nw1"
|
2015-10-21 12:08:19 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
|
|
|
|
assertNwIsAvailable(c, cstmBridgeNw)
|
|
|
|
|
|
|
|
// run two anonymous containers and store their etc/hosts content
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
|
|
|
|
cid1 := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
hosts1, err := readContainerFileWithExec(cid1, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
|
|
|
|
cid2 := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
hosts2, err := readContainerFileWithExec(cid2, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// verify first container etc/hosts file has not changed
|
|
|
|
hosts1post, err := readContainerFileWithExec(cid1, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts1), checker.Equals, string(hosts1post),
|
|
|
|
check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
|
|
|
|
|
2015-11-06 08:34:49 -05:00
|
|
|
// Connect the 2nd container to a new network and verify the
|
|
|
|
// first container /etc/hosts file still hasn't changed.
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw1)
|
|
|
|
assertNwIsAvailable(c, cstmBridgeNw1)
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "connect", cstmBridgeNw1, cid2)
|
|
|
|
|
2015-12-07 18:46:14 -05:00
|
|
|
hosts2, err = readContainerFileWithExec(cid2, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-11-06 08:34:49 -05:00
|
|
|
hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts1), checker.Equals, string(hosts1post),
|
|
|
|
check.Commentf("Unexpected %s change on container connect", hostsFile))
|
|
|
|
|
2015-10-21 12:08:19 -04:00
|
|
|
// start a named container
|
|
|
|
cName := "AnyName"
|
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
|
|
|
|
cid3 := strings.TrimSpace(out)
|
|
|
|
|
2016-01-08 16:38:52 -05:00
|
|
|
// verify that container 1 and 2 can ping the named container
|
|
|
|
dockerCmd(c, "exec", cid1, "ping", "-c", "1", cName)
|
|
|
|
dockerCmd(c, "exec", cid2, "ping", "-c", "1", cName)
|
2015-10-21 12:08:19 -04:00
|
|
|
|
2016-01-08 16:38:52 -05:00
|
|
|
// Stop named container and verify first two containers' etc/hosts file hasn't changed
|
2015-10-21 12:08:19 -04:00
|
|
|
dockerCmd(c, "stop", cid3)
|
|
|
|
hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts1), checker.Equals, string(hosts1post),
|
2016-01-08 16:38:52 -05:00
|
|
|
check.Commentf("Unexpected %s change on name container creation", hostsFile))
|
2015-10-21 12:08:19 -04:00
|
|
|
|
2016-01-08 16:38:52 -05:00
|
|
|
hosts2post, err := readContainerFileWithExec(cid2, hostsFile)
|
2015-10-21 12:08:19 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(hosts2), checker.Equals, string(hosts2post),
|
2016-01-08 16:38:52 -05:00
|
|
|
check.Commentf("Unexpected %s change on name container creation", hostsFile))
|
|
|
|
|
|
|
|
// verify that container 1 and 2 can't ping the named container now
|
|
|
|
_, _, err = dockerCmdWithError("exec", cid1, "ping", "-c", "1", cName)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
_, _, err = dockerCmdWithError("exec", cid2, "ping", "-c", "1", cName)
|
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-21 12:08:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkLinkOndefaultNetworkOnly(c *check.C) {
|
|
|
|
// Link feature must work only on default network, and not across networks
|
|
|
|
cnt1 := "container1"
|
|
|
|
cnt2 := "container2"
|
|
|
|
network := "anotherbridge"
|
|
|
|
|
|
|
|
// Run first container on default network
|
|
|
|
dockerCmd(c, "run", "-d", "--name", cnt1, "busybox", "top")
|
|
|
|
|
|
|
|
// Create another network and run the second container on it
|
|
|
|
dockerCmd(c, "network", "create", network)
|
|
|
|
assertNwIsAvailable(c, network)
|
|
|
|
dockerCmd(c, "run", "-d", "--net", network, "--name", cnt2, "busybox", "top")
|
|
|
|
|
|
|
|
// Try launching a container on default network, linking to the first container. Must succeed
|
|
|
|
dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt1, cnt1), "busybox", "top")
|
|
|
|
|
|
|
|
// Try launching a container on default network, linking to the second container. Must fail
|
|
|
|
_, _, err := dockerCmdWithError("run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
|
|
|
|
// Connect second container to default network. Now a container on default network can link to it
|
|
|
|
dockerCmd(c, "network", "connect", "bridge", cnt2)
|
|
|
|
dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
|
|
|
|
}
|
2015-10-29 12:25:29 -04:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkOverlayPortMapping(c *check.C) {
|
|
|
|
// Verify exposed ports are present in ps output when running a container on
|
|
|
|
// a network managed by a driver which does not provide the default gateway
|
|
|
|
// for the container
|
|
|
|
nwn := "ov"
|
|
|
|
ctn := "bb"
|
|
|
|
port1 := 80
|
|
|
|
port2 := 443
|
|
|
|
expose1 := fmt.Sprintf("--expose=%d", port1)
|
|
|
|
expose2 := fmt.Sprintf("--expose=%d", port2)
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
|
|
|
|
assertNwIsAvailable(c, nwn)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, expose1, expose2, "busybox", "top")
|
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the unpublished ports
|
|
|
|
unpPort1 := fmt.Sprintf("%d/tcp", port1)
|
|
|
|
unpPort2 := fmt.Sprintf("%d/tcp", port2)
|
|
|
|
out, _ := dockerCmd(c, "ps", "-n=1")
|
|
|
|
// Missing unpublished ports in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, unpPort1)
|
|
|
|
// Missing unpublished ports in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, unpPort2)
|
|
|
|
}
|
|
|
|
|
2016-01-17 22:34:23 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkDriverUngracefulRestart(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
|
|
|
dnd := "dnd"
|
|
|
|
did := "did"
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
|
|
|
|
|
|
|
|
s.d.StartWithBusybox()
|
|
|
|
_, err := s.d.Cmd("network", "create", "-d", dnd, "--subnet", "1.1.1.0/24", "net1")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
_, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "foo", "--ip", "1.1.1.10", "busybox", "sh")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Kill daemon and restart
|
|
|
|
if err = s.d.cmd.Process.Kill(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
startTime := time.Now().Unix()
|
|
|
|
if err = s.d.Restart(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
lapse := time.Now().Unix() - startTime
|
|
|
|
if lapse > 60 {
|
|
|
|
// In normal scenarios, daemon restart takes ~1 second.
|
|
|
|
// Plugin retry mechanism can delay the daemon start. systemd may not like it.
|
|
|
|
// Avoid accessing plugins during daemon bootup
|
|
|
|
c.Logf("daemon restart took too long : %d seconds", lapse)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart the custom dummy plugin
|
|
|
|
mux = http.NewServeMux()
|
|
|
|
server = httptest.NewServer(mux)
|
|
|
|
setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
|
|
|
|
|
|
|
|
// trying to reuse the same ip must succeed
|
|
|
|
_, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "bar", "--ip", "1.1.1.10", "busybox", "sh")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:25:29 -04:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
|
|
|
|
// Verify endpoint MAC address is correctly populated in container's network settings
|
|
|
|
nwn := "ov"
|
|
|
|
ctn := "bb"
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
|
|
|
|
assertNwIsAvailable(c, nwn)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, "busybox", "top")
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
mac := inspectField(c, ctn, "NetworkSettings.Networks."+nwn+".MacAddress")
|
2015-10-29 12:25:29 -04:00
|
|
|
c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
|
|
|
|
}
|
2015-10-30 14:57:15 -04:00
|
|
|
|
2015-11-02 20:30:16 -05:00
|
|
|
func (s *DockerSuite) TestInspectApiMultipleNetworks(c *check.C) {
|
2015-10-30 14:57:15 -04:00
|
|
|
dockerCmd(c, "network", "create", "mybridge1")
|
|
|
|
dockerCmd(c, "network", "create", "mybridge2")
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "connect", "mybridge1", id)
|
|
|
|
dockerCmd(c, "network", "connect", "mybridge2", id)
|
|
|
|
|
|
|
|
body := getInspectBody(c, "v1.20", id)
|
|
|
|
var inspect120 v1p20.ContainerJSON
|
|
|
|
err := json.Unmarshal(body, &inspect120)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
versionedIP := inspect120.NetworkSettings.IPAddress
|
|
|
|
|
|
|
|
body = getInspectBody(c, "v1.21", id)
|
|
|
|
var inspect121 types.ContainerJSON
|
|
|
|
err = json.Unmarshal(body, &inspect121)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
|
|
|
|
|
|
|
|
bridge := inspect121.NetworkSettings.Networks["bridge"]
|
|
|
|
c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
|
|
|
|
c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
|
|
|
|
}
|
2015-10-30 17:51:02 -04:00
|
|
|
|
|
|
|
func connectContainerToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
|
|
|
|
// Run a container on the default network
|
|
|
|
out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
|
2015-12-27 23:56:57 -05:00
|
|
|
// Attach the container to other networks
|
2015-10-30 17:51:02 -04:00
|
|
|
for _, nw := range nws {
|
|
|
|
out, err = d.Cmd("network", "create", nw)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
out, err = d.Cmd("network", "connect", nw, cName)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyContainerIsConnectedToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
|
2015-12-27 23:56:57 -05:00
|
|
|
// Verify container is connected to all the networks
|
2015-10-30 17:51:02 -04:00
|
|
|
for _, nw := range nws {
|
|
|
|
out, err := d.Cmd("inspect", "-f", fmt.Sprintf("{{.NetworkSettings.Networks.%s}}", nw), cName)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
c.Assert(out, checker.Not(checker.Equals), "<no value>\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksGracefulDaemonRestart(c *check.C) {
|
|
|
|
cName := "bb"
|
|
|
|
nwList := []string{"nw1", "nw2", "nw3"}
|
|
|
|
|
2015-11-20 15:55:19 -05:00
|
|
|
s.d.StartWithBusybox()
|
2015-10-30 17:51:02 -04:00
|
|
|
|
|
|
|
connectContainerToNetworks(c, s.d, cName, nwList)
|
|
|
|
verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
|
|
|
|
|
|
|
|
// Reload daemon
|
|
|
|
s.d.Restart()
|
|
|
|
|
|
|
|
_, err := s.d.Cmd("start", cName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRestart(c *check.C) {
|
|
|
|
cName := "cc"
|
|
|
|
nwList := []string{"nw1", "nw2", "nw3"}
|
|
|
|
|
2015-11-20 15:55:19 -05:00
|
|
|
s.d.StartWithBusybox()
|
2015-10-30 17:51:02 -04:00
|
|
|
|
|
|
|
connectContainerToNetworks(c, s.d, cName, nwList)
|
|
|
|
verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
|
|
|
|
|
|
|
|
// Kill daemon and restart
|
|
|
|
if err := s.d.cmd.Process.Kill(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
s.d.Restart()
|
|
|
|
|
|
|
|
// Restart container
|
|
|
|
_, err := s.d.Cmd("start", cName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
|
|
|
|
}
|
2015-10-30 23:32:03 -04:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "network", "create", "one")
|
2015-12-27 23:56:57 -05:00
|
|
|
containerOut, _, err := dockerCmdWithError("run", "-d", "--net", strings.TrimSpace(out), "busybox", "top")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(containerOut))
|
2015-10-30 23:32:03 -04:00
|
|
|
}
|
2015-11-02 13:38:10 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2015-11-20 15:55:19 -05:00
|
|
|
s.d.StartWithBusybox()
|
2015-11-02 13:38:10 -05:00
|
|
|
|
|
|
|
// Run a few containers on host network
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
cName := fmt.Sprintf("hostc-%d", i)
|
|
|
|
out, err := s.d.Cmd("run", "-d", "--name", cName, "--net=host", "--restart=always", "busybox", "top")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill daemon ungracefully and restart
|
|
|
|
if err := s.d.cmd.Process.Kill(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
s.d.Restart()
|
|
|
|
|
|
|
|
// make sure all the containers are up and running
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
cName := fmt.Sprintf("hostc-%d", i)
|
|
|
|
runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", cName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(runningOut), checker.Equals, "true")
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 21:19:53 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
|
|
|
|
c.Assert(waitRun("container1"), check.IsNil)
|
|
|
|
dockerCmd(c, "network", "disconnect", "bridge", "container1")
|
|
|
|
out, _, err := dockerCmdWithError("network", "connect", "host", "container1")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf(out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "container1", "--net=host", "busybox", "top")
|
|
|
|
c.Assert(waitRun("container1"), check.IsNil)
|
|
|
|
out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host"))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
|
|
|
|
}
|
2015-11-06 08:34:49 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *check.C) {
|
2015-11-25 18:38:33 -05:00
|
|
|
testRequires(c, NotArm)
|
2015-11-06 08:34:49 -05:00
|
|
|
dockerCmd(c, "network", "create", "test1")
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "c1", "-p", "5000:5000", "busybox", "top")
|
|
|
|
c.Assert(waitRun("c1"), check.IsNil)
|
|
|
|
dockerCmd(c, "network", "connect", "test1", "c1")
|
|
|
|
}
|
2015-11-10 04:47:32 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
|
|
|
|
macAddress := "02:42:ac:11:00:02"
|
|
|
|
dockerCmd(c, "network", "create", "mynetwork")
|
|
|
|
dockerCmd(c, "run", "--name=test", "-d", "--mac-address", macAddress, "busybox", "top")
|
|
|
|
c.Assert(waitRun("test"), check.IsNil)
|
2016-01-28 09:19:25 -05:00
|
|
|
mac1 := inspectField(c, "test", "NetworkSettings.Networks.bridge.MacAddress")
|
2015-11-10 04:47:32 -05:00
|
|
|
c.Assert(strings.TrimSpace(mac1), checker.Equals, macAddress)
|
|
|
|
dockerCmd(c, "network", "connect", "mynetwork", "test")
|
2016-01-28 09:19:25 -05:00
|
|
|
mac2 := inspectField(c, "test", "NetworkSettings.Networks.mynetwork.MacAddress")
|
2015-11-10 04:47:32 -05:00
|
|
|
c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
|
|
|
|
}
|
2015-12-13 02:33:18 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
|
|
|
|
dockerCmd(c, "create", "--name", "test", "busybox")
|
2016-01-28 09:19:25 -05:00
|
|
|
networks := inspectField(c, "test", "NetworkSettings.Networks")
|
2015-12-13 02:33:18 -05:00
|
|
|
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:58:54 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
|
2015-12-13 02:33:18 -05:00
|
|
|
dockerCmd(c, "network", "create", "test")
|
|
|
|
dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
|
|
|
|
c.Assert(waitRun("foo"), checker.IsNil)
|
|
|
|
dockerCmd(c, "network", "connect", "test", "foo")
|
|
|
|
dockerCmd(c, "restart", "foo")
|
2016-01-28 09:19:25 -05:00
|
|
|
networks := inspectField(c, "foo", "NetworkSettings.Networks")
|
2015-12-13 02:33:18 -05:00
|
|
|
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
|
2016-01-11 20:13:39 -05:00
|
|
|
c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectToStoppedContainer(c *check.C) {
|
|
|
|
dockerCmd(c, "network", "create", "test")
|
|
|
|
dockerCmd(c, "create", "--name=foo", "busybox", "top")
|
|
|
|
dockerCmd(c, "network", "connect", "test", "foo")
|
2016-01-28 09:19:25 -05:00
|
|
|
networks := inspectField(c, "foo", "NetworkSettings.Networks")
|
2016-01-11 20:13:39 -05:00
|
|
|
c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
|
|
|
|
|
|
|
|
// Restart docker daemon to test the config has persisted to disk
|
|
|
|
s.d.Restart()
|
2016-01-28 09:19:25 -05:00
|
|
|
networks = inspectField(c, "foo", "NetworkSettings.Networks")
|
2016-01-11 20:13:39 -05:00
|
|
|
c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
|
|
|
|
|
|
|
|
// start the container and test if we can ping it from another container in the same network
|
|
|
|
dockerCmd(c, "start", "foo")
|
|
|
|
c.Assert(waitRun("foo"), checker.IsNil)
|
2016-01-28 09:19:25 -05:00
|
|
|
ip := inspectField(c, "foo", "NetworkSettings.Networks.test.IPAddress")
|
2016-01-11 20:13:39 -05:00
|
|
|
ip = strings.TrimSpace(ip)
|
|
|
|
dockerCmd(c, "run", "--net=test", "busybox", "sh", "-c", fmt.Sprintf("ping -c 1 %s", ip))
|
|
|
|
|
|
|
|
dockerCmd(c, "stop", "foo")
|
|
|
|
|
|
|
|
// Test disconnect
|
|
|
|
dockerCmd(c, "network", "disconnect", "test", "foo")
|
2016-01-28 09:19:25 -05:00
|
|
|
networks = inspectField(c, "foo", "NetworkSettings.Networks")
|
2016-01-11 20:13:39 -05:00
|
|
|
c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
|
|
|
|
|
|
|
|
// Restart docker daemon to test the config has persisted to disk
|
|
|
|
s.d.Restart()
|
2016-01-28 09:19:25 -05:00
|
|
|
networks = inspectField(c, "foo", "NetworkSettings.Networks")
|
2016-01-11 20:13:39 -05:00
|
|
|
c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
|
|
|
|
|
2015-12-13 02:33:18 -05:00
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
|
|
|
|
// create two networks
|
|
|
|
dockerCmd(c, "network", "create", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "n0")
|
|
|
|
assertNwIsAvailable(c, "n0")
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "--subnet=172.30.0.0/16", "--ip-range=172.30.5.0/24", "--subnet=2001:db8:abcd::/64", "--ip-range=2001:db8:abcd::/80", "n1")
|
|
|
|
assertNwIsAvailable(c, "n1")
|
|
|
|
|
|
|
|
// run a container on first network specifying the ip addresses
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
|
|
|
|
c.Assert(waitRun("c0"), check.IsNil)
|
|
|
|
verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
|
|
|
|
2016-01-26 10:13:26 -05:00
|
|
|
// connect the container to the second network specifying an ip addresses
|
2016-01-07 19:18:34 -05:00
|
|
|
dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
|
|
|
|
verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
|
|
|
|
|
|
|
// Stop and restart the container
|
|
|
|
dockerCmd(c, "stop", "c0")
|
|
|
|
dockerCmd(c, "start", "c0")
|
|
|
|
|
2016-01-26 10:13:26 -05:00
|
|
|
// verify requested addresses are applied
|
2016-01-07 19:18:34 -05:00
|
|
|
verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
|
|
|
verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
|
|
|
|
|
|
|
// Still it should fail to connect to the default network with a specified IP (whatever ip)
|
|
|
|
out, _, err := dockerCmdWithError("network", "connect", "--ip", "172.21.55.44", "bridge", "c0")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-26 10:13:26 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedRequiredIP(c *check.C) {
|
|
|
|
// requested IP is not supported on predefined networks
|
2016-01-20 20:51:41 -05:00
|
|
|
for _, mode := range []string{"none", "host", "bridge", "default"} {
|
2016-01-07 19:18:34 -05:00
|
|
|
checkUnsupportedNetworkAndIP(c, mode)
|
|
|
|
}
|
|
|
|
|
2016-01-26 10:13:26 -05:00
|
|
|
// requested IP is not supported on networks with no user defined subnets
|
2016-01-07 19:18:34 -05:00
|
|
|
dockerCmd(c, "network", "create", "n0")
|
|
|
|
assertNwIsAvailable(c, "n0")
|
|
|
|
|
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--ip", "172.28.99.88", "--net", "n0", "busybox", "top")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
|
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("run", "-d", "--ip6", "2001:db8:1234::9988", "--net", "n0", "busybox", "top")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "rm", "n0")
|
|
|
|
assertNwNotAvailable(c, "n0")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
|
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--net", nwMode, "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAddress", nwname))
|
2016-01-07 19:18:34 -05:00
|
|
|
c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
out = inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.GlobalIPv6Address", nwname))
|
2016-01-07 19:18:34 -05:00
|
|
|
c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
|
|
|
|
}
|
2016-01-05 14:20:47 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
|
2015-11-25 18:38:33 -05:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
|
2016-01-05 14:20:47 -05:00
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "foo1")
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "foo2")
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=foo1", "--name=first", "busybox", "top")
|
|
|
|
c.Assert(waitRun("first"), check.IsNil)
|
|
|
|
|
2016-01-21 03:37:17 -05:00
|
|
|
// run a container in a user-defined network with a link for an existing container
|
2016-01-05 14:20:47 -05:00
|
|
|
// and a link for a container that doesnt exist
|
|
|
|
dockerCmd(c, "run", "-d", "--net=foo1", "--name=second", "--link=first:FirstInFoo1",
|
|
|
|
"--link=third:bar", "busybox", "top")
|
|
|
|
c.Assert(waitRun("second"), check.IsNil)
|
|
|
|
|
|
|
|
// ping to first and its alias FirstInFoo1 must succeed
|
|
|
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// connect first container to foo2 network
|
|
|
|
dockerCmd(c, "network", "connect", "foo2", "first")
|
|
|
|
// connect second container to foo2 network with a different alias for first container
|
|
|
|
dockerCmd(c, "network", "connect", "--link=first:FirstInFoo2", "foo2", "second")
|
|
|
|
|
|
|
|
// ping the new alias in network foo2
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// disconnect first container from foo1 network
|
|
|
|
dockerCmd(c, "network", "disconnect", "foo1", "first")
|
|
|
|
|
|
|
|
// link in foo1 network must fail
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
// link in foo2 network must succeed
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-01-14 01:58:54 -05:00
|
|
|
|
2016-01-21 03:37:17 -05:00
|
|
|
// #19100 This is a deprecated feature test, it should be removed in Docker 1.12
|
2016-01-14 01:58:54 -05:00
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkStartAPIWithHostconfig(c *check.C) {
|
|
|
|
netName := "test"
|
|
|
|
conName := "foo"
|
|
|
|
dockerCmd(c, "network", "create", netName)
|
|
|
|
dockerCmd(c, "create", "--name", conName, "busybox", "top")
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"HostConfig": map[string]interface{}{
|
|
|
|
"NetworkMode": netName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err := sockRequest("POST", "/containers/"+conName+"/start", config)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(waitRun(conName), checker.IsNil)
|
2016-01-28 09:19:25 -05:00
|
|
|
networks := inspectField(c, conName, "NetworkSettings.Networks")
|
2016-01-14 01:58:54 -05:00
|
|
|
c.Assert(networks, checker.Contains, netName, check.Commentf(fmt.Sprintf("Should contain '%s' network", netName)))
|
|
|
|
c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
|
|
|
|
netWorkName1 := "test1"
|
|
|
|
netWorkName2 := "test2"
|
|
|
|
containerName := "foo"
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", netWorkName1)
|
|
|
|
dockerCmd(c, "network", "create", netWorkName2)
|
|
|
|
dockerCmd(c, "create", "--name", containerName, "busybox", "top")
|
|
|
|
dockerCmd(c, "network", "connect", netWorkName1, containerName)
|
|
|
|
dockerCmd(c, "network", "connect", netWorkName2, containerName)
|
|
|
|
dockerCmd(c, "network", "disconnect", "bridge", containerName)
|
|
|
|
|
|
|
|
dockerCmd(c, "start", containerName)
|
|
|
|
c.Assert(waitRun(containerName), checker.IsNil)
|
2016-01-28 09:19:25 -05:00
|
|
|
networks := inspectField(c, containerName, "NetworkSettings.Networks")
|
2016-01-14 01:58:54 -05:00
|
|
|
c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
|
|
|
|
c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
|
|
|
|
c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
|
2016-01-08 08:45:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
2015-11-25 18:38:33 -05:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
|
2016-01-08 08:45:56 -05:00
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "net2")
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
|
|
|
|
c.Assert(waitRun("first"), check.IsNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
|
|
|
c.Assert(waitRun("second"), check.IsNil)
|
|
|
|
|
|
|
|
// ping first container and its alias
|
|
|
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// connect first container to net2 network
|
|
|
|
dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
|
|
|
|
// connect second container to foo2 network with a different alias for first container
|
|
|
|
dockerCmd(c, "network", "connect", "net2", "second")
|
|
|
|
|
|
|
|
// ping the new alias in network foo2
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// disconnect first container from net1 network
|
|
|
|
dockerCmd(c, "network", "disconnect", "net1", "first")
|
2016-01-14 01:58:54 -05:00
|
|
|
|
2016-01-08 08:45:56 -05:00
|
|
|
// ping to net1 scoped alias "foo" must fail
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
// ping to net2 scoped alias "bar" must still succeed
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
|
|
|
|
c.Assert(err, check.IsNil)
|
2016-01-20 20:51:41 -05:00
|
|
|
|
|
|
|
// verify the alias option is rejected when running on predefined network
|
|
|
|
out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
|
|
|
|
|
|
|
|
// verify the alias option is rejected when connecting to predefined network
|
|
|
|
out, _, err = dockerCmdWithError("network", "connect", "--alias=any", "bridge", "first")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
|
2016-01-14 01:58:54 -05:00
|
|
|
}
|
2016-01-21 23:49:02 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUserDefinedNetworkConnectivity(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "br.net1")
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c1.net1", "busybox", "top")
|
|
|
|
c.Assert(waitRun("c1.net1"), check.IsNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c2.net1", "busybox", "top")
|
|
|
|
c.Assert(waitRun("c2.net1"), check.IsNil)
|
|
|
|
|
|
|
|
// ping first container by its unqualified name
|
|
|
|
_, _, err := dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// ping first container by its qualified name
|
|
|
|
_, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// ping with first qualified name masked by an additional domain. should fail
|
|
|
|
_, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1.google.com")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
2016-01-25 14:14:29 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestDockerNetworkConnectFailsNoInspectChange(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--name=bb", "busybox", "top")
|
|
|
|
c.Assert(waitRun("bb"), check.IsNil)
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
ns0 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
|
2016-01-25 14:14:29 -05:00
|
|
|
|
|
|
|
// A failing redundant network connect should not alter current container's endpoint settings
|
|
|
|
_, _, err := dockerCmdWithError("network", "connect", "bridge", "bb")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
ns1 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
|
2016-01-25 14:14:29 -05:00
|
|
|
c.Assert(ns1, check.Equals, ns0)
|
|
|
|
}
|