Browse code

authz: cleanups

Signed-off-by: Antonio Murdaca <runcom@redhat.com>

Antonio Murdaca authored on 2016/06/13 00:23:19
Showing 3 changed files
... ...
@@ -76,7 +76,6 @@ func (s *DockerAuthzSuite) TearDownTest(c *check.C) {
76 76
 func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
77 77
 	mux := http.NewServeMux()
78 78
 	s.server = httptest.NewServer(mux)
79
-	c.Assert(s.server, check.NotNil, check.Commentf("Failed to start an HTTP Server"))
80 79
 
81 80
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
82 81
 		b, err := json.Marshal(plugins.Manifest{Implements: []string{authorization.AuthZApiImplements}})
... ...
@@ -6,6 +6,7 @@
6 6
 package authorization
7 7
 
8 8
 import (
9
+	"bytes"
9 10
 	"encoding/json"
10 11
 	"io/ioutil"
11 12
 	"net"
... ...
@@ -14,17 +15,17 @@ import (
14 14
 	"os"
15 15
 	"path"
16 16
 	"reflect"
17
-	"testing"
18
-
19
-	"bytes"
20 17
 	"strings"
18
+	"testing"
21 19
 
22 20
 	"github.com/docker/docker/pkg/plugins"
23 21
 	"github.com/docker/go-connections/tlsconfig"
24 22
 	"github.com/gorilla/mux"
25 23
 )
26 24
 
27
-const pluginAddress = "authzplugin.sock"
25
+const (
26
+	pluginAddress = "authz-test-plugin.sock"
27
+)
28 28
 
29 29
 func TestAuthZRequestPluginError(t *testing.T) {
30 30
 	server := authZPluginTestServer{t: t}
... ...
@@ -36,7 +37,7 @@ func TestAuthZRequestPluginError(t *testing.T) {
36 36
 	request := Request{
37 37
 		User:           "user",
38 38
 		RequestBody:    []byte("sample body"),
39
-		RequestURI:     "www.authz.com",
39
+		RequestURI:     "www.authz.com/auth",
40 40
 		RequestMethod:  "GET",
41 41
 		RequestHeaders: map[string]string{"header": "value"},
42 42
 	}
... ...
@@ -50,10 +51,10 @@ func TestAuthZRequestPluginError(t *testing.T) {
50 50
 	}
51 51
 
52 52
 	if !reflect.DeepEqual(server.replayResponse, *actualResponse) {
53
-		t.Fatalf("Response must be equal")
53
+		t.Fatal("Response must be equal")
54 54
 	}
55 55
 	if !reflect.DeepEqual(request, server.recordedRequest) {
56
-		t.Fatalf("Requests must be equal")
56
+		t.Fatal("Requests must be equal")
57 57
 	}
58 58
 }
59 59
 
... ...
@@ -67,7 +68,7 @@ func TestAuthZRequestPlugin(t *testing.T) {
67 67
 	request := Request{
68 68
 		User:           "user",
69 69
 		RequestBody:    []byte("sample body"),
70
-		RequestURI:     "www.authz.com",
70
+		RequestURI:     "www.authz.com/auth",
71 71
 		RequestMethod:  "GET",
72 72
 		RequestHeaders: map[string]string{"header": "value"},
73 73
 	}
... ...
@@ -82,10 +83,10 @@ func TestAuthZRequestPlugin(t *testing.T) {
82 82
 	}
83 83
 
84 84
 	if !reflect.DeepEqual(server.replayResponse, *actualResponse) {
85
-		t.Fatalf("Response must be equal")
85
+		t.Fatal("Response must be equal")
86 86
 	}
87 87
 	if !reflect.DeepEqual(request, server.recordedRequest) {
88
-		t.Fatalf("Requests must be equal")
88
+		t.Fatal("Requests must be equal")
89 89
 	}
90 90
 }
91 91
 
... ...
@@ -98,6 +99,7 @@ func TestAuthZResponsePlugin(t *testing.T) {
98 98
 
99 99
 	request := Request{
100 100
 		User:        "user",
101
+		RequestURI:  "someting.com/auth",
101 102
 		RequestBody: []byte("sample body"),
102 103
 	}
103 104
 	server.replayResponse = Response{
... ...
@@ -111,10 +113,10 @@ func TestAuthZResponsePlugin(t *testing.T) {
111 111
 	}
112 112
 
113 113
 	if !reflect.DeepEqual(server.replayResponse, *actualResponse) {
114
-		t.Fatalf("Response must be equal")
114
+		t.Fatal("Response must be equal")
115 115
 	}
116 116
 	if !reflect.DeepEqual(request, server.recordedRequest) {
117
-		t.Fatalf("Requests must be equal")
117
+		t.Fatal("Requests must be equal")
118 118
 	}
119 119
 }
120 120
 
... ...
@@ -158,7 +160,7 @@ func TestDrainBody(t *testing.T) {
158 158
 			t.Fatalf("Body must be copied, actual length: '%d'", len(body))
159 159
 		}
160 160
 		if closer == nil {
161
-			t.Fatalf("Closer must not be nil")
161
+			t.Fatal("Closer must not be nil")
162 162
 		}
163 163
 		modified, err := ioutil.ReadAll(closer)
164 164
 		if err != nil {
... ...
@@ -229,8 +231,10 @@ type authZPluginTestServer struct {
229 229
 // start starts the test server that implements the plugin
230 230
 func (t *authZPluginTestServer) start() {
231 231
 	r := mux.NewRouter()
232
-	os.Remove(pluginAddress)
233
-	l, _ := net.ListenUnix("unix", &net.UnixAddr{Name: pluginAddress, Net: "unix"})
232
+	l, err := net.Listen("unix", pluginAddress)
233
+	if err != nil {
234
+		t.t.Fatal(err)
235
+	}
234 236
 	t.listener = l
235 237
 	r.HandleFunc("/Plugin.Activate", t.activate)
236 238
 	r.HandleFunc("/"+AuthZApiRequest, t.auth)
... ...
@@ -257,14 +261,23 @@ func (t *authZPluginTestServer) stop() {
257 257
 // auth is a used to record/replay the authentication api messages
258 258
 func (t *authZPluginTestServer) auth(w http.ResponseWriter, r *http.Request) {
259 259
 	t.recordedRequest = Request{}
260
-	body, _ := ioutil.ReadAll(r.Body)
260
+	body, err := ioutil.ReadAll(r.Body)
261
+	if err != nil {
262
+		t.t.Fatal(err)
263
+	}
261 264
 	r.Body.Close()
262 265
 	json.Unmarshal(body, &t.recordedRequest)
263
-	b, _ := json.Marshal(t.replayResponse)
266
+	b, err := json.Marshal(t.replayResponse)
267
+	if err != nil {
268
+		t.t.Fatal(err)
269
+	}
264 270
 	w.Write(b)
265 271
 }
266 272
 
267 273
 func (t *authZPluginTestServer) activate(w http.ResponseWriter, r *http.Request) {
268
-	b, _ := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
274
+	b, err := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
275
+	if err != nil {
276
+		t.t.Fatal(err)
277
+	}
269 278
 	w.Write(b)
270 279
 }
... ...
@@ -130,7 +130,7 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool)
130 130
 				return nil, err
131 131
 			}
132 132
 			retries++
133
-			logrus.Warnf("Unable to connect to plugin: %s:%s, retrying in %v", req.URL.Host, req.URL.Path, timeOff)
133
+			logrus.Warnf("Unable to connect to plugin: %s%s: %v, retrying in %v", req.URL.Host, req.URL.Path, err, timeOff)
134 134
 			time.Sleep(timeOff)
135 135
 			continue
136 136
 		}