2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2014-04-18 02:24:19 +00:00
|
|
|
"strings"
|
2014-02-25 16:17:48 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommitAfterContainerIsDone(t *testing.T) {
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 18:26:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run container: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
cleanedContainerID := stripTrailingCharacters(out)
|
|
|
|
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
|
2014-10-14 18:26:53 +00:00
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
|
|
|
t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
2014-10-14 18:26:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to commit container to image: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
cleanedImageID := stripTrailingCharacters(out)
|
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
|
2014-10-14 18:26:53 +00:00
|
|
|
if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
|
|
|
|
t.Fatalf("failed to inspect image: %s, %v", out, err)
|
|
|
|
}
|
2014-06-08 06:37:31 +00:00
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
deleteImages(cleanedImageID)
|
|
|
|
|
|
|
|
logDone("commit - echo foo and commit the image")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitWithoutPause(t *testing.T) {
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 18:26:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run container: %s, %v", out, err)
|
|
|
|
}
|
2014-06-08 06:37:31 +00:00
|
|
|
|
|
|
|
cleanedContainerID := stripTrailingCharacters(out)
|
|
|
|
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
|
2014-10-14 18:26:53 +00:00
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
|
|
|
t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
|
|
|
|
}
|
2014-06-08 06:37:31 +00:00
|
|
|
|
2014-07-01 00:39:38 +00:00
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
|
2014-06-08 06:37:31 +00:00
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
2014-10-14 18:26:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to commit container to image: %s, %v", out, err)
|
|
|
|
}
|
2014-06-08 06:37:31 +00:00
|
|
|
|
|
|
|
cleanedImageID := stripTrailingCharacters(out)
|
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
|
2014-10-14 18:26:53 +00:00
|
|
|
if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
|
|
|
|
t.Fatalf("failed to inspect image: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
|
2014-04-04 00:22:32 +00:00
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
deleteImages(cleanedImageID)
|
2014-02-25 16:17:48 +00:00
|
|
|
|
2014-07-01 00:39:38 +00:00
|
|
|
logDone("commit - echo foo and commit the image with --pause=false")
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
2014-04-18 02:24:19 +00:00
|
|
|
|
2015-02-24 11:28:40 +00:00
|
|
|
//test commit a paused container should not unpause it after commit
|
|
|
|
func TestCommitPausedContainer(t *testing.T) {
|
|
|
|
defer deleteAllContainers()
|
|
|
|
defer unpauseAllContainers()
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanedContainerID := stripTrailingCharacters(out)
|
|
|
|
cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to pause container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to commit container to image: %s, %v", out, err)
|
|
|
|
}
|
|
|
|
cleanedImageID := stripTrailingCharacters(out)
|
|
|
|
defer deleteImages(cleanedImageID)
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to inspect container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(out, "true") {
|
|
|
|
t.Fatalf("commit should not unpause a paused container")
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("commit - commit a paused container will not unpause it")
|
|
|
|
}
|
|
|
|
|
2014-04-18 02:24:19 +00:00
|
|
|
func TestCommitNewFile(t *testing.T) {
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-04-18 02:24:19 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "commiter")
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-04-18 02:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteImages(imageID)
|
2014-04-18 02:24:19 +00:00
|
|
|
|
2014-10-06 14:26:55 +00:00
|
|
|
cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
|
2014-04-18 02:24:19 +00:00
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err, out)
|
|
|
|
}
|
|
|
|
if actual := strings.Trim(out, "\r\n"); actual != "koye" {
|
2014-10-14 18:26:53 +00:00
|
|
|
t.Fatalf("expected output koye received %q", actual)
|
2014-04-18 02:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("commit - commit file and read")
|
|
|
|
}
|
2014-03-27 19:16:03 +00:00
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
func TestCommitHardlink(t *testing.T) {
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
|
|
|
|
firstOuput, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
|
|
|
|
inode := chunks[0]
|
|
|
|
found := false
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
if chunk == inode {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
|
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(imageID, err)
|
|
|
|
}
|
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteImages(imageID)
|
2014-09-15 18:45:53 +00:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
|
|
|
|
secondOuput, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
|
|
|
|
inode = chunks[0]
|
|
|
|
found = false
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
if chunk == inode {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("commit - commit hardlinks")
|
|
|
|
}
|
|
|
|
|
2014-03-27 19:16:03 +00:00
|
|
|
func TestCommitTTY(t *testing.T) {
|
2014-11-17 16:05:49 +00:00
|
|
|
defer deleteImages("ttytest")
|
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-03-27 19:16:03 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-03-27 19:16:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2014-03-27 19:16:03 +00:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-28 05:25:57 +00:00
|
|
|
|
|
|
|
logDone("commit - commit tty")
|
2014-03-27 19:16:03 +00:00
|
|
|
}
|
2014-05-19 22:57:29 +00:00
|
|
|
|
|
|
|
func TestCommitWithHostBindMount(t *testing.T) {
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-05-19 22:57:29 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-05-19 22:57:29 +00:00
|
|
|
if err != nil {
|
2014-10-06 14:26:55 +00:00
|
|
|
t.Fatal(imageID, err)
|
2014-05-19 22:57:29 +00:00
|
|
|
}
|
2014-10-14 18:26:53 +00:00
|
|
|
|
2014-10-06 14:26:55 +00:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2015-02-20 06:56:02 +00:00
|
|
|
defer deleteImages(imageID)
|
2014-05-19 22:57:29 +00:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
|
|
|
|
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("commit - commit bind mounted file")
|
|
|
|
}
|
2014-10-01 09:35:44 +00:00
|
|
|
|
|
|
|
func TestCommitChange(t *testing.T) {
|
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-02-05 11:26:05 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit",
|
2014-10-01 09:35:44 +00:00
|
|
|
"--change", "EXPOSE 8080",
|
|
|
|
"--change", "ENV DEBUG true",
|
2015-01-12 19:52:44 +00:00
|
|
|
"--change", "ENV test 1",
|
2014-10-01 09:35:44 +00:00
|
|
|
"test", "test-commit")
|
|
|
|
imageId, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(imageId, err)
|
|
|
|
}
|
|
|
|
imageId = strings.Trim(imageId, "\r\n")
|
|
|
|
defer deleteImages(imageId)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"Config.ExposedPorts": "map[8080/tcp:map[]]",
|
2015-01-12 19:52:44 +00:00
|
|
|
"Config.Env": "[DEBUG=true test=1]",
|
2014-10-01 09:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for conf, value := range expected {
|
|
|
|
res, err := inspectField(imageId, conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get value %s, error: %s", conf, err)
|
|
|
|
}
|
|
|
|
if res != value {
|
|
|
|
t.Errorf("%s('%s'), expected %s", conf, res, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("commit - commit --change")
|
|
|
|
}
|