Browse code

Fix volume plugin serialization.

Unmarshal errors into strings.
Fix `omit` typos.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/06/10 02:23:01
Showing 3 changed files
... ...
@@ -61,21 +61,21 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
61 61
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
62 62
 		s.ec.activations++
63 63
 
64
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
64
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
65 65
 		fmt.Fprintln(w, `{"Implements": ["VolumeDriver"]}`)
66 66
 	})
67 67
 
68 68
 	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
69 69
 		s.ec.creations++
70 70
 
71
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
71
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
72 72
 		fmt.Fprintln(w, `{}`)
73 73
 	})
74 74
 
75 75
 	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
76 76
 		s.ec.removals++
77 77
 
78
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
78
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
79 79
 		fmt.Fprintln(w, `{}`)
80 80
 	})
81 81
 
... ...
@@ -89,7 +89,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
89 89
 
90 90
 		p := hostVolumePath(pr.name)
91 91
 
92
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
92
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
93 93
 		fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", p))
94 94
 	})
95 95
 
... ...
@@ -110,7 +110,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
110 110
 			http.Error(w, err.Error(), 500)
111 111
 		}
112 112
 
113
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
113
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
114 114
 		fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", p))
115 115
 	})
116 116
 
... ...
@@ -127,7 +127,7 @@ func (s *DockerExternalVolumeSuite) SetUpSuite(c *check.C) {
127 127
 			http.Error(w, err.Error(), 500)
128 128
 		}
129 129
 
130
-		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
130
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
131 131
 		fmt.Fprintln(w, `{}`)
132 132
 	})
133 133
 
... ...
@@ -10,8 +10,8 @@ type volumeDriverRequest struct {
10 10
 }
11 11
 
12 12
 type volumeDriverResponse struct {
13
-	Mountpoint string `json:",ommitempty"`
14
-	Err        error  `json:",ommitempty"`
13
+	Mountpoint string `json:",omitempty"`
14
+	Err        string `json:",omitempty"`
15 15
 }
16 16
 
17 17
 type volumeDriverProxy struct {
... ...
@@ -23,7 +23,7 @@ func (pp *volumeDriverProxy) Create(name string) error {
23 23
 	var ret volumeDriverResponse
24 24
 	err := pp.c.Call("VolumeDriver.Create", args, &ret)
25 25
 	if err != nil {
26
-		return pp.fmtError(name, err)
26
+		return pp.fmtError(name, err.Error())
27 27
 	}
28 28
 	return pp.fmtError(name, ret.Err)
29 29
 }
... ...
@@ -33,7 +33,7 @@ func (pp *volumeDriverProxy) Remove(name string) error {
33 33
 	var ret volumeDriverResponse
34 34
 	err := pp.c.Call("VolumeDriver.Remove", args, &ret)
35 35
 	if err != nil {
36
-		return pp.fmtError(name, err)
36
+		return pp.fmtError(name, err.Error())
37 37
 	}
38 38
 	return pp.fmtError(name, ret.Err)
39 39
 }
... ...
@@ -42,7 +42,7 @@ func (pp *volumeDriverProxy) Path(name string) (string, error) {
42 42
 	args := volumeDriverRequest{name}
43 43
 	var ret volumeDriverResponse
44 44
 	if err := pp.c.Call("VolumeDriver.Path", args, &ret); err != nil {
45
-		return "", pp.fmtError(name, err)
45
+		return "", pp.fmtError(name, err.Error())
46 46
 	}
47 47
 	return ret.Mountpoint, pp.fmtError(name, ret.Err)
48 48
 }
... ...
@@ -51,7 +51,7 @@ func (pp *volumeDriverProxy) Mount(name string) (string, error) {
51 51
 	args := volumeDriverRequest{name}
52 52
 	var ret volumeDriverResponse
53 53
 	if err := pp.c.Call("VolumeDriver.Mount", args, &ret); err != nil {
54
-		return "", pp.fmtError(name, err)
54
+		return "", pp.fmtError(name, err.Error())
55 55
 	}
56 56
 	return ret.Mountpoint, pp.fmtError(name, ret.Err)
57 57
 }
... ...
@@ -61,13 +61,13 @@ func (pp *volumeDriverProxy) Unmount(name string) error {
61 61
 	var ret volumeDriverResponse
62 62
 	err := pp.c.Call("VolumeDriver.Unmount", args, &ret)
63 63
 	if err != nil {
64
-		return pp.fmtError(name, err)
64
+		return pp.fmtError(name, err.Error())
65 65
 	}
66 66
 	return pp.fmtError(name, ret.Err)
67 67
 }
68 68
 
69
-func (pp *volumeDriverProxy) fmtError(name string, err error) error {
70
-	if err == nil {
69
+func (pp *volumeDriverProxy) fmtError(name string, err string) error {
70
+	if len(err) == 0 {
71 71
 		return nil
72 72
 	}
73 73
 	return fmt.Errorf("External volume driver request failed for %s: %v", name, err)
74 74
new file mode 100644
... ...
@@ -0,0 +1,92 @@
0
+package volumedrivers
1
+
2
+import (
3
+	"fmt"
4
+	"net/http"
5
+	"net/http/httptest"
6
+	"net/url"
7
+	"strings"
8
+	"testing"
9
+
10
+	"github.com/docker/docker/pkg/plugins"
11
+)
12
+
13
+func TestVolumeRequestError(t *testing.T) {
14
+	mux := http.NewServeMux()
15
+	server := httptest.NewServer(mux)
16
+	defer server.Close()
17
+
18
+	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
19
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
20
+		fmt.Fprintln(w, `{"Err": "Cannot create volume"}`)
21
+	})
22
+
23
+	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
24
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
25
+		fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`)
26
+	})
27
+
28
+	mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
29
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
30
+		fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`)
31
+	})
32
+
33
+	mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
34
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
35
+		fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`)
36
+	})
37
+
38
+	mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
39
+		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
40
+		fmt.Fprintln(w, `{"Err": "Unknown volume"}`)
41
+	})
42
+
43
+	u, _ := url.Parse(server.URL)
44
+	client := plugins.NewClient("tcp://" + u.Host)
45
+	driver := volumeDriverProxy{client}
46
+
47
+	err := driver.Create("volume")
48
+	if err == nil {
49
+		t.Fatal("Expected error, was nil")
50
+	}
51
+
52
+	if !strings.Contains(err.Error(), "Cannot create volume") {
53
+		t.Fatalf("Unexpected error: %v\n", err)
54
+	}
55
+
56
+	_, err = driver.Mount("volume")
57
+	if err == nil {
58
+		t.Fatal("Expected error, was nil")
59
+	}
60
+
61
+	if !strings.Contains(err.Error(), "Cannot mount volume") {
62
+		t.Fatalf("Unexpected error: %v\n", err)
63
+	}
64
+
65
+	err = driver.Unmount("volume")
66
+	if err == nil {
67
+		t.Fatal("Expected error, was nil")
68
+	}
69
+
70
+	if !strings.Contains(err.Error(), "Cannot unmount volume") {
71
+		t.Fatalf("Unexpected error: %v\n", err)
72
+	}
73
+
74
+	err = driver.Remove("volume")
75
+	if err == nil {
76
+		t.Fatal("Expected error, was nil")
77
+	}
78
+
79
+	if !strings.Contains(err.Error(), "Cannot remove volume") {
80
+		t.Fatalf("Unexpected error: %v\n", err)
81
+	}
82
+
83
+	_, err = driver.Path("volume")
84
+	if err == nil {
85
+		t.Fatal("Expected error, was nil")
86
+	}
87
+
88
+	if !strings.Contains(err.Error(), "Unknown volume") {
89
+		t.Fatalf("Unexpected error: %v\n", err)
90
+	}
91
+}