Merge pull request #8570 from brahmaroutu/rename_container_3036

Rename a existing container
This commit is contained in:
Jessie Frazelle
2015-01-13 10:37:29 -08:00
10 changed files with 275 additions and 2 deletions
+47 -2
View File
@@ -1,6 +1,7 @@
package main
import (
"github.com/docker/docker/pkg/iptables"
"io/ioutil"
"os"
"os/exec"
@@ -8,8 +9,6 @@ import (
"strings"
"testing"
"time"
"github.com/docker/docker/pkg/iptables"
)
func TestLinksEtcHostsRegularFile(t *testing.T) {
@@ -76,6 +75,52 @@ func TestLinksPingLinkedContainers(t *testing.T) {
logDone("links - ping linked container")
}
func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
idA := stripTrailingCharacters(out)
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
idB := stripTrailingCharacters(out)
dockerCmd(t, "rename", "container1", "container_new")
dockerCmd(t, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
dockerCmd(t, "kill", idA)
dockerCmd(t, "kill", idB)
deleteAllContainers()
logDone("links - ping linked container after rename")
}
func TestLinksPingLinkedContainersOnRename(t *testing.T) {
var out string
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
idA := stripTrailingCharacters(out)
if idA == "" {
t.Fatal(out, "id should not be nil")
}
out, _, _ = dockerCmd(t, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "sleep", "10")
idB := stripTrailingCharacters(out)
if idB == "" {
t.Fatal(out, "id should not be nil")
}
execCmd := exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
out, _, err := runCommandWithOutput(execCmd)
if err != nil {
t.Fatal(out, err)
}
dockerCmd(t, "rename", "container1", "container_new")
execCmd = exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
out, _, err = runCommandWithOutput(execCmd)
if err != nil {
t.Fatal(out, err)
}
deleteAllContainers()
logDone("links - ping linked container upon rename")
}
func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
+99
View File
@@ -0,0 +1,99 @@
package main
import (
"os/exec"
"strings"
"testing"
)
func TestRenameStoppedContainer(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name")
runCmd = exec.Command(dockerBinary, "rename", "first_name", "new_name")
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
name, err = inspectField(cleanedContainerID, "Name")
if err != nil {
t.Fatal(err)
}
if name != "new_name" {
t.Fatal("Failed to rename container ", name)
}
deleteAllContainers()
logDone("rename - stopped container")
}
func TestRenameRunningContainer(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
runCmd = exec.Command(dockerBinary, "rename", "first_name", "new_name")
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name")
if err != nil {
t.Fatal(err)
}
if name != "new_name" {
t.Fatal("Failed to rename container ")
}
deleteAllContainers()
logDone("rename - running container")
}
func TestRenameCheckNames(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
runCmd = exec.Command(dockerBinary, "rename", "first_name", "new_name")
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
t.Fatalf(out, err)
}
name, err := inspectField("new_name", "Name")
if err != nil {
t.Fatal(err)
}
if name != "new_name" {
t.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") {
t.Fatal(err)
}
deleteAllContainers()
logDone("rename - running container")
}