client_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. "github.com/docker/docker/api"
  13. "github.com/docker/docker/api/types"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. "gotest.tools/v3/env"
  17. "gotest.tools/v3/skip"
  18. )
  19. func TestNewClientWithOpsFromEnv(t *testing.T) {
  20. skip.If(t, runtime.GOOS == "windows")
  21. testcases := []struct {
  22. doc string
  23. envs map[string]string
  24. expectedError string
  25. expectedVersion string
  26. }{
  27. {
  28. doc: "default api version",
  29. envs: map[string]string{},
  30. expectedVersion: api.DefaultVersion,
  31. },
  32. {
  33. doc: "invalid cert path",
  34. envs: map[string]string{
  35. "DOCKER_CERT_PATH": "invalid/path",
  36. },
  37. expectedError: "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory",
  38. },
  39. {
  40. doc: "default api version with cert path",
  41. envs: map[string]string{
  42. "DOCKER_CERT_PATH": "testdata/",
  43. },
  44. expectedVersion: api.DefaultVersion,
  45. },
  46. {
  47. doc: "default api version with cert path and tls verify",
  48. envs: map[string]string{
  49. "DOCKER_CERT_PATH": "testdata/",
  50. "DOCKER_TLS_VERIFY": "1",
  51. },
  52. expectedVersion: api.DefaultVersion,
  53. },
  54. {
  55. doc: "default api version with cert path and host",
  56. envs: map[string]string{
  57. "DOCKER_CERT_PATH": "testdata/",
  58. "DOCKER_HOST": "https://notaunixsocket",
  59. },
  60. expectedVersion: api.DefaultVersion,
  61. },
  62. {
  63. doc: "invalid docker host",
  64. envs: map[string]string{
  65. "DOCKER_HOST": "host",
  66. },
  67. expectedError: "unable to parse docker host `host`",
  68. },
  69. {
  70. doc: "invalid docker host, with good format",
  71. envs: map[string]string{
  72. "DOCKER_HOST": "invalid://url",
  73. },
  74. expectedVersion: api.DefaultVersion,
  75. },
  76. {
  77. doc: "override api version",
  78. envs: map[string]string{
  79. "DOCKER_API_VERSION": "1.22",
  80. },
  81. expectedVersion: "1.22",
  82. },
  83. }
  84. defer env.PatchAll(t, nil)()
  85. for _, c := range testcases {
  86. env.PatchAll(t, c.envs)
  87. apiclient, err := NewClientWithOpts(FromEnv)
  88. if c.expectedError != "" {
  89. assert.Check(t, is.Error(err, c.expectedError), c.doc)
  90. } else {
  91. assert.Check(t, err, c.doc)
  92. version := apiclient.ClientVersion()
  93. assert.Check(t, is.Equal(c.expectedVersion, version), c.doc)
  94. }
  95. if c.envs["DOCKER_TLS_VERIFY"] != "" {
  96. // pedantic checking that this is handled correctly
  97. tr := apiclient.client.Transport.(*http.Transport)
  98. assert.Assert(t, tr.TLSClientConfig != nil, c.doc)
  99. assert.Check(t, is.Equal(tr.TLSClientConfig.InsecureSkipVerify, false), c.doc)
  100. }
  101. }
  102. }
  103. func TestGetAPIPath(t *testing.T) {
  104. testcases := []struct {
  105. version string
  106. path string
  107. query url.Values
  108. expected string
  109. }{
  110. {"", "/containers/json", nil, "/containers/json"},
  111. {"", "/containers/json", url.Values{}, "/containers/json"},
  112. {"", "/containers/json", url.Values{"s": []string{"c"}}, "/containers/json?s=c"},
  113. {"1.22", "/containers/json", nil, "/v1.22/containers/json"},
  114. {"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
  115. {"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
  116. {"v1.22", "/containers/json", nil, "/v1.22/containers/json"},
  117. {"v1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
  118. {"v1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
  119. {"v1.22", "/networks/kiwl$%^", nil, "/v1.22/networks/kiwl$%25%5E"},
  120. }
  121. ctx := context.TODO()
  122. for _, testcase := range testcases {
  123. c := Client{version: testcase.version, basePath: "/"}
  124. actual := c.getAPIPath(ctx, testcase.path, testcase.query)
  125. assert.Check(t, is.Equal(actual, testcase.expected))
  126. }
  127. }
  128. func TestParseHostURL(t *testing.T) {
  129. testcases := []struct {
  130. host string
  131. expected *url.URL
  132. expectedErr string
  133. }{
  134. {
  135. host: "",
  136. expectedErr: "unable to parse docker host",
  137. },
  138. {
  139. host: "foobar",
  140. expectedErr: "unable to parse docker host",
  141. },
  142. {
  143. host: "foo://bar",
  144. expected: &url.URL{Scheme: "foo", Host: "bar"},
  145. },
  146. {
  147. host: "tcp://localhost:2476",
  148. expected: &url.URL{Scheme: "tcp", Host: "localhost:2476"},
  149. },
  150. {
  151. host: "tcp://localhost:2476/path",
  152. expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
  153. },
  154. }
  155. for _, testcase := range testcases {
  156. actual, err := ParseHostURL(testcase.host)
  157. if testcase.expectedErr != "" {
  158. assert.Check(t, is.ErrorContains(err, testcase.expectedErr))
  159. }
  160. assert.Check(t, is.DeepEqual(testcase.expected, actual))
  161. }
  162. }
  163. func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
  164. defer env.PatchAll(t, map[string]string{
  165. "DOCKER_HOST": "",
  166. "DOCKER_API_VERSION": "",
  167. "DOCKER_TLS_VERIFY": "",
  168. "DOCKER_CERT_PATH": "",
  169. })()
  170. client, err := NewClientWithOpts(FromEnv)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. assert.Check(t, is.Equal(client.version, api.DefaultVersion))
  175. expected := "1.22"
  176. os.Setenv("DOCKER_API_VERSION", expected)
  177. client, err = NewClientWithOpts(FromEnv)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. assert.Check(t, is.Equal(expected, client.version))
  182. }
  183. // TestNegotiateAPIVersionEmpty asserts that client.Client can
  184. // negotiate a compatible APIVersion when omitted
  185. func TestNegotiateAPIVersionEmpty(t *testing.T) {
  186. defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": ""})()
  187. client, err := NewClientWithOpts(FromEnv)
  188. assert.NilError(t, err)
  189. ping := types.Ping{
  190. APIVersion: "",
  191. OSType: "linux",
  192. Experimental: false,
  193. }
  194. // set our version to something new
  195. client.version = "1.25"
  196. // if no version from server, expect the earliest
  197. // version before APIVersion was implemented
  198. expected := "1.24"
  199. // test downgrade
  200. client.NegotiateAPIVersionPing(ping)
  201. assert.Check(t, is.Equal(expected, client.version))
  202. }
  203. // TestNegotiateAPIVersion asserts that client.Client can
  204. // negotiate a compatible APIVersion with the server
  205. func TestNegotiateAPIVersion(t *testing.T) {
  206. client, err := NewClientWithOpts(FromEnv)
  207. assert.NilError(t, err)
  208. expected := "1.21"
  209. ping := types.Ping{
  210. APIVersion: expected,
  211. OSType: "linux",
  212. Experimental: false,
  213. }
  214. // set our version to something new
  215. client.version = "1.22"
  216. // test downgrade
  217. client.NegotiateAPIVersionPing(ping)
  218. assert.Check(t, is.Equal(expected, client.version))
  219. // set the client version to something older, and verify that we keep the
  220. // original setting.
  221. expected = "1.20"
  222. client.version = expected
  223. client.NegotiateAPIVersionPing(ping)
  224. assert.Check(t, is.Equal(expected, client.version))
  225. }
  226. // TestNegotiateAPIVersionOverride asserts that we honor
  227. // the environment variable DOCKER_API_VERSION when negotiating versions
  228. func TestNegotiateAPVersionOverride(t *testing.T) {
  229. expected := "9.99"
  230. defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": expected})()
  231. client, err := NewClientWithOpts(FromEnv)
  232. assert.NilError(t, err)
  233. ping := types.Ping{
  234. APIVersion: "1.24",
  235. OSType: "linux",
  236. Experimental: false,
  237. }
  238. // test that we honored the env var
  239. client.NegotiateAPIVersionPing(ping)
  240. assert.Check(t, is.Equal(expected, client.version))
  241. }
  242. func TestNegotiateAPIVersionAutomatic(t *testing.T) {
  243. var pingVersion string
  244. httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
  245. resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}}
  246. resp.Header.Set("API-Version", pingVersion)
  247. resp.Body = io.NopCloser(strings.NewReader("OK"))
  248. return resp, nil
  249. })
  250. client, err := NewClientWithOpts(
  251. WithHTTPClient(httpClient),
  252. WithAPIVersionNegotiation(),
  253. )
  254. assert.NilError(t, err)
  255. ctx := context.Background()
  256. assert.Equal(t, client.ClientVersion(), api.DefaultVersion)
  257. // First request should trigger negotiation
  258. pingVersion = "1.35"
  259. _, _ = client.Info(ctx)
  260. assert.Equal(t, client.ClientVersion(), "1.35")
  261. // Once successfully negotiated, subsequent requests should not re-negotiate
  262. pingVersion = "1.25"
  263. _, _ = client.Info(ctx)
  264. assert.Equal(t, client.ClientVersion(), "1.35")
  265. }
  266. // TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client
  267. // with an empty version string does still allow API-version negotiation
  268. func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) {
  269. client, err := NewClientWithOpts(WithVersion(""))
  270. assert.NilError(t, err)
  271. client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.35"})
  272. assert.Equal(t, client.version, "1.35")
  273. }
  274. // TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client
  275. // with an fixed version disables API-version negotiation
  276. func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) {
  277. client, err := NewClientWithOpts(WithVersion("1.35"))
  278. assert.NilError(t, err)
  279. client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.31"})
  280. assert.Equal(t, client.version, "1.35")
  281. }
  282. type roundTripFunc func(*http.Request) (*http.Response, error)
  283. func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
  284. return rtf(req)
  285. }
  286. type bytesBufferClose struct {
  287. *bytes.Buffer
  288. }
  289. func (bbc bytesBufferClose) Close() error {
  290. return nil
  291. }
  292. func TestClientRedirect(t *testing.T) {
  293. client := &http.Client{
  294. CheckRedirect: CheckRedirect,
  295. Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
  296. if req.URL.String() == "/bla" {
  297. return &http.Response{StatusCode: 404}, nil
  298. }
  299. return &http.Response{
  300. StatusCode: 301,
  301. Header: map[string][]string{"Location": {"/bla"}},
  302. Body: bytesBufferClose{bytes.NewBuffer(nil)},
  303. }, nil
  304. }),
  305. }
  306. cases := []struct {
  307. httpMethod string
  308. expectedErr *url.Error
  309. statusCode int
  310. }{
  311. {http.MethodGet, nil, 301},
  312. {http.MethodPost, &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, 301},
  313. {http.MethodPut, &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, 301},
  314. {http.MethodDelete, &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, 301},
  315. }
  316. for _, tc := range cases {
  317. req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
  318. assert.Check(t, err)
  319. resp, err := client.Do(req)
  320. assert.Check(t, is.Equal(tc.statusCode, resp.StatusCode))
  321. if tc.expectedErr == nil {
  322. assert.Check(t, is.Nil(err))
  323. } else {
  324. urlError, ok := err.(*url.Error)
  325. assert.Assert(t, ok, "%T is not *url.Error", err)
  326. assert.Check(t, is.Equal(*tc.expectedErr, *urlError))
  327. }
  328. }
  329. }