Browse code

Migrate several docker rm tests to api tests

This fix migrates several docker rm tests to api tests

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/12 10:18:35
Showing 2 changed files
... ...
@@ -1,56 +1,11 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"io/ioutil"
5
-	"os"
6
-
7 4
 	"github.com/docker/docker/integration-cli/checker"
8 5
 	"github.com/docker/docker/integration-cli/cli/build"
9 6
 	"github.com/go-check/check"
10 7
 )
11 8
 
12
-func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
13
-	testRequires(c, SameHostDaemon)
14
-
15
-	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
16
-
17
-	tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
18
-	if err != nil {
19
-		c.Fatalf("failed to create temporary directory: %s", tempDir)
20
-	}
21
-	defer os.RemoveAll(tempDir)
22
-
23
-	dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
24
-
25
-	err = os.RemoveAll(tempDir)
26
-	c.Assert(err, check.IsNil)
27
-
28
-	dockerCmd(c, "rm", "-v", "losemyvolumes")
29
-}
30
-
31
-func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
32
-	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
33
-
34
-	dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
35
-
36
-	dockerCmd(c, "rm", "-v", "foo")
37
-}
38
-
39
-func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
40
-	createRunningContainer(c, "foo")
41
-
42
-	res, _, err := dockerCmdWithError("rm", "foo")
43
-	c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
44
-	c.Assert(res, checker.Contains, "cannot remove a running container")
45
-}
46
-
47
-func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
48
-	createRunningContainer(c, "foo")
49
-
50
-	// Stop then remove with -f
51
-	dockerCmd(c, "rm", "-f", "foo")
52
-}
53
-
54 9
 func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
55 10
 	dockerfile1 := `FROM busybox:latest
56 11
 	ENTRYPOINT ["true"]`
... ...
@@ -75,13 +30,3 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
75 75
 	c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
76 76
 
77 77
 }
78
-
79
-func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
80
-	out, _, err := dockerCmdWithError("rm", "unknown")
81
-	c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
82
-	c.Assert(out, checker.Contains, "No such container")
83
-}
84
-
85
-func createRunningContainer(c *check.C, name string) {
86
-	runSleepingContainer(c, "-dt", "--name", name)
87
-}
88 78
new file mode 100644
... ...
@@ -0,0 +1,113 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"context"
4
+	"os"
5
+	"testing"
6
+	"time"
7
+
8
+	"github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/api/types/filters"
10
+	"github.com/docker/docker/integration/internal/container"
11
+	"github.com/docker/docker/integration/internal/request"
12
+	"github.com/docker/docker/internal/testutil"
13
+	"github.com/gotestyourself/gotestyourself/fs"
14
+	"github.com/gotestyourself/gotestyourself/poll"
15
+	"github.com/gotestyourself/gotestyourself/skip"
16
+	"github.com/stretchr/testify/assert"
17
+	"github.com/stretchr/testify/require"
18
+)
19
+
20
+func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
21
+	if testEnv.OSType == "windows" {
22
+		return "c:", `\`
23
+	}
24
+	return "", "/"
25
+}
26
+
27
+// Test case for #5244: `docker rm` fails if bind dir doesn't exist anymore
28
+func TestRemoveContainerWithRemovedVolume(t *testing.T) {
29
+	skip.If(t, !testEnv.IsLocalDaemon())
30
+
31
+	defer setupTest(t)()
32
+	ctx := context.Background()
33
+	client := request.NewAPIClient(t)
34
+
35
+	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
36
+
37
+	tempDir := fs.NewDir(t, "test-rm-container-with-removed-volume", fs.WithMode(0755))
38
+	defer tempDir.Remove()
39
+
40
+	cID := container.Run(t, ctx, client, container.WithCmd("true"), container.WithBind(tempDir.Path(), prefix+slash+"test"))
41
+	poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
42
+
43
+	err := os.RemoveAll(tempDir.Path())
44
+	require.NoError(t, err)
45
+
46
+	err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
47
+		RemoveVolumes: true,
48
+	})
49
+	require.NoError(t, err)
50
+
51
+	_, _, err = client.ContainerInspectWithRaw(ctx, cID, true)
52
+	testutil.ErrorContains(t, err, "No such container")
53
+}
54
+
55
+// Test case for #2099/#2125
56
+func TestRemoveContainerWithVolume(t *testing.T) {
57
+	defer setupTest(t)()
58
+	ctx := context.Background()
59
+	client := request.NewAPIClient(t)
60
+
61
+	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
62
+
63
+	cID := container.Run(t, ctx, client, container.WithCmd("true"), container.WithVolume(prefix+slash+"srv"))
64
+	poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
65
+
66
+	insp, _, err := client.ContainerInspectWithRaw(ctx, cID, true)
67
+	require.NoError(t, err)
68
+	assert.Equal(t, len(insp.Mounts), 1)
69
+	volName := insp.Mounts[0].Name
70
+
71
+	err = client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
72
+		RemoveVolumes: true,
73
+	})
74
+	require.NoError(t, err)
75
+
76
+	volumes, err := client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", volName)))
77
+	require.NoError(t, err)
78
+	assert.Equal(t, len(volumes.Volumes), 0)
79
+}
80
+
81
+func TestRemoveContainerRunning(t *testing.T) {
82
+	defer setupTest(t)()
83
+	ctx := context.Background()
84
+	client := request.NewAPIClient(t)
85
+
86
+	cID := container.Run(t, ctx, client)
87
+
88
+	err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{})
89
+	testutil.ErrorContains(t, err, "cannot remove a running container")
90
+}
91
+
92
+func TestRemoveContainerForceRemoveRunning(t *testing.T) {
93
+	defer setupTest(t)()
94
+	ctx := context.Background()
95
+	client := request.NewAPIClient(t)
96
+
97
+	cID := container.Run(t, ctx, client)
98
+
99
+	err := client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{
100
+		Force: true,
101
+	})
102
+	require.NoError(t, err)
103
+}
104
+
105
+func TestRemoveInvalidContainer(t *testing.T) {
106
+	defer setupTest(t)()
107
+	ctx := context.Background()
108
+	client := request.NewAPIClient(t)
109
+
110
+	err := client.ContainerRemove(ctx, "unknown", types.ContainerRemoveOptions{})
111
+	testutil.ErrorContains(t, err, "No such container")
112
+}