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"
|
2018-03-25 13:19:50 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2017-08-01 17:30:18 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2018-05-04 17:15:00 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-02-09 13:37:55 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-02-05 19:27:14 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/poll"
|
|
|
|
"gotest.tools/skip"
|
2017-08-01 17:30:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2018-05-04 17:15:00 -04:00
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.32"), "broken in earlier versions")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2017-08-01 17:30:18 -04:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
aName := "a0" + t.Name()
|
|
|
|
bName := "b0" + t.Name()
|
|
|
|
aID := container.Run(t, ctx, client, container.WithName(aName))
|
|
|
|
bID := container.Run(t, ctx, client, container.WithName(bName), container.WithLinks(aName))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
err := client.ContainerRename(ctx, aID, "a1"+t.Name())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
container.Run(t, ctx, client, container.WithName(aName))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
|
|
|
err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
bID = container.Run(t, ctx, client, container.WithName(bName), container.WithLinks(aName))
|
2017-08-01 17:30:18 -04:00
|
|
|
|
2018-02-06 12:11:39 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, bID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-03-25 13:19:50 -04:00
|
|
|
assert.Check(t, is.DeepEqual([]string{"/" + aName + ":/" + bName + "/" + aName}, 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)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
oldName := "first_name" + t.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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("/"+oldName, inspect.Name))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
|
|
|
err = client.ContainerRename(ctx, oldName, newName)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err = client.ContainerInspect(ctx, cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("/"+newName, inspect.Name))
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameRunningContainerAndReuse(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
oldName := "first_name" + t.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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("/"+newName, inspect.Name))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
_, err = client.ContainerInspect(ctx, oldName)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "No such container: "+oldName))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("/"+oldName, inspect.Name))
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameInvalidName(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
oldName := "first_name" + t.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")
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "Invalid container name"))
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, oldName)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(cID, inspect.ID))
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2018-02-05 19:27:14 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
networkName := "network1" + t.Name()
|
|
|
|
_, err := client.NetworkCreate(ctx, networkName, types.NetworkCreate{})
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(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-03-25 13:19:50 -04:00
|
|
|
networkName: {},
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
2018-03-25 13:19:50 -04:00
|
|
|
c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
|
2018-02-05 19:27:14 -05:00
|
|
|
})
|
2018-03-25 13:19:50 -04:00
|
|
|
|
|
|
|
container1Name := "container1" + t.Name()
|
|
|
|
err = client.ContainerRename(ctx, cID, container1Name)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-03-02 08:45:14 -05:00
|
|
|
// Stop/Start the container to get registered
|
|
|
|
// FIXME(vdemeester) this is a really weird behavior as it fails otherwise
|
2018-03-25 13:19:50 -04:00
|
|
|
err = client.ContainerStop(ctx, container1Name, nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-03-25 13:19:50 -04:00
|
|
|
err = client.ContainerStart(ctx, container1Name, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-05 19:27:14 -05:00
|
|
|
|
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-03-25 13:19:50 -04:00
|
|
|
networkName: {},
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
2018-03-25 13:19:50 -04:00
|
|
|
c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
|
|
|
|
}, container.WithCmd("ping", count, "1", container1Name))
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(0, inspect.State.ExitCode), "container %s exited with the wrong exitcode: %+v", cID, inspect)
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should be a unit test
|
|
|
|
func TestRenameContainerWithSameName(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
oldName := "old" + t.Name()
|
|
|
|
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-03-25 13:19:50 -04:00
|
|
|
err := client.ContainerRename(ctx, oldName, oldName)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
|
2018-03-25 13:19:50 -04:00
|
|
|
err = client.ContainerRename(ctx, cID, oldName)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-03-02 08:45:14 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon())
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2018-02-05 19:27:14 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
db1Name := "db1" + t.Name()
|
|
|
|
db1ID := container.Run(t, ctx, client, container.WithName(db1Name))
|
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-03-25 13:19:50 -04:00
|
|
|
app1Name := "app1" + t.Name()
|
|
|
|
app2Name := "app2" + t.Name()
|
|
|
|
app1ID := container.Run(t, ctx, client, container.WithName(app1Name), container.WithLinks(db1Name+":/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
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
err := client.ContainerRename(ctx, app1Name, app2Name)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-05 19:27:14 -05:00
|
|
|
|
2018-03-25 13:19:50 -04:00
|
|
|
inspect, err := client.ContainerInspect(ctx, app2Name+"/mysql")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(db1ID, inspect.ID))
|
2018-02-05 19:27:14 -05:00
|
|
|
}
|