client_test.go 8.5 KB

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