f2c432636b
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>
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
// +build !windows
|
|
|
|
package authorization // import "github.com/docker/docker/pkg/authorization"
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestMiddlewareWrapHandler(t *testing.T) {
|
|
server := authZPluginTestServer{t: t}
|
|
server.start()
|
|
defer server.stop()
|
|
|
|
authZPlugin := createTestPlugin(t, server.socketAddress())
|
|
pluginNames := []string{authZPlugin.name}
|
|
|
|
var pluginGetter plugingetter.PluginGetter
|
|
middleWare := NewMiddleware(pluginNames, pluginGetter)
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
return nil
|
|
}
|
|
|
|
authList := []Plugin{authZPlugin}
|
|
middleWare.SetPlugins([]string{"My Test Plugin"})
|
|
setAuthzPlugins(middleWare, authList)
|
|
mdHandler := middleWare.WrapHandler(handler)
|
|
assert.Assert(t, mdHandler != nil)
|
|
|
|
addr := "www.example.com/auth"
|
|
req, _ := http.NewRequest(http.MethodGet, addr, nil)
|
|
req.RequestURI = addr
|
|
req.Header.Add("header", "value")
|
|
|
|
resp := httptest.NewRecorder()
|
|
ctx := context.Background()
|
|
|
|
t.Run("Error Test Case :", func(t *testing.T) {
|
|
server.replayResponse = Response{
|
|
Allow: false,
|
|
Msg: "Server Auth Not Allowed",
|
|
}
|
|
if err := mdHandler(ctx, resp, req, map[string]string{}); err == nil {
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Positive Test Case :", func(t *testing.T) {
|
|
server.replayResponse = Response{
|
|
Allow: true,
|
|
Msg: "Server Auth Allowed",
|
|
}
|
|
if err := mdHandler(ctx, resp, req, map[string]string{}); err != nil {
|
|
assert.NilError(t, err)
|
|
}
|
|
|
|
})
|
|
|
|
}
|