image_build_test.go 6.3 KB

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