image_build_test.go 6.2 KB

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