github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
    89  	out, _, _, err = runCommandWithStdoutStderr(cmd)
    90  	if err != nil {
    91  		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
    92  	}
    93  
    94  	if !strings.Contains(out, "true") {
    95  		c.Fatalf("commit should not unpause a paused container")
    96  	}
    97  
    98  }
    99  
   100  func (s *DockerSuite) TestCommitNewFile(c *check.C) {
   101  
   102  	cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
   103  	if _, err := runCommand(cmd); err != nil {
   104  		c.Fatal(err)
   105  	}
   106  
   107  	cmd = exec.Command(dockerBinary, "commit", "commiter")
   108  	imageID, _, err := runCommandWithOutput(cmd)
   109  	if err != nil {
   110  		c.Fatal(err)
   111  	}
   112  	imageID = strings.Trim(imageID, "\r\n")
   113  
   114  	cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
   115  
   116  	out, _, err := runCommandWithOutput(cmd)
   117  	if err != nil {
   118  		c.Fatal(err, out)
   119  	}
   120  	if actual := strings.Trim(out, "\r\n"); actual != "koye" {
   121  		c.Fatalf("expected output koye received %q", actual)
   122  	}
   123  
   124  }
   125  
   126  func (s *DockerSuite) TestCommitHardlink(c *check.C) {
   127  
   128  	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
   129  	firstOuput, _, err := runCommandWithOutput(cmd)
   130  	if err != nil {
   131  		c.Fatal(err)
   132  	}
   133  
   134  	chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
   135  	inode := chunks[0]
   136  	found := false
   137  	for _, chunk := range chunks[1:] {
   138  		if chunk == inode {
   139  			found = true
   140  			break
   141  		}
   142  	}
   143  	if !found {
   144  		c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
   145  	}
   146  
   147  	cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
   148  	imageID, _, err := runCommandWithOutput(cmd)
   149  	if err != nil {
   150  		c.Fatal(imageID, err)
   151  	}
   152  	imageID = strings.Trim(imageID, "\r\n")
   153  
   154  	cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
   155  	secondOuput, _, err := runCommandWithOutput(cmd)
   156  	if err != nil {
   157  		c.Fatal(err)
   158  	}
   159  
   160  	chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
   161  	inode = chunks[0]
   162  	found = false
   163  	for _, chunk := range chunks[1:] {
   164  		if chunk == inode {
   165  			found = true
   166  			break
   167  		}
   168  	}
   169  	if !found {
   170  		c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
   171  	}
   172  
   173  }
   174  
   175  func (s *DockerSuite) TestCommitTTY(c *check.C) {
   176  
   177  	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
   178  	if _, err := runCommand(cmd); err != nil {
   179  		c.Fatal(err)
   180  	}
   181  
   182  	cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
   183  	imageID, _, err := runCommandWithOutput(cmd)
   184  	if err != nil {
   185  		c.Fatal(err)
   186  	}
   187  	imageID = strings.Trim(imageID, "\r\n")
   188  
   189  	cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
   190  	if _, err := runCommand(cmd); err != nil {
   191  		c.Fatal(err)
   192  	}
   193  
   194  }
   195  
   196  func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
   197  
   198  	cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
   199  	if _, err := runCommand(cmd); err != nil {
   200  		c.Fatal(err)
   201  	}
   202  
   203  	cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
   204  	imageID, _, err := runCommandWithOutput(cmd)
   205  	if err != nil {
   206  		c.Fatal(imageID, err)
   207  	}
   208  
   209  	imageID = strings.Trim(imageID, "\r\n")
   210  
   211  	cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
   212  
   213  	if _, err := runCommand(cmd); err != nil {
   214  		c.Fatal(err)
   215  	}
   216  
   217  }
   218  
   219  func (s *DockerSuite) TestCommitChange(c *check.C) {
   220  
   221  	cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
   222  	if _, err := runCommand(cmd); err != nil {
   223  		c.Fatal(err)
   224  	}
   225  
   226  	cmd = exec.Command(dockerBinary, "commit",
   227  		"--change", "EXPOSE 8080",
   228  		"--change", "ENV DEBUG true",
   229  		"--change", "ENV test 1",
   230  		"--change", "ENV PATH /foo",
   231  		"test", "test-commit")
   232  	imageId, _, err := runCommandWithOutput(cmd)
   233  	if err != nil {
   234  		c.Fatal(imageId, err)
   235  	}
   236  	imageId = strings.Trim(imageId, "\r\n")
   237  
   238  	expected := map[string]string{
   239  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   240  		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
   241  	}
   242  
   243  	for conf, value := range expected {
   244  		res, err := inspectField(imageId, conf)
   245  		if err != nil {
   246  			c.Errorf("failed to get value %s, error: %s", conf, err)
   247  		}
   248  		if res != value {
   249  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   250  		}
   251  	}
   252  
   253  }
   254  
   255  // TODO: commit --run is deprecated, remove this once --run is removed
   256  func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
   257  	name := "commit-test"
   258  	out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
   259  	id := strings.TrimSpace(out)
   260  
   261  	dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
   262  
   263  	out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
   264  	if strings.TrimSpace(out) != "testing" {
   265  		c.Fatal("run config in committed container was not merged")
   266  	}
   267  
   268  	type cfg struct {
   269  		Env []string
   270  		Cmd []string
   271  	}
   272  	config1 := cfg{}
   273  	if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
   274  		c.Fatal(err)
   275  	}
   276  	config2 := cfg{}
   277  	if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
   278  		c.Fatal(err)
   279  	}
   280  
   281  	// Env has at least PATH loaded as well here, so let's just grab the FOO one
   282  	var env1, env2 string
   283  	for _, e := range config1.Env {
   284  		if strings.HasPrefix(e, "FOO") {
   285  			env1 = e
   286  			break
   287  		}
   288  	}
   289  	for _, e := range config2.Env {
   290  		if strings.HasPrefix(e, "FOO") {
   291  			env2 = e
   292  			break
   293  		}
   294  	}
   295  
   296  	if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
   297  		c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
   298  	}
   299  
   300  }