docker_api_volumes_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "path/filepath"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestVolumesApiList(c *check.C) {
  11. prefix := ""
  12. if daemonPlatform == "windows" {
  13. prefix = "c:"
  14. testRequires(c, WindowsDaemonSupportsVolumes)
  15. }
  16. dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "busybox")
  17. status, b, err := sockRequest("GET", "/volumes", nil)
  18. c.Assert(err, checker.IsNil)
  19. c.Assert(status, checker.Equals, http.StatusOK)
  20. var volumes types.VolumesListResponse
  21. c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
  22. c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
  23. }
  24. func (s *DockerSuite) TestVolumesApiCreate(c *check.C) {
  25. if daemonPlatform == "windows" {
  26. testRequires(c, WindowsDaemonSupportsVolumes)
  27. }
  28. config := types.VolumeCreateRequest{
  29. Name: "test",
  30. }
  31. status, b, err := sockRequest("POST", "/volumes/create", config)
  32. c.Assert(err, check.IsNil)
  33. c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b)))
  34. var vol types.Volume
  35. err = json.Unmarshal(b, &vol)
  36. c.Assert(err, checker.IsNil)
  37. c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
  38. }
  39. func (s *DockerSuite) TestVolumesApiRemove(c *check.C) {
  40. prefix := ""
  41. if daemonPlatform == "windows" {
  42. testRequires(c, WindowsDaemonSupportsVolumes)
  43. prefix = "c:"
  44. }
  45. dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "--name=test", "busybox")
  46. status, b, err := sockRequest("GET", "/volumes", nil)
  47. c.Assert(err, checker.IsNil)
  48. c.Assert(status, checker.Equals, http.StatusOK)
  49. var volumes types.VolumesListResponse
  50. c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
  51. c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
  52. v := volumes.Volumes[0]
  53. status, _, err = sockRequest("DELETE", "/volumes/"+v.Name, nil)
  54. c.Assert(err, checker.IsNil)
  55. c.Assert(status, checker.Equals, http.StatusConflict, check.Commentf("Should not be able to remove a volume that is in use"))
  56. dockerCmd(c, "rm", "-f", "test")
  57. status, data, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
  58. c.Assert(err, checker.IsNil)
  59. c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf(string(data)))
  60. }
  61. func (s *DockerSuite) TestVolumesApiInspect(c *check.C) {
  62. if daemonPlatform == "windows" {
  63. testRequires(c, WindowsDaemonSupportsVolumes)
  64. }
  65. config := types.VolumeCreateRequest{
  66. Name: "test",
  67. }
  68. status, b, err := sockRequest("POST", "/volumes/create", config)
  69. c.Assert(err, check.IsNil)
  70. c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b)))
  71. status, b, err = sockRequest("GET", "/volumes", nil)
  72. c.Assert(err, checker.IsNil)
  73. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b)))
  74. var volumes types.VolumesListResponse
  75. c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
  76. c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
  77. var vol types.Volume
  78. status, b, err = sockRequest("GET", "/volumes/"+config.Name, nil)
  79. c.Assert(err, checker.IsNil)
  80. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b)))
  81. c.Assert(json.Unmarshal(b, &vol), checker.IsNil)
  82. c.Assert(vol.Name, checker.Equals, config.Name)
  83. }