Browse code

pkg: authorization: cleanup tests

- do use use log pkg
- do not t.Fatal in goroutine
- cleanups

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

Antonio Murdaca authored on 2016/05/08 21:01:23
Showing 1 changed files
... ...
@@ -8,7 +8,6 @@ package authorization
8 8
 import (
9 9
 	"encoding/json"
10 10
 	"io/ioutil"
11
-	"log"
12 11
 	"net"
13 12
 	"net/http"
14 13
 	"net/http/httptest"
... ...
@@ -139,7 +138,6 @@ func TestResponseModifier(t *testing.T) {
139 139
 }
140 140
 
141 141
 func TestDrainBody(t *testing.T) {
142
-
143 142
 	tests := []struct {
144 143
 		length             int // length is the message length send to drainBody
145 144
 		expectedBodyLength int // expectedBodyLength is the expected body length after drainBody is called
... ...
@@ -151,18 +149,17 @@ func TestDrainBody(t *testing.T) {
151 151
 	}
152 152
 
153 153
 	for _, test := range tests {
154
-
155 154
 		msg := strings.Repeat("a", test.length)
156 155
 		body, closer, err := drainBody(ioutil.NopCloser(bytes.NewReader([]byte(msg))))
156
+		if err != nil {
157
+			t.Fatal(err)
158
+		}
157 159
 		if len(body) != test.expectedBodyLength {
158 160
 			t.Fatalf("Body must be copied, actual length: '%d'", len(body))
159 161
 		}
160 162
 		if closer == nil {
161 163
 			t.Fatalf("Closer must not be nil")
162 164
 		}
163
-		if err != nil {
164
-			t.Fatalf("Error must not be nil: '%v'", err)
165
-		}
166 165
 		modified, err := ioutil.ReadAll(closer)
167 166
 		if err != nil {
168 167
 			t.Fatalf("Error must not be nil: '%v'", err)
... ...
@@ -207,7 +204,7 @@ func createTestPlugin(t *testing.T) *authorizationPlugin {
207 207
 	plugin := &plugins.Plugin{Name: "authz"}
208 208
 	pwd, err := os.Getwd()
209 209
 	if err != nil {
210
-		log.Fatal(err)
210
+		t.Fatal(err)
211 211
 	}
212 212
 
213 213
 	plugin.Client, err = plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), tlsconfig.Options{InsecureSkipVerify: true})
... ...
@@ -232,16 +229,11 @@ type authZPluginTestServer struct {
232 232
 func (t *authZPluginTestServer) start() {
233 233
 	r := mux.NewRouter()
234 234
 	os.Remove(pluginAddress)
235
-	l, err := net.ListenUnix("unix", &net.UnixAddr{Name: pluginAddress, Net: "unix"})
236
-	if err != nil {
237
-		t.t.Fatalf("Failed to listen %v", err)
238
-	}
235
+	l, _ := net.ListenUnix("unix", &net.UnixAddr{Name: pluginAddress, Net: "unix"})
239 236
 	t.listener = l
240
-
241 237
 	r.HandleFunc("/Plugin.Activate", t.activate)
242 238
 	r.HandleFunc("/"+AuthZApiRequest, t.auth)
243 239
 	r.HandleFunc("/"+AuthZApiResponse, t.auth)
244
-	t.listener, _ = net.Listen("tcp", pluginAddress)
245 240
 	server := http.Server{Handler: r, Addr: pluginAddress}
246 241
 	server.Serve(l)
247 242
 }
... ...
@@ -257,20 +249,14 @@ 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
-	defer r.Body.Close()
261
-	body, err := ioutil.ReadAll(r.Body)
260
+	body, _ := ioutil.ReadAll(r.Body)
261
+	r.Body.Close()
262 262
 	json.Unmarshal(body, &t.recordedRequest)
263
-	b, err := json.Marshal(t.replayResponse)
264
-	if err != nil {
265
-		log.Fatal(err)
266
-	}
263
+	b, _ := json.Marshal(t.replayResponse)
267 264
 	w.Write(b)
268 265
 }
269 266
 
270 267
 func (t *authZPluginTestServer) activate(w http.ResponseWriter, r *http.Request) {
271
-	b, err := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
272
-	if err != nil {
273
-		log.Fatal(err)
274
-	}
268
+	b, _ := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
275 269
 	w.Write(b)
276 270
 }