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