Explorar o código

Merge pull request #13828 from calavera/plugin_fixes

Fix volume plugin serialization.
Alexander Morozov %!s(int64=10) %!d(string=hai) anos
pai
achega
5e86b818ed

+ 6 - 6
integration-cli/docker_cli_start_volume_driver_unix_test.go

@@ -61,21 +61,21 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
 		s.ec.activations++
 		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"]}`)
 		fmt.Fprintln(w, `{"Implements": ["VolumeDriver"]}`)
 	})
 	})
 
 
 	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
 	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
 		s.ec.creations++
 		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, `{}`)
 		fmt.Fprintln(w, `{}`)
 	})
 	})
 
 
 	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
 	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
 		s.ec.removals++
 		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, `{}`)
 		fmt.Fprintln(w, `{}`)
 	})
 	})
 
 
@@ -89,7 +89,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
 
 
 		p := hostVolumePath(pr.name)
 		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))
 		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)
 			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))
 		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)
 			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.Fprintln(w, `{}`)
 	})
 	})
 
 

+ 9 - 9
volume/drivers/proxy.go

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

+ 92 - 0
volume/drivers/proxy_test.go

@@ -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)
+	}
+}