mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 05:31:47 +00:00
Merge pull request #16645 from mavenugo/ux
Docker Network UX & remote API changes
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,113 +0,0 @@
|
||||
// +build experimental
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func isServiceAvailable(c *check.C, name string, network string) bool {
|
||||
status, body, err := sockRequest("GET", "/services", nil)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
var inspectJSON []struct {
|
||||
Name string
|
||||
ID string
|
||||
Network string
|
||||
}
|
||||
if err = json.Unmarshal(body, &inspectJSON); err != nil {
|
||||
c.Fatalf("unable to unmarshal response body: %v", err)
|
||||
}
|
||||
for _, s := range inspectJSON {
|
||||
if s.Name == name && s.Network == network {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
func isServiceNetworkAvailable(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 {
|
||||
if n.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestServiceApiCreateDelete(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)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
if !isServiceNetworkAvailable(c, name) {
|
||||
c.Fatalf("Network %s not found", name)
|
||||
}
|
||||
|
||||
var nid string
|
||||
err = json.Unmarshal(resp, &nid)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
sname := "service1"
|
||||
sconfig := map[string]interface{}{
|
||||
"name": sname,
|
||||
"network_name": name,
|
||||
}
|
||||
|
||||
status, resp, err = sockRequest("POST", "/services", sconfig)
|
||||
c.Assert(status, check.Equals, http.StatusCreated)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
if !isServiceAvailable(c, sname, name) {
|
||||
c.Fatalf("Service %s.%s not found", sname, name)
|
||||
}
|
||||
|
||||
var id string
|
||||
err = json.Unmarshal(resp, &id)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
status, _, err = sockRequest("DELETE", fmt.Sprintf("/services/%s", id), nil)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
if isServiceAvailable(c, sname, name) {
|
||||
c.Fatalf("Service %s.%s not deleted", sname, name)
|
||||
}
|
||||
|
||||
status, _, err = sockRequest("DELETE", fmt.Sprintf("/networks/%s", nid), nil)
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
if isNetworkAvailable(c, name) {
|
||||
c.Fatalf("Network %s not deleted", name)
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// +build experimental
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
@@ -31,6 +32,14 @@ func isNwPresent(c *check.C, name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func getNwResource(c *check.C, name string) *types.NetworkResource {
|
||||
out, _ := dockerCmd(c, "network", "inspect", name)
|
||||
nr := types.NetworkResource{}
|
||||
err := json.Unmarshal([]byte(out), &nr)
|
||||
c.Assert(err, check.IsNil)
|
||||
return &nr
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestDockerNetworkLsDefault(c *check.C) {
|
||||
defaults := []string{"bridge", "host", "none"}
|
||||
for _, nn := range defaults {
|
||||
@@ -45,3 +54,45 @@ func (s *DockerSuite) TestDockerNetworkCreateDelete(c *check.C) {
|
||||
dockerCmd(c, "network", "rm", "test")
|
||||
assertNwNotAvailable(c, "test")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
|
||||
dockerCmd(c, "network", "create", "test")
|
||||
assertNwIsAvailable(c, "test")
|
||||
nr := getNwResource(c, "test")
|
||||
|
||||
c.Assert(nr.Name, check.Equals, "test")
|
||||
c.Assert(len(nr.Containers), check.Equals, 0)
|
||||
|
||||
// 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)
|
||||
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
|
||||
dockerCmd(c, "network", "disconnect", "test", containerID)
|
||||
nr = getNwResource(c, "test")
|
||||
c.Assert(nr.Name, check.Equals, "test")
|
||||
c.Assert(len(nr.Containers), check.Equals, 0)
|
||||
|
||||
// check if network connect fails for inactive containers
|
||||
dockerCmd(c, "stop", containerID)
|
||||
_, _, err = dockerCmdWithError("network", "connect", "test", containerID)
|
||||
c.Assert(err, check.NotNil)
|
||||
|
||||
dockerCmd(c, "network", "rm", "test")
|
||||
assertNwNotAvailable(c, "test")
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/pkg/integration/checker"
|
||||
"github.com/docker/docker/pkg/nat"
|
||||
"github.com/docker/docker/runconfig"
|
||||
"github.com/docker/libnetwork/resolvconf"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
@@ -3168,7 +3170,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 +3406,193 @@ 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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
// 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 container in testnetwork1 and another in testnetwork2
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork2", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
|
||||
// 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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
// 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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
// 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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
|
||||
// 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")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
// Run second container in first container's network namespace
|
||||
dockerCmd(c, "run", "-d", "--net=container:first", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
|
||||
// Create a network using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
|
||||
// Connecting to the user defined network must fail
|
||||
out, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "second")
|
||||
c.Assert(err, check.NotNil)
|
||||
c.Assert(out, checker.Contains, runconfig.ErrConflictSharedNetwork.Error())
|
||||
|
||||
dockerCmd(c, "stop", "first")
|
||||
dockerCmd(c, "stop", "second")
|
||||
dockerCmd(c, "network", "rm", "testnetwork1")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestContainerWithConflictingNoneNetwork(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
dockerCmd(c, "run", "-d", "--net=none", "--name=first", "busybox", "top")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
|
||||
// Create a network using bridge driver
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
|
||||
|
||||
// Connecting to the user defined network must fail
|
||||
out, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "first")
|
||||
c.Assert(err, check.NotNil)
|
||||
c.Assert(out, checker.Contains, runconfig.ErrConflictNoNetwork.Error())
|
||||
|
||||
// create a container connected to testnetwork1
|
||||
dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
|
||||
// Connect second container to none network. it must fail as well
|
||||
_, _, err = dockerCmdWithError("network", "connect", "none", "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")
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
// +build experimental
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func assertSrvIsAvailable(c *check.C, sname, name string) {
|
||||
if !isSrvPresent(c, sname, name) {
|
||||
c.Fatalf("Service %s on network %s not found in service ls o/p", sname, name)
|
||||
}
|
||||
}
|
||||
|
||||
func assertSrvNotAvailable(c *check.C, sname, name string) {
|
||||
if isSrvPresent(c, sname, name) {
|
||||
c.Fatalf("Found service %s on network %s in service ls o/p", sname, name)
|
||||
}
|
||||
}
|
||||
|
||||
func isSrvPresent(c *check.C, sname, name string) bool {
|
||||
out, _, _ := dockerCmdWithStdoutStderr(c, "service", "ls")
|
||||
lines := strings.Split(out, "\n")
|
||||
for i := 1; i < len(lines)-1; i++ {
|
||||
if strings.Contains(lines[i], sname) && strings.Contains(lines[i], name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isCntPresent(c *check.C, cname, sname, name string) bool {
|
||||
out, _, _ := dockerCmdWithStdoutStderr(c, "service", "ls", "--no-trunc")
|
||||
lines := strings.Split(out, "\n")
|
||||
for i := 1; i < len(lines)-1; i++ {
|
||||
fmt.Println(lines)
|
||||
if strings.Contains(lines[i], name) && strings.Contains(lines[i], sname) && strings.Contains(lines[i], cname) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestDockerServiceCreateDelete(c *check.C) {
|
||||
dockerCmdWithStdoutStderr(c, "network", "create", "test")
|
||||
assertNwIsAvailable(c, "test")
|
||||
|
||||
dockerCmdWithStdoutStderr(c, "service", "publish", "s1.test")
|
||||
assertSrvIsAvailable(c, "s1", "test")
|
||||
|
||||
dockerCmdWithStdoutStderr(c, "service", "unpublish", "s1.test")
|
||||
assertSrvNotAvailable(c, "s1", "test")
|
||||
|
||||
dockerCmdWithStdoutStderr(c, "network", "rm", "test")
|
||||
assertNwNotAvailable(c, "test")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestDockerPublishServiceFlag(c *check.C) {
|
||||
// Run saying the container is the backend for the specified service on the specified network
|
||||
out, _ := dockerCmd(c, "run", "-d", "--expose=23", "--publish-service", "telnet.production", "busybox", "top")
|
||||
cid := strings.TrimSpace(out)
|
||||
|
||||
// Verify container is attached in service ps o/p
|
||||
assertSrvIsAvailable(c, "telnet", "production")
|
||||
dockerCmd(c, "rm", "-f", cid)
|
||||
}
|
||||
Reference in New Issue
Block a user