github.com/lacework-dev/go-moby@v20.10.12+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  		t.Run(ep, func(t *testing.T) {
   114  			t.Parallel()
   115  
   116  			res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
   117  			assert.NilError(t, err)
   118  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
   119  
   120  			buf, err := request.ReadBody(body)
   121  			assert.NilError(t, err)
   122  			assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
   123  
   124  			res, body, err = request.Post(ep, request.JSON)
   125  			assert.NilError(t, err)
   126  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
   127  
   128  			buf, err = request.ReadBody(body)
   129  			assert.NilError(t, err)
   130  			assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
   131  		})
   132  	}
   133  }
   134  
   135  func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
   136  	if testEnv.OSType == "windows" {
   137  		return "c:", `\`
   138  	}
   139  	return "", "/"
   140  }