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