Browse code

pkg/authorization: Fix test failures on macOS

On macOS, unit tests were failing with

root@c4101a75c792:/go/src/github.com/docker/docker/pkg/authorization# go test .
--- FAIL: TestAuthZRequestPluginError (0.00s)
authz_unix_test.go:295: listen unix authz-test-plugin.sock: bind: file name too long
--- FAIL: TestAuthZRequestPlugin (0.00s)
authz_unix_test.go:295: listen unix authz-test-plugin.sock: bind: file name too long
--- FAIL: TestAuthZResponsePlugin (0.00s)
authz_unix_test.go:295: listen unix authz-test-plugin.sock: bind: file name too long
time="2020-04-07T10:07:04Z" level=warning msg="Request body is larger than: '1048576' skipping body"
--- FAIL: TestMiddlewareWrapHandler (0.00s)
authz_unix_test.go:295: listen unix authz-test-plugin.sock: bind: file name too long
FAIL
FAIL github.com/docker/docker/pkg/authorization 0.120s

This change moves the socket creation from a working test directory to a tmp directory,
so the path is shorter.

Change-type: patch
Signed-off-by: Roman Mazur <roman@balena.io>

Roman Mazur authored on 2020/04/07 19:05:25
Showing 2 changed files
... ...
@@ -32,7 +32,7 @@ func TestAuthZRequestPluginError(t *testing.T) {
32 32
 	server.start()
33 33
 	defer server.stop()
34 34
 
35
-	authZPlugin := createTestPlugin(t)
35
+	authZPlugin := createTestPlugin(t, server.socketAddress())
36 36
 
37 37
 	request := Request{
38 38
 		User:           "user",
... ...
@@ -63,7 +63,7 @@ func TestAuthZRequestPlugin(t *testing.T) {
63 63
 	server.start()
64 64
 	defer server.stop()
65 65
 
66
-	authZPlugin := createTestPlugin(t)
66
+	authZPlugin := createTestPlugin(t, server.socketAddress())
67 67
 
68 68
 	request := Request{
69 69
 		User:           "user",
... ...
@@ -95,7 +95,7 @@ func TestAuthZResponsePlugin(t *testing.T) {
95 95
 	server.start()
96 96
 	defer server.stop()
97 97
 
98
-	authZPlugin := createTestPlugin(t)
98
+	authZPlugin := createTestPlugin(t, server.socketAddress())
99 99
 
100 100
 	request := Request{
101 101
 		User:        "user",
... ...
@@ -262,13 +262,8 @@ func TestResponseModifierOverride(t *testing.T) {
262 262
 }
263 263
 
264 264
 // createTestPlugin creates a new sample authorization plugin
265
-func createTestPlugin(t *testing.T) *authorizationPlugin {
266
-	pwd, err := os.Getwd()
267
-	if err != nil {
268
-		t.Fatal(err)
269
-	}
270
-
271
-	client, err := plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), &tlsconfig.Options{InsecureSkipVerify: true})
265
+func createTestPlugin(t *testing.T, socketAddress string) *authorizationPlugin {
266
+	client, err := plugins.NewClient("unix:///"+socketAddress, &tlsconfig.Options{InsecureSkipVerify: true})
272 267
 	if err != nil {
273 268
 		t.Fatalf("Failed to create client %v", err)
274 269
 	}
... ...
@@ -285,12 +280,23 @@ type authZPluginTestServer struct {
285 285
 	// response stores the response sent from the plugin to the daemon
286 286
 	replayResponse Response
287 287
 	server         *httptest.Server
288
+	tmpDir         string
289
+}
290
+
291
+func (t *authZPluginTestServer) socketAddress() string {
292
+	return path.Join(t.tmpDir, pluginAddress)
288 293
 }
289 294
 
290 295
 // start starts the test server that implements the plugin
291 296
 func (t *authZPluginTestServer) start() {
297
+	var err error
298
+	t.tmpDir, err = ioutil.TempDir("", "authz")
299
+	if err != nil {
300
+		t.t.Fatal(err)
301
+	}
302
+
292 303
 	r := mux.NewRouter()
293
-	l, err := net.Listen("unix", pluginAddress)
304
+	l, err := net.Listen("unix", t.socketAddress())
294 305
 	if err != nil {
295 306
 		t.t.Fatal(err)
296 307
 	}
... ...
@@ -311,7 +317,7 @@ func (t *authZPluginTestServer) start() {
311 311
 // stop stops the test server that implements the plugin
312 312
 func (t *authZPluginTestServer) stop() {
313 313
 	t.server.Close()
314
-	os.Remove(pluginAddress)
314
+	_ = os.RemoveAll(t.tmpDir)
315 315
 	if t.listener != nil {
316 316
 		t.listener.Close()
317 317
 	}
... ...
@@ -18,7 +18,7 @@ func TestMiddlewareWrapHandler(t *testing.T) {
18 18
 	server.start()
19 19
 	defer server.stop()
20 20
 
21
-	authZPlugin := createTestPlugin(t)
21
+	authZPlugin := createTestPlugin(t, server.socketAddress())
22 22
 	pluginNames := []string{authZPlugin.name}
23 23
 
24 24
 	var pluginGetter plugingetter.PluginGetter