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