diff --git a/cli/command/container/rename.go b/cli/command/container/rename.go index 84d6c1a34..f875bddea 100644 --- a/cli/command/container/rename.go +++ b/cli/command/container/rename.go @@ -1,7 +1,6 @@ package container import ( - "errors" "fmt" "github.com/docker/cli/cli" @@ -19,10 +18,6 @@ func newRenameCommand(dockerCLI command.Cli) *cobra.Command { Args: cli.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { oldName, newName := args[0], args[1] - if newName == "" { - // TODO(thaJeztah): remove once https://github.com/moby/moby/pull/51336 is merged and vendored. - return errors.New("new name cannot be blank") - } _, err := dockerCLI.Client().ContainerRename(cmd.Context(), oldName, client.ContainerRenameOptions{ NewName: newName, }) diff --git a/cli/command/container/rename_test.go b/cli/command/container/rename_test.go deleted file mode 100644 index 4ca80c70f..000000000 --- a/cli/command/container/rename_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package container - -import ( - "context" - "errors" - "io" - "testing" - - "github.com/docker/cli/internal/test" - "gotest.tools/v3/assert" -) - -func TestRunRename(t *testing.T) { - testcases := []struct { - doc, oldName, newName, expectedErr string - }{ - { - doc: "success", - oldName: "oldName", - newName: "newName", - expectedErr: "", - }, - { - doc: "empty old name", - oldName: "", - newName: "newName", - expectedErr: "invalid container name or ID: value is empty", - }, - { - doc: "empty new name", - oldName: "oldName", - newName: "", - expectedErr: "new name cannot be blank", - }, - } - - for _, tc := range testcases { - t.Run(tc.doc, func(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - containerRenameFunc: func(ctx context.Context, oldName, newName string) error { - if oldName == "" { - return errors.New("invalid container name or ID: value is empty") - } - return nil - }, - }) - - cmd := newRenameCommand(cli) - cmd.SetOut(io.Discard) - cmd.SetErr(io.Discard) - cmd.SetArgs([]string{tc.oldName, tc.newName}) - - err := cmd.Execute() - - if tc.expectedErr != "" { - assert.ErrorContains(t, err, tc.expectedErr) - } else { - assert.NilError(t, err) - } - }) - } -} diff --git a/e2e/container/rename_test.go b/e2e/container/rename_test.go index bbaf5ebc7..c1879bd11 100644 --- a/e2e/container/rename_test.go +++ b/e2e/container/rename_test.go @@ -26,3 +26,27 @@ func TestContainerRename(t *testing.T) { res.Assert(t, icmd.Success) assert.Equal(t, "/"+newName, strings.TrimSpace(res.Stdout())) } + +func TestContainerRenameEmptyOldName(t *testing.T) { + res := icmd.RunCommand("docker", "container", "rename", "", "newName") + res.Assert(t, icmd.Expected{ + ExitCode: 1, + Err: "invalid container name or ID: value is empty", + }) +} + +func TestContainerRenameEmptyNewName(t *testing.T) { + oldName := "old_name_" + t.Name() + res := icmd.RunCommand("docker", "run", "-d", "--name", oldName, fixtures.AlpineImage, "sleep", "60") + res.Assert(t, icmd.Success) + cID := strings.TrimSpace(res.Stdout()) + t.Cleanup(func() { + icmd.RunCommand("docker", "container", "rm", "-f", cID).Assert(t, icmd.Success) + }) + + res = icmd.RunCommand("docker", "container", "rename", oldName, "") + res.Assert(t, icmd.Expected{ + ExitCode: 1, + Err: "new name cannot be blank", + }) +}