client_test.go 8.7 KB

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