Fix volume plugin serialization.

Unmarshal errors into strings.
Fix `omit` typos.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-06-09 10:23:01 -07:00
Родитель 3b01dac44d
Коммит 30448166de
3 изменённых файлов: 107 добавлений и 15 удалений

Просмотреть файл

@ -61,21 +61,21 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
s.ec.activations++
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Implements": ["VolumeDriver"]}`)
})
mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
s.ec.creations++
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{}`)
})
mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
s.ec.removals++
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{}`)
})
@ -89,7 +89,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
p := hostVolumePath(pr.name)
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", p))
})
@ -110,7 +110,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
http.Error(w, err.Error(), 500)
}
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", p))
})
@ -127,7 +127,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
http.Error(w, err.Error(), 500)
}
w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{}`)
})

Просмотреть файл

@ -10,8 +10,8 @@ type volumeDriverRequest struct {
}
type volumeDriverResponse struct {
Mountpoint string `json:",ommitempty"`
Err error `json:",ommitempty"`
Mountpoint string `json:",omitempty"`
Err string `json:",omitempty"`
}
type volumeDriverProxy struct {
@ -23,7 +23,7 @@ func (pp *volumeDriverProxy) Create(name string) error {
var ret volumeDriverResponse
err := pp.c.Call("VolumeDriver.Create", args, &ret)
if err != nil {
return pp.fmtError(name, err)
return pp.fmtError(name, err.Error())
}
return pp.fmtError(name, ret.Err)
}
@ -33,7 +33,7 @@ func (pp *volumeDriverProxy) Remove(name string) error {
var ret volumeDriverResponse
err := pp.c.Call("VolumeDriver.Remove", args, &ret)
if err != nil {
return pp.fmtError(name, err)
return pp.fmtError(name, err.Error())
}
return pp.fmtError(name, ret.Err)
}
@ -42,7 +42,7 @@ func (pp *volumeDriverProxy) Path(name string) (string, error) {
args := volumeDriverRequest{name}
var ret volumeDriverResponse
if err := pp.c.Call("VolumeDriver.Path", args, &ret); err != nil {
return "", pp.fmtError(name, err)
return "", pp.fmtError(name, err.Error())
}
return ret.Mountpoint, pp.fmtError(name, ret.Err)
}
@ -51,7 +51,7 @@ func (pp *volumeDriverProxy) Mount(name string) (string, error) {
args := volumeDriverRequest{name}
var ret volumeDriverResponse
if err := pp.c.Call("VolumeDriver.Mount", args, &ret); err != nil {
return "", pp.fmtError(name, err)
return "", pp.fmtError(name, err.Error())
}
return ret.Mountpoint, pp.fmtError(name, ret.Err)
}
@ -61,13 +61,13 @@ func (pp *volumeDriverProxy) Unmount(name string) error {
var ret volumeDriverResponse
err := pp.c.Call("VolumeDriver.Unmount", args, &ret)
if err != nil {
return pp.fmtError(name, err)
return pp.fmtError(name, err.Error())
}
return pp.fmtError(name, ret.Err)
}
func (pp *volumeDriverProxy) fmtError(name string, err error) error {
if err == nil {
func (pp *volumeDriverProxy) fmtError(name string, err string) error {
if len(err) == 0 {
return nil
}
return fmt.Errorf("External volume driver request failed for %s: %v", name, err)

Просмотреть файл

@ -0,0 +1,92 @@
package volumedrivers
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/docker/docker/pkg/plugins"
)
func TestVolumeRequestError(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot create volume"}`)
})
mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`)
})
mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`)
})
mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`)
})
mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Unknown volume"}`)
})
u, _ := url.Parse(server.URL)
client := plugins.NewClient("tcp://" + u.Host)
driver := volumeDriverProxy{client}
err := driver.Create("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot create volume") {
t.Fatalf("Unexpected error: %v\n", err)
}
_, err = driver.Mount("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot mount volume") {
t.Fatalf("Unexpected error: %v\n", err)
}
err = driver.Unmount("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot unmount volume") {
t.Fatalf("Unexpected error: %v\n", err)
}
err = driver.Remove("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot remove volume") {
t.Fatalf("Unexpected error: %v\n", err)
}
_, err = driver.Path("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Unknown volume") {
t.Fatalf("Unexpected error: %v\n", err)
}
}