options_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package client
  2. import (
  3. "context"
  4. "net/http"
  5. "runtime"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api"
  9. "gotest.tools/v3/assert"
  10. is "gotest.tools/v3/assert/cmp"
  11. )
  12. func TestOptionWithHostFromEnv(t *testing.T) {
  13. c, err := NewClientWithOpts(WithHostFromEnv())
  14. assert.NilError(t, err)
  15. assert.Check(t, c.client != nil)
  16. assert.Check(t, is.Equal(c.basePath, ""))
  17. if runtime.GOOS == "windows" {
  18. assert.Check(t, is.Equal(c.host, "npipe:////./pipe/docker_engine"))
  19. assert.Check(t, is.Equal(c.proto, "npipe"))
  20. assert.Check(t, is.Equal(c.addr, "//./pipe/docker_engine"))
  21. } else {
  22. assert.Check(t, is.Equal(c.host, "unix:///var/run/docker.sock"))
  23. assert.Check(t, is.Equal(c.proto, "unix"))
  24. assert.Check(t, is.Equal(c.addr, "/var/run/docker.sock"))
  25. }
  26. t.Setenv("DOCKER_HOST", "tcp://foo.example.com:2376/test/")
  27. c, err = NewClientWithOpts(WithHostFromEnv())
  28. assert.NilError(t, err)
  29. assert.Check(t, c.client != nil)
  30. assert.Check(t, is.Equal(c.basePath, "/test/"))
  31. assert.Check(t, is.Equal(c.host, "tcp://foo.example.com:2376/test/"))
  32. assert.Check(t, is.Equal(c.proto, "tcp"))
  33. assert.Check(t, is.Equal(c.addr, "foo.example.com:2376"))
  34. }
  35. func TestOptionWithTimeout(t *testing.T) {
  36. timeout := 10 * time.Second
  37. c, err := NewClientWithOpts(WithTimeout(timeout))
  38. assert.NilError(t, err)
  39. assert.Check(t, c.client != nil)
  40. assert.Equal(t, c.client.Timeout, timeout)
  41. }
  42. func TestOptionWithVersionFromEnv(t *testing.T) {
  43. c, err := NewClientWithOpts(WithVersionFromEnv())
  44. assert.NilError(t, err)
  45. assert.Check(t, c.client != nil)
  46. assert.Equal(t, c.version, api.DefaultVersion)
  47. assert.Equal(t, c.manualOverride, false)
  48. t.Setenv("DOCKER_API_VERSION", "2.9999")
  49. c, err = NewClientWithOpts(WithVersionFromEnv())
  50. assert.NilError(t, err)
  51. assert.Check(t, c.client != nil)
  52. assert.Equal(t, c.version, "2.9999")
  53. assert.Equal(t, c.manualOverride, true)
  54. }
  55. func TestWithUserAgent(t *testing.T) {
  56. const userAgent = "Magic-Client/v1.2.3"
  57. t.Run("user-agent", func(t *testing.T) {
  58. c, err := NewClientWithOpts(
  59. WithUserAgent(userAgent),
  60. WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
  61. assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
  62. return &http.Response{StatusCode: http.StatusOK}, nil
  63. })),
  64. )
  65. assert.Check(t, err)
  66. _, err = c.Ping(context.Background())
  67. assert.Check(t, err)
  68. assert.Check(t, c.Close())
  69. })
  70. t.Run("user-agent and custom headers", func(t *testing.T) {
  71. c, err := NewClientWithOpts(
  72. WithUserAgent(userAgent),
  73. WithHTTPHeaders(map[string]string{"User-Agent": "should-be-ignored/1.0.0", "Other-Header": "hello-world"}),
  74. WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
  75. assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
  76. assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
  77. return &http.Response{StatusCode: http.StatusOK}, nil
  78. })),
  79. )
  80. assert.Check(t, err)
  81. _, err = c.Ping(context.Background())
  82. assert.Check(t, err)
  83. assert.Check(t, c.Close())
  84. })
  85. t.Run("custom headers", func(t *testing.T) {
  86. c, err := NewClientWithOpts(
  87. WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
  88. WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
  89. assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "from-custom-headers/1.0.0"))
  90. assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
  91. return &http.Response{StatusCode: http.StatusOK}, nil
  92. })),
  93. )
  94. assert.Check(t, err)
  95. _, err = c.Ping(context.Background())
  96. assert.Check(t, err)
  97. assert.Check(t, c.Close())
  98. })
  99. t.Run("no user-agent set", func(t *testing.T) {
  100. c, err := NewClientWithOpts(
  101. WithHTTPHeaders(map[string]string{"Other-Header": "hello-world"}),
  102. WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
  103. assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
  104. assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
  105. return &http.Response{StatusCode: http.StatusOK}, nil
  106. })),
  107. )
  108. assert.Check(t, err)
  109. _, err = c.Ping(context.Background())
  110. assert.Check(t, err)
  111. assert.Check(t, c.Close())
  112. })
  113. t.Run("reset custom user-agent", func(t *testing.T) {
  114. c, err := NewClientWithOpts(
  115. WithUserAgent(""),
  116. WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
  117. WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) {
  118. assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
  119. assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
  120. return &http.Response{StatusCode: http.StatusOK}, nil
  121. })),
  122. )
  123. assert.Check(t, err)
  124. _, err = c.Ping(context.Background())
  125. assert.Check(t, err)
  126. assert.Check(t, c.Close())
  127. })
  128. }