2018-02-12 16:54:12 -05:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-05 08:50:33 -05:00
|
|
|
"net/http"
|
2018-11-02 04:50:50 -04:00
|
|
|
"path/filepath"
|
2018-02-12 16:54:12 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2018-03-27 12:13:47 -04:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2018-02-12 16:54:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVolumesCreateAndList(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-12 16:54:12 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
name := t.Name()
|
2019-05-14 16:01:12 -04:00
|
|
|
// Windows file system is case insensitive
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
}
|
2018-05-14 11:03:48 -04:00
|
|
|
vol, err := client.VolumeCreate(ctx, volumetypes.VolumeCreateBody{
|
2018-02-12 16:54:12 -05:00
|
|
|
Name: name,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
|
|
|
expected := types.Volume{
|
|
|
|
// Ignore timestamp of CreatedAt
|
|
|
|
CreatedAt: vol.CreatedAt,
|
|
|
|
Driver: "local",
|
|
|
|
Scope: "local",
|
|
|
|
Name: name,
|
2018-11-02 04:50:50 -04:00
|
|
|
Mountpoint: filepath.Join(testEnv.DaemonInfo.DockerRootDir, "volumes", name, "_data"),
|
2018-02-12 16:54:12 -05:00
|
|
|
}
|
2018-03-27 12:13:47 -04:00
|
|
|
assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty()))
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2019-04-19 13:05:18 -04:00
|
|
|
volList, err := client.VolumeList(ctx, filters.Args{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-04-19 13:05:18 -04:00
|
|
|
assert.Assert(t, len(volList.Volumes) > 0)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2019-04-19 13:05:18 -04:00
|
|
|
volumes := volList.Volumes[:0]
|
|
|
|
for _, v := range volList.Volumes {
|
|
|
|
if v.Name == vol.Name {
|
|
|
|
volumes = append(volumes, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Check(t, is.Equal(len(volumes), 1))
|
|
|
|
assert.Check(t, volumes[0] != nil)
|
|
|
|
assert.Check(t, is.DeepEqual(*volumes[0], expected, cmpopts.EquateEmpty()))
|
2018-02-12 16:54:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumesRemove(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2018-02-12 16:54:12 -05:00
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-12 16:54:12 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-04 17:15:00 -04:00
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2019-06-06 07:00:37 -04:00
|
|
|
id := container.Create(ctx, t, client, container.WithVolume(prefix+slash+"foo"))
|
2018-02-12 16:54:12 -05:00
|
|
|
|
|
|
|
c, err := client.ContainerInspect(ctx, id)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
vname := c.Mounts[0].Name
|
|
|
|
|
|
|
|
err = client.VolumeRemove(ctx, vname, false)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "volume is in use"))
|
2018-02-12 16:54:12 -05:00
|
|
|
|
|
|
|
err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
|
|
|
err = client.VolumeRemove(ctx, vname, false)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumesInspect(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-12 16:54:12 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-11-02 04:49:50 -04:00
|
|
|
now := time.Now()
|
|
|
|
vol, err := client.VolumeCreate(ctx, volumetypes.VolumeCreateBody{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2018-11-02 04:49:50 -04:00
|
|
|
inspected, err := client.VolumeInspect(ctx, vol.Name)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2018-11-02 04:49:50 -04:00
|
|
|
assert.Check(t, is.DeepEqual(inspected, vol, cmpopts.EquateEmpty()))
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2018-11-02 04:49:50 -04:00
|
|
|
// comparing CreatedAt field time for the new volume to now. Truncate to 1 minute precision to avoid false positive
|
|
|
|
createdAt, err := time.Parse(time.RFC3339, strings.TrimSpace(inspected.CreatedAt))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-09-01 22:36:37 -04:00
|
|
|
assert.Check(t, createdAt.Unix()-now.Unix() < 60, "CreatedAt (%s) exceeds creation time (%s) 60s", createdAt, now)
|
2018-02-12 16:54:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-05 08:50:33 -05:00
|
|
|
func TestVolumesInvalidJSON(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
endpoints := []string{"/volumes/create"}
|
|
|
|
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
t.Run(ep, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, res.StatusCode, http.StatusBadRequest)
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
|
|
|
|
|
|
|
|
res, body, err = request.Post(ep, request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, res.StatusCode, http.StatusBadRequest)
|
|
|
|
|
|
|
|
buf, err = request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:12 -05:00
|
|
|
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
return "c:", `\`
|
|
|
|
}
|
|
|
|
return "", "/"
|
|
|
|
}
|