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"
networktypes "github.com/docker/docker/api/types/network"
"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/request"
"github.com/docker/docker/pkg/ioutils"
@ -347,25 +348,29 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
// Problematic on Windows as Windows does not support pause
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)
status, _, err := request.SockRequest("POST", "/containers/"+ContainerID+"/pause", nil, daemonHost())
resp, _, err := request.Post("/containers/" + ContainerID + "/pause")
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] {
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(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."))
}
@ -1262,7 +1267,6 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(
readOnly: true,
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
// 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) {
testRequires(c, IsPausable)
defer unpauseAllContainers(c)
runSleepingContainer(c, "-d", "--name=test")
dockerCmd(c, "pause", "test")

View file

@ -12,6 +12,7 @@ import (
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/docker/api/types"
"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/pkg/stringutils"
"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
// changes. if this was committed as empty layer it would be skipped on pull
// 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
repoAndTag := repoName + ":" + tag
out, _, err := dockerCmdWithError("commit", containerName, repoAndTag)
c.Assert(err, checker.IsNil, check.Commentf("image tagging failed: %s", out))
cli.DockerCmd(c, "commit", containerName, repoAndTag)
// delete the container as we don't need it any more
err = deleteContainer(containerName)
c.Assert(err, checker.IsNil)
cli.DockerCmd(c, "rm", "-fv", containerName)
// push the image
out, _, err = dockerCmdWithError("push", repoAndTag)
c.Assert(err, checker.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
out := cli.DockerCmd(c, "push", repoAndTag).Combined()
// delete our local repo that we previously tagged
rmiout, _, err := dockerCmdWithError("rmi", repoAndTag)
c.Assert(err, checker.IsNil, check.Commentf("error deleting images prior to real test: %s", rmiout))
cli.DockerCmd(c, "rmi", repoAndTag)
matches := pushDigestRegex.FindStringSubmatch(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
func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
defer unpauseAllContainers(c)
out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
cleanedContainerID := strings.TrimSpace(out)

View file

@ -20,6 +20,7 @@ import (
"github.com/docker/docker/integration-cli/request"
icmd "github.com/docker/docker/pkg/testutil/cmd"
"github.com/go-check/check"
"github.com/docker/docker/integration-cli/cli"
)
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) {
testRequires(c, IsPausable)
defer unpauseAllContainers(c)
out, _ := runSleepingContainer(c, "-d", "--name", "testing")
ContainerID := strings.TrimSpace(out)
@ -389,7 +389,10 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
// Not applicable on Windows to Windows CI.
testRequires(c, SameHostDaemon, DaemonIsLinux)
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))

View file

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

View file

@ -4,23 +4,25 @@ import (
"strings"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/go-check/check"
)
func (s *DockerSuite) TestPause(c *check.C) {
testRequires(c, IsPausable)
defer unpauseAllContainers(c)
name := "testeventpause"
runSleepingContainer(c, "-d", "--name", name)
dockerCmd(c, "pause", name)
pausedContainers := getPausedContainers(c)
cli.DockerCmd(c, "pause", name)
pausedContainers := strings.Fields(
cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
)
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")
actions := eventActionsByIDAndType(c, events, name, "container")
@ -30,7 +32,6 @@ func (s *DockerSuite) TestPause(c *check.C) {
func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
testRequires(c, IsPausable)
defer unpauseAllContainers(c)
containers := []string{
"testpausewithmorecontainers1",
@ -39,13 +40,15 @@ func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
for _, name := range containers {
runSleepingContainer(c, "-d", "--name", name)
}
dockerCmd(c, append([]string{"pause"}, containers...)...)
pausedContainers := getPausedContainers(c)
cli.DockerCmd(c, append([]string{"pause"}, containers...)...)
pausedContainers := strings.Fields(
cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
)
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")
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.
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)
ip := inspectField(c, id, "NetworkSettings.Networks.bridge.IPAddress")
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)
if err := deleteContainer(id); err != nil {
c.Fatal(err)
}
cli.DockerCmd(c, "rm", "-fv", id)
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) {
@ -2817,12 +2815,11 @@ func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
// run container with --rm should remove container if exit code != 0
func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
name := "flowers"
out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "ls", "/notexists")
if err == nil {
c.Fatal("Expected docker run to fail", out, err)
}
cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "ls", "/notexists")).Assert(c, icmd.Expected{
ExitCode: 1,
})
out = getAllContainers(c)
out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
if 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) {
name := "sparkles"
out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "commandNotFound")
if err == nil {
c.Fatal("Expected docker run to fail", out, err)
}
out = getAllContainers(c)
cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "commandNotFound")).Assert(c, icmd.Expected{
ExitCode: 127,
})
out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
if 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) {
// Windows does not support pausing containers
testRequires(c, IsPausable)
defer unpauseAllContainers(c)
runSleepingContainer(c, "-d", "--name", "testing")

View file

@ -33,44 +33,6 @@ func daemonHost() string {
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 {
args := []string{dockerBinary, "rmi", "-f"}
return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error