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"
|
|
|
|
"strings"
|
|
|
|
"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"
|
2017-09-07 12:22:11 -04:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
2017-09-19 18:00:54 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
2017-03-27 05:05:35 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewEnvClient(t *testing.T) {
|
2017-09-19 18:00:54 -04:00
|
|
|
skip.IfCondition(t, runtime.GOOS == "windows")
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
env := envToMap()
|
|
|
|
defer mapToEnv(env)
|
2017-09-19 18:00:54 -04:00
|
|
|
for _, c := range testcases {
|
2017-06-21 01:58:16 -04:00
|
|
|
mapToEnv(c.envs)
|
2016-09-06 14:46:37 -04:00
|
|
|
apiclient, err := NewEnvClient()
|
|
|
|
if c.expectedError != "" {
|
2017-09-19 18:00:54 -04:00
|
|
|
assert.Error(t, err, c.doc)
|
|
|
|
assert.Equal(t, c.expectedError, err.Error(), c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
} else {
|
2017-09-19 18:00:54 -04:00
|
|
|
assert.NoError(t, err, c.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
version := apiclient.ClientVersion()
|
2017-09-19 18:00:54 -04:00
|
|
|
assert.Equal(t, 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-09-19 18:00:54 -04:00
|
|
|
assert.NotNil(t, tr.TLSClientConfig, c.doc)
|
|
|
|
assert.Equal(t, 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)
|
|
|
|
assert.Equal(t, actual, testcase.expected)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseHost(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
host string
|
|
|
|
proto string
|
|
|
|
addr string
|
|
|
|
base string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"", "", "", "", true},
|
|
|
|
{"foobar", "", "", "", true},
|
|
|
|
{"foo://bar", "foo", "bar", "", false},
|
|
|
|
{"tcp://localhost:2476", "tcp", "localhost:2476", "", false},
|
|
|
|
{"tcp://localhost:2476/path", "tcp", "localhost:2476", "/path", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cs := range cases {
|
|
|
|
p, a, b, e := ParseHost(cs.host)
|
2017-06-21 01:58:16 -04:00
|
|
|
if cs.err {
|
|
|
|
assert.Error(t, e)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
assert.Equal(t, cs.proto, p)
|
|
|
|
assert.Equal(t, cs.addr, a)
|
|
|
|
assert.Equal(t, cs.base, b)
|
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 != "" {
|
|
|
|
testutil.ErrorContains(t, err, testcase.expectedErr)
|
|
|
|
}
|
|
|
|
assert.Equal(t, testcase.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
|
|
|
|
env := envToMap()
|
|
|
|
defer mapToEnv(env)
|
|
|
|
|
|
|
|
envMap := map[string]string{
|
|
|
|
"DOCKER_HOST": "",
|
|
|
|
"DOCKER_API_VERSION": "",
|
|
|
|
"DOCKER_TLS_VERIFY": "",
|
|
|
|
"DOCKER_CERT_PATH": "",
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
mapToEnv(envMap)
|
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
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
assert.Equal(t, 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
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
assert.Equal(t, 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) {
|
|
|
|
env := envToMap()
|
|
|
|
defer mapToEnv(env)
|
|
|
|
|
|
|
|
envMap := map[string]string{
|
|
|
|
"DOCKER_API_VERSION": "",
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
mapToEnv(envMap)
|
|
|
|
|
|
|
|
client, err := NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert.Equal(t, expected, client.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(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)
|
|
|
|
assert.Equal(t, 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)
|
|
|
|
assert.Equal(t, expected, client.version)
|
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestNegotiateAPIVersionOverride asserts that we honor
|
|
|
|
// the environment variable DOCKER_API_VERSION when negotianing versions
|
|
|
|
func TestNegotiateAPVersionOverride(t *testing.T) {
|
|
|
|
env := envToMap()
|
|
|
|
defer mapToEnv(env)
|
|
|
|
|
|
|
|
envMap := map[string]string{
|
|
|
|
"DOCKER_API_VERSION": "9.99",
|
|
|
|
}
|
|
|
|
mapToEnv(envMap)
|
|
|
|
|
|
|
|
client, err := NewEnvClient()
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
ping := types.Ping{
|
|
|
|
APIVersion: "1.24",
|
|
|
|
OSType: "linux",
|
|
|
|
Experimental: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := envMap["DOCKER_API_VERSION"]
|
|
|
|
|
|
|
|
// test that we honored the env var
|
|
|
|
client.NegotiateAPIVersionPing(ping)
|
|
|
|
assert.Equal(t, expected, client.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapToEnv takes a map of environment variables and sets them
|
|
|
|
func mapToEnv(env map[string]string) {
|
|
|
|
for k, v := range env {
|
|
|
|
os.Setenv(k, v)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2017-06-21 01:58:16 -04:00
|
|
|
// envToMap returns a map of environment variables
|
|
|
|
func envToMap() map[string]string {
|
|
|
|
env := make(map[string]string)
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
kv := strings.SplitAfterN(e, "=", 2)
|
|
|
|
env[kv[0]] = kv[1]
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
return env
|
2016-09-06 14:46:37 -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
|
|
|
|
expectedErr error
|
|
|
|
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)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert.Equal(t, tc.expectedErr, err)
|
|
|
|
assert.Equal(t, tc.statusCode, resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|