github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration/volume/volume_test.go (about)

     1  package volume
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/filters"
    12  	volumetypes "github.com/docker/docker/api/types/volume"
    13  	"github.com/docker/docker/integration/internal/container"
    14  	"github.com/docker/docker/integration/internal/request"
    15  	"github.com/docker/docker/internal/testutil"
    16  	"github.com/google/go-cmp/cmp/cmpopts"
    17  	"github.com/gotestyourself/gotestyourself/assert"
    18  	is "github.com/gotestyourself/gotestyourself/assert/cmp"
    19  )
    20  
    21  func TestVolumesCreateAndList(t *testing.T) {
    22  	defer setupTest(t)()
    23  	client := request.NewAPIClient(t)
    24  	ctx := context.Background()
    25  
    26  	name := t.Name()
    27  	vol, err := client.VolumeCreate(ctx, volumetypes.VolumesCreateBody{
    28  		Name: name,
    29  	})
    30  	assert.NilError(t, err)
    31  
    32  	expected := types.Volume{
    33  		// Ignore timestamp of CreatedAt
    34  		CreatedAt:  vol.CreatedAt,
    35  		Driver:     "local",
    36  		Scope:      "local",
    37  		Name:       name,
    38  		Mountpoint: fmt.Sprintf("%s/volumes/%s/_data", testEnv.DaemonInfo.DockerRootDir, name),
    39  	}
    40  	assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty()))
    41  
    42  	volumes, err := client.VolumeList(ctx, filters.Args{})
    43  	assert.NilError(t, err)
    44  
    45  	assert.Check(t, is.Equal(len(volumes.Volumes), 1))
    46  	assert.Check(t, volumes.Volumes[0] != nil)
    47  	assert.Check(t, is.DeepEqual(*volumes.Volumes[0], expected, cmpopts.EquateEmpty()))
    48  }
    49  
    50  func TestVolumesRemove(t *testing.T) {
    51  	defer setupTest(t)()
    52  	client := request.NewAPIClient(t)
    53  	ctx := context.Background()
    54  
    55  	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
    56  
    57  	id := container.Create(t, ctx, client, container.WithVolume(prefix+"foo"))
    58  
    59  	c, err := client.ContainerInspect(ctx, id)
    60  	assert.NilError(t, err)
    61  	vname := c.Mounts[0].Name
    62  
    63  	err = client.VolumeRemove(ctx, vname, false)
    64  	testutil.ErrorContains(t, err, "volume is in use")
    65  
    66  	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{
    67  		Force: true,
    68  	})
    69  	assert.NilError(t, err)
    70  
    71  	err = client.VolumeRemove(ctx, vname, false)
    72  	assert.NilError(t, err)
    73  }
    74  
    75  func TestVolumesInspect(t *testing.T) {
    76  	defer setupTest(t)()
    77  	client := request.NewAPIClient(t)
    78  	ctx := context.Background()
    79  
    80  	// sampling current time minus a minute so to now have false positive in case of delays
    81  	now := time.Now().Truncate(time.Minute)
    82  
    83  	name := t.Name()
    84  	_, err := client.VolumeCreate(ctx, volumetypes.VolumesCreateBody{
    85  		Name: name,
    86  	})
    87  	assert.NilError(t, err)
    88  
    89  	vol, err := client.VolumeInspect(ctx, name)
    90  	assert.NilError(t, err)
    91  
    92  	expected := types.Volume{
    93  		// Ignore timestamp of CreatedAt
    94  		CreatedAt:  vol.CreatedAt,
    95  		Driver:     "local",
    96  		Scope:      "local",
    97  		Name:       name,
    98  		Mountpoint: fmt.Sprintf("%s/volumes/%s/_data", testEnv.DaemonInfo.DockerRootDir, name),
    99  	}
   100  	assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty()))
   101  
   102  	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
   103  	testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
   104  	assert.NilError(t, err)
   105  	testCreatedAt = testCreatedAt.Truncate(time.Minute)
   106  	assert.Check(t, is.Equal(testCreatedAt.Equal(now), true), "Time Volume is CreatedAt not equal to current time")
   107  }
   108  
   109  func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
   110  	if testEnv.OSType == "windows" {
   111  		return "c:", `\`
   112  	}
   113  	return "", "/"
   114  }