Files
docker-cli/components/engine/integration-cli/docker_cli_rm_test.go
Josh Hawn 8939f52c95 Fix Flakiness in TestRmRestartingContainer
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
Upstream-commit: e979cf74640213ffe4d883cb00adecb957056e9a
Component: engine
2017-04-02 17:45:30 -07:00

130 lines
4.4 KiB
Go

package main
import (
"io/ioutil"
"os"
"strings"
"time"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/go-check/check"
)
func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
testRequires(c, SameHostDaemon)
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
if err != nil {
c.Fatalf("failed to create temporary directory: %s", tempDir)
}
defer os.RemoveAll(tempDir)
dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
err = os.RemoveAll(tempDir)
c.Assert(err, check.IsNil)
dockerCmd(c, "rm", "-v", "losemyvolumes")
}
func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
dockerCmd(c, "rm", "-v", "foo")
}
func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
createRunningContainer(c, "foo")
res, _, err := dockerCmdWithError("rm", "foo")
c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
c.Assert(res, checker.Contains, "cannot remove a running container")
c.Assert(res, checker.Contains, "Stop the container before attempting removal or force remove")
}
func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
createRunningContainer(c, "foo")
// Stop then remove with -f
dockerCmd(c, "rm", "-f", "foo")
}
func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
dockerfile1 := `FROM busybox:latest
ENTRYPOINT ["true"]`
img := "test-container-orphaning"
dockerfile2 := `FROM busybox:latest
ENTRYPOINT ["true"]
MAINTAINER Integration Tests`
// build first dockerfile
buildImageSuccessfully(c, img, build.WithDockerfile(dockerfile1))
img1 := getIDByName(c, img)
// run container on first image
dockerCmd(c, "run", img)
// rebuild dockerfile with a small addition at the end
buildImageSuccessfully(c, img, build.WithDockerfile(dockerfile2))
// try to remove the image, should not error out.
out, _, err := dockerCmdWithError("rmi", img)
c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
// check if we deleted the first image
out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
}
func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
out, _, err := dockerCmdWithError("rm", "unknown")
c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
c.Assert(out, checker.Contains, "No such container")
}
func createRunningContainer(c *check.C, name string) {
runSleepingContainer(c, "-dt", "--name", name)
}
// #30842
func (s *DockerSuite) TestRmRestartingContainer(c *check.C) {
name := "rst"
dockerCmd(c, "run", "--name", name, "--restart=always", "busybox", "date")
res, _, err := dockerCmdWithError("rm", name)
for strings.Contains(res, "cannot remove a running container") {
// The `date` command hasn't exited yet. It should end
// up in a "restarting" state if we wait a bit, though
// it is possible that it might be running again by
// that time. The wait period between each restart
// increases though so we just loop in this condition.
time.Sleep(100 * time.Millisecond)
res, _, err = dockerCmdWithError("rm", name)
}
c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm a restarting container, got none"))
c.Assert(res, checker.Contains, "cannot remove a restarting container")
c.Assert(res, checker.Contains, "Stop the container before attempting removal or force remove")
dockerCmd(c, "rm", "-f", name)
}
// #30842
func (s *DockerSuite) TestRmPausedContainer(c *check.C) {
testRequires(c, IsPausable)
name := "psd"
dockerCmd(c, "run", "--name", name, "-d", "busybox", "sleep", "1m")
dockerCmd(c, "pause", name)
res, _, err := dockerCmdWithError("rm", name)
c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm a paused container, got none"))
c.Assert(res, checker.Contains, "cannot remove a paused container")
c.Assert(res, checker.Contains, "Unpause and then stop the container before attempting removal or force remove")
unpauseContainer(c, name)
dockerCmd(c, "stop", name)
dockerCmd(c, "rm", name)
}