client_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. package client
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/internal/testutil"
  13. "github.com/gotestyourself/gotestyourself/skip"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestNewEnvClient(t *testing.T) {
  17. skip.IfCondition(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. env := envToMap()
  82. defer mapToEnv(env)
  83. for _, c := range testcases {
  84. mapToEnv(c.envs)
  85. apiclient, err := NewEnvClient()
  86. if c.expectedError != "" {
  87. assert.Error(t, err, c.doc)
  88. assert.Equal(t, c.expectedError, err.Error(), c.doc)
  89. } else {
  90. assert.NoError(t, err, c.doc)
  91. version := apiclient.ClientVersion()
  92. assert.Equal(t, c.expectedVersion, version, c.doc)
  93. }
  94. if c.envs["DOCKER_TLS_VERIFY"] != "" {
  95. // pedantic checking that this is handled correctly
  96. tr := apiclient.client.Transport.(*http.Transport)
  97. assert.NotNil(t, tr.TLSClientConfig, c.doc)
  98. assert.Equal(t, tr.TLSClientConfig.InsecureSkipVerify, false, c.doc)
  99. }
  100. }
  101. }
  102. func TestGetAPIPath(t *testing.T) {
  103. testcases := []struct {
  104. version string
  105. path string
  106. query url.Values
  107. expected string
  108. }{
  109. {"", "/containers/json", nil, "/containers/json"},
  110. {"", "/containers/json", url.Values{}, "/containers/json"},
  111. {"", "/containers/json", url.Values{"s": []string{"c"}}, "/containers/json?s=c"},
  112. {"1.22", "/containers/json", nil, "/v1.22/containers/json"},
  113. {"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
  114. {"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
  115. {"v1.22", "/containers/json", nil, "/v1.22/containers/json"},
  116. {"v1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
  117. {"v1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
  118. {"v1.22", "/networks/kiwl$%^", nil, "/v1.22/networks/kiwl$%25%5E"},
  119. }
  120. for _, testcase := range testcases {
  121. c := Client{version: testcase.version, basePath: "/"}
  122. actual := c.getAPIPath(testcase.path, testcase.query)
  123. assert.Equal(t, actual, testcase.expected)
  124. }
  125. }
  126. func TestParseHost(t *testing.T) {
  127. cases := []struct {
  128. host string
  129. proto string
  130. addr string
  131. base string
  132. err bool
  133. }{
  134. {"", "", "", "", true},
  135. {"foobar", "", "", "", true},
  136. {"foo://bar", "foo", "bar", "", false},
  137. {"tcp://localhost:2476", "tcp", "localhost:2476", "", false},
  138. {"tcp://localhost:2476/path", "tcp", "localhost:2476", "/path", false},
  139. }
  140. for _, cs := range cases {
  141. p, a, b, e := ParseHost(cs.host)
  142. if cs.err {
  143. assert.Error(t, e)
  144. }
  145. assert.Equal(t, cs.proto, p)
  146. assert.Equal(t, cs.addr, a)
  147. assert.Equal(t, cs.base, b)
  148. }
  149. }
  150. func TestParseHostURL(t *testing.T) {
  151. testcases := []struct {
  152. host string
  153. expected *url.URL
  154. expectedErr string
  155. }{
  156. {
  157. host: "",
  158. expectedErr: "unable to parse docker host",
  159. },
  160. {
  161. host: "foobar",
  162. expectedErr: "unable to parse docker host",
  163. },
  164. {
  165. host: "foo://bar",
  166. expected: &url.URL{Scheme: "foo", Host: "bar"},
  167. },
  168. {
  169. host: "tcp://localhost:2476",
  170. expected: &url.URL{Scheme: "tcp", Host: "localhost:2476"},
  171. },
  172. {
  173. host: "tcp://localhost:2476/path",
  174. expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
  175. },
  176. }
  177. for _, testcase := range testcases {
  178. actual, err := ParseHostURL(testcase.host)
  179. if testcase.expectedErr != "" {
  180. testutil.ErrorContains(t, err, testcase.expectedErr)
  181. }
  182. assert.Equal(t, testcase.expected, actual)
  183. }
  184. }
  185. func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
  186. env := envToMap()
  187. defer mapToEnv(env)
  188. envMap := map[string]string{
  189. "DOCKER_HOST": "",
  190. "DOCKER_API_VERSION": "",
  191. "DOCKER_TLS_VERIFY": "",
  192. "DOCKER_CERT_PATH": "",
  193. }
  194. mapToEnv(envMap)
  195. client, err := NewEnvClient()
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. assert.Equal(t, client.version, api.DefaultVersion)
  200. expected := "1.22"
  201. os.Setenv("DOCKER_API_VERSION", expected)
  202. client, err = NewEnvClient()
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. assert.Equal(t, expected, client.version)
  207. }
  208. // TestNegotiateAPIVersionEmpty asserts that client.Client can
  209. // negotiate a compatible APIVersion when omitted
  210. func TestNegotiateAPIVersionEmpty(t *testing.T) {
  211. env := envToMap()
  212. defer mapToEnv(env)
  213. envMap := map[string]string{
  214. "DOCKER_API_VERSION": "",
  215. }
  216. mapToEnv(envMap)
  217. client, err := NewEnvClient()
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. ping := types.Ping{
  222. APIVersion: "",
  223. OSType: "linux",
  224. Experimental: false,
  225. }
  226. // set our version to something new
  227. client.version = "1.25"
  228. // if no version from server, expect the earliest
  229. // version before APIVersion was implemented
  230. expected := "1.24"
  231. // test downgrade
  232. client.NegotiateAPIVersionPing(ping)
  233. assert.Equal(t, expected, client.version)
  234. }
  235. // TestNegotiateAPIVersion asserts that client.Client can
  236. // negotiate a compatible APIVersion with the server
  237. func TestNegotiateAPIVersion(t *testing.T) {
  238. client, err := NewEnvClient()
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. expected := "1.21"
  243. ping := types.Ping{
  244. APIVersion: expected,
  245. OSType: "linux",
  246. Experimental: false,
  247. }
  248. // set our version to something new
  249. client.version = "1.22"
  250. // test downgrade
  251. client.NegotiateAPIVersionPing(ping)
  252. assert.Equal(t, expected, client.version)
  253. // set the client version to something older, and verify that we keep the
  254. // original setting.
  255. expected = "1.20"
  256. client.version = expected
  257. client.NegotiateAPIVersionPing(ping)
  258. assert.Equal(t, expected, client.version)
  259. }
  260. // TestNegotiateAPIVersionOverride asserts that we honor
  261. // the environment variable DOCKER_API_VERSION when negotianing versions
  262. func TestNegotiateAPVersionOverride(t *testing.T) {
  263. env := envToMap()
  264. defer mapToEnv(env)
  265. envMap := map[string]string{
  266. "DOCKER_API_VERSION": "9.99",
  267. }
  268. mapToEnv(envMap)
  269. client, err := NewEnvClient()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. ping := types.Ping{
  274. APIVersion: "1.24",
  275. OSType: "linux",
  276. Experimental: false,
  277. }
  278. expected := envMap["DOCKER_API_VERSION"]
  279. // test that we honored the env var
  280. client.NegotiateAPIVersionPing(ping)
  281. assert.Equal(t, expected, client.version)
  282. }
  283. // mapToEnv takes a map of environment variables and sets them
  284. func mapToEnv(env map[string]string) {
  285. for k, v := range env {
  286. os.Setenv(k, v)
  287. }
  288. }
  289. // envToMap returns a map of environment variables
  290. func envToMap() map[string]string {
  291. env := make(map[string]string)
  292. for _, e := range os.Environ() {
  293. kv := strings.SplitAfterN(e, "=", 2)
  294. env[kv[0]] = kv[1]
  295. }
  296. return env
  297. }
  298. type roundTripFunc func(*http.Request) (*http.Response, error)
  299. func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
  300. return rtf(req)
  301. }
  302. type bytesBufferClose struct {
  303. *bytes.Buffer
  304. }
  305. func (bbc bytesBufferClose) Close() error {
  306. return nil
  307. }
  308. func TestClientRedirect(t *testing.T) {
  309. client := &http.Client{
  310. CheckRedirect: CheckRedirect,
  311. Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
  312. if req.URL.String() == "/bla" {
  313. return &http.Response{StatusCode: 404}, nil
  314. }
  315. return &http.Response{
  316. StatusCode: 301,
  317. Header: map[string][]string{"Location": {"/bla"}},
  318. Body: bytesBufferClose{bytes.NewBuffer(nil)},
  319. }, nil
  320. }),
  321. }
  322. cases := []struct {
  323. httpMethod string
  324. expectedErr error
  325. statusCode int
  326. }{
  327. {http.MethodGet, nil, 301},
  328. {http.MethodPost, &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, 301},
  329. {http.MethodPut, &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, 301},
  330. {http.MethodDelete, &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, 301},
  331. }
  332. for _, tc := range cases {
  333. req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
  334. assert.NoError(t, err)
  335. resp, err := client.Do(req)
  336. assert.Equal(t, tc.expectedErr, err)
  337. assert.Equal(t, tc.statusCode, resp.StatusCode)
  338. }
  339. }