|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/docker/docker/pkg/integration/checker" |
| 8 | + "github.com/go-check/check" |
| 9 | +) |
| 10 | + |
| 11 | +func (s *DockerSuite) TestCliCommitAfterContainerIsDone(c *check.C) { |
| 12 | + printTestCaseName() |
| 13 | + defer printTestDuration(time.Now()) |
| 14 | + testRequires(c, DaemonIsLinux) |
| 15 | + |
| 16 | + pullImageIfNotExist("busybox") |
| 17 | + out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo") |
| 18 | + |
| 19 | + cleanedContainerID := strings.TrimSpace(out) |
| 20 | + |
| 21 | + dockerCmd(c, "wait", cleanedContainerID) |
| 22 | + |
| 23 | + out, _ = dockerCmd(c, "commit", cleanedContainerID) |
| 24 | + |
| 25 | + cleanedImageID := strings.TrimSpace(out) |
| 26 | + |
| 27 | + dockerCmd(c, "inspect", cleanedImageID) |
| 28 | +} |
| 29 | + |
| 30 | +func (s *DockerSuite) TestCliCommitRunningContainer(c *check.C) { |
| 31 | + printTestCaseName() |
| 32 | + defer printTestDuration(time.Now()) |
| 33 | + testRequires(c, DaemonIsLinux) |
| 34 | + |
| 35 | + pullImageIfNotExist("busybox") |
| 36 | + out, _ := dockerCmd(c, "run", "-d", "busybox", "top") |
| 37 | + cleanedContainerID := strings.TrimSpace(out) |
| 38 | + |
| 39 | + out, _, err := dockerCmdWithError("commit", cleanedContainerID) |
| 40 | + c.Assert(err, checker.NotNil) |
| 41 | + c.Assert(out, checker.Equals, "Error response from daemon: Bad request parameters: only stopped container could be committed\n") |
| 42 | +} |
| 43 | + |
| 44 | +func (s *DockerSuite) TestCliCommitNewFileBasic(c *check.C) { |
| 45 | + printTestCaseName() |
| 46 | + defer printTestDuration(time.Now()) |
| 47 | + testRequires(c, DaemonIsLinux) |
| 48 | + |
| 49 | + pullImageIfNotExist("busybox") |
| 50 | + dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo") |
| 51 | + |
| 52 | + imageID, _ := dockerCmd(c, "commit", "commiter") |
| 53 | + imageID = strings.TrimSpace(imageID) |
| 54 | + |
| 55 | + out, _ := dockerCmd(c, "run", imageID, "cat", "/foo") |
| 56 | + actual := strings.TrimSpace(out) |
| 57 | + c.Assert(actual, checker.Equals, "koye") |
| 58 | +} |
| 59 | + |
| 60 | +func (s *DockerSuite) TestCliCommitChange(c *check.C) { |
| 61 | + printTestCaseName() |
| 62 | + defer printTestDuration(time.Now()) |
| 63 | + testRequires(c, DaemonIsLinux) |
| 64 | + |
| 65 | + pullImageIfNotExist("busybox") |
| 66 | + dockerCmd(c, "run", "--name", "test", "busybox", "true") |
| 67 | + |
| 68 | + imageID, _ := dockerCmd(c, "commit", |
| 69 | + "--change", "EXPOSE 8080", |
| 70 | + "--change", "ENV DEBUG true", |
| 71 | + "--change", "ENV test 1", |
| 72 | + "--change", "ENV PATH /foo", |
| 73 | + "--change", "LABEL foo bar", |
| 74 | + "--change", "CMD [\"/bin/sh\"]", |
| 75 | + "--change", "WORKDIR /opt", |
| 76 | + "--change", "ENTRYPOINT [\"/bin/sh\"]", |
| 77 | + "--change", "USER testuser", |
| 78 | + "--change", "VOLUME /var/lib/docker", |
| 79 | + "test", "test-commit", |
| 80 | + ) |
| 81 | + imageID = strings.TrimSpace(imageID) |
| 82 | + |
| 83 | + expected := map[string]string{ |
| 84 | + "Config.ExposedPorts": "map[8080/tcp:{}]", |
| 85 | + "Config.Env": "[DEBUG=true test=1 PATH=/foo]", |
| 86 | + "Config.Labels": "map[foo:bar]", |
| 87 | + "Config.Cmd": "[/bin/sh]", |
| 88 | + "Config.WorkingDir": "/opt", |
| 89 | + "Config.Entrypoint": "[/bin/sh]", |
| 90 | + "Config.User": "testuser", |
| 91 | + "Config.Volumes": "map[/var/lib/docker:{}]", |
| 92 | + } |
| 93 | + |
| 94 | + for conf, value := range expected { |
| 95 | + res := inspectField(c, imageID, conf) |
| 96 | + if res != value { |
| 97 | + c.Errorf("%s('%s'), expected %s", conf, res, value) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments