mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 13:41:36 +00:00
Network remote APIs using new router, --net=<user-defined-network> changes
* Moving Network Remote APIs out of experimental * --net can now accept user created networks using network drivers/plugins * Removed the experimental services concept and --default-network option * Neccessary backend changes to accomodate multiple networks per container * Integration Tests Signed-off-by: David Calavera <david.calavera@gmail.com> Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
@@ -1,72 +1,201 @@
|
||||
// +build experimental
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/pkg/parsers/filters"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func (s *DockerSuite) TestApiNetworkGetDefaults(c *check.C) {
|
||||
// By default docker daemon creates 3 networks. check if they are present
|
||||
defaults := []string{"bridge", "host", "none"}
|
||||
for _, nn := range defaults {
|
||||
c.Assert(isNetworkAvailable(c, nn), check.Equals, true)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestApiNetworkCreateDelete(c *check.C) {
|
||||
// Create a network
|
||||
name := "testnetwork"
|
||||
id := createNetwork(c, name, true)
|
||||
c.Assert(isNetworkAvailable(c, name), check.Equals, true)
|
||||
|
||||
// POST another network with same name and CheckDuplicate must fail
|
||||
createNetwork(c, name, false)
|
||||
|
||||
// delete the network and make sure it is deleted
|
||||
deleteNetwork(c, id, true)
|
||||
c.Assert(isNetworkAvailable(c, name), check.Equals, false)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestApiNetworkFilter(c *check.C) {
|
||||
nr := getNetworkResource(c, getNetworkIDByName(c, "bridge"))
|
||||
c.Assert(nr.Name, check.Equals, "bridge")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
|
||||
// Inspect default bridge network
|
||||
nr := getNetworkResource(c, "bridge")
|
||||
c.Assert(nr.Name, check.Equals, "bridge")
|
||||
|
||||
// run a container and attach it to the default bridge network
|
||||
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
||||
containerID := strings.TrimSpace(out)
|
||||
containerIP := findContainerIP(c, "test")
|
||||
|
||||
// inspect default bridge network again and make sure the container is connected
|
||||
nr = getNetworkResource(c, nr.ID)
|
||||
c.Assert(len(nr.Containers), check.Equals, 1)
|
||||
c.Assert(nr.Containers[containerID], check.NotNil)
|
||||
|
||||
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(ip.String(), check.Equals, containerIP)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestApiNetworkConnectDisconnect(c *check.C) {
|
||||
// Create test network
|
||||
name := "testnetwork"
|
||||
id := createNetwork(c, name, true)
|
||||
nr := getNetworkResource(c, id)
|
||||
c.Assert(nr.Name, check.Equals, name)
|
||||
c.Assert(nr.ID, check.Equals, id)
|
||||
c.Assert(len(nr.Containers), check.Equals, 0)
|
||||
|
||||
// run a container
|
||||
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
||||
containerID := strings.TrimSpace(out)
|
||||
|
||||
// connect the container to the test network
|
||||
connectNetwork(c, nr.ID, containerID)
|
||||
|
||||
// inspect the network to make sure container is connected
|
||||
nr = getNetworkResource(c, nr.ID)
|
||||
c.Assert(len(nr.Containers), check.Equals, 1)
|
||||
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)
|
||||
containerIP := findContainerIP(c, "test")
|
||||
c.Assert(ip.String(), check.Equals, containerIP)
|
||||
|
||||
// disconnect container from the network
|
||||
disconnectNetwork(c, nr.ID, containerID)
|
||||
nr = getNetworkResource(c, nr.ID)
|
||||
c.Assert(nr.Name, check.Equals, name)
|
||||
c.Assert(len(nr.Containers), check.Equals, 0)
|
||||
|
||||
// delete the network
|
||||
deleteNetwork(c, nr.ID, true)
|
||||
}
|
||||
|
||||
func isNetworkAvailable(c *check.C, name string) bool {
|
||||
status, body, err := sockRequest("GET", "/networks", nil)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
var inspectJSON []struct {
|
||||
Name string
|
||||
ID string
|
||||
Type string
|
||||
}
|
||||
if err = json.Unmarshal(body, &inspectJSON); err != nil {
|
||||
c.Fatalf("unable to unmarshal response body: %v", err)
|
||||
}
|
||||
for _, n := range inspectJSON {
|
||||
nJSON := []types.NetworkResource{}
|
||||
err = json.Unmarshal(body, &nJSON)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
for _, n := range nJSON {
|
||||
if n.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkApiGetAll(c *check.C) {
|
||||
defaults := []string{"bridge", "host", "none"}
|
||||
for _, nn := range defaults {
|
||||
if !isNetworkAvailable(c, nn) {
|
||||
c.Fatalf("Missing Default network : %s", nn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkApiCreateDelete(c *check.C) {
|
||||
name := "testnetwork"
|
||||
config := map[string]interface{}{
|
||||
"name": name,
|
||||
"network_type": "bridge",
|
||||
}
|
||||
|
||||
status, resp, err := sockRequest("POST", "/networks", config)
|
||||
c.Assert(status, check.Equals, http.StatusCreated)
|
||||
func getNetworkIDByName(c *check.C, name string) string {
|
||||
var (
|
||||
v = url.Values{}
|
||||
filterArgs = filters.Args{}
|
||||
)
|
||||
filterArgs["name"] = []string{name}
|
||||
filterJSON, err := filters.ToParam(filterArgs)
|
||||
c.Assert(err, check.IsNil)
|
||||
v.Set("filters", filterJSON)
|
||||
|
||||
if !isNetworkAvailable(c, name) {
|
||||
c.Fatalf("Network %s not found", name)
|
||||
}
|
||||
|
||||
var id string
|
||||
err = json.Unmarshal(resp, &id)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
status, _, err = sockRequest("DELETE", fmt.Sprintf("/networks/%s", id), nil)
|
||||
status, body, err := sockRequest("GET", "/networks?"+v.Encode(), nil)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
if isNetworkAvailable(c, name) {
|
||||
c.Fatalf("Network %s not deleted", name)
|
||||
nJSON := []types.NetworkResource{}
|
||||
err = json.Unmarshal(body, &nJSON)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(len(nJSON), check.Equals, 1)
|
||||
|
||||
return nJSON[0].ID
|
||||
}
|
||||
|
||||
func getNetworkResource(c *check.C, id string) *types.NetworkResource {
|
||||
_, obj, err := sockRequest("GET", "/networks/"+id, nil)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
nr := types.NetworkResource{}
|
||||
err = json.Unmarshal(obj, &nr)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
return &nr
|
||||
}
|
||||
|
||||
func createNetwork(c *check.C, name string, shouldSucceed bool) string {
|
||||
config := types.NetworkCreate{
|
||||
Name: name,
|
||||
Driver: "bridge",
|
||||
CheckDuplicate: true,
|
||||
}
|
||||
|
||||
status, resp, err := sockRequest("POST", "/networks/create", config)
|
||||
if !shouldSucceed {
|
||||
c.Assert(status, check.Not(check.Equals), http.StatusCreated)
|
||||
return ""
|
||||
}
|
||||
|
||||
c.Assert(status, check.Equals, http.StatusCreated)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
var nr types.NetworkCreateResponse
|
||||
err = json.Unmarshal(resp, &nr)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
return nr.ID
|
||||
}
|
||||
|
||||
func connectNetwork(c *check.C, nid, cid string) {
|
||||
config := types.NetworkConnect{
|
||||
Container: cid,
|
||||
}
|
||||
|
||||
status, _, err := sockRequest("POST", "/networks/"+nid+"/connect", config)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
func disconnectNetwork(c *check.C, nid, cid string) {
|
||||
config := types.NetworkConnect{
|
||||
Container: cid,
|
||||
}
|
||||
|
||||
status, _, err := sockRequest("POST", "/networks/"+nid+"/disconnect", config)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
func deleteNetwork(c *check.C, id string, shouldSucceed bool) {
|
||||
status, _, err := sockRequest("DELETE", "/networks/"+id, nil)
|
||||
if !shouldSucceed {
|
||||
c.Assert(status, check.Not(check.Equals), http.StatusOK)
|
||||
c.Assert(err, check.NotNil)
|
||||
return
|
||||
}
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// +build daemon,experimental,!windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func assertNetwork(c *check.C, d *Daemon, name string) {
|
||||
out, err := d.Cmd("network", "ls")
|
||||
c.Assert(err, check.IsNil)
|
||||
lines := strings.Split(out, "\n")
|
||||
for i := 1; i < len(lines)-1; i++ {
|
||||
if strings.Contains(lines[i], name) {
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Fatalf("Network %s not found in network ls o/p", name)
|
||||
}
|
||||
|
||||
func (s *DockerDaemonSuite) TestDaemonDefaultNetwork(c *check.C) {
|
||||
testRequires(c, SameHostDaemon)
|
||||
d := s.d
|
||||
|
||||
networkName := "testdefault"
|
||||
err := d.StartWithBusybox("--default-network", "bridge:"+networkName)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
_, err = d.Cmd("run", "busybox", "true")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
assertNetwork(c, d, networkName)
|
||||
|
||||
ifconfigCmd := exec.Command("ifconfig", networkName)
|
||||
_, _, _, err = runCommandWithStdoutStderr(ifconfigCmd)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func (s *DockerSuite) TestNetHostname(c *check.C) {
|
||||
if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
||||
c.Fatalf(out, err)
|
||||
}
|
||||
checkContains("invalid --net: weird", out, c)
|
||||
checkContains("network weird not found", out, c)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
|
||||
|
||||
@@ -3168,7 +3168,7 @@ func (s *DockerSuite) TestRunCreateContainerFailedCleanUp(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
name := "unique_name"
|
||||
_, _, err := dockerCmdWithError("run", "--name", name, "--link", "nothing:nothing", "busybox")
|
||||
c.Assert(err, check.Not(check.IsNil), check.Commentf("Expected docker run to fail!"))
|
||||
c.Assert(err, check.NotNil, check.Commentf("Expected docker run to fail!"))
|
||||
|
||||
containerID, err := inspectField(name, "Id")
|
||||
c.Assert(containerID, check.Equals, "", check.Commentf("Expected not to have this container: %s!", containerID))
|
||||
@@ -3404,6 +3404,154 @@ func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) {
|
||||
dockerCmd(c, "stop", "second")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainersInUserDefinedNetwork(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork")
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork", "--name=first", "busybox", "top")
|
||||
dockerCmd(c, "run", "-t", "--net=testnetwork", "--name=second", "busybox", "ping", "-c", "1", "first")
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainersInMultipleNetworks(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
// Create 2 networks using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
|
||||
// Run and connect containers to testnetwork1
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
// Check connectivity between containers in testnetwork2
|
||||
dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
|
||||
// Connect containers to testnetwork2
|
||||
dockerCmd(c, "network", "connect", "testnetwork2", "first")
|
||||
dockerCmd(c, "network", "connect", "testnetwork2", "second")
|
||||
// Check connectivity between containers
|
||||
dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
dockerCmd(c, "network", "rm", "testnetwork2")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainersNetworkIsolation(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
// Create 2 networks using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
|
||||
// Run 1 containers in testnetwork1 and another in testnetwork2
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork2", "--name=second", "busybox", "top")
|
||||
|
||||
// Check Isolation between containers : ping must fail
|
||||
_, _, err := dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
|
||||
c.Assert(err, check.NotNil)
|
||||
// Connect first container to testnetwork2
|
||||
dockerCmd(c, "network", "connect", "testnetwork2", "first")
|
||||
// ping must succeed now
|
||||
_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
// Disconnect first container from testnetwork2
|
||||
dockerCmd(c, "network", "disconnect", "testnetwork2", "first")
|
||||
// ping must fail again
|
||||
_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
dockerCmd(c, "network", "rm", "testnetwork2")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkRmWithActiveContainers(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
// Create 2 networks using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
// Run and connect containers to testnetwork1
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
// Network delete with active containers must fail
|
||||
_, _, err := dockerCmdWithError("network", "rm", "testnetwork1")
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
dockerCmd(c, "stop", "first")
|
||||
_, _, err = dockerCmdWithError("network", "rm", "testnetwork1")
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
dockerCmd(c, "stop", "second")
|
||||
// Network delete must succeed after all the connected containers are inactive
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainerRestartInMultipleNetworks(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
// Create 2 networks using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
|
||||
// Run and connect containers to testnetwork1
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
// Check connectivity between containers in testnetwork2
|
||||
dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
|
||||
// Connect containers to testnetwork2
|
||||
dockerCmd(c, "network", "connect", "testnetwork2", "first")
|
||||
dockerCmd(c, "network", "connect", "testnetwork2", "second")
|
||||
// Check connectivity between containers
|
||||
dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
|
||||
|
||||
// Stop second container and test ping failures on both networks
|
||||
dockerCmd(c, "stop", "second")
|
||||
_, _, err := dockerCmdWithError("exec", "first", "ping", "-c", "1", "second.testnetwork1")
|
||||
c.Assert(err, check.NotNil)
|
||||
_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second.testnetwork2")
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
// Start second container and connectivity must be restored on both networks
|
||||
dockerCmd(c, "start", "second")
|
||||
dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
|
||||
dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
|
||||
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
dockerCmd(c, "network", "rm", "testnetwork2")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainerWithConflictingHostNetworks(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
// Run a container with --net=host
|
||||
dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
|
||||
|
||||
// Create a network using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
|
||||
// Connecting to the user defined network must fail
|
||||
_, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "first")
|
||||
c.Assert(err, check.NotNil)
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainerWithConflictingSharedNetwork(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
dockerCmd(c, "run", "-d", "--name=first", "busybox", "top")
|
||||
// Run second container in first container's network namespace
|
||||
dockerCmd(c, "run", "-d", "--net=container:first", "--name=second", "busybox", "top")
|
||||
|
||||
// Create a network using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
|
||||
// Connecting to the user defined network must fail
|
||||
_, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "second")
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
}
|
||||
|
||||
// #11957 - stdin with no tty does not exit if stdin is not closed even though container exited
|
||||
func (s *DockerSuite) TestRunStdinBlockedAfterContainerExit(c *check.C) {
|
||||
cmd := exec.Command(dockerBinary, "run", "-i", "--name=test", "busybox", "true")
|
||||
|
||||
Reference in New Issue
Block a user