2019-04-09 08:07:30 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-06-01 20:42:46 +00:00
|
|
|
"context"
|
|
|
|
"net/http"
|
2022-10-09 20:40:18 +00:00
|
|
|
"runtime"
|
2019-04-09 08:07:30 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-03-30 13:15:42 +00:00
|
|
|
"github.com/docker/docker/api"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2022-10-09 20:40:18 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2019-04-09 08:07:30 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 13:15:42 +00:00
|
|
|
func TestOptionWithHostFromEnv(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(WithHostFromEnv())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, c.client != nil)
|
2022-10-09 20:40:18 +00:00
|
|
|
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"))
|
|
|
|
}
|
2021-03-30 13:15:42 +00:00
|
|
|
|
2022-03-06 21:45:04 +00:00
|
|
|
t.Setenv("DOCKER_HOST", "tcp://foo.example.com:2376/test/")
|
2021-03-30 13:15:42 +00:00
|
|
|
|
|
|
|
c, err = NewClientWithOpts(WithHostFromEnv())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, c.client != nil)
|
2022-10-09 20:40:18 +00:00
|
|
|
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"))
|
2021-03-30 13:15:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 08:07:30 +00:00
|
|
|
func TestOptionWithTimeout(t *testing.T) {
|
|
|
|
timeout := 10 * time.Second
|
|
|
|
c, err := NewClientWithOpts(WithTimeout(timeout))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, c.client != nil)
|
|
|
|
assert.Equal(t, c.client.Timeout, timeout)
|
|
|
|
}
|
2021-03-30 13:15:42 +00:00
|
|
|
|
|
|
|
func TestOptionWithVersionFromEnv(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(WithVersionFromEnv())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, c.client != nil)
|
|
|
|
assert.Equal(t, c.version, api.DefaultVersion)
|
|
|
|
assert.Equal(t, c.manualOverride, false)
|
|
|
|
|
2022-03-06 21:45:04 +00:00
|
|
|
t.Setenv("DOCKER_API_VERSION", "2.9999")
|
2021-03-30 13:15:42 +00:00
|
|
|
|
|
|
|
c, err = NewClientWithOpts(WithVersionFromEnv())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, c.client != nil)
|
|
|
|
assert.Equal(t, c.version, "2.9999")
|
|
|
|
assert.Equal(t, c.manualOverride, true)
|
|
|
|
}
|
2023-06-01 20:42:46 +00:00
|
|
|
|
|
|
|
func TestWithUserAgent(t *testing.T) {
|
|
|
|
const userAgent = "Magic-Client/v1.2.3"
|
|
|
|
t.Run("user-agent", func(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(
|
|
|
|
WithUserAgent(userAgent),
|
|
|
|
WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
|
|
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
assert.Check(t, err)
|
|
|
|
_, err = c.Ping(context.Background())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, c.Close())
|
|
|
|
})
|
|
|
|
t.Run("user-agent and custom headers", func(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(
|
|
|
|
WithUserAgent(userAgent),
|
|
|
|
WithHTTPHeaders(map[string]string{"User-Agent": "should-be-ignored/1.0.0", "Other-Header": "hello-world"}),
|
|
|
|
WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
|
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
assert.Check(t, err)
|
|
|
|
_, err = c.Ping(context.Background())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, c.Close())
|
|
|
|
})
|
|
|
|
t.Run("custom headers", func(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(
|
|
|
|
WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
|
|
|
|
WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "from-custom-headers/1.0.0"))
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
|
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
assert.Check(t, err)
|
|
|
|
_, err = c.Ping(context.Background())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, c.Close())
|
|
|
|
})
|
|
|
|
t.Run("no user-agent set", func(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(
|
|
|
|
WithHTTPHeaders(map[string]string{"Other-Header": "hello-world"}),
|
|
|
|
WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
|
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
assert.Check(t, err)
|
|
|
|
_, err = c.Ping(context.Background())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, c.Close())
|
|
|
|
})
|
|
|
|
t.Run("reset custom user-agent", func(t *testing.T) {
|
|
|
|
c, err := NewClientWithOpts(
|
|
|
|
WithUserAgent(""),
|
|
|
|
WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
|
|
|
|
WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
|
|
|
|
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
|
|
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
assert.Check(t, err)
|
|
|
|
_, err = c.Ping(context.Background())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, c.Close())
|
|
|
|
})
|
|
|
|
}
|