github.com/michael-k/docker@v1.7.0-rc2/integration-cli/docker_cli_commit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
    11  	runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    12  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    13  	if err != nil {
    14  		c.Fatalf("failed to run container: %s, %v", out, err)
    15  	}
    16  
    17  	cleanedContainerID := strings.TrimSpace(out)
    18  
    19  	waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
    20  	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
    21  		c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
    22  	}
    23  
    24  	commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
    25  	out, _, err = runCommandWithOutput(commitCmd)
    26  	if err != nil {
    27  		c.Fatalf("failed to commit container to image: %s, %v", out, err)
    28  	}
    29  
    30  	cleanedImageID := strings.TrimSpace(out)
    31  
    32  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
    33  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    34  		c.Fatalf("failed to inspect image: %s, %v", out, err)
    35  	}
    36  }
    37  
    38  func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
    39  	runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    40  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    41  	if err != nil {
    42  		c.Fatalf("failed to run container: %s, %v", out, err)
    43  	}
    44  
    45  	cleanedContainerID := strings.TrimSpace(out)
    46  
    47  	waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
    48  	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
    49  		c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
    50  	}
    51  
    52  	commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
    53  	out, _, err = runCommandWithOutput(commitCmd)
    54  	if err != nil {
    55  		c.Fatalf("failed to commit container to image: %s, %v", out, err)
    56  	}
    57  
    58  	cleanedImageID := strings.TrimSpace(out)
    59  
    60  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
    61  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    62  		c.Fatalf("failed to inspect image: %s, %v", out, err)
    63  	}
    64  }
    65  
    66  //test commit a paused container should not unpause it after commit
    67  func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
    68  	defer unpauseAllContainers()
    69  	cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
    70  	out, _, _, err := runCommandWithStdoutStderr(cmd)
    71  	if err != nil {
    72  		c.Fatalf("failed to run container: %v, output: %q", err, out)
    73  	}
    74  
    75  	cleanedContainerID := strings.TrimSpace(out)
    76  	cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
    77  	out, _, _, err = runCommandWithStdoutStderr(cmd)
    78  	if err != nil {
    79  		c.Fatalf("failed to pause container: %v, output: %q", err, out)
    80  	}
    81  
    82  	commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
    83  	out, _, err = runCommandWithOutput(commitCmd)
    84  	if err != nil {
    85  		c.Fatalf("failed to commit container to image: %s, %v", out, err)
    86  	}
    87  
    88  	out, err = inspectField(cleanedContainerID, "State.Paused")
    89  	c.Assert(err, check.IsNil)
    90  	if !strings.Contains(out, "true") {
    91  		c.Fatalf("commit should not unpause a paused container")
    92  	}
    93  }
    94  
    95  func (s *DockerSuite) TestCommitNewFile(c *check.C) {
    96  
    97  	cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    98  	if _, err := runCommand(cmd); err != nil {
    99  		c.Fatal(err)
   100  	}
   101  
   102  	cmd = exec.Command(dockerBinary, "commit", "commiter")
   103  	imageID, _, err := runCommandWithOutput(cmd)
   104  	if err != nil {
   105  		c.Fatal(err)
   106  	}
   107  	imageID = strings.Trim(imageID, "\r\n")
   108  
   109  	cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
   110  
   111  	out, _, err := runCommandWithOutput(cmd)
   112  	if err != nil {
   113  		c.Fatal(err, out)
   114  	}
   115  	if actual := strings.Trim(out, "\r\n"); actual != "koye" {
   116  		c.Fatalf("expected output koye received %q", actual)
   117  	}
   118  
   119  }
   120  
   121  func (s *DockerSuite) TestCommitHardlink(c *check.C) {
   122  
   123  	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
   124  	firstOuput, _, err := runCommandWithOutput(cmd)
   125  	if err != nil {
   126  		c.Fatal(err)
   127  	}
   128  
   129  	chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
   130  	inode := chunks[0]
   131  	found := false
   132  	for _, chunk := range chunks[1:] {
   133  		if chunk == inode {
   134  			found = true
   135  			break
   136  		}
   137  	}
   138  	if !found {
   139  		c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
   140  	}
   141  
   142  	cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
   143  	imageID, _, err := runCommandWithOutput(cmd)
   144  	if err != nil {
   145  		c.Fatal(imageID, err)
   146  	}
   147  	imageID = strings.Trim(imageID, "\r\n")
   148  
   149  	cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
   150  	secondOuput, _, err := runCommandWithOutput(cmd)
   151  	if err != nil {
   152  		c.Fatal(err)
   153  	}
   154  
   155  	chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
   156  	inode = chunks[0]
   157  	found = false
   158  	for _, chunk := range chunks[1:] {
   159  		if chunk == inode {
   160  			found = true
   161  			break
   162  		}
   163  	}
   164  	if !found {
   165  		c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
   166  	}
   167  
   168  }
   169  
   170  func (s *DockerSuite) TestCommitTTY(c *check.C) {
   171  
   172  	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
   173  	if _, err := runCommand(cmd); err != nil {
   174  		c.Fatal(err)
   175  	}
   176  
   177  	cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
   178  	imageID, _, err := runCommandWithOutput(cmd)
   179  	if err != nil {
   180  		c.Fatal(err)
   181  	}
   182  	imageID = strings.Trim(imageID, "\r\n")
   183  
   184  	cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
   185  	if _, err := runCommand(cmd); err != nil {
   186  		c.Fatal(err)
   187  	}
   188  
   189  }
   190  
   191  func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
   192  
   193  	cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
   194  	if _, err := runCommand(cmd); err != nil {
   195  		c.Fatal(err)
   196  	}
   197  
   198  	cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
   199  	imageID, _, err := runCommandWithOutput(cmd)
   200  	if err != nil {
   201  		c.Fatal(imageID, err)
   202  	}
   203  
   204  	imageID = strings.Trim(imageID, "\r\n")
   205  
   206  	cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
   207  
   208  	if _, err := runCommand(cmd); err != nil {
   209  		c.Fatal(err)
   210  	}
   211  
   212  }
   213  
   214  func (s *DockerSuite) TestCommitChange(c *check.C) {
   215  
   216  	cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
   217  	if _, err := runCommand(cmd); err != nil {
   218  		c.Fatal(err)
   219  	}
   220  
   221  	cmd = exec.Command(dockerBinary, "commit",
   222  		"--change", "EXPOSE 8080",
   223  		"--change", "ENV DEBUG true",
   224  		"--change", "ENV test 1",
   225  		"--change", "ENV PATH /foo",
   226  		"test", "test-commit")
   227  	imageId, _, err := runCommandWithOutput(cmd)
   228  	if err != nil {
   229  		c.Fatal(imageId, err)
   230  	}
   231  	imageId = strings.Trim(imageId, "\r\n")
   232  
   233  	expected := map[string]string{
   234  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   235  		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
   236  	}
   237  
   238  	for conf, value := range expected {
   239  		res, err := inspectField(imageId, conf)
   240  		c.Assert(err, check.IsNil)
   241  		if res != value {
   242  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   243  		}
   244  	}
   245  
   246  }
   247  
   248  // TODO: commit --run is deprecated, remove this once --run is removed
   249  func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
   250  	name := "commit-test"
   251  	out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
   252  	id := strings.TrimSpace(out)
   253  
   254  	dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
   255  
   256  	out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
   257  	if strings.TrimSpace(out) != "testing" {
   258  		c.Fatal("run config in committed container was not merged")
   259  	}
   260  
   261  	type cfg struct {
   262  		Env []string
   263  		Cmd []string
   264  	}
   265  	config1 := cfg{}
   266  	if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
   267  		c.Fatal(err)
   268  	}
   269  	config2 := cfg{}
   270  	if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
   271  		c.Fatal(err)
   272  	}
   273  
   274  	// Env has at least PATH loaded as well here, so let's just grab the FOO one
   275  	var env1, env2 string
   276  	for _, e := range config1.Env {
   277  		if strings.HasPrefix(e, "FOO") {
   278  			env1 = e
   279  			break
   280  		}
   281  	}
   282  	for _, e := range config2.Env {
   283  		if strings.HasPrefix(e, "FOO") {
   284  			env2 = e
   285  			break
   286  		}
   287  	}
   288  
   289  	if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
   290  		c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
   291  	}
   292  
   293  }