volume_test.go 3.8 KB

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