2018-02-05 16:05:59 -05:00
|
|
|
package network // import "github.com/docker/docker/integration/network"
|
2017-08-14 16:23:57 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-05-04 17:15:00 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2019-01-02 08:16:25 -05:00
|
|
|
dclient "github.com/docker/docker/client"
|
2018-06-09 06:39:22 -04:00
|
|
|
"github.com/docker/docker/integration/internal/network"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2017-08-14 16:23:57 -04:00
|
|
|
)
|
|
|
|
|
2018-06-09 06:39:22 -04:00
|
|
|
func containsNetwork(nws []types.NetworkResource, networkID string) bool {
|
2017-08-14 16:23:57 -04:00
|
|
|
for _, n := range nws {
|
2018-06-09 06:39:22 -04:00
|
|
|
if n.ID == networkID {
|
2017-08-14 16:23:57 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// createAmbiguousNetworks creates three networks, of which the second network
|
|
|
|
// uses a prefix of the first network's ID as name. The third network uses the
|
|
|
|
// first network's ID as name.
|
|
|
|
//
|
|
|
|
// After successful creation, properties of all three networks is returned
|
2019-01-02 08:16:25 -05:00
|
|
|
func createAmbiguousNetworks(t *testing.T, ctx context.Context, client dclient.APIClient) (string, string, string) { // nolint: golint
|
2018-06-09 06:39:22 -04:00
|
|
|
testNet := network.CreateNoError(t, ctx, client, "testNet")
|
|
|
|
idPrefixNet := network.CreateNoError(t, ctx, client, testNet[:12])
|
|
|
|
fullIDNet := network.CreateNoError(t, ctx, client, testNet)
|
2017-08-14 16:23:57 -04:00
|
|
|
|
|
|
|
nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-14 16:23:57 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(true, containsNetwork(nws, testNet)), "failed to create network testNet")
|
|
|
|
assert.Check(t, is.Equal(true, containsNetwork(nws, idPrefixNet)), "failed to create network idPrefixNet")
|
|
|
|
assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "failed to create network fullIDNet")
|
2017-08-14 16:23:57 -04:00
|
|
|
return testNet, idPrefixNet, fullIDNet
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:24:51 -04:00
|
|
|
// TestNetworkCreateDelete tests creation and deletion of a network.
|
2018-06-15 16:06:45 -04:00
|
|
|
func TestNetworkCreateDelete(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-06-15 16:06:45 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
netName := "testnetwork_" + t.Name()
|
|
|
|
network.CreateNoError(t, ctx, client, netName,
|
|
|
|
network.WithCheckDuplicate(),
|
|
|
|
)
|
|
|
|
assert.Check(t, IsNetworkAvailable(client, netName))
|
|
|
|
|
|
|
|
// delete the network and make sure it is deleted
|
|
|
|
err := client.NetworkRemove(ctx, netName)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, IsNetworkNotAvailable(client, netName))
|
|
|
|
}
|
|
|
|
|
2017-08-14 16:23:57 -04:00
|
|
|
// TestDockerNetworkDeletePreferID tests that if a network with a name
|
|
|
|
// equal to another network's ID exists, the Network with the given
|
|
|
|
// ID is removed, and not the network with the given name.
|
|
|
|
func TestDockerNetworkDeletePreferID(t *testing.T) {
|
2018-05-04 17:15:00 -04:00
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.34"), "broken in earlier versions")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows",
|
|
|
|
"FIXME. Windows doesn't run DinD and uses networks shared between control daemon and daemon under test")
|
2017-08-14 16:23:57 -04:00
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2017-08-14 16:23:57 -04:00
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
testNet, idPrefixNet, fullIDNet := createAmbiguousNetworks(t, ctx, client)
|
2017-08-14 16:23:57 -04:00
|
|
|
|
|
|
|
// Delete the network using a prefix of the first network's ID as name.
|
|
|
|
// This should the network name with the id-prefix, not the original network.
|
2018-06-09 06:39:22 -04:00
|
|
|
err := client.NetworkRemove(ctx, testNet[:12])
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-14 16:23:57 -04:00
|
|
|
|
|
|
|
// Delete the network using networkID. This should remove the original
|
|
|
|
// network, not the network with the name equal to the networkID
|
2018-06-09 06:39:22 -04:00
|
|
|
err = client.NetworkRemove(ctx, testNet)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-14 16:23:57 -04:00
|
|
|
|
|
|
|
// networks "testNet" and "idPrefixNet" should be removed, but "fullIDNet" should still exist
|
|
|
|
nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(false, containsNetwork(nws, testNet)), "Network testNet not removed")
|
|
|
|
assert.Check(t, is.Equal(false, containsNetwork(nws, idPrefixNet)), "Network idPrefixNet not removed")
|
|
|
|
assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "Network fullIDNet not found")
|
2017-08-14 16:23:57 -04:00
|
|
|
}
|