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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-09 22:40:18 +02:00
parent d109e429dd
commit 31ee158394
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
5 changed files with 33 additions and 22 deletions

View file

@ -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{

View file

@ -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 {

View file

@ -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"

View file

@ -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"

View file

@ -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) {