Browse code

Migrate volumes tests in integration-cli to api tests

This fix migrates volumes tests in integration-cli to api tests
in integration/

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

Yong Tang authored on 2018/02/13 06:54:12
Showing 4 changed files
1 1
deleted file mode 100644
... ...
@@ -1,103 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"fmt"
5
-	"path/filepath"
6
-	"strings"
7
-	"time"
8
-
9
-	"github.com/docker/docker/api/types/filters"
10
-	volumetypes "github.com/docker/docker/api/types/volume"
11
-	"github.com/docker/docker/client"
12
-	"github.com/docker/docker/integration-cli/checker"
13
-	"github.com/go-check/check"
14
-	"golang.org/x/net/context"
15
-)
16
-
17
-func (s *DockerSuite) TestVolumesAPIList(c *check.C) {
18
-	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
19
-	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "busybox")
20
-
21
-	cli, err := client.NewEnvClient()
22
-	c.Assert(err, checker.IsNil)
23
-	defer cli.Close()
24
-
25
-	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
26
-	c.Assert(err, checker.IsNil)
27
-	vname := container.Mounts[0].Name
28
-
29
-	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
30
-	c.Assert(err, checker.IsNil)
31
-
32
-	found := false
33
-	for _, vol := range volumes.Volumes {
34
-		if vol.Name == vname {
35
-			found = true
36
-			break
37
-		}
38
-	}
39
-	c.Assert(found, checker.Equals, true)
40
-}
41
-
42
-func (s *DockerSuite) TestVolumesAPICreate(c *check.C) {
43
-	config := volumetypes.VolumesCreateBody{
44
-		Name: "test",
45
-	}
46
-
47
-	cli, err := client.NewEnvClient()
48
-	c.Assert(err, checker.IsNil)
49
-	defer cli.Close()
50
-
51
-	vol, err := cli.VolumeCreate(context.Background(), config)
52
-	c.Assert(err, check.IsNil)
53
-
54
-	c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
55
-}
56
-
57
-func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) {
58
-	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
59
-	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "--name=test", "busybox")
60
-
61
-	cli, err := client.NewEnvClient()
62
-	c.Assert(err, checker.IsNil)
63
-	defer cli.Close()
64
-
65
-	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
66
-	c.Assert(err, checker.IsNil)
67
-	vname := container.Mounts[0].Name
68
-
69
-	err = cli.VolumeRemove(context.Background(), vname, false)
70
-	c.Assert(err.Error(), checker.Contains, "volume is in use")
71
-
72
-	dockerCmd(c, "rm", "-f", "test")
73
-	err = cli.VolumeRemove(context.Background(), vname, false)
74
-	c.Assert(err, checker.IsNil)
75
-}
76
-
77
-func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) {
78
-	config := volumetypes.VolumesCreateBody{
79
-		Name: "test",
80
-	}
81
-
82
-	// sampling current time minus a minute so to now have false positive in case of delays
83
-	now := time.Now().Truncate(time.Minute)
84
-
85
-	cli, err := client.NewEnvClient()
86
-	c.Assert(err, checker.IsNil)
87
-	defer cli.Close()
88
-
89
-	_, err = cli.VolumeCreate(context.Background(), config)
90
-	c.Assert(err, check.IsNil)
91
-
92
-	vol, err := cli.VolumeInspect(context.Background(), config.Name)
93
-	c.Assert(err, checker.IsNil)
94
-	c.Assert(vol.Name, checker.Equals, config.Name)
95
-
96
-	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
97
-	testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
98
-	c.Assert(err, check.IsNil)
99
-	testCreatedAt = testCreatedAt.Truncate(time.Minute)
100
-	if !testCreatedAt.Equal(now) {
101
-		c.Assert(fmt.Errorf("Time Volume is CreatedAt not equal to current time"), check.NotNil)
102
-	}
103
-}
... ...
@@ -1,6 +1,8 @@
1 1
 package container
2 2
 
3 3
 import (
4
+	"fmt"
5
+
4 6
 	containertypes "github.com/docker/docker/api/types/container"
5 7
 	"github.com/docker/docker/api/types/strslice"
6 8
 	"github.com/docker/go-connections/nat"
... ...
@@ -57,3 +59,20 @@ func WithWorkingDir(dir string) func(*TestContainerConfig) {
57 57
 		c.Config.WorkingDir = dir
58 58
 	}
59 59
 }
60
+
61
+// WithVolume sets the volume of the container
62
+func WithVolume(name string) func(*TestContainerConfig) {
63
+	return func(c *TestContainerConfig) {
64
+		if c.Config.Volumes == nil {
65
+			c.Config.Volumes = map[string]struct{}{}
66
+		}
67
+		c.Config.Volumes[name] = struct{}{}
68
+	}
69
+}
70
+
71
+// WithBind sets the bind mount of the container
72
+func WithBind(src, target string) func(*TestContainerConfig) {
73
+	return func(c *TestContainerConfig) {
74
+		c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
75
+	}
76
+}
60 77
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+package volume // import "github.com/docker/docker/integration/volume"
1
+
2
+import (
3
+	"fmt"
4
+	"os"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/internal/test/environment"
8
+)
9
+
10
+var testEnv *environment.Execution
11
+
12
+func TestMain(m *testing.M) {
13
+	var err error
14
+	testEnv, err = environment.New()
15
+	if err != nil {
16
+		fmt.Println(err)
17
+		os.Exit(1)
18
+	}
19
+	err = environment.EnsureFrozenImagesLinux(testEnv)
20
+	if err != nil {
21
+		fmt.Println(err)
22
+		os.Exit(1)
23
+	}
24
+
25
+	testEnv.Print()
26
+	os.Exit(m.Run())
27
+}
28
+
29
+func setupTest(t *testing.T) func() {
30
+	environment.ProtectAll(t, testEnv)
31
+	return func() { testEnv.Clean(t) }
32
+}
0 33
new file mode 100644
... ...
@@ -0,0 +1,115 @@
0
+package volume
1
+
2
+import (
3
+	"context"
4
+	"fmt"
5
+	"strings"
6
+	"testing"
7
+	"time"
8
+
9
+	"github.com/docker/docker/api/types"
10
+	"github.com/docker/docker/api/types/filters"
11
+	volumetypes "github.com/docker/docker/api/types/volume"
12
+	"github.com/docker/docker/integration/internal/container"
13
+	"github.com/docker/docker/integration/internal/request"
14
+	"github.com/docker/docker/internal/testutil"
15
+	"github.com/stretchr/testify/assert"
16
+	"github.com/stretchr/testify/require"
17
+)
18
+
19
+func TestVolumesCreateAndList(t *testing.T) {
20
+	defer setupTest(t)()
21
+	client := request.NewAPIClient(t)
22
+	ctx := context.Background()
23
+
24
+	name := t.Name()
25
+	vol, err := client.VolumeCreate(ctx, volumetypes.VolumesCreateBody{
26
+		Name: name,
27
+	})
28
+	require.NoError(t, err)
29
+
30
+	expected := types.Volume{
31
+		// Ignore timestamp of CreatedAt
32
+		CreatedAt:  vol.CreatedAt,
33
+		Driver:     "local",
34
+		Scope:      "local",
35
+		Name:       name,
36
+		Options:    map[string]string{},
37
+		Mountpoint: fmt.Sprintf("%s/volumes/%s/_data", testEnv.DaemonInfo.DockerRootDir, name),
38
+	}
39
+	assert.Equal(t, vol, expected)
40
+
41
+	volumes, err := client.VolumeList(ctx, filters.Args{})
42
+	require.NoError(t, err)
43
+
44
+	assert.Equal(t, len(volumes.Volumes), 1)
45
+	assert.NotNil(t, volumes.Volumes[0])
46
+	assert.Equal(t, *volumes.Volumes[0], expected)
47
+}
48
+
49
+func TestVolumesRemove(t *testing.T) {
50
+	defer setupTest(t)()
51
+	client := request.NewAPIClient(t)
52
+	ctx := context.Background()
53
+
54
+	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
55
+
56
+	id := container.Create(t, ctx, client, container.WithVolume(prefix+"foo"))
57
+
58
+	c, err := client.ContainerInspect(ctx, id)
59
+	require.NoError(t, err)
60
+	vname := c.Mounts[0].Name
61
+
62
+	err = client.VolumeRemove(ctx, vname, false)
63
+	testutil.ErrorContains(t, err, "volume is in use")
64
+
65
+	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{
66
+		Force: true,
67
+	})
68
+	require.NoError(t, err)
69
+
70
+	err = client.VolumeRemove(ctx, vname, false)
71
+	require.NoError(t, err)
72
+}
73
+
74
+func TestVolumesInspect(t *testing.T) {
75
+	defer setupTest(t)()
76
+	client := request.NewAPIClient(t)
77
+	ctx := context.Background()
78
+
79
+	// sampling current time minus a minute so to now have false positive in case of delays
80
+	now := time.Now().Truncate(time.Minute)
81
+
82
+	name := t.Name()
83
+	_, err := client.VolumeCreate(ctx, volumetypes.VolumesCreateBody{
84
+		Name: name,
85
+	})
86
+	require.NoError(t, err)
87
+
88
+	vol, err := client.VolumeInspect(ctx, name)
89
+	require.NoError(t, err)
90
+
91
+	expected := types.Volume{
92
+		// Ignore timestamp of CreatedAt
93
+		CreatedAt:  vol.CreatedAt,
94
+		Driver:     "local",
95
+		Scope:      "local",
96
+		Name:       name,
97
+		Options:    map[string]string{},
98
+		Mountpoint: fmt.Sprintf("%s/volumes/%s/_data", testEnv.DaemonInfo.DockerRootDir, name),
99
+	}
100
+	assert.Equal(t, vol, expected)
101
+
102
+	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
103
+	testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
104
+	require.NoError(t, err)
105
+	testCreatedAt = testCreatedAt.Truncate(time.Minute)
106
+	assert.Equal(t, testCreatedAt.Equal(now), true, "Time Volume is CreatedAt not equal to current time")
107
+}
108
+
109
+func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
110
+	if testEnv.OSType == "windows" {
111
+		return "c:", `\`
112
+	}
113
+	return "", "/"
114
+}