From 5eb3dd2d281065d8e301726cf24137ff26dab553 Mon Sep 17 00:00:00 2001 From: Dan Ardelean Date: Thu, 9 Feb 2023 00:43:09 -0800 Subject: [PATCH] Fix the rename command on Windows Signed-off-by: Dan Ardelean --- cmd/nerdctl/container_rename.go | 8 +++-- cmd/nerdctl/container_rename_windows_test.go | 37 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 cmd/nerdctl/container_rename_windows_test.go diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container_rename.go index 40716a79a00..33360f9eef9 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container_rename.go @@ -19,6 +19,7 @@ package main import ( "context" "fmt" + "runtime" "github.com/containerd/containerd" "github.com/containerd/nerdctl/pkg/clientutil" @@ -92,8 +93,11 @@ func renameContainer(ctx context.Context, container containerd.Container, newNam if err := namst.Rename(name, container.ID(), newName); err != nil { return err } - if err := hostst.Update(ns, container.ID(), newName); err != nil { - return err + + if runtime.GOOS == "linux" { + if err := hostst.Update(ns, container.ID(), newName); err != nil { + return err + } } labels := map[string]string{ labels.Name: newName, diff --git a/cmd/nerdctl/container_rename_windows_test.go b/cmd/nerdctl/container_rename_windows_test.go new file mode 100644 index 00000000000..3cb6189c65f --- /dev/null +++ b/cmd/nerdctl/container_rename_windows_test.go @@ -0,0 +1,37 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "testing" + + "github.com/containerd/nerdctl/pkg/testutil" +) + +func TestRename(t *testing.T) { + testContainerName := testutil.Identifier(t) + base := testutil.NewBase(t) + + defer base.Cmd("rm", "-f", testContainerName).Run() + base.Cmd("run", "-d", "--name", testContainerName, testutil.CommonImage, "sleep", "infinity").AssertOK() + + defer base.Cmd("rm", "-f", testContainerName+"_new").Run() + base.Cmd("rename", testContainerName, testContainerName+"_new").AssertOK() + base.Cmd("ps", "-a").AssertOutContains(testContainerName + "_new") + base.Cmd("rename", testContainerName, testContainerName+"_new").AssertFail() + base.Cmd("rename", testContainerName+"_new", testContainerName+"_new").AssertFail() +}