github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/volume_create_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 volumetypes "github.com/docker/docker/api/types/volume" 14 "golang.org/x/net/context" 15 ) 16 17 func TestVolumeCreateError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 22 _, err := client.VolumeCreate(context.Background(), volumetypes.VolumesCreateBody{}) 23 if err == nil || err.Error() != "Error response from daemon: Server error" { 24 t.Fatalf("expected a Server Error, got %v", err) 25 } 26 } 27 28 func TestVolumeCreate(t *testing.T) { 29 expectedURL := "/volumes/create" 30 31 client := &Client{ 32 client: newMockClient(func(req *http.Request) (*http.Response, error) { 33 if !strings.HasPrefix(req.URL.Path, expectedURL) { 34 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 35 } 36 37 if req.Method != "POST" { 38 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 39 } 40 41 content, err := json.Marshal(types.Volume{ 42 Name: "volume", 43 Driver: "local", 44 Mountpoint: "mountpoint", 45 }) 46 if err != nil { 47 return nil, err 48 } 49 return &http.Response{ 50 StatusCode: http.StatusOK, 51 Body: ioutil.NopCloser(bytes.NewReader(content)), 52 }, nil 53 }), 54 } 55 56 volume, err := client.VolumeCreate(context.Background(), volumetypes.VolumesCreateBody{ 57 Name: "myvolume", 58 Driver: "mydriver", 59 DriverOpts: map[string]string{ 60 "opt-key": "opt-value", 61 }, 62 }) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if volume.Name != "volume" { 67 t.Fatalf("expected volume.Name to be 'volume', got %s", volume.Name) 68 } 69 if volume.Driver != "local" { 70 t.Fatalf("expected volume.Driver to be 'local', got %s", volume.Driver) 71 } 72 if volume.Mountpoint != "mountpoint" { 73 t.Fatalf("expected volume.Mountpoint to be 'mountpoint', got %s", volume.Mountpoint) 74 } 75 }