docker_api_volumes_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/api/types/filters"
  8. volumetypes "github.com/docker/docker/api/types/volume"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration-cli/checker"
  11. "github.com/go-check/check"
  12. "golang.org/x/net/context"
  13. )
  14. func (s *DockerSuite) TestVolumesAPIList(c *check.C) {
  15. prefix, _ := getPrefixAndSlashFromDaemonPlatform()
  16. cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "busybox")
  17. cli, err := client.NewEnvClient()
  18. c.Assert(err, checker.IsNil)
  19. defer cli.Close()
  20. container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
  21. c.Assert(err, checker.IsNil)
  22. vname := container.Mounts[0].Name
  23. volumes, err := cli.VolumeList(context.Background(), filters.Args{})
  24. c.Assert(err, checker.IsNil)
  25. found := false
  26. for _, vol := range volumes.Volumes {
  27. if vol.Name == vname {
  28. found = true
  29. break
  30. }
  31. }
  32. c.Assert(found, checker.Equals, true)
  33. }
  34. func (s *DockerSuite) TestVolumesAPICreate(c *check.C) {
  35. config := volumetypes.VolumesCreateBody{
  36. Name: "test",
  37. }
  38. cli, err := client.NewEnvClient()
  39. c.Assert(err, checker.IsNil)
  40. defer cli.Close()
  41. vol, err := cli.VolumeCreate(context.Background(), config)
  42. c.Assert(err, check.IsNil)
  43. c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
  44. }
  45. func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) {
  46. prefix, _ := getPrefixAndSlashFromDaemonPlatform()
  47. cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "--name=test", "busybox")
  48. cli, err := client.NewEnvClient()
  49. c.Assert(err, checker.IsNil)
  50. defer cli.Close()
  51. container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
  52. c.Assert(err, checker.IsNil)
  53. vname := container.Mounts[0].Name
  54. err = cli.VolumeRemove(context.Background(), vname, false)
  55. c.Assert(err.Error(), checker.Contains, "volume is in use")
  56. dockerCmd(c, "rm", "-f", "test")
  57. err = cli.VolumeRemove(context.Background(), vname, false)
  58. c.Assert(err, checker.IsNil)
  59. }
  60. func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) {
  61. config := volumetypes.VolumesCreateBody{
  62. Name: "test",
  63. }
  64. // sampling current time minus a minute so to now have false positive in case of delays
  65. now := time.Now().Truncate(time.Minute)
  66. cli, err := client.NewEnvClient()
  67. c.Assert(err, checker.IsNil)
  68. defer cli.Close()
  69. _, err = cli.VolumeCreate(context.Background(), config)
  70. c.Assert(err, check.IsNil)
  71. vol, err := cli.VolumeInspect(context.Background(), config.Name)
  72. c.Assert(err, checker.IsNil)
  73. c.Assert(vol.Name, checker.Equals, config.Name)
  74. // comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
  75. testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
  76. c.Assert(err, check.IsNil)
  77. testCreatedAt = testCreatedAt.Truncate(time.Minute)
  78. if !testCreatedAt.Equal(now) {
  79. c.Assert(fmt.Errorf("Time Volume is CreatedAt not equal to current time"), check.NotNil)
  80. }
  81. }