From 35a51fd697de3aecce010558186e97e8d5e2e343 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 11 Dec 2023 11:30:28 -0800 Subject: [PATCH] Update authz plugin test to not use httputil Signed-off-by: Derek McGowan --- integration/plugin/authz/authz_plugin_test.go | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/integration/plugin/authz/authz_plugin_test.go b/integration/plugin/authz/authz_plugin_test.go index bfedc0606a..f271bfb9fe 100644 --- a/integration/plugin/authz/authz_plugin_test.go +++ b/integration/plugin/authz/authz_plugin_test.go @@ -8,7 +8,6 @@ import ( "io" "net" "net/http" - "net/http/httputil" "net/url" "os" "path/filepath" @@ -25,6 +24,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/authorization" "github.com/docker/docker/testutil/environment" + "github.com/docker/go-connections/sockets" "gotest.tools/v3/assert" "gotest.tools/v3/skip" ) @@ -81,6 +81,17 @@ func isAllowed(reqURI string) bool { return false } +func socketHTTPClient(u *url.URL) (*http.Client, error) { + transport := &http.Transport{} + err := sockets.ConfigureTransport(transport, u.Scheme, u.Path) + if err != nil { + return nil, err + } + return &http.Client{ + Transport: transport, + }, nil +} + func TestAuthZPluginAllowRequest(t *testing.T) { ctx := setupTestV1(t) @@ -176,15 +187,17 @@ func TestAuthZPluginAPIDenyResponse(t *testing.T) { daemonURL, err := url.Parse(d.Sock()) assert.NilError(t, err) - conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10) + socketClient, err := socketHTTPClient(daemonURL) assert.NilError(t, err) - c := httputil.NewClientConn(conn, nil) - req, err := http.NewRequest(http.MethodGet, "/version", nil) - assert.NilError(t, err) - req = req.WithContext(ctx) - resp, err := c.Do(req) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil) assert.NilError(t, err) + req.URL.Scheme = "http" + req.URL.Host = client.DummyHost + + resp, err := socketClient.Do(req) + assert.NilError(t, err) + assert.DeepEqual(t, http.StatusForbidden, resp.StatusCode) } @@ -471,13 +484,15 @@ func TestAuthZPluginHeader(t *testing.T) { daemonURL, err := url.Parse(d.Sock()) assert.NilError(t, err) - conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10) + socketClient, err := socketHTTPClient(daemonURL) assert.NilError(t, err) - client := httputil.NewClientConn(conn, nil) - req, err := http.NewRequest(http.MethodGet, "/version", nil) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil) assert.NilError(t, err) - req = req.WithContext(ctx) - resp, err := client.Do(req) + req.URL.Scheme = "http" + req.URL.Host = client.DummyHost + + resp, err := socketClient.Do(req) assert.NilError(t, err) assert.Equal(t, "application/json", resp.Header["Content-Type"][0]) }