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