1
0
mirror of https://github.com/moby/moby.git synced 2025-08-08 13:22:22 +03:00

Clean some function in docker_utils_test.go

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester
2017-04-11 19:42:54 +02:00
parent 2a17d048de
commit 10e171cd94
10 changed files with 48 additions and 90 deletions

View File

@@ -21,6 +21,7 @@ import (
mounttypes "github.com/docker/docker/api/types/mount" mounttypes "github.com/docker/docker/api/types/mount"
networktypes "github.com/docker/docker/api/types/network" networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
@@ -347,25 +348,29 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
func (s *DockerSuite) TestContainerAPIPause(c *check.C) { func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
// Problematic on Windows as Windows does not support pause // Problematic on Windows as Windows does not support pause
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
defer unpauseAllContainers(c)
out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "30") getPaused := func(c *check.C) []string {
return strings.Fields(cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined())
}
out := cli.DockerCmd(c, "run", "-d", "busybox", "sleep", "30").Combined()
ContainerID := strings.TrimSpace(out) ContainerID := strings.TrimSpace(out)
status, _, err := request.SockRequest("POST", "/containers/"+ContainerID+"/pause", nil, daemonHost()) resp, _, err := request.Post("/containers/" + ContainerID + "/pause")
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, http.StatusNoContent) c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
pausedContainers := getPausedContainers(c) pausedContainers := getPaused(c)
if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] { if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
c.Fatalf("there should be one paused container and not %d", len(pausedContainers)) c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
} }
status, _, err = request.SockRequest("POST", "/containers/"+ContainerID+"/unpause", nil, daemonHost()) resp, _, err = request.Post("/containers/" + ContainerID + "/unpause")
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, http.StatusNoContent) c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
pausedContainers = getPausedContainers(c) pausedContainers = getPaused(c)
c.Assert(pausedContainers, checker.HasLen, 0, check.Commentf("There should be no paused container.")) c.Assert(pausedContainers, checker.HasLen, 0, check.Commentf("There should be no paused container."))
} }
@@ -1262,7 +1267,6 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(
readOnly: true, readOnly: true,
volumes: defaultVolumes(testVol), // Our bind mount is at /vol2 volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
}) })
defer deleteContainer(cID)
// Attempt to extract to a symlink in the volume which points to a // Attempt to extract to a symlink in the volume which points to a
// directory outside the volume. This should cause an error because the // directory outside the volume. This should cause an error because the

View File

@@ -155,7 +155,6 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
func (s *DockerSuite) TestAttachPausedContainer(c *check.C) { func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
testRequires(c, IsPausable) testRequires(c, IsPausable)
defer unpauseAllContainers(c)
runSleepingContainer(c, "-d", "--name=test") runSleepingContainer(c, "-d", "--name=test")
dockerCmd(c, "pause", "test") dockerCmd(c, "pause", "test")

View File

@@ -12,6 +12,7 @@ import (
"github.com/docker/distribution/manifest/schema2" "github.com/docker/distribution/manifest/schema2"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/pkg/stringutils" "github.com/docker/docker/pkg/stringutils"
"github.com/go-check/check" "github.com/go-check/check"
@@ -35,24 +36,20 @@ func setupImageWithTag(c *check.C, tag string) (digest.Digest, error) {
// new file is committed because this layer is used for detecting malicious // new file is committed because this layer is used for detecting malicious
// changes. if this was committed as empty layer it would be skipped on pull // changes. if this was committed as empty layer it would be skipped on pull
// and malicious changes would never be detected. // and malicious changes would never be detected.
dockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile") cli.DockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile")
// tag the image to upload it to the private registry // tag the image to upload it to the private registry
repoAndTag := repoName + ":" + tag repoAndTag := repoName + ":" + tag
out, _, err := dockerCmdWithError("commit", containerName, repoAndTag) cli.DockerCmd(c, "commit", containerName, repoAndTag)
c.Assert(err, checker.IsNil, check.Commentf("image tagging failed: %s", out))
// delete the container as we don't need it any more // delete the container as we don't need it any more
err = deleteContainer(containerName) cli.DockerCmd(c, "rm", "-fv", containerName)
c.Assert(err, checker.IsNil)
// push the image // push the image
out, _, err = dockerCmdWithError("push", repoAndTag) out := cli.DockerCmd(c, "push", repoAndTag).Combined()
c.Assert(err, checker.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
// delete our local repo that we previously tagged // delete our local repo that we previously tagged
rmiout, _, err := dockerCmdWithError("rmi", repoAndTag) cli.DockerCmd(c, "rmi", repoAndTag)
c.Assert(err, checker.IsNil, check.Commentf("error deleting images prior to real test: %s", rmiout))
matches := pushDigestRegex.FindStringSubmatch(out) matches := pushDigestRegex.FindStringSubmatch(out)
c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from push output: %s", out)) c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from push output: %s", out))

View File

@@ -40,7 +40,6 @@ func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
//test commit a paused container should not unpause it after commit //test commit a paused container should not unpause it after commit
func (s *DockerSuite) TestCommitPausedContainer(c *check.C) { func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
defer unpauseAllContainers(c)
out, _ := dockerCmd(c, "run", "-i", "-d", "busybox") out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)

View File

@@ -20,6 +20,7 @@ import (
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
icmd "github.com/docker/docker/pkg/testutil/cmd" icmd "github.com/docker/docker/pkg/testutil/cmd"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/docker/docker/integration-cli/cli"
) )
func (s *DockerSuite) TestExec(c *check.C) { func (s *DockerSuite) TestExec(c *check.C) {
@@ -137,7 +138,6 @@ func (s *DockerSuite) TestExecExitStatus(c *check.C) {
func (s *DockerSuite) TestExecPausedContainer(c *check.C) { func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
testRequires(c, IsPausable) testRequires(c, IsPausable)
defer unpauseAllContainers(c)
out, _ := runSleepingContainer(c, "-d", "--name", "testing") out, _ := runSleepingContainer(c, "-d", "--name", "testing")
ContainerID := strings.TrimSpace(out) ContainerID := strings.TrimSpace(out)
@@ -389,7 +389,10 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
// Not applicable on Windows to Windows CI. // Not applicable on Windows to Windows CI.
testRequires(c, SameHostDaemon, DaemonIsLinux) testRequires(c, SameHostDaemon, DaemonIsLinux)
for _, fn := range []string{"resolv.conf", "hosts"} { for _, fn := range []string{"resolv.conf", "hosts"} {
deleteAllContainers(c) containers := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
if containers != "" {
cli.DockerCmd(c, append([]string{"rm", "-fv"}, strings.Split(strings.TrimSpace(containers), "\n")...)...)
}
content := runCommandAndReadContainerFile(c, fn, dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)) content := runCommandAndReadContainerFile(c, fn, dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn))

View File

@@ -53,9 +53,6 @@ func (s *DockerSuite) TestInspectDefault(c *check.C) {
} }
func (s *DockerSuite) TestInspectStatus(c *check.C) { func (s *DockerSuite) TestInspectStatus(c *check.C) {
if testEnv.DaemonPlatform() != "windows" {
defer unpauseAllContainers(c)
}
out, _ := runSleepingContainer(c, "-d") out, _ := runSleepingContainer(c, "-d")
out = strings.TrimSpace(out) out = strings.TrimSpace(out)

View File

@@ -4,23 +4,25 @@ import (
"strings" "strings"
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/go-check/check" "github.com/go-check/check"
) )
func (s *DockerSuite) TestPause(c *check.C) { func (s *DockerSuite) TestPause(c *check.C) {
testRequires(c, IsPausable) testRequires(c, IsPausable)
defer unpauseAllContainers(c)
name := "testeventpause" name := "testeventpause"
runSleepingContainer(c, "-d", "--name", name) runSleepingContainer(c, "-d", "--name", name)
dockerCmd(c, "pause", name) cli.DockerCmd(c, "pause", name)
pausedContainers := getPausedContainers(c) pausedContainers := strings.Fields(
cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
)
c.Assert(len(pausedContainers), checker.Equals, 1) c.Assert(len(pausedContainers), checker.Equals, 1)
dockerCmd(c, "unpause", name) cli.DockerCmd(c, "unpause", name)
out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)) out := cli.DockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)).Combined()
events := strings.Split(strings.TrimSpace(out), "\n") events := strings.Split(strings.TrimSpace(out), "\n")
actions := eventActionsByIDAndType(c, events, name, "container") actions := eventActionsByIDAndType(c, events, name, "container")
@@ -30,7 +32,6 @@ func (s *DockerSuite) TestPause(c *check.C) {
func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) { func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
testRequires(c, IsPausable) testRequires(c, IsPausable)
defer unpauseAllContainers(c)
containers := []string{ containers := []string{
"testpausewithmorecontainers1", "testpausewithmorecontainers1",
@@ -39,13 +40,15 @@ func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
for _, name := range containers { for _, name := range containers {
runSleepingContainer(c, "-d", "--name", name) runSleepingContainer(c, "-d", "--name", name)
} }
dockerCmd(c, append([]string{"pause"}, containers...)...) cli.DockerCmd(c, append([]string{"pause"}, containers...)...)
pausedContainers := getPausedContainers(c) pausedContainers := strings.Fields(
cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
)
c.Assert(len(pausedContainers), checker.Equals, len(containers)) c.Assert(len(pausedContainers), checker.Equals, len(containers))
dockerCmd(c, append([]string{"unpause"}, containers...)...) cli.DockerCmd(c, append([]string{"unpause"}, containers...)...)
out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)) out := cli.DockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)).Combined()
events := strings.Split(strings.TrimSpace(out), "\n") events := strings.Split(strings.TrimSpace(out), "\n")
for _, name := range containers { for _, name := range containers {

View File

@@ -2025,18 +2025,16 @@ func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
// TODO Windows. Network settings are not propagated back to inspect. // TODO Windows. Network settings are not propagated back to inspect.
testRequires(c, SameHostDaemon, DaemonIsLinux) testRequires(c, SameHostDaemon, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top") out := cli.DockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top").Combined()
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
ip := inspectField(c, id, "NetworkSettings.Networks.bridge.IPAddress") ip := inspectField(c, id, "NetworkSettings.Networks.bridge.IPAddress")
icmd.RunCommand("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip), icmd.RunCommand("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT").Assert(c, icmd.Success) "!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT").Assert(c, icmd.Success)
if err := deleteContainer(id); err != nil { cli.DockerCmd(c, "rm", "-fv", id)
c.Fatal(err)
}
dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top") cli.DockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
} }
func (s *DockerSuite) TestRunPortInUse(c *check.C) { func (s *DockerSuite) TestRunPortInUse(c *check.C) {
@@ -2817,12 +2815,11 @@ func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
// run container with --rm should remove container if exit code != 0 // run container with --rm should remove container if exit code != 0
func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) { func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
name := "flowers" name := "flowers"
out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "ls", "/notexists") cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "ls", "/notexists")).Assert(c, icmd.Expected{
if err == nil { ExitCode: 1,
c.Fatal("Expected docker run to fail", out, err) })
}
out = getAllContainers(c) out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
if out != "" { if out != "" {
c.Fatal("Expected not to have containers", out) c.Fatal("Expected not to have containers", out)
} }
@@ -2830,12 +2827,10 @@ func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.
func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) { func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) {
name := "sparkles" name := "sparkles"
out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "commandNotFound") cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "commandNotFound")).Assert(c, icmd.Expected{
if err == nil { ExitCode: 127,
c.Fatal("Expected docker run to fail", out, err) })
} out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
out = getAllContainers(c)
if out != "" { if out != "" {
c.Fatal("Expected not to have containers", out) c.Fatal("Expected not to have containers", out)
} }

View File

@@ -94,7 +94,6 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
func (s *DockerSuite) TestStartPausedContainer(c *check.C) { func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
// Windows does not support pausing containers // Windows does not support pausing containers
testRequires(c, IsPausable) testRequires(c, IsPausable)
defer unpauseAllContainers(c)
runSleepingContainer(c, "-d", "--name", "testing") runSleepingContainer(c, "-d", "--name", "testing")

View File

@@ -33,44 +33,6 @@ func daemonHost() string {
return request.DaemonHost() return request.DaemonHost()
} }
// FIXME(vdemeester) move this away are remove ignoreNoSuchContainer bool
func deleteContainer(container ...string) error {
return icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...).Compare(icmd.Success)
}
func getAllContainers(c *check.C) string {
result := icmd.RunCommand(dockerBinary, "ps", "-q", "-a")
result.Assert(c, icmd.Success)
return result.Combined()
}
// Deprecated
func deleteAllContainers(c *check.C) {
containers := getAllContainers(c)
if containers != "" {
err := deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...)
c.Assert(err, checker.IsNil)
}
}
func getPausedContainers(c *check.C) []string {
result := icmd.RunCommand(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
result.Assert(c, icmd.Success)
return strings.Fields(result.Combined())
}
func unpauseContainer(c *check.C, container string) {
dockerCmd(c, "unpause", container)
}
// Deprecated
func unpauseAllContainers(c *check.C) {
containers := getPausedContainers(c)
for _, value := range containers {
unpauseContainer(c, value)
}
}
func deleteImages(images ...string) error { func deleteImages(images ...string) error {
args := []string{dockerBinary, "rmi", "-f"} args := []string{dockerBinary, "rmi", "-f"}
return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error