github.com/lmars/docker@v1.6.0-rc2/integration-cli/docker_cli_commit_test.go (about)

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