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"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
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"
|
2023-07-27 11:13:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-05-20 12:20:19 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkGetDefaults(c *testing.T) {
|
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 {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, nn))
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkFilter(c *testing.T) {
|
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"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, nr.Name, "bridge")
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkInspectBridge(c *testing.T) {
|
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")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, nr.Name, "bridge")
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// run a container and attach it to the default bridge network
|
2023-07-27 11:13:00 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "--name", "test", "busybox", "top").Stdout()
|
2015-09-25 10:19:17 +00:00
|
|
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, nr.Driver, "bridge")
|
|
|
|
assert.Equal(c, nr.Scope, "local")
|
|
|
|
assert.Equal(c, nr.Internal, false)
|
|
|
|
assert.Equal(c, nr.EnableIPv6, false)
|
|
|
|
assert.Equal(c, nr.IPAM.Driver, "default")
|
|
|
|
_, ok := nr.Containers[containerID]
|
|
|
|
assert.Assert(c, ok)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, ip.String(), containerIP)
|
2017-09-12 15:18:02 +00:00
|
|
|
}
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkInspectUserDefinedNetwork(c *testing.T) {
|
2017-09-12 15:18:02 +00:00
|
|
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "br0"))
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2017-09-12 15:18:02 +00:00
|
|
|
nr := getNetworkResource(c, id0)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(nr.IPAM.Config), 1)
|
|
|
|
assert.Equal(c, nr.IPAM.Config[0].Subnet, "172.28.0.0/16")
|
|
|
|
assert.Equal(c, nr.IPAM.Config[0].IPRange, "172.28.5.0/24")
|
|
|
|
assert.Equal(c, nr.IPAM.Config[0].Gateway, "172.28.5.254")
|
|
|
|
assert.Equal(c, nr.Options["foo"], "bar")
|
|
|
|
assert.Equal(c, nr.Options["opts"], "dopts")
|
2015-10-15 01:49:27 +00:00
|
|
|
|
2015-10-09 18:21:48 +00:00
|
|
|
// delete the network and make sure it is deleted
|
|
|
|
deleteNetwork(c, id0, true)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, !isNetworkAvailable(c, "br0"))
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkConnectDisconnect(c *testing.T) {
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, nr.Name, name)
|
|
|
|
assert.Equal(c, nr.ID, id)
|
|
|
|
assert.Equal(c, len(nr.Containers), 0)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// run a container
|
2023-07-27 11:13:00 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "--name", "test", "busybox", "top").Stdout()
|
2015-09-25 10:19:17 +00:00
|
|
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(nr.Containers), 1)
|
|
|
|
_, ok := nr.Containers[containerID]
|
|
|
|
assert.Assert(c, ok)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// check if container IP matches network inspect
|
|
|
|
ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-26 12:00:49 +00:00
|
|
|
containerIP := findContainerIP(c, "test", "testnetwork")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, ip.String(), containerIP)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// disconnect container from the network
|
|
|
|
disconnectNetwork(c, nr.ID, containerID)
|
|
|
|
nr = getNetworkResource(c, nr.ID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, nr.Name, name)
|
|
|
|
assert.Equal(c, len(nr.Containers), 0)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
// delete the network
|
|
|
|
deleteNetwork(c, nr.ID, true)
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPINetworkIPAMMultipleBridgeNetworks(c *testing.T) {
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test0"))
|
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
|
|
|
}
|
2023-12-04 22:26:00 +00:00
|
|
|
createNetwork(c, config1, http.StatusForbidden)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, !isNetworkAvailable(c, "test1"))
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test2"))
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test1"))
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test3"))
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, types.NetworkCreateRequest{Name: "test4"}, http.StatusCreated)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test4"))
|
2017-09-29 02:13:44 +00:00
|
|
|
createNetwork(c, types.NetworkCreateRequest{Name: "test5"}, http.StatusCreated)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, isNetworkAvailable(c, "test5"))
|
2015-10-09 18:21:48 +00:00
|
|
|
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
deleteNetwork(c, fmt.Sprintf("test%d", i), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPICreateDeletePredefinedNetworks(c *testing.T) {
|
2018-05-04 21:15:00 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SwarmInactive)
|
2015-10-25 23:09:54 +00:00
|
|
|
createDeletePredefinedNetwork(c, "bridge")
|
|
|
|
createDeletePredefinedNetwork(c, "none")
|
|
|
|
createDeletePredefinedNetwork(c, "host")
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func createDeletePredefinedNetwork(c *testing.T, name string) {
|
2015-10-25 23:09:54 +00:00
|
|
|
// Create pre-defined network
|
libnet: Make sure network names are unique
Fixes #18864, #20648, #33561, #40901.
[This GH comment][1] makes clear network name uniqueness has never been
enforced due to the eventually consistent nature of Classic Swarm
datastores:
> there is no guaranteed way to check for duplicates across a cluster of
> docker hosts.
And this is further confirmed by other comments made by @mrjana in that
same issue, eg. [this one][2]:
> we want to adopt a schema which can pave the way in the future for a
> completely decentralized cluster of docker hosts (if scalability is
> needed).
This decentralized model is what Classic Swarm was trying to be. It's
been superseded since then by Docker Swarm, which has a centralized
control plane.
To circumvent this drawback, the `NetworkCreate` endpoint accepts a
`CheckDuplicate` flag. However it's not perfectly reliable as it won't
catch concurrent requests.
Due to this design decision, API clients like Compose have to implement
workarounds to make sure names are really unique (eg.
docker/compose#9585). And the daemon itself has seen a string of issues
due to that decision, including some that aren't fixed to this day (for
instance moby/moby#40901):
> The problem is, that if you specify a network for a container using
> the ID, it will add that network to the container but it will then
> change it to reference the network by using the name.
To summarize, this "feature" is broken, has no practical use and is a
source of pain for Docker users and API consumers. So let's just remove
it for _all_ API versions.
[1]: https://github.com/moby/moby/issues/18864#issuecomment-167201414
[2]: https://github.com/moby/moby/issues/18864#issuecomment-167202589
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-08-16 18:11:10 +00:00
|
|
|
config := types.NetworkCreateRequest{Name: name}
|
2018-05-04 21:15:00 +00:00
|
|
|
expectedStatus := http.StatusForbidden
|
|
|
|
createNetwork(c, config, expectedStatus)
|
2017-09-29 02:13:44 +00:00
|
|
|
deleteNetwork(c, name, false)
|
2015-10-25 23:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func isNetworkAvailable(c *testing.T, name string) bool {
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, body, err := request.Get(testutil.GetContext(c), "/networks")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer resp.Body.Close()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusOK)
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2018-05-19 11:38:54 +00:00
|
|
|
var nJSON []types.NetworkResource
|
2016-12-30 09:49:36 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&nJSON)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getNetworkIDByName(c *testing.T, name string) string {
|
2023-04-25 13:14:27 +00:00
|
|
|
filterJSON, err := filters.ToJSON(filters.NewArgs(filters.Arg("name", name)))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-25 13:14:27 +00:00
|
|
|
v := url.Values{}
|
2015-09-25 10:19:17 +00:00
|
|
|
v.Set("filters", filterJSON)
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, body, err := request.Get(testutil.GetContext(c), "/networks?"+v.Encode())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusOK)
|
|
|
|
assert.NilError(c, err)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
2018-05-19 11:38:54 +00:00
|
|
|
var nJSON []types.NetworkResource
|
2016-12-30 09:49:36 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&nJSON)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, res != "")
|
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
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getNetworkResource(c *testing.T, id string) *types.NetworkResource {
|
2023-07-14 18:02:38 +00:00
|
|
|
_, obj, err := request.Get(testutil.GetContext(c), "/networks/"+id)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
|
|
|
return &nr
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func createNetwork(c *testing.T, config types.NetworkCreateRequest, expectedStatusCode int) string {
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, body, err := request.Post(testutil.GetContext(c), "/networks/create", request.JSONBody(config))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer resp.Body.Close()
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2018-05-04 21:15:00 +00:00
|
|
|
if expectedStatusCode >= 0 {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, expectedStatusCode)
|
2018-05-04 21:15:00 +00:00
|
|
|
} else {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, resp.StatusCode != -expectedStatusCode)
|
2018-05-04 21:15:00 +00:00
|
|
|
}
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2018-05-04 21:15:00 +00:00
|
|
|
if expectedStatusCode == http.StatusCreated || expectedStatusCode < 0 {
|
2017-09-29 02:13:44 +00:00
|
|
|
var nr types.NetworkCreateResponse
|
|
|
|
err = json.NewDecoder(body).Decode(&nr)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-09-25 10:19:17 +00:00
|
|
|
|
2017-09-29 02:13:44 +00:00
|
|
|
return nr.ID
|
|
|
|
}
|
2018-01-14 23:42:25 +00:00
|
|
|
return ""
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func connectNetwork(c *testing.T, nid, cid string) {
|
2015-09-25 10:19:17 +00:00
|
|
|
config := types.NetworkConnect{
|
|
|
|
Container: cid,
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, _, err := request.Post(testutil.GetContext(c), "/networks/"+nid+"/connect", request.JSONBody(config))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusOK)
|
|
|
|
assert.NilError(c, err)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func disconnectNetwork(c *testing.T, nid, cid string) {
|
2015-09-25 10:19:17 +00:00
|
|
|
config := types.NetworkConnect{
|
|
|
|
Container: cid,
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, _, err := request.Post(testutil.GetContext(c), "/networks/"+nid+"/disconnect", request.JSONBody(config))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusOK)
|
|
|
|
assert.NilError(c, err)
|
2015-09-25 10:19:17 +00:00
|
|
|
}
|
2015-05-20 12:20:19 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func deleteNetwork(c *testing.T, id string, shouldSucceed bool) {
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, _, err := request.Delete(testutil.GetContext(c), "/networks/"+id)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer resp.Body.Close()
|
2015-09-25 10:19:17 +00:00
|
|
|
if !shouldSucceed {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, resp.StatusCode != http.StatusOK)
|
2015-09-25 10:19:17 +00:00
|
|
|
return
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusNoContent)
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|