github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/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 func TestCommitNewFile(t *testing.T) { 76 cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo") 77 if _, err := runCommand(cmd); err != nil { 78 t.Fatal(err) 79 } 80 81 cmd = exec.Command(dockerBinary, "commit", "commiter") 82 imageID, _, err := runCommandWithOutput(cmd) 83 if err != nil { 84 t.Fatal(err) 85 } 86 imageID = strings.Trim(imageID, "\r\n") 87 88 cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo") 89 90 out, _, err := runCommandWithOutput(cmd) 91 if err != nil { 92 t.Fatal(err, out) 93 } 94 if actual := strings.Trim(out, "\r\n"); actual != "koye" { 95 t.Fatalf("expected output koye received %q", actual) 96 } 97 98 deleteAllContainers() 99 deleteImages(imageID) 100 101 logDone("commit - commit file and read") 102 } 103 104 func TestCommitHardlink(t *testing.T) { 105 cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2") 106 firstOuput, _, err := runCommandWithOutput(cmd) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 chunks := strings.Split(strings.TrimSpace(firstOuput), " ") 112 inode := chunks[0] 113 found := false 114 for _, chunk := range chunks[1:] { 115 if chunk == inode { 116 found = true 117 break 118 } 119 } 120 if !found { 121 t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]) 122 } 123 124 cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks") 125 imageID, _, err := runCommandWithOutput(cmd) 126 if err != nil { 127 t.Fatal(imageID, err) 128 } 129 imageID = strings.Trim(imageID, "\r\n") 130 131 cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2") 132 secondOuput, _, err := runCommandWithOutput(cmd) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 chunks = strings.Split(strings.TrimSpace(secondOuput), " ") 138 inode = chunks[0] 139 found = false 140 for _, chunk := range chunks[1:] { 141 if chunk == inode { 142 found = true 143 break 144 } 145 } 146 if !found { 147 t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]) 148 } 149 150 deleteAllContainers() 151 deleteImages(imageID) 152 153 logDone("commit - commit hardlinks") 154 } 155 156 func TestCommitTTY(t *testing.T) { 157 defer deleteImages("ttytest") 158 defer deleteAllContainers() 159 160 cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls") 161 if _, err := runCommand(cmd); err != nil { 162 t.Fatal(err) 163 } 164 165 cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest") 166 imageID, _, err := runCommandWithOutput(cmd) 167 if err != nil { 168 t.Fatal(err) 169 } 170 imageID = strings.Trim(imageID, "\r\n") 171 172 cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls") 173 if _, err := runCommand(cmd); err != nil { 174 t.Fatal(err) 175 } 176 177 logDone("commit - commit tty") 178 } 179 180 func TestCommitWithHostBindMount(t *testing.T) { 181 cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true") 182 if _, err := runCommand(cmd); err != nil { 183 t.Fatal(err) 184 } 185 186 cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest") 187 imageID, _, err := runCommandWithOutput(cmd) 188 if err != nil { 189 t.Fatal(imageID, err) 190 } 191 192 imageID = strings.Trim(imageID, "\r\n") 193 194 cmd = exec.Command(dockerBinary, "run", "bindtest", "true") 195 196 if _, err := runCommand(cmd); err != nil { 197 t.Fatal(err) 198 } 199 200 deleteAllContainers() 201 deleteImages(imageID) 202 203 logDone("commit - commit bind mounted file") 204 }