| ... | ... |
@@ -3,6 +3,7 @@ package main |
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 | 5 |
"os/exec" |
| 6 |
+ "strings" |
|
| 6 | 7 |
"testing" |
| 7 | 8 |
) |
| 8 | 9 |
|
| ... | ... |
@@ -32,3 +33,32 @@ func TestCommitAfterContainerIsDone(t *testing.T) {
|
| 32 | 32 |
|
| 33 | 33 |
logDone("commit - echo foo and commit the image")
|
| 34 | 34 |
} |
| 35 |
+ |
|
| 36 |
+func TestCommitNewFile(t *testing.T) {
|
|
| 37 |
+ cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo") |
|
| 38 |
+ if _, err := runCommand(cmd); err != nil {
|
|
| 39 |
+ t.Fatal(err) |
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ cmd = exec.Command(dockerBinary, "commit", "commiter") |
|
| 43 |
+ imageId, _, err := runCommandWithOutput(cmd) |
|
| 44 |
+ if err != nil {
|
|
| 45 |
+ t.Fatal(err) |
|
| 46 |
+ } |
|
| 47 |
+ imageId = strings.Trim(imageId, "\r\n") |
|
| 48 |
+ |
|
| 49 |
+ cmd = exec.Command(dockerBinary, "run", imageId, "cat", "/foo") |
|
| 50 |
+ |
|
| 51 |
+ out, _, err := runCommandWithOutput(cmd) |
|
| 52 |
+ if err != nil {
|
|
| 53 |
+ t.Fatal(err, out) |
|
| 54 |
+ } |
|
| 55 |
+ if actual := strings.Trim(out, "\r\n"); actual != "koye" {
|
|
| 56 |
+ t.Fatalf("expected output koye received %s", actual)
|
|
| 57 |
+ } |
|
| 58 |
+ |
|
| 59 |
+ deleteAllContainers() |
|
| 60 |
+ deleteImages(imageId) |
|
| 61 |
+ |
|
| 62 |
+ logDone("commit - commit file and read")
|
|
| 63 |
+} |
| ... | ... |
@@ -16,202 +16,6 @@ import ( |
| 16 | 16 |
"time" |
| 17 | 17 |
) |
| 18 | 18 |
|
| 19 |
-func TestDiff(t *testing.T) {
|
|
| 20 |
- eng := NewTestEngine(t) |
|
| 21 |
- daemon := mkDaemonFromEngine(eng, t) |
|
| 22 |
- defer nuke(daemon) |
|
| 23 |
- // Create a container and remove a file |
|
| 24 |
- container1, _, _ := mkContainer(daemon, []string{"_", "/bin/rm", "/etc/passwd"}, t)
|
|
| 25 |
- defer daemon.Destroy(container1) |
|
| 26 |
- |
|
| 27 |
- // The changelog should be empty and not fail before run. See #1705 |
|
| 28 |
- c, err := container1.Changes() |
|
| 29 |
- if err != nil {
|
|
| 30 |
- t.Fatal(err) |
|
| 31 |
- } |
|
| 32 |
- if len(c) != 0 {
|
|
| 33 |
- t.Fatalf("Changelog should be empty before run")
|
|
| 34 |
- } |
|
| 35 |
- |
|
| 36 |
- if err := container1.Run(); err != nil {
|
|
| 37 |
- t.Fatal(err) |
|
| 38 |
- } |
|
| 39 |
- |
|
| 40 |
- // Check the changelog |
|
| 41 |
- c, err = container1.Changes() |
|
| 42 |
- if err != nil {
|
|
| 43 |
- t.Fatal(err) |
|
| 44 |
- } |
|
| 45 |
- success := false |
|
| 46 |
- for _, elem := range c {
|
|
| 47 |
- if elem.Path == "/etc/passwd" && elem.Kind == 2 {
|
|
| 48 |
- success = true |
|
| 49 |
- } |
|
| 50 |
- } |
|
| 51 |
- if !success {
|
|
| 52 |
- t.Fatalf("/etc/passwd as been removed but is not present in the diff")
|
|
| 53 |
- } |
|
| 54 |
- |
|
| 55 |
- // Commit the container |
|
| 56 |
- img, err := daemon.Commit(container1, "", "", "unit test commited image - diff", "", nil) |
|
| 57 |
- if err != nil {
|
|
| 58 |
- t.Fatal(err) |
|
| 59 |
- } |
|
| 60 |
- |
|
| 61 |
- // Create a new container from the commited image |
|
| 62 |
- container2, _, _ := mkContainer(daemon, []string{img.ID, "cat", "/etc/passwd"}, t)
|
|
| 63 |
- defer daemon.Destroy(container2) |
|
| 64 |
- |
|
| 65 |
- if err := container2.Run(); err != nil {
|
|
| 66 |
- t.Fatal(err) |
|
| 67 |
- } |
|
| 68 |
- |
|
| 69 |
- // Check the changelog |
|
| 70 |
- c, err = container2.Changes() |
|
| 71 |
- if err != nil {
|
|
| 72 |
- t.Fatal(err) |
|
| 73 |
- } |
|
| 74 |
- for _, elem := range c {
|
|
| 75 |
- if elem.Path == "/etc/passwd" {
|
|
| 76 |
- t.Fatalf("/etc/passwd should not be present in the diff after commit.")
|
|
| 77 |
- } |
|
| 78 |
- } |
|
| 79 |
- |
|
| 80 |
- // Create a new container |
|
| 81 |
- container3, _, _ := mkContainer(daemon, []string{"_", "rm", "/bin/httpd"}, t)
|
|
| 82 |
- defer daemon.Destroy(container3) |
|
| 83 |
- |
|
| 84 |
- if err := container3.Run(); err != nil {
|
|
| 85 |
- t.Fatal(err) |
|
| 86 |
- } |
|
| 87 |
- |
|
| 88 |
- // Check the changelog |
|
| 89 |
- c, err = container3.Changes() |
|
| 90 |
- if err != nil {
|
|
| 91 |
- t.Fatal(err) |
|
| 92 |
- } |
|
| 93 |
- success = false |
|
| 94 |
- for _, elem := range c {
|
|
| 95 |
- if elem.Path == "/bin/httpd" && elem.Kind == 2 {
|
|
| 96 |
- success = true |
|
| 97 |
- } |
|
| 98 |
- } |
|
| 99 |
- if !success {
|
|
| 100 |
- t.Fatalf("/bin/httpd should be present in the diff after commit.")
|
|
| 101 |
- } |
|
| 102 |
-} |
|
| 103 |
- |
|
| 104 |
-func TestCommitAutoRun(t *testing.T) {
|
|
| 105 |
- daemon := mkDaemon(t) |
|
| 106 |
- defer nuke(daemon) |
|
| 107 |
- container1, _, _ := mkContainer(daemon, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
|
| 108 |
- defer daemon.Destroy(container1) |
|
| 109 |
- |
|
| 110 |
- if container1.State.IsRunning() {
|
|
| 111 |
- t.Errorf("Container shouldn't be running")
|
|
| 112 |
- } |
|
| 113 |
- if err := container1.Run(); err != nil {
|
|
| 114 |
- t.Fatal(err) |
|
| 115 |
- } |
|
| 116 |
- if container1.State.IsRunning() {
|
|
| 117 |
- t.Errorf("Container shouldn't be running")
|
|
| 118 |
- } |
|
| 119 |
- |
|
| 120 |
- img, err := daemon.Commit(container1, "", "", "unit test commited image", "", &runconfig.Config{Cmd: []string{"cat", "/world"}})
|
|
| 121 |
- if err != nil {
|
|
| 122 |
- t.Error(err) |
|
| 123 |
- } |
|
| 124 |
- |
|
| 125 |
- // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world |
|
| 126 |
- container2, _, _ := mkContainer(daemon, []string{img.ID}, t)
|
|
| 127 |
- defer daemon.Destroy(container2) |
|
| 128 |
- stdout, err := container2.StdoutPipe() |
|
| 129 |
- if err != nil {
|
|
| 130 |
- t.Fatal(err) |
|
| 131 |
- } |
|
| 132 |
- stderr, err := container2.StderrPipe() |
|
| 133 |
- if err != nil {
|
|
| 134 |
- t.Fatal(err) |
|
| 135 |
- } |
|
| 136 |
- if err := container2.Start(); err != nil {
|
|
| 137 |
- t.Fatal(err) |
|
| 138 |
- } |
|
| 139 |
- container2.Wait() |
|
| 140 |
- output, err := ioutil.ReadAll(stdout) |
|
| 141 |
- if err != nil {
|
|
| 142 |
- t.Fatal(err) |
|
| 143 |
- } |
|
| 144 |
- output2, err := ioutil.ReadAll(stderr) |
|
| 145 |
- if err != nil {
|
|
| 146 |
- t.Fatal(err) |
|
| 147 |
- } |
|
| 148 |
- if err := stdout.Close(); err != nil {
|
|
| 149 |
- t.Fatal(err) |
|
| 150 |
- } |
|
| 151 |
- if err := stderr.Close(); err != nil {
|
|
| 152 |
- t.Fatal(err) |
|
| 153 |
- } |
|
| 154 |
- if string(output) != "hello\n" {
|
|
| 155 |
- t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
|
|
| 156 |
- } |
|
| 157 |
-} |
|
| 158 |
- |
|
| 159 |
-func TestCommitRun(t *testing.T) {
|
|
| 160 |
- daemon := mkDaemon(t) |
|
| 161 |
- defer nuke(daemon) |
|
| 162 |
- |
|
| 163 |
- container1, _, _ := mkContainer(daemon, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
|
| 164 |
- defer daemon.Destroy(container1) |
|
| 165 |
- |
|
| 166 |
- if container1.State.IsRunning() {
|
|
| 167 |
- t.Errorf("Container shouldn't be running")
|
|
| 168 |
- } |
|
| 169 |
- if err := container1.Run(); err != nil {
|
|
| 170 |
- t.Fatal(err) |
|
| 171 |
- } |
|
| 172 |
- if container1.State.IsRunning() {
|
|
| 173 |
- t.Errorf("Container shouldn't be running")
|
|
| 174 |
- } |
|
| 175 |
- |
|
| 176 |
- img, err := daemon.Commit(container1, "", "", "unit test commited image", "", nil) |
|
| 177 |
- if err != nil {
|
|
| 178 |
- t.Error(err) |
|
| 179 |
- } |
|
| 180 |
- |
|
| 181 |
- // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world |
|
| 182 |
- container2, _, _ := mkContainer(daemon, []string{img.ID, "cat", "/world"}, t)
|
|
| 183 |
- defer daemon.Destroy(container2) |
|
| 184 |
- stdout, err := container2.StdoutPipe() |
|
| 185 |
- if err != nil {
|
|
| 186 |
- t.Fatal(err) |
|
| 187 |
- } |
|
| 188 |
- stderr, err := container2.StderrPipe() |
|
| 189 |
- if err != nil {
|
|
| 190 |
- t.Fatal(err) |
|
| 191 |
- } |
|
| 192 |
- if err := container2.Start(); err != nil {
|
|
| 193 |
- t.Fatal(err) |
|
| 194 |
- } |
|
| 195 |
- container2.Wait() |
|
| 196 |
- output, err := ioutil.ReadAll(stdout) |
|
| 197 |
- if err != nil {
|
|
| 198 |
- t.Fatal(err) |
|
| 199 |
- } |
|
| 200 |
- output2, err := ioutil.ReadAll(stderr) |
|
| 201 |
- if err != nil {
|
|
| 202 |
- t.Fatal(err) |
|
| 203 |
- } |
|
| 204 |
- if err := stdout.Close(); err != nil {
|
|
| 205 |
- t.Fatal(err) |
|
| 206 |
- } |
|
| 207 |
- if err := stderr.Close(); err != nil {
|
|
| 208 |
- t.Fatal(err) |
|
| 209 |
- } |
|
| 210 |
- if string(output) != "hello\n" {
|
|
| 211 |
- t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
|
|
| 212 |
- } |
|
| 213 |
-} |
|
| 214 |
- |
|
| 215 | 19 |
func TestStart(t *testing.T) {
|
| 216 | 20 |
daemon := mkDaemon(t) |
| 217 | 21 |
defer nuke(daemon) |