github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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 = inspectField(c, cleanedContainerID, "State.Paused")
    53  	// commit should not unpause a paused container
    54  	c.Assert(out, checker.Contains, "true")
    55  }
    56  
    57  func (s *DockerSuite) TestCommitNewFile(c *check.C) {
    58  	testRequires(c, DaemonIsLinux)
    59  	dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    60  
    61  	imageID, _ := dockerCmd(c, "commit", "commiter")
    62  	imageID = strings.TrimSpace(imageID)
    63  
    64  	out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
    65  	actual := strings.TrimSpace(out)
    66  	c.Assert(actual, checker.Equals, "koye")
    67  }
    68  
    69  func (s *DockerSuite) TestCommitHardlink(c *check.C) {
    70  	testRequires(c, DaemonIsLinux)
    71  	firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
    72  
    73  	chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
    74  	inode := chunks[0]
    75  	chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
    76  	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:]))
    77  
    78  	imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
    79  	imageID = strings.TrimSpace(imageID)
    80  
    81  	secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
    82  
    83  	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
    84  	inode = chunks[0]
    85  	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
    86  	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:]))
    87  }
    88  
    89  func (s *DockerSuite) TestCommitTTY(c *check.C) {
    90  	testRequires(c, DaemonIsLinux)
    91  	dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
    92  
    93  	imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
    94  	imageID = strings.TrimSpace(imageID)
    95  
    96  	dockerCmd(c, "run", "ttytest", "/bin/ls")
    97  }
    98  
    99  func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
   100  	testRequires(c, DaemonIsLinux)
   101  	dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
   102  
   103  	imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
   104  	imageID = strings.TrimSpace(imageID)
   105  
   106  	dockerCmd(c, "run", "bindtest", "true")
   107  }
   108  
   109  func (s *DockerSuite) TestCommitChange(c *check.C) {
   110  	testRequires(c, DaemonIsLinux)
   111  	dockerCmd(c, "run", "--name", "test", "busybox", "true")
   112  
   113  	imageID, _ := dockerCmd(c, "commit",
   114  		"--change", "EXPOSE 8080",
   115  		"--change", "ENV DEBUG true",
   116  		"--change", "ENV test 1",
   117  		"--change", "ENV PATH /foo",
   118  		"--change", "LABEL foo bar",
   119  		"--change", "CMD [\"/bin/sh\"]",
   120  		"--change", "WORKDIR /opt",
   121  		"--change", "ENTRYPOINT [\"/bin/sh\"]",
   122  		"--change", "USER testuser",
   123  		"--change", "VOLUME /var/lib/docker",
   124  		"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
   125  		"test", "test-commit")
   126  	imageID = strings.TrimSpace(imageID)
   127  
   128  	expected := map[string]string{
   129  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   130  		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
   131  		"Config.Labels":       "map[foo:bar]",
   132  		"Config.Cmd":          "[/bin/sh]",
   133  		"Config.WorkingDir":   "/opt",
   134  		"Config.Entrypoint":   "[/bin/sh]",
   135  		"Config.User":         "testuser",
   136  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   137  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   138  	}
   139  
   140  	for conf, value := range expected {
   141  		res := inspectField(c, imageID, conf)
   142  		if res != value {
   143  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   144  		}
   145  	}
   146  }
   147  
   148  // TODO: commit --run is deprecated, remove this once --run is removed
   149  func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
   150  	testRequires(c, DaemonIsLinux)
   151  	name := "commit-test"
   152  	out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
   153  	id := strings.TrimSpace(out)
   154  
   155  	dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
   156  
   157  	out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
   158  	//run config in committed container was not merged
   159  	c.Assert(strings.TrimSpace(out), checker.Equals, "testing")
   160  
   161  	type cfg struct {
   162  		Env []string
   163  		Cmd []string
   164  	}
   165  	config1 := cfg{}
   166  	inspectFieldAndMarshall(c, id, "Config", &config1)
   167  
   168  	config2 := cfg{}
   169  	inspectFieldAndMarshall(c, name, "Config", &config2)
   170  
   171  	// Env has at least PATH loaded as well here, so let's just grab the FOO one
   172  	var env1, env2 string
   173  	for _, e := range config1.Env {
   174  		if strings.HasPrefix(e, "FOO") {
   175  			env1 = e
   176  			break
   177  		}
   178  	}
   179  	for _, e := range config2.Env {
   180  		if strings.HasPrefix(e, "FOO") {
   181  			env2 = e
   182  			break
   183  		}
   184  	}
   185  
   186  	if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
   187  		c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
   188  	}
   189  }