2015-05-20 12:20:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-10-09 18:21:48 +00:00
|
|
|
"fmt"
|
2015-09-25 10:19:17 +00:00
|
|
|
"net"
|
2015-05-20 12:20:19 +00:00
|
|
|
"net/http"
|
2015-09-25 10:19:17 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-12-30 09:49:36 +00:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2015-05-20 12:20:19 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkGetDefaults(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-25 10:19:17 +00:00
|
|
|
// By default docker daemon creates 3 networks. check if they are present
|
|
|
|
defaults := []string{"bridge", "host", "none"}
|
|
|
|
for _, nn := range defaults {
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, nn), checker.Equals, true)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkCreateDelete(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-25 10:19:17 +00:00
|
|
|
// Create a network
|
|
|
|
name := "testnetwork"
|
2016-04-13 08:33:46 +00:00
|
|
|
config := types.NetworkCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
CheckDuplicate: true,
|
|
|
|
},
|
2015-10-09 18:21:48 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
id := createNetwork(c, config, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, name), checker.Equals, true)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// delete the network and make sure it is deleted
|
|
|
|
deleteNetwork(c, id, true)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, name), checker.Equals, false)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkCreateCheckDuplicate(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-12-18 06:35:16 +00:00
|
|
|
name := "testcheckduplicate"
|
2016-04-13 08:33:46 +00:00
|
|
|
configOnCheck := types.NetworkCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
CheckDuplicate: true,
|
|
|
|
},
|
2015-12-18 06:35:16 +00:00
|
|
|
}
|
2016-04-13 08:33:46 +00:00
|
|
|
configNotCheck := types.NetworkCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
CheckDuplicate: false,
|
|
|
|
},
|
2015-12-18 06:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a new network first
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, configOnCheck, http.StatusCreated)
|
2015-12-18 06:35:16 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, name), checker.Equals, true)
|
|
|
|
|
|
|
|
// Creating another network with same name and CheckDuplicate must fail
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, configOnCheck, http.StatusConflict)
|
2015-12-18 06:35:16 +00:00
|
|
|
|
|
|
|
// Creating another network with same name and not CheckDuplicate must succeed
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, configNotCheck, http.StatusCreated)
|
2015-12-18 06:35:16 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkFilter(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-25 10:19:17 +00:00
|
|
|
nr := getNetworkResource(c, getNetworkIDByName(c, "bridge"))
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.Name, checker.Equals, "bridge")
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 15:18:02 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkInspectBridge(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-25 10:19:17 +00:00
|
|
|
// Inspect default bridge network
|
|
|
|
nr := getNetworkResource(c, "bridge")
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.Name, checker.Equals, "bridge")
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// run a container and attach it to the default bridge network
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
containerID := strings.TrimSpace(out)
|
2015-10-26 12:00:49 +00:00
|
|
|
containerIP := findContainerIP(c, "test", "bridge")
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// inspect default bridge network again and make sure the container is connected
|
|
|
|
nr = getNetworkResource(c, nr.ID)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.Driver, checker.Equals, "bridge")
|
|
|
|
c.Assert(nr.Scope, checker.Equals, "local")
|
2015-12-10 14:02:50 +00:00
|
|
|
c.Assert(nr.Internal, checker.Equals, false)
|
|
|
|
c.Assert(nr.EnableIPv6, checker.Equals, false)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.IPAM.Driver, checker.Equals, "default")
|
|
|
|
c.Assert(nr.Containers[containerID], checker.NotNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(ip.String(), checker.Equals, containerIP)
|
2017-09-12 15:18:02 +00:00
|
|
|
}
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2017-09-12 15:18:02 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkInspectUserDefinedNetwork(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-09 18:21:48 +00:00
|
|
|
// IPAM configuration inspect
|
2016-08-31 15:25:14 +00:00
|
|
|
ipam := &network.IPAM{
|
2015-10-09 18:21:48 +00:00
|
|
|
Driver: "default",
|
|
|
|
Config: []network.IPAMConfig{{Subnet: "172.28.0.0/16", IPRange: "172.28.5.0/24", Gateway: "172.28.5.254"}},
|
|
|
|
}
|
2016-04-13 08:33:46 +00:00
|
|
|
config := types.NetworkCreateRequest{
|
|
|
|
Name: "br0",
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
Driver: "bridge",
|
|
|
|
IPAM: ipam,
|
|
|
|
Options: map[string]string{"foo": "bar", "opts": "dopts"},
|
|
|
|
},
|
2015-10-09 18:21:48 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
id0 := createNetwork(c, config, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, true)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2017-09-12 15:18:02 +00:00
|
|
|
nr := getNetworkResource(c, id0)
|
2015-10-15 17:37:54 +00:00
|
|
|
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-15 01:49:27 +00:00
|
|
|
c.Assert(nr.Options["foo"], checker.Equals, "bar")
|
|
|
|
c.Assert(nr.Options["opts"], checker.Equals, "dopts")
|
|
|
|
|
2015-10-09 18:21:48 +00:00
|
|
|
// delete the network and make sure it is deleted
|
|
|
|
deleteNetwork(c, id0, true)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, false)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkConnectDisconnect(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-25 10:19:17 +00:00
|
|
|
// Create test network
|
|
|
|
name := "testnetwork"
|
2016-04-13 08:33:46 +00:00
|
|
|
config := types.NetworkCreateRequest{
|
2015-10-09 18:21:48 +00:00
|
|
|
Name: name,
|
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
id := createNetwork(c, config, http.StatusCreated)
|
2015-09-25 10:19:17 +00:00
|
|
|
nr := getNetworkResource(c, id)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.Name, checker.Equals, name)
|
|
|
|
c.Assert(nr.ID, checker.Equals, id)
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 0)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// 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)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 1)
|
|
|
|
c.Assert(nr.Containers[containerID], checker.NotNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// check if container IP matches network inspect
|
|
|
|
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-26 12:00:49 +00:00
|
|
|
containerIP := findContainerIP(c, "test", "testnetwork")
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(ip.String(), checker.Equals, containerIP)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// disconnect container from the network
|
|
|
|
disconnectNetwork(c, nr.ID, containerID)
|
|
|
|
nr = getNetworkResource(c, nr.ID)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(nr.Name, checker.Equals, name)
|
|
|
|
c.Assert(len(nr.Containers), checker.Equals, 0)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// delete the network
|
|
|
|
deleteNetwork(c, nr.ID, true)
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPINetworkIPAMMultipleBridgeNetworks(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-09 18:21:48 +00:00
|
|
|
// test0 bridge network
|
2016-08-31 15:25:14 +00:00
|
|
|
ipam0 := &network.IPAM{
|
2015-10-09 18:21:48 +00:00
|
|
|
Driver: "default",
|
|
|
|
Config: []network.IPAMConfig{{Subnet: "192.178.0.0/16", IPRange: "192.178.128.0/17", Gateway: "192.178.138.100"}},
|
|
|
|
}
|
2016-04-13 08:33:46 +00:00
|
|
|
config0 := types.NetworkCreateRequest{
|
|
|
|
Name: "test0",
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
Driver: "bridge",
|
|
|
|
IPAM: ipam0,
|
|
|
|
},
|
2015-10-09 18:21:48 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
id0 := createNetwork(c, config0, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test0"), checker.Equals, true)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2016-08-31 15:25:14 +00:00
|
|
|
ipam1 := &network.IPAM{
|
2015-10-09 18:21:48 +00:00
|
|
|
Driver: "default",
|
|
|
|
Config: []network.IPAMConfig{{Subnet: "192.178.128.0/17", Gateway: "192.178.128.1"}},
|
|
|
|
}
|
|
|
|
// test1 bridge network overlaps with test0
|
2016-04-13 08:33:46 +00:00
|
|
|
config1 := types.NetworkCreateRequest{
|
|
|
|
Name: "test1",
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
Driver: "bridge",
|
|
|
|
IPAM: ipam1,
|
|
|
|
},
|
2015-10-09 18:21:48 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, config1, http.StatusForbidden)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test1"), checker.Equals, false)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2016-08-31 15:25:14 +00:00
|
|
|
ipam2 := &network.IPAM{
|
2015-10-09 18:21:48 +00:00
|
|
|
Driver: "default",
|
|
|
|
Config: []network.IPAMConfig{{Subnet: "192.169.0.0/16", Gateway: "192.169.100.100"}},
|
|
|
|
}
|
|
|
|
// test2 bridge network does not overlap
|
2016-04-13 08:33:46 +00:00
|
|
|
config2 := types.NetworkCreateRequest{
|
|
|
|
Name: "test2",
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
Driver: "bridge",
|
|
|
|
IPAM: ipam2,
|
|
|
|
},
|
2015-10-09 18:21:48 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, config2, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test2"), checker.Equals, true)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
|
|
|
// remove test0 and retry to create test1
|
|
|
|
deleteNetwork(c, id0, true)
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, config1, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test1"), checker.Equals, true)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
|
|
|
// for networks w/o ipam specified, docker will choose proper non-overlapping subnets
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, types.NetworkCreateRequest{Name: "test3"}, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test3"), checker.Equals, true)
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, types.NetworkCreateRequest{Name: "test4"}, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test4"), checker.Equals, true)
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, types.NetworkCreateRequest{Name: "test5"}, http.StatusCreated)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(isNetworkAvailable(c, "test5"), checker.Equals, true)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
deleteNetwork(c, fmt.Sprintf("test%d", i), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestAPICreateDeletePredefinedNetworks(c *check.C) {
|
2016-01-08 20:58:54 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-25 23:09:54 +00:00
|
|
|
createDeletePredefinedNetwork(c, "bridge")
|
|
|
|
createDeletePredefinedNetwork(c, "none")
|
|
|
|
createDeletePredefinedNetwork(c, "host")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDeletePredefinedNetwork(c *check.C, name string) {
|
|
|
|
// Create pre-defined network
|
2016-04-13 08:33:46 +00:00
|
|
|
config := types.NetworkCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
NetworkCreate: types.NetworkCreate{
|
|
|
|
CheckDuplicate: true,
|
|
|
|
},
|
2015-10-25 23:09:54 +00:00
|
|
|
}
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, config, http.StatusForbidden)
|
|
|
|
deleteNetwork(c, name, false)
|
2015-10-25 23:09:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:20:19 +00:00
|
|
|
func isNetworkAvailable(c *check.C, name string) bool {
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, body, err := request.Get("/networks")
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2015-09-25 10:19:17 +00:00
|
|
|
nJSON := []types.NetworkResource{}
|
2016-12-30 09:49:36 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&nJSON)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
for _, n := range nJSON {
|
2015-05-20 12:20:19 +00:00
|
|
|
if n.Name == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkIDByName(c *check.C, name string) string {
|
|
|
|
var (
|
|
|
|
v = url.Values{}
|
2015-11-26 01:27:11 +00:00
|
|
|
filterArgs = filters.NewArgs()
|
2015-09-25 10:19:17 +00:00
|
|
|
)
|
2015-11-26 01:27:11 +00:00
|
|
|
filterArgs.Add("name", name)
|
2017-09-26 11:59:45 +00:00
|
|
|
filterJSON, err := filters.ToJSON(filterArgs)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
v.Set("filters", filterJSON)
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, body, err := request.Get("/networks?" + v.Encode())
|
2016-12-30 09:49:36 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
nJSON := []types.NetworkResource{}
|
2016-12-30 09:49:36 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&nJSON)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-09-08 15:16:15 +00:00
|
|
|
var res string
|
|
|
|
for _, n := range nJSON {
|
|
|
|
// Find exact match
|
|
|
|
if n.Name == name {
|
|
|
|
res = n.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(res, checker.Not(checker.Equals), "")
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2017-09-08 15:16:15 +00:00
|
|
|
return res
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:19:17 +00:00
|
|
|
func getNetworkResource(c *check.C, id string) *types.NetworkResource {
|
2017-03-06 15:35:27 +00:00
|
|
|
_, obj, err := request.Get("/networks/" + id)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
nr := types.NetworkResource{}
|
2016-12-30 09:49:36 +00:00
|
|
|
err = json.NewDecoder(obj).Decode(&nr)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
return &nr
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 02:13:44 +00:00
|
|
|
func createNetwork(c *check.C, config types.NetworkCreateRequest, expectedStatusCode int) string {
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, body, err := request.Post("/networks/create", request.JSONBody(config))
|
2017-01-11 20:38:52 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer resp.Body.Close()
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2017-09-29 02:13:44 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, expectedStatusCode)
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2017-09-29 02:13:44 +00:00
|
|
|
if expectedStatusCode == http.StatusCreated {
|
|
|
|
var nr types.NetworkCreateResponse
|
|
|
|
err = json.NewDecoder(body).Decode(&nr)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
2017-09-29 02:13:44 +00:00
|
|
|
return nr.ID
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func connectNetwork(c *check.C, nid, cid string) {
|
|
|
|
config := types.NetworkConnect{
|
|
|
|
Container: cid,
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, _, err := request.Post("/networks/"+nid+"/connect", request.JSONBody(config))
|
2016-12-30 09:49:36 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func disconnectNetwork(c *check.C, nid, cid string) {
|
|
|
|
config := types.NetworkConnect{
|
|
|
|
Container: cid,
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, _, err := request.Post("/networks/"+nid+"/disconnect", request.JSONBody(config))
|
2016-12-30 09:49:36 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
2015-10-15 17:37:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2015-09-25 10:19:17 +00:00
|
|
|
func deleteNetwork(c *check.C, id string, shouldSucceed bool) {
|
2017-03-06 15:35:27 +00:00
|
|
|
resp, _, err := request.Delete("/networks/" + id)
|
2017-01-11 20:38:52 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer resp.Body.Close()
|
2015-09-25 10:19:17 +00:00
|
|
|
if !shouldSucceed {
|
2016-12-30 09:49:36 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Not(checker.Equals), http.StatusOK)
|
2015-09-25 10:19:17 +00:00
|
|
|
return
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
2016-12-30 09:49:36 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|