diff --git a/client/client.go b/client/client.go index 8d32976b2b..1c081a51ae 100644 --- a/client/client.go +++ b/client/client.go @@ -126,7 +126,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 } @@ -134,8 +139,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 { @@ -161,13 +166,12 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) { return c, nil } -func defaultHTTPClient(host string) (*http.Client, error) { - hostURL, err := ParseHostURL(host) +func defaultHTTPClient(hostURL *url.URL) (*http.Client, error) { + transport := &http.Transport{} + err := sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host) if err != nil { return nil, err } - transport := &http.Transport{} - _ = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host) return &http.Client{ Transport: transport, CheckRedirect: CheckRedirect, 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) {