Skip to content

Commit

Permalink
Fix the rename command on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Ardelean <[email protected]>
  • Loading branch information
Dan Ardelean committed Feb 9, 2023
1 parent aa64c9c commit 5eb3dd2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cmd/nerdctl/container_rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"fmt"
"runtime"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/clientutil"
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions cmd/nerdctl/container_rename_windows_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 5eb3dd2

Please sign in to comment.