-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_cli_rename_test.go
112 lines (93 loc) · 3.08 KB
/
docker_cli_rename_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"os/exec"
"strings"
"github.com/docker/docker/pkg/stringid"
"github.com/go-check/check"
)
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
cleanedContainerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name")
newName := "new_name" + stringid.GenerateRandomID()
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err = inspectField(cleanedContainerID, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ", name)
}
}
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
newName := "new_name" + stringid.GenerateRandomID()
cleanedContainerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ")
}
}
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
newName := "new_name" + stringid.GenerateRandomID()
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(newName, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ")
}
name, err = inspectField("first_name", "Name")
if err == nil && !strings.Contains(err.Error(), "No such image or container: first_name") {
c.Fatal(err)
}
}
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf(out, err)
}
runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid")
if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") {
c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err)
}
runCmd = exec.Command(dockerBinary, "ps", "-a")
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") {
c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err)
}
}