From 31ee1583941fedf1a9883426637517abf07eb2fc Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 9 Oct 2022 22:40:18 +0200 Subject: [PATCH] client: defaultHTTPClient() accept URL We're parsing the URL either way, so may just as well use the result. Signed-off-by: Sebastiaan van Stijn --- client/client.go | 17 +++++++++-------- client/client_test.go | 8 ++++++++ client/client_unix.go | 3 --- client/client_windows.go | 3 --- client/options_test.go | 24 ++++++++++++++++-------- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/client/client.go b/client/client.go index 224ed6f454..cf903b0762 100644 --- a/client/client.go +++ b/client/client.go @@ -125,7 +125,12 @@ func CheckRedirect(req *http.Request, via []*http.Request) error { // client.WithAPIVersionNegotiation(), // ) func NewClientWithOpts(ops ...Opt) (*Client, error) { - client, err := defaultHTTPClient(DefaultDockerHost) + hostURL, err := ParseHostURL(DefaultDockerHost) + if err != nil { + return nil, err + } + + client, err := defaultHTTPClient(hostURL) if err != nil { return nil, err } @@ -133,8 +138,8 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) { host: DefaultDockerHost, version: api.DefaultVersion, client: client, - proto: defaultProto, - addr: defaultAddr, + proto: hostURL.Scheme, + addr: hostURL.Host, } for _, op := range ops { @@ -160,11 +165,7 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) { return c, nil } -func defaultHTTPClient(host string) (*http.Client, error) { - hostURL, err := ParseHostURL(host) - if err != nil { - return nil, err - } +func defaultHTTPClient(hostURL *url.URL) (*http.Client, error) { transport := &http.Transport{} _ = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host) return &http.Client{ diff --git a/client/client_test.go b/client/client_test.go index bb31ff069e..44586075d1 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -165,6 +165,14 @@ func TestParseHostURL(t *testing.T) { host: "tcp://localhost:2476/path", expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"}, }, + { + host: "unix:///var/run/docker.sock", + expected: &url.URL{Scheme: "unix", Host: "/var/run/docker.sock"}, + }, + { + host: "npipe:////./pipe/docker_engine", + expected: &url.URL{Scheme: "npipe", Host: "//./pipe/docker_engine"}, + }, } for _, testcase := range testcases { diff --git a/client/client_unix.go b/client/client_unix.go index 685149a68c..319b738d3e 100644 --- a/client/client_unix.go +++ b/client/client_unix.go @@ -6,6 +6,3 @@ package client // import "github.com/docker/docker/client" // DefaultDockerHost defines OS-specific default host if the DOCKER_HOST // (EnvOverrideHost) environment variable is unset or empty. const DefaultDockerHost = "unix:///var/run/docker.sock" - -const defaultProto = "unix" -const defaultAddr = "/var/run/docker.sock" diff --git a/client/client_windows.go b/client/client_windows.go index 5abe60457d..56572d1a27 100644 --- a/client/client_windows.go +++ b/client/client_windows.go @@ -3,6 +3,3 @@ package client // import "github.com/docker/docker/client" // DefaultDockerHost defines OS-specific default host if the DOCKER_HOST // (EnvOverrideHost) environment variable is unset or empty. const DefaultDockerHost = "npipe:////./pipe/docker_engine" - -const defaultProto = "npipe" -const defaultAddr = "//./pipe/docker_engine" diff --git a/client/options_test.go b/client/options_test.go index 51900cef26..7153e24c70 100644 --- a/client/options_test.go +++ b/client/options_test.go @@ -1,31 +1,39 @@ package client import ( + "runtime" "testing" "time" "github.com/docker/docker/api" "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func TestOptionWithHostFromEnv(t *testing.T) { c, err := NewClientWithOpts(WithHostFromEnv()) assert.NilError(t, err) assert.Check(t, c.client != nil) - assert.Equal(t, c.host, DefaultDockerHost) - assert.Equal(t, c.proto, defaultProto) - assert.Equal(t, c.addr, defaultAddr) - assert.Equal(t, c.basePath, "") + assert.Check(t, is.Equal(c.basePath, "")) + if runtime.GOOS == "windows" { + assert.Check(t, is.Equal(c.host, "npipe:////./pipe/docker_engine")) + assert.Check(t, is.Equal(c.proto, "npipe")) + assert.Check(t, is.Equal(c.addr, "//./pipe/docker_engine")) + } else { + assert.Check(t, is.Equal(c.host, "unix:///var/run/docker.sock")) + assert.Check(t, is.Equal(c.proto, "unix")) + assert.Check(t, is.Equal(c.addr, "/var/run/docker.sock")) + } t.Setenv("DOCKER_HOST", "tcp://foo.example.com:2376/test/") c, err = NewClientWithOpts(WithHostFromEnv()) assert.NilError(t, err) assert.Check(t, c.client != nil) - assert.Equal(t, c.host, "tcp://foo.example.com:2376/test/") - assert.Equal(t, c.proto, "tcp") - assert.Equal(t, c.addr, "foo.example.com:2376") - assert.Equal(t, c.basePath, "/test/") + assert.Check(t, is.Equal(c.basePath, "/test/")) + assert.Check(t, is.Equal(c.host, "tcp://foo.example.com:2376/test/")) + assert.Check(t, is.Equal(c.proto, "tcp")) + assert.Check(t, is.Equal(c.addr, "foo.example.com:2376")) } func TestOptionWithTimeout(t *testing.T) {