2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2014-04-17 22:24:19 -04:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
|
2014-02-25 11:17:48 -05:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 14:26:53 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to run container: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
|
2014-10-14 14:26:53 -04:00
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
2014-10-14 14:26:53 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to commit container to image: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedImageID := strings.TrimSpace(out)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
|
2014-10-14 14:26:53 -04:00
|
|
|
if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to inspect image: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-06-08 02:37:31 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
|
2014-06-08 02:37:31 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 14:26:53 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to run container: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-06-08 02:37:31 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-08 02:37:31 -04:00
|
|
|
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
|
2014-10-14 14:26:53 -04:00
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-06-08 02:37:31 -04:00
|
|
|
|
2014-06-30 20:39:38 -04:00
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
|
2014-06-08 02:37:31 -04:00
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
2014-10-14 14:26:53 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to commit container to image: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-06-08 02:37:31 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedImageID := strings.TrimSpace(out)
|
2014-06-08 02:37:31 -04:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
|
2014-10-14 14:26:53 -04:00
|
|
|
if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to inspect image: %s, %v", out, err)
|
2014-10-14 14:26:53 -04:00
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
2014-04-17 22:24:19 -04:00
|
|
|
|
2015-02-24 06:28:40 -05:00
|
|
|
//test commit a paused container should not unpause it after commit
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
|
2015-02-24 06:28:40 -05:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
2015-02-24 06:28:40 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-24 06:28:40 -05:00
|
|
|
cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to pause container: %v, output: %q", err, out)
|
2015-02-24 06:28:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(commitCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to commit container to image: %s, %v", out, err)
|
2015-02-24 06:28:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to inspect container: %v, output: %q", err, out)
|
2015-02-24 06:28:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(out, "true") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("commit should not unpause a paused container")
|
2015-02-24 06:28:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitNewFile(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-04-17 22:24:19 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-04-17 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "commiter")
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-04-17 22:24:19 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-04-17 22:24:19 -04:00
|
|
|
}
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2014-04-17 22:24:19 -04:00
|
|
|
|
2014-10-06 10:26:55 -04:00
|
|
|
cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
|
2014-04-17 22:24:19 -04:00
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err, out)
|
2014-04-17 22:24:19 -04:00
|
|
|
}
|
|
|
|
if actual := strings.Trim(out, "\r\n"); actual != "koye" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected output koye received %q", actual)
|
2014-04-17 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-03-27 15:16:03 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitHardlink(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-09-15 14:45:53 -04: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 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
|
|
|
|
inode := chunks[0]
|
|
|
|
found := false
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
if chunk == inode {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
|
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(imageID, err)
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
|
|
|
|
secondOuput, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
|
|
|
|
inode = chunks[0]
|
|
|
|
found = false
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
if chunk == inode {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitTTY(c *check.C) {
|
2014-11-17 11:05:49 -05:00
|
|
|
|
2014-03-27 15:16:03 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-03-27 15:16:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-03-27 15:16:03 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-03-27 15:16:03 -04:00
|
|
|
}
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2014-03-27 15:16:03 -04:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-03-27 15:16:03 -04:00
|
|
|
}
|
2014-08-28 01:25:57 -04:00
|
|
|
|
2014-03-27 15:16:03 -04:00
|
|
|
}
|
2014-05-19 18:57:29 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-05-19 18:57:29 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-19 18:57:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID, _, err := runCommandWithOutput(cmd)
|
2014-05-19 18:57:29 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(imageID, err)
|
2014-05-19 18:57:29 -04:00
|
|
|
}
|
2014-10-14 14:26:53 -04:00
|
|
|
|
2014-10-06 10:26:55 -04:00
|
|
|
imageID = strings.Trim(imageID, "\r\n")
|
2014-05-19 18:57:29 -04:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
|
|
|
|
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-05-19 18:57:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-10-01 05:35:44 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitChange(c *check.C) {
|
2014-10-01 05:35:44 -04:00
|
|
|
|
2015-02-05 06:26:05 -05:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-02-05 06:26:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "commit",
|
2014-10-01 05:35:44 -04:00
|
|
|
"--change", "EXPOSE 8080",
|
|
|
|
"--change", "ENV DEBUG true",
|
2015-01-12 14:52:44 -05:00
|
|
|
"--change", "ENV test 1",
|
2015-02-25 03:04:46 -05:00
|
|
|
"--change", "ENV PATH /foo",
|
2014-10-01 05:35:44 -04:00
|
|
|
"test", "test-commit")
|
|
|
|
imageId, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(imageId, err)
|
2014-10-01 05:35:44 -04:00
|
|
|
}
|
|
|
|
imageId = strings.Trim(imageId, "\r\n")
|
|
|
|
|
|
|
|
expected := map[string]string{
|
2015-03-26 15:43:00 -04:00
|
|
|
"Config.ExposedPorts": "map[8080/tcp:{}]",
|
2015-02-25 03:04:46 -05:00
|
|
|
"Config.Env": "[DEBUG=true test=1 PATH=/foo]",
|
2014-10-01 05:35:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for conf, value := range expected {
|
|
|
|
res, err := inspectField(imageId, conf)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("failed to get value %s, error: %s", conf, err)
|
2014-10-01 05:35:44 -04:00
|
|
|
}
|
|
|
|
if res != value {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("%s('%s'), expected %s", conf, res, value)
|
2014-10-01 05:35:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-10 17:01:40 -04:00
|
|
|
|
|
|
|
// TODO: commit --run is deprecated, remove this once --run is removed
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
|
2015-04-10 17:01:40 -04:00
|
|
|
name := "commit-test"
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
|
2015-04-10 17:01:40 -04:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
|
2015-04-10 17:01:40 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
|
2015-04-10 17:01:40 -04:00
|
|
|
if strings.TrimSpace(out) != "testing" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatal("run config in committed container was not merged")
|
2015-04-10 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
Env []string
|
|
|
|
Cmd []string
|
|
|
|
}
|
|
|
|
config1 := cfg{}
|
|
|
|
if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 17:01:40 -04:00
|
|
|
}
|
|
|
|
config2 := cfg{}
|
|
|
|
if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Env has at least PATH loaded as well here, so let's just grab the FOO one
|
|
|
|
var env1, env2 string
|
|
|
|
for _, e := range config1.Env {
|
|
|
|
if strings.HasPrefix(e, "FOO") {
|
|
|
|
env1 = e
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, e := range config2.Env {
|
|
|
|
if strings.HasPrefix(e, "FOO") {
|
|
|
|
env2 = e
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
|
2015-04-10 17:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|