Migrate rename tests in integration-cli to api tests

This fix migrates rename tests in integration-cli to api tests

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2018-02-06 00:27:14 +00:00
parent 15001f83bd
commit be24a6b11e
2 changed files with 161 additions and 127 deletions

View File

@ -1,127 +0,0 @@
package main
import (
"strings"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/pkg/stringid"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
name := inspectField(c, cleanedContainerID, "Name")
newName := "new_name" + stringid.GenerateNonCryptoID()
dockerCmd(c, "rename", "first_name", newName)
name = inspectField(c, cleanedContainerID, "Name")
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
}
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
newName := "new_name" + stringid.GenerateNonCryptoID()
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "rename", "first_name", newName)
name := inspectField(c, cleanedContainerID, "Name")
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
}
func (s *DockerSuite) TestRenameRunningContainerAndReuse(c *check.C) {
out := runSleepingContainer(c, "--name", "first_name")
c.Assert(waitRun("first_name"), check.IsNil)
newName := "new_name"
ContainerID := strings.TrimSpace(out)
dockerCmd(c, "rename", "first_name", newName)
name := inspectField(c, ContainerID, "Name")
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
out = runSleepingContainer(c, "--name", "first_name")
c.Assert(waitRun("first_name"), check.IsNil)
newContainerID := strings.TrimSpace(out)
name = inspectField(c, newContainerID, "Name")
c.Assert(name, checker.Equals, "/first_name", check.Commentf("Failed to reuse container name"))
}
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
newName := "new_name" + stringid.GenerateNonCryptoID()
dockerCmd(c, "rename", "first_name", newName)
name := inspectField(c, newName, "Name")
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
result := dockerCmdWithResult("inspect", "-f={{.Name}}", "--type=container", "first_name")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "No such container: first_name",
})
}
// TODO: move to unit test
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
runSleepingContainer(c, "--name", "myname")
out, _, err := dockerCmdWithError("rename", "myname", "new:invalid")
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err))
out, _ = dockerCmd(c, "ps", "-a")
c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
}
func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "network", "create", "network1")
out, _ := dockerCmd(c, "create", "-it", "--net", "network1", "busybox", "top")
anonymousContainerID := strings.TrimSpace(out)
dockerCmd(c, "rename", anonymousContainerID, "container1")
dockerCmd(c, "start", "container1")
count := "-c"
if testEnv.OSType == "windows" {
count = "-n"
}
_, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1")
c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err))
}
func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
out := runSleepingContainer(c, "--name", "old")
ContainerID := strings.TrimSpace(out)
out, _, err := dockerCmdWithError("rename", "old", "old")
c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
out, _, err = dockerCmdWithError("rename", ContainerID, "old")
c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
}
// Test case for #23973
func (s *DockerSuite) TestRenameContainerWithLinkedContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
db1, _ := dockerCmd(c, "run", "--name", "db1", "-d", "busybox", "top")
dockerCmd(c, "run", "--name", "app1", "-d", "--link", "db1:/mysql", "busybox", "top")
dockerCmd(c, "rename", "app1", "app2")
out, _, err := dockerCmdWithError("inspect", "--format={{ .Id }}", "app2/mysql")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(db1))
}

View File

@ -3,11 +3,16 @@ package container // import "github.com/docker/docker/integration/container"
import (
"context"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/integration/util/request"
"github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/stringid"
"github.com/gotestyourself/gotestyourself/poll"
"github.com/gotestyourself/gotestyourself/skip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -43,3 +48,159 @@ func TestRenameLinkedContainer(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []string{"/a0:/b0/a0"}, inspect.HostConfig.Links)
}
func TestRenameStoppedContainer(t *testing.T) {
defer setupTest(t)()
ctx := context.Background()
client := request.NewAPIClient(t)
oldName := "first_name"
cID := runSimpleContainer(ctx, t, client, oldName, func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
config.Cmd = []string{"sh"}
})
poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
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"
cID := runSimpleContainer(ctx, t, client, oldName)
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
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)
cID = runSimpleContainer(ctx, t, client, oldName)
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
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"
cID := runSimpleContainer(ctx, t, client, oldName)
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
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)
cID := createSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
"network1": {},
}
hostConfig.NetworkMode = "network1"
})
err = client.ContainerRename(ctx, cID, "container1")
require.NoError(t, err)
err = client.ContainerStart(ctx, "container1", types.ContainerStartOptions{})
require.NoError(t, err)
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
count := "-c"
if testEnv.OSType == "windows" {
count = "-n"
}
cID = runSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
"network1": {},
}
hostConfig.NetworkMode = "network1"
config.Cmd = []string{"ping", count, "1", "container1"}
})
poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
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)
cID := runSimpleContainer(ctx, t, client, "old")
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
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)
db1ID := runSimpleContainer(ctx, t, client, "db1")
poll.WaitOn(t, containerIsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond))
app1ID := runSimpleContainer(ctx, t, client, "app1", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
hostConfig.Links = []string{"db1:/mysql"}
})
poll.WaitOn(t, containerIsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond))
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)
}