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"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/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) {
|
2018-04-25 05:03:43 -04:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
2018-02-12 16:54:12 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
name := t.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
|
|
|
|
|
|
|
volumes, err := client.VolumeList(ctx, filters.Args{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(len(volumes.Volumes), 1))
|
|
|
|
assert.Check(t, volumes.Volumes[0] != nil)
|
2018-03-27 12:13:47 -04:00
|
|
|
assert.Check(t, is.DeepEqual(*volumes.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)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-04 17:15:00 -04:00
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
2018-02-12 16:54:12 -05:00
|
|
|
|
2018-05-04 17:15:00 -04:00
|
|
|
id := container.Create(t, ctx, 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) {
|
2018-05-04 17:15:00 -04:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
2018-02-12 16:54:12 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
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)
|
2018-11-02 04:49:50 -04:00
|
|
|
assert.Check(t, createdAt.Truncate(time.Minute).Equal(now.Truncate(time.Minute)), "CreatedAt (%s) not equal to creation time (%s)", 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 "", "/"
|
|
|
|
}
|