2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-19 03:43:08 -05:00
|
|
|
"github.com/docker/docker/api"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/env"
|
|
|
|
"gotest.tools/skip"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewEnvClient(t *testing.T) {
|
2017-09-28 17:10:06 -04:00
|
|
|
skip.If(t, runtime.GOOS == "windows")
|
2017-09-19 18:00:54 -04:00
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
doc string
|
2016-09-06 14:46:37 -04:00
|
|
|
envs map[string]string
|
|
|
|
expectedError string
|
|
|
|
expectedVersion string
|
|
|
|
}{
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid cert path",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "invalid/path",
|
|
|
|
},
|
2017-02-25 15:17:23 -05:00
|
|
|
expectedError: "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory",
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
2016-09-21 22:16:44 -04:00
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path and tls verify",
|
2016-09-21 22:16:44 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
"DOCKER_TLS_VERIFY": "1",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-21 22:16:44 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path and host",
|
2016-09-21 22:16:44 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
"DOCKER_HOST": "https://notaunixsocket",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-21 22:16:44 -04:00
|
|
|
},
|
2016-09-06 14:46:37 -04:00
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid docker host",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_HOST": "host",
|
|
|
|
},
|
|
|
|
expectedError: "unable to parse docker host `host`",
|
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid docker host, with good format",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_HOST": "invalid://url",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "override api version",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_API_VERSION": "1.22",
|
|
|
|
},
|
|
|
|
expectedVersion: "1.22",
|
|
|
|
},
|
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
|
2017-09-28 17:10:06 -04:00
|
|
|
defer env.PatchAll(t, nil)()
|
2017-09-19 18:00:54 -04:00
|
|
|
for _, c := range testcases {
|
2017-09-28 17:10:06 -04:00
|
|
|
env.PatchAll(t, c.envs)
|
2016-09-06 14:46:37 -04:00
|
|
|
apiclient, err := NewEnvClient()
|
|
|
|
if c.expectedError != "" {
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, is.Error(err, c.expectedError), c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
} else {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
version := apiclient.ClientVersion()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(c.expectedVersion, version), c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2016-09-21 22:16:44 -04:00
|
|
|
|
|
|
|
if c.envs["DOCKER_TLS_VERIFY"] != "" {
|
|
|
|
// pedantic checking that this is handled correctly
|
|
|
|
tr := apiclient.client.Transport.(*http.Transport)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Assert(t, tr.TLSClientConfig != nil, c.doc)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(tr.TLSClientConfig.InsecureSkipVerify, false), c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAPIPath(t *testing.T) {
|
2017-09-07 13:46:23 -04:00
|
|
|
testcases := []struct {
|
|
|
|
version string
|
|
|
|
path string
|
|
|
|
query url.Values
|
|
|
|
expected string
|
2016-09-06 14:46:37 -04:00
|
|
|
}{
|
|
|
|
{"", "/containers/json", nil, "/containers/json"},
|
|
|
|
{"", "/containers/json", url.Values{}, "/containers/json"},
|
|
|
|
{"", "/containers/json", url.Values{"s": []string{"c"}}, "/containers/json?s=c"},
|
|
|
|
{"1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
|
|
|
{"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
|
|
|
{"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
|
|
|
{"v1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
|
|
|
{"v1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
|
|
|
{"v1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
|
|
|
{"v1.22", "/networks/kiwl$%^", nil, "/v1.22/networks/kiwl$%25%5E"},
|
|
|
|
}
|
|
|
|
|
2017-09-07 13:46:23 -04:00
|
|
|
for _, testcase := range testcases {
|
|
|
|
c := Client{version: testcase.version, basePath: "/"}
|
|
|
|
actual := c.getAPIPath(testcase.path, testcase.query)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(actual, testcase.expected))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:22:11 -04:00
|
|
|
func TestParseHostURL(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
host string
|
|
|
|
expected *url.URL
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
host: "",
|
|
|
|
expectedErr: "unable to parse docker host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "foobar",
|
|
|
|
expectedErr: "unable to parse docker host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "foo://bar",
|
|
|
|
expected: &url.URL{Scheme: "foo", Host: "bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "tcp://localhost:2476",
|
|
|
|
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "tcp://localhost:2476/path",
|
|
|
|
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
actual, err := ParseHostURL(testcase.host)
|
|
|
|
if testcase.expectedErr != "" {
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, testcase.expectedErr))
|
2017-09-07 12:22:11 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(testcase.expected, actual))
|
2017-09-07 12:22:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
|
2017-09-28 17:10:06 -04:00
|
|
|
defer env.PatchAll(t, map[string]string{
|
2017-06-21 01:58:16 -04:00
|
|
|
"DOCKER_HOST": "",
|
|
|
|
"DOCKER_API_VERSION": "",
|
|
|
|
"DOCKER_TLS_VERIFY": "",
|
|
|
|
"DOCKER_CERT_PATH": "",
|
2017-09-28 17:10:06 -04:00
|
|
|
})()
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
client, err := NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(client.version, api.DefaultVersion))
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
expected := "1.22"
|
|
|
|
os.Setenv("DOCKER_API_VERSION", expected)
|
|
|
|
client, err = NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, client.version))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
// TestNegotiateAPIVersionEmpty asserts that client.Client can
|
|
|
|
// negotiate a compatible APIVersion when omitted
|
|
|
|
func TestNegotiateAPIVersionEmpty(t *testing.T) {
|
2018-06-07 22:58:28 -04:00
|
|
|
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": ""})()
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
client, err := NewEnvClient()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
ping := types.Ping{
|
|
|
|
APIVersion: "",
|
|
|
|
OSType: "linux",
|
|
|
|
Experimental: false,
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
// set our version to something new
|
|
|
|
client.version = "1.25"
|
|
|
|
|
|
|
|
// if no version from server, expect the earliest
|
|
|
|
// version before APIVersion was implemented
|
|
|
|
expected := "1.24"
|
|
|
|
|
|
|
|
// test downgrade
|
|
|
|
client.NegotiateAPIVersionPing(ping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, client.version))
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestNegotiateAPIVersion asserts that client.Client can
|
|
|
|
// negotiate a compatible APIVersion with the server
|
|
|
|
func TestNegotiateAPIVersion(t *testing.T) {
|
2016-09-06 14:46:37 -04:00
|
|
|
client, err := NewEnvClient()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
expected := "1.21"
|
|
|
|
ping := types.Ping{
|
|
|
|
APIVersion: expected,
|
|
|
|
OSType: "linux",
|
|
|
|
Experimental: false,
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
// set our version to something new
|
|
|
|
client.version = "1.22"
|
|
|
|
|
|
|
|
// test downgrade
|
|
|
|
client.NegotiateAPIVersionPing(ping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, client.version))
|
2017-09-27 21:12:13 -04:00
|
|
|
|
|
|
|
// set the client version to something older, and verify that we keep the
|
|
|
|
// original setting.
|
|
|
|
expected = "1.20"
|
|
|
|
client.version = expected
|
|
|
|
client.NegotiateAPIVersionPing(ping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, client.version))
|
2017-09-27 21:12:13 -04:00
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestNegotiateAPIVersionOverride asserts that we honor
|
2018-02-10 06:27:14 -05:00
|
|
|
// the environment variable DOCKER_API_VERSION when negotiating versions
|
2017-06-21 01:58:16 -04:00
|
|
|
func TestNegotiateAPVersionOverride(t *testing.T) {
|
2017-09-28 17:10:06 -04:00
|
|
|
expected := "9.99"
|
|
|
|
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": expected})()
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
client, err := NewEnvClient()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
ping := types.Ping{
|
|
|
|
APIVersion: "1.24",
|
|
|
|
OSType: "linux",
|
|
|
|
Experimental: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that we honored the env var
|
|
|
|
client.NegotiateAPIVersionPing(ping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, client.version))
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 05:05:35 -04:00
|
|
|
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return rtf(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
type bytesBufferClose struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bbc bytesBufferClose) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientRedirect(t *testing.T) {
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: CheckRedirect,
|
|
|
|
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
|
|
|
if req.URL.String() == "/bla" {
|
|
|
|
return &http.Response{StatusCode: 404}, nil
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: 301,
|
|
|
|
Header: map[string][]string{"Location": {"/bla"}},
|
|
|
|
Body: bytesBufferClose{bytes.NewBuffer(nil)},
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
httpMethod string
|
2017-12-22 16:30:49 -05:00
|
|
|
expectedErr *url.Error
|
2017-03-27 05:05:35 -04:00
|
|
|
statusCode int
|
|
|
|
}{
|
|
|
|
{http.MethodGet, nil, 301},
|
|
|
|
{http.MethodPost, &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
{http.MethodPut, &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
{http.MethodDelete, &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-03-27 05:05:35 -04:00
|
|
|
resp, err := client.Do(req)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(tc.statusCode, resp.StatusCode))
|
2017-12-22 16:30:49 -05:00
|
|
|
if tc.expectedErr == nil {
|
|
|
|
assert.Check(t, is.Nil(err))
|
|
|
|
} else {
|
|
|
|
urlError, ok := err.(*url.Error)
|
|
|
|
assert.Assert(t, ok, "%T is not *url.Error", err)
|
|
|
|
assert.Check(t, is.Equal(*tc.expectedErr, *urlError))
|
|
|
|
}
|
2017-03-27 05:05:35 -04:00
|
|
|
}
|
|
|
|
}
|