2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2017-08-01 17:30:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2018-02-05 19:27:14 -05:00
|
|
|
"time"
|
2017-08-01 17:30:18 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
2018-02-09 13:37:55 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/request"
|
2018-02-05 19:27:14 -05:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
2017-08-01 17:30:18 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This test simulates the scenario mentioned in #31392:
|
|
|
|
// Having two linked container, renaming the target and bringing a replacement
|
|
|
|
// and then deleting and recreating the source container linked to the new target.
|
|
|
|
// This checks that "rename" updates source container correctly and doesn't set it to null.
|
|
|
|
func TestRenameLinkedContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
aID := container.Run(t, ctx, client, container.WithName("a0"))
|
|
|
|
bID := container.Run(t, ctx, client, container.WithName("b0"), container.WithLinks("a0"))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-02-06 12:11:39 -05:00
|
|
|
err := client.ContainerRename(ctx, aID, "a1")
|
2017-08-01 17:30:18 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
container.Run(t, ctx, client, container.WithName("a0"))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
|
|
|
err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
bID = container.Run(t, ctx, client, container.WithName("b0"), container.WithLinks("a0"))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-02-06 12:11:39 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, bID)
|
2017-08-01 17:30:18 -04:00
|
|
|
require.NoError(t, err)
|
2018-02-06 12:11:39 -05:00
|
|
|
assert.Equal(t, []string{"/a0:/b0/a0"}, inspect.HostConfig.Links)
|
2017-08-01 17:30:18 -04:00
|
|
|
}
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
func TestRenameStoppedContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
oldName := "first_name"
|
2018-02-09 13:37:55 -05:00
|
|
|
cID := container.Run(t, ctx, client, container.WithName(oldName), container.WithCmd("sh"))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.Name, "/"+oldName)
|
|
|
|
|
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
|
|
|
err = client.ContainerRename(ctx, oldName, newName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
inspect, err = client.ContainerInspect(ctx, cID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.Name, "/"+newName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameRunningContainerAndReuse(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
oldName := "first_name"
|
2018-02-09 13:37:55 -05:00
|
|
|
cID := container.Run(t, ctx, client, container.WithName(oldName))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
|
|
|
err := client.ContainerRename(ctx, oldName, newName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.Name, "/"+newName)
|
|
|
|
|
|
|
|
_, err = client.ContainerInspect(ctx, oldName)
|
|
|
|
testutil.ErrorContains(t, err, "No such container: "+oldName)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
cID = container.Run(t, ctx, client, container.WithName(oldName))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err = client.ContainerInspect(ctx, cID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.Name, "/"+oldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameInvalidName(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
oldName := "first_name"
|
2018-02-09 13:37:55 -05:00
|
|
|
cID := container.Run(t, ctx, client, container.WithName(oldName))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
err := client.ContainerRename(ctx, oldName, "new:invalid")
|
|
|
|
testutil.ErrorContains(t, err, "Invalid container name")
|
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, oldName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.ID, cID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case for GitHub issue 22466
|
|
|
|
// Docker's service discovery works for named containers so
|
|
|
|
// ping to a named container should work, and an anonymous
|
|
|
|
// container without a name does not work with service discovery.
|
|
|
|
// However, an anonymous could be renamed to a named container.
|
|
|
|
// This test is to make sure once the container has been renamed,
|
|
|
|
// the service discovery for the (re)named container works.
|
|
|
|
func TestRenameAnonymousContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
_, err := client.NetworkCreate(ctx, "network1", types.NetworkCreate{})
|
|
|
|
require.NoError(t, err)
|
2018-02-09 13:37:55 -05:00
|
|
|
cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
|
|
|
|
c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
|
2018-02-05 19:27:14 -05:00
|
|
|
"network1": {},
|
|
|
|
}
|
2018-02-09 13:37:55 -05:00
|
|
|
c.HostConfig.NetworkMode = "network1"
|
2018-02-05 19:27:14 -05:00
|
|
|
})
|
|
|
|
err = client.ContainerRename(ctx, cID, "container1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = client.ContainerStart(ctx, "container1", types.ContainerStartOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
count := "-c"
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
count = "-n"
|
|
|
|
}
|
2018-02-09 13:37:55 -05:00
|
|
|
cID = container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
|
|
|
|
c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
|
2018-02-05 19:27:14 -05:00
|
|
|
"network1": {},
|
|
|
|
}
|
2018-02-09 13:37:55 -05:00
|
|
|
c.HostConfig.NetworkMode = "network1"
|
|
|
|
}, container.WithCmd("ping", count, "1", "container1"))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.State.ExitCode, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should be a unit test
|
|
|
|
func TestRenameContainerWithSameName(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
cID := container.Run(t, ctx, client, container.WithName("old"))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
err := client.ContainerRename(ctx, "old", "old")
|
|
|
|
testutil.ErrorContains(t, err, "Renaming a container with the same name")
|
|
|
|
err = client.ContainerRename(ctx, cID, "old")
|
|
|
|
testutil.ErrorContains(t, err, "Renaming a container with the same name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case for GitHub issue 23973
|
|
|
|
// When a container is being renamed, the container might
|
|
|
|
// be linked to another container. In that case, the meta data
|
|
|
|
// of the linked container should be updated so that the other
|
|
|
|
// container could still reference to the container that is renamed.
|
|
|
|
func TestRenameContainerWithLinkedContainer(t *testing.T) {
|
|
|
|
skip.If(t, !testEnv.IsLocalDaemon())
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
db1ID := container.Run(t, ctx, client, container.WithName("db1"))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
app1ID := container.Run(t, ctx, client, container.WithName("app1"), container.WithLinks("db1:/mysql"))
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
err := client.ContainerRename(ctx, "app1", "app2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, "app2/mysql")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, inspect.ID, db1ID)
|
|
|
|
}
|