github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/integration/volume/volume_test.go (about)

     1  package volume
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/filters"
    13  	volumetypes "github.com/docker/docker/api/types/volume"
    14  	"github.com/docker/docker/integration/internal/container"
    15  	"github.com/docker/docker/testutil/request"
    16  	"github.com/google/go-cmp/cmp/cmpopts"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  )
    20  
    21  func TestVolumesCreateAndList(t *testing.T) {
    22  	defer setupTest(t)()
    23  	client := testEnv.APIClient()
    24  	ctx := context.Background()
    25  
    26  	name := t.Name()
    27  	// Windows file system is case insensitive
    28  	if testEnv.OSType == "windows" {
    29  		name = strings.ToLower(name)
    30  	}
    31  	vol, err := client.VolumeCreate(ctx, volumetypes.VolumeCreateBody{
    32  		Name: name,
    33  	})
    34  	assert.NilError(t, err)
    35  
    36  	expected := types.Volume{
    37  		// Ignore timestamp of CreatedAt
    38  		CreatedAt:  vol.CreatedAt,
    39  		Driver:     "local",
    40  		Scope:      "local",
    41  		Name:       name,
    42  		Mountpoint: filepath.Join(testEnv.DaemonInfo.DockerRootDir, "volumes", name, "_data"),
    43  	}
    44  	assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty()))
    45  
    46  	volList, err := client.VolumeList(ctx, filters.Args{})
    47  	assert.NilError(t, err)
    48  	assert.Assert(t, len(volList.Volumes) > 0)
    49  
    50  	volumes := volList.Volumes[:0]
    51  	for _, v := range volList.Volumes {
    52  		if v.Name == vol.Name {
    53  			volumes = append(volumes, v)
    54  		}
    55  	}
    56  
    57  	assert.Check(t, is.Equal(len(volumes), 1))
    58  	assert.Check(t, volumes[0] != nil)
    59  	assert.Check(t, is.DeepEqual(*volumes[0], expected, cmpopts.EquateEmpty()))
    60  }
    61  
    62  func TestVolumesRemove(t *testing.T) {
    63  	defer setupTest(t)()
    64  	client := testEnv.APIClient()
    65  	ctx := context.Background()
    66  
    67  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
    68  
    69  	id := container.Create(ctx, t, client, container.WithVolume(prefix+slash+"foo"))
    70  
    71  	c, err := client.ContainerInspect(ctx, id)
    72  	assert.NilError(t, err)
    73  	vname := c.Mounts[0].Name
    74  
    75  	err = client.VolumeRemove(ctx, vname, false)
    76  	assert.Check(t, is.ErrorContains(err, "volume is in use"))
    77  
    78  	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{
    79  		Force: true,
    80  	})
    81  	assert.NilError(t, err)
    82  
    83  	err = client.VolumeRemove(ctx, vname, false)
    84  	assert.NilError(t, err)
    85  }
    86  
    87  func TestVolumesInspect(t *testing.T) {
    88  	defer setupTest(t)()
    89  	client := testEnv.APIClient()
    90  	ctx := context.Background()
    91  
    92  	now := time.Now()
    93  	vol, err := client.VolumeCreate(ctx, volumetypes.VolumeCreateBody{})
    94  	assert.NilError(t, err)
    95  
    96  	inspected, err := client.VolumeInspect(ctx, vol.Name)
    97  	assert.NilError(t, err)
    98  
    99  	assert.Check(t, is.DeepEqual(inspected, vol, cmpopts.EquateEmpty()))
   100  
   101  	// comparing CreatedAt field time for the new volume to now. Truncate to 1 minute precision to avoid false positive
   102  	createdAt, err := time.Parse(time.RFC3339, strings.TrimSpace(inspected.CreatedAt))
   103  	assert.NilError(t, err)
   104  	assert.Check(t, createdAt.Unix()-now.Unix() < 60, "CreatedAt (%s) exceeds creation time (%s) 60s", createdAt, now)
   105  }
   106  
   107  func TestVolumesInvalidJSON(t *testing.T) {
   108  	defer setupTest(t)()
   109  
   110  	endpoints := []string{"/volumes/create"}
   111  
   112  	for _, ep := range endpoints {
   113  		ep := ep
   114  		t.Run(ep, func(t *testing.T) {
   115  			t.Parallel()
   116  
   117  			res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
   118  			assert.NilError(t, err)
   119  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
   120  
   121  			buf, err := request.ReadBody(body)
   122  			assert.NilError(t, err)
   123  			assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
   124  
   125  			res, body, err = request.Post(ep, request.JSON)
   126  			assert.NilError(t, err)
   127  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
   128  
   129  			buf, err = request.ReadBody(body)
   130  			assert.NilError(t, err)
   131  			assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
   132  		})
   133  	}
   134  }
   135  
   136  func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
   137  	if testEnv.OSType == "windows" {
   138  		return "c:", `\`
   139  	}
   140  	return "", "/"
   141  }