github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/integration-cli/docker_cli_commit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/pkg/integration/checker"
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
    11  	testRequires(c, DaemonIsLinux)
    12  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    13  
    14  	cleanedContainerID := strings.TrimSpace(out)
    15  
    16  	dockerCmd(c, "wait", cleanedContainerID)
    17  
    18  	out, _ = dockerCmd(c, "commit", cleanedContainerID)
    19  
    20  	cleanedImageID := strings.TrimSpace(out)
    21  
    22  	dockerCmd(c, "inspect", cleanedImageID)
    23  }
    24  
    25  func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
    26  	testRequires(c, DaemonIsLinux)
    27  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    28  
    29  	cleanedContainerID := strings.TrimSpace(out)
    30  
    31  	dockerCmd(c, "wait", cleanedContainerID)
    32  
    33  	out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
    34  
    35  	cleanedImageID := strings.TrimSpace(out)
    36  
    37  	dockerCmd(c, "inspect", cleanedImageID)
    38  }
    39  
    40  //test commit a paused container should not unpause it after commit
    41  func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
    42  	testRequires(c, DaemonIsLinux)
    43  	defer unpauseAllContainers()
    44  	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
    45  
    46  	cleanedContainerID := strings.TrimSpace(out)
    47  
    48  	dockerCmd(c, "pause", cleanedContainerID)
    49  
    50  	out, _ = dockerCmd(c, "commit", cleanedContainerID)
    51  
    52  	out, err := inspectField(cleanedContainerID, "State.Paused")
    53  	c.Assert(err, checker.IsNil, check.Commentf("%s", out))
    54  	// commit should not unpause a paused container
    55  	c.Assert(out, checker.Contains, "true")
    56  }
    57  
    58  func (s *DockerSuite) TestCommitNewFile(c *check.C) {
    59  	testRequires(c, DaemonIsLinux)
    60  	dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    61  
    62  	imageID, _ := dockerCmd(c, "commit", "commiter")
    63  	imageID = strings.TrimSpace(imageID)
    64  
    65  	out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
    66  	actual := strings.TrimSpace(out)
    67  	c.Assert(actual, checker.Equals, "koye")
    68  }
    69  
    70  func (s *DockerSuite) TestCommitHardlink(c *check.C) {
    71  	testRequires(c, DaemonIsLinux)
    72  	firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
    73  
    74  	chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
    75  	inode := chunks[0]
    76  	chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
    77  	c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
    78  
    79  	imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
    80  	imageID = strings.TrimSpace(imageID)
    81  
    82  	secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
    83  
    84  	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
    85  	inode = chunks[0]
    86  	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
    87  	c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
    88  }
    89  
    90  func (s *DockerSuite) TestCommitTTY(c *check.C) {
    91  	testRequires(c, DaemonIsLinux)
    92  	dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
    93  
    94  	imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
    95  	imageID = strings.TrimSpace(imageID)
    96  
    97  	dockerCmd(c, "run", "ttytest", "/bin/ls")
    98  }
    99  
   100  func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
   101  	testRequires(c, DaemonIsLinux)
   102  	dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
   103  
   104  	imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
   105  	imageID = strings.TrimSpace(imageID)
   106  
   107  	dockerCmd(c, "run", "bindtest", "true")
   108  }
   109  
   110  func (s *DockerSuite) TestCommitChange(c *check.C) {
   111  	testRequires(c, DaemonIsLinux)
   112  	dockerCmd(c, "run", "--name", "test", "busybox", "true")
   113  
   114  	imageID, _ := dockerCmd(c, "commit",
   115  		"--change", "EXPOSE 8080",
   116  		"--change", "ENV DEBUG true",
   117  		"--change", "ENV test 1",
   118  		"--change", "ENV PATH /foo",
   119  		"--change", "LABEL foo bar",
   120  		"--change", "CMD [\"/bin/sh\"]",
   121  		"--change", "WORKDIR /opt",
   122  		"--change", "ENTRYPOINT [\"/bin/sh\"]",
   123  		"--change", "USER testuser",
   124  		"--change", "VOLUME /var/lib/docker",
   125  		"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
   126  		"test", "test-commit")
   127  	imageID = strings.TrimSpace(imageID)
   128  
   129  	expected := map[string]string{
   130  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   131  		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
   132  		"Config.Labels":       "map[foo:bar]",
   133  		"Config.Cmd":          "{[/bin/sh]}",
   134  		"Config.WorkingDir":   "/opt",
   135  		"Config.Entrypoint":   "{[/bin/sh]}",
   136  		"Config.User":         "testuser",
   137  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   138  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   139  	}
   140  
   141  	for conf, value := range expected {
   142  		res, err := inspectField(imageID, conf)
   143  		c.Assert(err, checker.IsNil, check.Commentf("%s('%s')", conf, res))
   144  		if res != value {
   145  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   146  		}
   147  	}
   148  }
   149  
   150  // TODO: commit --run is deprecated, remove this once --run is removed
   151  func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
   152  	testRequires(c, DaemonIsLinux)
   153  	name := "commit-test"
   154  	out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
   155  	id := strings.TrimSpace(out)
   156  
   157  	dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
   158  
   159  	out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
   160  	//run config in committed container was not merged
   161  	c.Assert(strings.TrimSpace(out), checker.Equals, "testing")
   162  
   163  	type cfg struct {
   164  		Env []string
   165  		Cmd []string
   166  	}
   167  	config1 := cfg{}
   168  	err := inspectFieldAndMarshall(id, "Config", &config1)
   169  	c.Assert(err, checker.IsNil)
   170  
   171  	config2 := cfg{}
   172  	err = inspectFieldAndMarshall(name, "Config", &config2)
   173  	c.Assert(err, checker.IsNil)
   174  
   175  	// Env has at least PATH loaded as well here, so let's just grab the FOO one
   176  	var env1, env2 string
   177  	for _, e := range config1.Env {
   178  		if strings.HasPrefix(e, "FOO") {
   179  			env1 = e
   180  			break
   181  		}
   182  	}
   183  	for _, e := range config2.Env {
   184  		if strings.HasPrefix(e, "FOO") {
   185  			env2 = e
   186  			break
   187  		}
   188  	}
   189  
   190  	if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
   191  		c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
   192  	}
   193  }