Cleaned up integration-cli/docker_api_network_test.go

Signed-off-by: Zachary Jaffee <zij@case.edu>
This commit is contained in:
Zachary Jaffee 2015-10-15 13:37:54 -04:00
parent 9216b08536
commit 7345050e4d

View file

@ -10,6 +10,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/daemon/network"
"github.com/docker/docker/pkg/integration/checker"
"github.com/docker/docker/pkg/parsers/filters"
"github.com/go-check/check"
)
@ -18,7 +19,7 @@ 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)
c.Assert(isNetworkAvailable(c, nn), checker.Equals, true)
}
}
@ -30,25 +31,25 @@ func (s *DockerSuite) TestApiNetworkCreateDelete(c *check.C) {
CheckDuplicate: true,
}
id := createNetwork(c, config, true)
c.Assert(isNetworkAvailable(c, name), check.Equals, true)
c.Assert(isNetworkAvailable(c, name), checker.Equals, true)
// POST another network with same name and CheckDuplicate must fail
createNetwork(c, config, false)
// delete the network and make sure it is deleted
deleteNetwork(c, id, true)
c.Assert(isNetworkAvailable(c, name), check.Equals, false)
c.Assert(isNetworkAvailable(c, name), checker.Equals, false)
}
func (s *DockerSuite) TestApiNetworkFilter(c *check.C) {
nr := getNetworkResource(c, getNetworkIDByName(c, "bridge"))
c.Assert(nr.Name, check.Equals, "bridge")
c.Assert(nr.Name, checker.Equals, "bridge")
}
func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
// Inspect default bridge network
nr := getNetworkResource(c, "bridge")
c.Assert(nr.Name, check.Equals, "bridge")
c.Assert(nr.Name, checker.Equals, "bridge")
// run a container and attach it to the default bridge network
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
@ -57,15 +58,15 @@ func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
// inspect default bridge network again and make sure the container is connected
nr = getNetworkResource(c, nr.ID)
c.Assert(nr.Driver, check.Equals, "bridge")
c.Assert(nr.Scope, check.Equals, "local")
c.Assert(nr.IPAM.Driver, check.Equals, "default")
c.Assert(len(nr.Containers), check.Equals, 1)
c.Assert(nr.Containers[containerID], check.NotNil)
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.Containers), checker.Equals, 1)
c.Assert(nr.Containers[containerID], checker.NotNil)
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
c.Assert(err, check.IsNil)
c.Assert(ip.String(), check.Equals, containerIP)
c.Assert(err, checker.IsNil)
c.Assert(ip.String(), checker.Equals, containerIP)
// IPAM configuration inspect
ipam := network.IPAM{
@ -78,16 +79,16 @@ func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
IPAM: ipam,
}
id0 := createNetwork(c, config, true)
c.Assert(isNetworkAvailable(c, "br0"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, true)
nr = getNetworkResource(c, id0)
c.Assert(len(nr.IPAM.Config), check.Equals, 1)
c.Assert(nr.IPAM.Config[0].Subnet, check.Equals, "172.28.0.0/16")
c.Assert(nr.IPAM.Config[0].IPRange, check.Equals, "172.28.5.0/24")
c.Assert(nr.IPAM.Config[0].Gateway, check.Equals, "172.28.5.254")
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")
// delete the network and make sure it is deleted
deleteNetwork(c, id0, true)
c.Assert(isNetworkAvailable(c, "br0"), check.Equals, false)
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, false)
}
func (s *DockerSuite) TestApiNetworkConnectDisconnect(c *check.C) {
@ -98,9 +99,9 @@ func (s *DockerSuite) TestApiNetworkConnectDisconnect(c *check.C) {
}
id := createNetwork(c, config, 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)
c.Assert(nr.Name, checker.Equals, name)
c.Assert(nr.ID, checker.Equals, id)
c.Assert(len(nr.Containers), checker.Equals, 0)
// run a container
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
@ -111,20 +112,20 @@ func (s *DockerSuite) TestApiNetworkConnectDisconnect(c *check.C) {
// 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)
c.Assert(len(nr.Containers), checker.Equals, 1)
c.Assert(nr.Containers[containerID], checker.NotNil)
// check if container IP matches network inspect
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
c.Assert(err, check.IsNil)
c.Assert(err, checker.IsNil)
containerIP := findContainerIP(c, "test")
c.Assert(ip.String(), check.Equals, containerIP)
c.Assert(ip.String(), checker.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)
c.Assert(nr.Name, checker.Equals, name)
c.Assert(len(nr.Containers), checker.Equals, 0)
// delete the network
deleteNetwork(c, nr.ID, true)
@ -142,7 +143,7 @@ func (s *DockerSuite) TestApiNetworkIpamMultipleBridgeNetworks(c *check.C) {
IPAM: ipam0,
}
id0 := createNetwork(c, config0, true)
c.Assert(isNetworkAvailable(c, "test0"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test0"), checker.Equals, true)
ipam1 := network.IPAM{
Driver: "default",
@ -155,7 +156,7 @@ func (s *DockerSuite) TestApiNetworkIpamMultipleBridgeNetworks(c *check.C) {
IPAM: ipam1,
}
createNetwork(c, config1, false)
c.Assert(isNetworkAvailable(c, "test1"), check.Equals, false)
c.Assert(isNetworkAvailable(c, "test1"), checker.Equals, false)
ipam2 := network.IPAM{
Driver: "default",
@ -168,20 +169,20 @@ func (s *DockerSuite) TestApiNetworkIpamMultipleBridgeNetworks(c *check.C) {
IPAM: ipam2,
}
createNetwork(c, config2, true)
c.Assert(isNetworkAvailable(c, "test2"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test2"), checker.Equals, true)
// remove test0 and retry to create test1
deleteNetwork(c, id0, true)
createNetwork(c, config1, true)
c.Assert(isNetworkAvailable(c, "test1"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test1"), checker.Equals, true)
// for networks w/o ipam specified, docker will choose proper non-overlapping subnets
createNetwork(c, types.NetworkCreate{Name: "test3"}, true)
c.Assert(isNetworkAvailable(c, "test3"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test3"), checker.Equals, true)
createNetwork(c, types.NetworkCreate{Name: "test4"}, true)
c.Assert(isNetworkAvailable(c, "test4"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test4"), checker.Equals, true)
createNetwork(c, types.NetworkCreate{Name: "test5"}, true)
c.Assert(isNetworkAvailable(c, "test5"), check.Equals, true)
c.Assert(isNetworkAvailable(c, "test5"), checker.Equals, true)
for i := 1; i < 6; i++ {
deleteNetwork(c, fmt.Sprintf("test%d", i), true)
@ -190,12 +191,12 @@ func (s *DockerSuite) TestApiNetworkIpamMultipleBridgeNetworks(c *check.C) {
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)
c.Assert(status, checker.Equals, http.StatusOK)
c.Assert(err, checker.IsNil)
nJSON := []types.NetworkResource{}
err = json.Unmarshal(body, &nJSON)
c.Assert(err, check.IsNil)
c.Assert(err, checker.IsNil)
for _, n := range nJSON {
if n.Name == name {
@ -212,28 +213,28 @@ func getNetworkIDByName(c *check.C, name string) string {
)
filterArgs["name"] = []string{name}
filterJSON, err := filters.ToParam(filterArgs)
c.Assert(err, check.IsNil)
c.Assert(err, checker.IsNil)
v.Set("filters", filterJSON)
status, body, err := sockRequest("GET", "/networks?"+v.Encode(), nil)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, checker.Equals, http.StatusOK)
c.Assert(err, checker.IsNil)
nJSON := []types.NetworkResource{}
err = json.Unmarshal(body, &nJSON)
c.Assert(err, check.IsNil)
c.Assert(len(nJSON), check.Equals, 1)
c.Assert(err, checker.IsNil)
c.Assert(len(nJSON), checker.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)
c.Assert(err, checker.IsNil)
nr := types.NetworkResource{}
err = json.Unmarshal(obj, &nr)
c.Assert(err, check.IsNil)
c.Assert(err, checker.IsNil)
return &nr
}
@ -241,16 +242,16 @@ func getNetworkResource(c *check.C, id string) *types.NetworkResource {
func createNetwork(c *check.C, config types.NetworkCreate, shouldSucceed bool) string {
status, resp, err := sockRequest("POST", "/networks/create", config)
if !shouldSucceed {
c.Assert(status, check.Not(check.Equals), http.StatusCreated)
c.Assert(status, checker.Not(checker.Equals), http.StatusCreated)
return ""
}
c.Assert(status, check.Equals, http.StatusCreated)
c.Assert(err, check.IsNil)
c.Assert(status, checker.Equals, http.StatusCreated)
c.Assert(err, checker.IsNil)
var nr types.NetworkCreateResponse
err = json.Unmarshal(resp, &nr)
c.Assert(err, check.IsNil)
c.Assert(err, checker.IsNil)
return nr.ID
}
@ -261,8 +262,8 @@ func connectNetwork(c *check.C, nid, cid string) {
}
status, _, err := sockRequest("POST", "/networks/"+nid+"/connect", config)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, checker.Equals, http.StatusOK)
c.Assert(err, checker.IsNil)
}
func disconnectNetwork(c *check.C, nid, cid string) {
@ -271,17 +272,17 @@ func disconnectNetwork(c *check.C, nid, cid string) {
}
status, _, err := sockRequest("POST", "/networks/"+nid+"/disconnect", config)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, checker.Equals, http.StatusOK)
c.Assert(err, checker.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)
c.Assert(status, checker.Not(checker.Equals), http.StatusOK)
c.Assert(err, checker.NotNil)
return
}
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, checker.Equals, http.StatusOK)
c.Assert(err, checker.IsNil)
}