image_build_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/registry"
  14. "github.com/docker/docker/errdefs"
  15. units "github.com/docker/go-units"
  16. "gotest.tools/v3/assert"
  17. is "gotest.tools/v3/assert/cmp"
  18. )
  19. func TestImageBuildError(t *testing.T) {
  20. client := &Client{
  21. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  22. }
  23. _, err := client.ImageBuild(context.Background(), nil, types.ImageBuildOptions{})
  24. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  25. }
  26. func TestImageBuild(t *testing.T) {
  27. v1 := "value1"
  28. v2 := "value2"
  29. emptyRegistryConfig := "bnVsbA=="
  30. buildCases := []struct {
  31. buildOptions types.ImageBuildOptions
  32. expectedQueryParams map[string]string
  33. expectedTags []string
  34. expectedRegistryConfig string
  35. }{
  36. {
  37. buildOptions: types.ImageBuildOptions{
  38. SuppressOutput: true,
  39. NoCache: true,
  40. Remove: true,
  41. ForceRemove: true,
  42. PullParent: true,
  43. },
  44. expectedQueryParams: map[string]string{
  45. "q": "1",
  46. "nocache": "1",
  47. "rm": "1",
  48. "forcerm": "1",
  49. "pull": "1",
  50. },
  51. expectedTags: []string{},
  52. expectedRegistryConfig: emptyRegistryConfig,
  53. },
  54. {
  55. buildOptions: types.ImageBuildOptions{
  56. SuppressOutput: false,
  57. NoCache: false,
  58. Remove: false,
  59. ForceRemove: false,
  60. PullParent: false,
  61. },
  62. expectedQueryParams: map[string]string{
  63. "q": "",
  64. "nocache": "",
  65. "rm": "0",
  66. "forcerm": "",
  67. "pull": "",
  68. },
  69. expectedTags: []string{},
  70. expectedRegistryConfig: emptyRegistryConfig,
  71. },
  72. {
  73. buildOptions: types.ImageBuildOptions{
  74. RemoteContext: "remoteContext",
  75. Isolation: container.Isolation("isolation"),
  76. CPUSetCPUs: "2",
  77. CPUSetMems: "12",
  78. CPUShares: 20,
  79. CPUQuota: 10,
  80. CPUPeriod: 30,
  81. Memory: 256,
  82. MemorySwap: 512,
  83. ShmSize: 10,
  84. CgroupParent: "cgroup_parent",
  85. Dockerfile: "Dockerfile",
  86. },
  87. expectedQueryParams: map[string]string{
  88. "remote": "remoteContext",
  89. "isolation": "isolation",
  90. "cpusetcpus": "2",
  91. "cpusetmems": "12",
  92. "cpushares": "20",
  93. "cpuquota": "10",
  94. "cpuperiod": "30",
  95. "memory": "256",
  96. "memswap": "512",
  97. "shmsize": "10",
  98. "cgroupparent": "cgroup_parent",
  99. "dockerfile": "Dockerfile",
  100. "rm": "0",
  101. },
  102. expectedTags: []string{},
  103. expectedRegistryConfig: emptyRegistryConfig,
  104. },
  105. {
  106. buildOptions: types.ImageBuildOptions{
  107. BuildArgs: map[string]*string{
  108. "ARG1": &v1,
  109. "ARG2": &v2,
  110. "ARG3": nil,
  111. },
  112. },
  113. expectedQueryParams: map[string]string{
  114. "buildargs": `{"ARG1":"value1","ARG2":"value2","ARG3":null}`,
  115. "rm": "0",
  116. },
  117. expectedTags: []string{},
  118. expectedRegistryConfig: emptyRegistryConfig,
  119. },
  120. {
  121. buildOptions: types.ImageBuildOptions{
  122. Ulimits: []*units.Ulimit{
  123. {
  124. Name: "nproc",
  125. Hard: 65557,
  126. Soft: 65557,
  127. },
  128. {
  129. Name: "nofile",
  130. Hard: 20000,
  131. Soft: 40000,
  132. },
  133. },
  134. },
  135. expectedQueryParams: map[string]string{
  136. "ulimits": `[{"Name":"nproc","Hard":65557,"Soft":65557},{"Name":"nofile","Hard":20000,"Soft":40000}]`,
  137. "rm": "0",
  138. },
  139. expectedTags: []string{},
  140. expectedRegistryConfig: emptyRegistryConfig,
  141. },
  142. {
  143. buildOptions: types.ImageBuildOptions{
  144. AuthConfigs: map[string]registry.AuthConfig{
  145. "https://index.docker.io/v1/": {
  146. Auth: "dG90bwo=",
  147. },
  148. },
  149. },
  150. expectedQueryParams: map[string]string{
  151. "rm": "0",
  152. },
  153. expectedTags: []string{},
  154. expectedRegistryConfig: "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289In19",
  155. },
  156. }
  157. for _, buildCase := range buildCases {
  158. expectedURL := "/build"
  159. client := &Client{
  160. client: newMockClient(func(r *http.Request) (*http.Response, error) {
  161. if !strings.HasPrefix(r.URL.Path, expectedURL) {
  162. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
  163. }
  164. // Check request headers
  165. registryConfig := r.Header.Get("X-Registry-Config")
  166. if registryConfig != buildCase.expectedRegistryConfig {
  167. return nil, fmt.Errorf("X-Registry-Config header not properly set in the request. Expected '%s', got %s", buildCase.expectedRegistryConfig, registryConfig)
  168. }
  169. contentType := r.Header.Get("Content-Type")
  170. if contentType != "application/x-tar" {
  171. return nil, fmt.Errorf("Content-type header not properly set in the request. Expected 'application/x-tar', got %s", contentType)
  172. }
  173. // Check query parameters
  174. query := r.URL.Query()
  175. for key, expected := range buildCase.expectedQueryParams {
  176. actual := query.Get(key)
  177. if actual != expected {
  178. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  179. }
  180. }
  181. // Check tags
  182. if len(buildCase.expectedTags) > 0 {
  183. tags := query["t"]
  184. if !reflect.DeepEqual(tags, buildCase.expectedTags) {
  185. return nil, fmt.Errorf("t (tags) not set in URL query properly. Expected '%s', got %s", buildCase.expectedTags, tags)
  186. }
  187. }
  188. headers := http.Header{}
  189. headers.Add("Server", "Docker/v1.23 (MyOS)")
  190. return &http.Response{
  191. StatusCode: http.StatusOK,
  192. Body: io.NopCloser(bytes.NewReader([]byte("body"))),
  193. Header: headers,
  194. }, nil
  195. }),
  196. }
  197. buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. if buildResponse.OSType != "MyOS" {
  202. t.Fatalf("expected OSType to be 'MyOS', got %s", buildResponse.OSType)
  203. }
  204. response, err := io.ReadAll(buildResponse.Body)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. buildResponse.Body.Close()
  209. if string(response) != "body" {
  210. t.Fatalf("expected Body to contain 'body' string, got %s", response)
  211. }
  212. }
  213. }
  214. func TestGetDockerOS(t *testing.T) {
  215. cases := map[string]string{
  216. "Docker/v1.22 (linux)": "linux",
  217. "Docker/v1.22 (windows)": "windows",
  218. "Foo/v1.22 (bar)": "",
  219. }
  220. for header, os := range cases {
  221. g := getDockerOS(header)
  222. if g != os {
  223. t.Fatalf("Expected %s, got %s", os, g)
  224. }
  225. }
  226. }