github.com/iamlotus/docker@v1.8.1/integration-cli/docker_cli_commit_test.go (about)

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