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"
|
2022-03-18 11:33:43 -04:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2022-09-28 15:43:45 -04:00
|
|
|
clientpkg "github.com/docker/docker/client"
|
2018-02-12 16:54:12 -05:00
|
|
|
"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"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2022-09-28 15:43:45 -04:00
|
|
|
"gotest.tools/v3/assert/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
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)
|
|
|
|
}
|
2022-03-05 17:25:55 -05:00
|
|
|
vol, err := client.VolumeCreate(ctx, volume.CreateOptions{
|
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
|
|
|
|
2022-03-18 11:33:43 -04:00
|
|
|
expected := volume.Volume{
|
2018-02-12 16:54:12 -05:00
|
|
|
// 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) {
|
|
|
|
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()
|
2022-03-05 17:25:55 -05:00
|
|
|
vol, err := client.VolumeCreate(ctx, volume.CreateOptions{})
|
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
|
|
|
}
|
|
|
|
|
2022-04-05 05:43:06 -04:00
|
|
|
// TestVolumesInvalidJSON tests that POST endpoints that expect a body return
|
|
|
|
// the correct error when sending invalid JSON requests.
|
2018-11-05 08:50:33 -05:00
|
|
|
func TestVolumesInvalidJSON(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
2022-04-05 05:43:06 -04:00
|
|
|
// POST endpoints that accept / expect a JSON body;
|
2018-11-05 08:50:33 -05:00
|
|
|
endpoints := []string{"/volumes/create"}
|
|
|
|
|
|
|
|
for _, ep := range endpoints {
|
2021-07-22 16:41:01 -04:00
|
|
|
ep := ep
|
2022-04-05 05:43:06 -04:00
|
|
|
t.Run(ep[1:], func(t *testing.T) {
|
2018-11-05 08:50:33 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-05 05:43:06 -04:00
|
|
|
t.Run("invalid content type", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString("{}"), request.ContentType("text/plain"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "unsupported Content-Type header (text/plain): must be 'application/json'"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid JSON", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "invalid JSON: invalid character 'i' looking for beginning of object key string"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("extra content after JSON", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString(`{} trailing content`), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "unexpected content after JSON"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty body", func(t *testing.T) {
|
|
|
|
// empty body should not produce an 500 internal server error, or
|
|
|
|
// any 5XX error (this is assuming the request does not produce
|
|
|
|
// an internal server error for another reason, but it shouldn't)
|
|
|
|
res, _, err := request.Post(ep, request.RawString(``), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, res.StatusCode < http.StatusInternalServerError)
|
|
|
|
})
|
2018-11-05 08:50:33 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:12 -05:00
|
|
|
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
return "c:", `\`
|
|
|
|
}
|
|
|
|
return "", "/"
|
|
|
|
}
|
2022-09-28 15:43:45 -04:00
|
|
|
|
|
|
|
func TestVolumePruneAnonymous(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
client := testEnv.APIClient()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Create an anonymous volume
|
|
|
|
v, err := client.VolumeCreate(ctx, volume.CreateOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Create a named volume
|
|
|
|
vNamed, err := client.VolumeCreate(ctx, volume.CreateOptions{
|
|
|
|
Name: "test",
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Prune anonymous volumes
|
|
|
|
pruneReport, err := client.VolumesPrune(ctx, filters.Args{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 1))
|
|
|
|
assert.Check(t, is.Equal(pruneReport.VolumesDeleted[0], v.Name))
|
|
|
|
|
|
|
|
_, err = client.VolumeInspect(ctx, vNamed.Name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Prune all volumes
|
|
|
|
_, err = client.VolumeCreate(ctx, volume.CreateOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
pruneReport, err = client.VolumesPrune(ctx, filters.NewArgs(filters.Arg("all", "1")))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 2))
|
|
|
|
|
|
|
|
// Validate that older API versions still have the old behavior of pruning all local volumes
|
|
|
|
clientOld, err := clientpkg.NewClientWithOpts(clientpkg.FromEnv, clientpkg.WithVersion("1.41"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer clientOld.Close()
|
|
|
|
assert.Equal(t, clientOld.ClientVersion(), "1.41")
|
|
|
|
|
|
|
|
v, err = client.VolumeCreate(ctx, volume.CreateOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
vNamed, err = client.VolumeCreate(ctx, volume.CreateOptions{Name: "test-api141"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
pruneReport, err = clientOld.VolumesPrune(ctx, filters.Args{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 2))
|
|
|
|
assert.Check(t, cmp.Contains(pruneReport.VolumesDeleted, v.Name))
|
|
|
|
assert.Check(t, cmp.Contains(pruneReport.VolumesDeleted, vNamed.Name))
|
|
|
|
}
|