mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
22a9ba090e
* Exiting experimental network UX * removed experimental service UX * integrated with the new network remote API Signed-off-by: Madhu Venugopal <madhu@docker.com>
98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/go-check/check"
|
|
)
|
|
|
|
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++ {
|
|
if strings.Contains(lines[i], name) {
|
|
return true
|
|
}
|
|
}
|
|
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 {
|
|
assertNwIsAvailable(c, nn)
|
|
}
|
|
}
|
|
|
|
func (s *DockerSuite) TestDockerNetworkCreateDelete(c *check.C) {
|
|
dockerCmd(c, "network", "create", "test")
|
|
assertNwIsAvailable(c, "test")
|
|
|
|
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")
|
|
}
|