2018-02-05 16:05:59 -05:00
|
|
|
package service // import "github.com/docker/docker/integration/service"
|
2018-01-20 18:06:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
2018-02-10 18:01:37 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-06-16 06:04:31 -04:00
|
|
|
net "github.com/docker/docker/integration/internal/network"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-01-20 18:06:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerNetworkConnectAlias(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-01-20 18:06:14 -05:00
|
|
|
defer setupTest(t)()
|
2018-02-02 17:36:59 -05:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2018-01-20 18:06:14 -05:00
|
|
|
defer d.Stop(t)
|
2018-04-13 11:02:56 -04:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2018-01-20 18:06:14 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
name := t.Name() + "test-alias"
|
2019-06-06 06:00:19 -04:00
|
|
|
net.CreateNoError(ctx, t, client, name,
|
2018-06-16 06:04:31 -04:00
|
|
|
net.WithDriver("overlay"),
|
|
|
|
net.WithAttachable(),
|
|
|
|
)
|
2018-02-10 18:01:37 -05:00
|
|
|
|
2019-06-06 07:00:37 -04:00
|
|
|
cID1 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
|
2018-02-10 18:01:37 -05:00
|
|
|
c.NetworkingConfig = &network.NetworkingConfig{
|
2018-05-17 10:29:52 -04:00
|
|
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
2018-01-20 18:06:14 -05:00
|
|
|
name: {},
|
|
|
|
},
|
2018-02-10 18:01:37 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-06-16 06:04:31 -04:00
|
|
|
err := client.NetworkConnect(ctx, name, cID1, &network.EndpointSettings{
|
2018-01-20 18:06:14 -05:00
|
|
|
Aliases: []string{
|
|
|
|
"aaa",
|
|
|
|
},
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-20 18:06:14 -05:00
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
err = client.ContainerStart(ctx, cID1, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-20 18:06:14 -05:00
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
ng1, err := client.ContainerInspect(ctx, cID1)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(ng1.NetworkSettings.Networks[name].Aliases), 2))
|
|
|
|
assert.Check(t, is.Equal(ng1.NetworkSettings.Networks[name].Aliases[0], "aaa"))
|
2018-01-20 18:06:14 -05:00
|
|
|
|
2019-06-06 07:00:37 -04:00
|
|
|
cID2 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
|
2018-02-10 18:01:37 -05:00
|
|
|
c.NetworkingConfig = &network.NetworkingConfig{
|
2018-05-17 10:29:52 -04:00
|
|
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
2018-01-20 18:06:14 -05:00
|
|
|
name: {},
|
|
|
|
},
|
2018-02-10 18:01:37 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
err = client.NetworkConnect(ctx, name, cID2, &network.EndpointSettings{
|
2018-01-20 18:06:14 -05:00
|
|
|
Aliases: []string{
|
|
|
|
"bbb",
|
|
|
|
},
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-20 18:06:14 -05:00
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
err = client.ContainerStart(ctx, cID2, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-20 18:06:14 -05:00
|
|
|
|
2018-05-11 16:04:34 -04:00
|
|
|
ng2, err := client.ContainerInspect(ctx, cID2)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(ng2.NetworkSettings.Networks[name].Aliases), 2))
|
|
|
|
assert.Check(t, is.Equal(ng2.NetworkSettings.Networks[name].Aliases[0], "bbb"))
|
2018-01-20 18:06:14 -05:00
|
|
|
}
|
2019-04-24 14:24:39 -04:00
|
|
|
|
|
|
|
func TestDockerNetworkReConnect(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
name := t.Name() + "dummyNet"
|
2019-06-12 16:41:36 -04:00
|
|
|
net.CreateNoError(ctx, t, client, name,
|
2019-04-24 14:24:39 -04:00
|
|
|
net.WithDriver("overlay"),
|
|
|
|
net.WithAttachable(),
|
|
|
|
)
|
|
|
|
|
2019-06-12 16:41:36 -04:00
|
|
|
c1 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
|
2019-04-24 14:24:39 -04:00
|
|
|
c.NetworkingConfig = &network.NetworkingConfig{
|
|
|
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
|
|
|
name: {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = client.ContainerStart(ctx, c1, types.ContainerStartOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
n1, err := client.ContainerInspect(ctx, c1)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{})
|
|
|
|
assert.ErrorContains(t, err, "is already attached to network")
|
|
|
|
|
|
|
|
n2, err := client.ContainerInspect(ctx, c1)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(n1, n2))
|
|
|
|
}
|