12 "golang.org/x/net/context"
14 "github.com/docker/docker/api/types"
15 "github.com/docker/docker/api/types/container"
16 "github.com/docker/go-units"
19 func TestImageBuildError(t *testing.T) {
21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23 _, err := client.ImageBuild(context.Background(), nil, types.ImageBuildOptions{})
24 if err == nil || err.Error() != "Error response from daemon: Server error" {
25 t.Fatalf("expected a Server Error, got %v", err)
29 func TestImageBuild(t *testing.T) {
32 emptyRegistryConfig := "bnVsbA=="
33 buildCases := []struct {
34 buildOptions types.ImageBuildOptions
35 expectedQueryParams map[string]string
37 expectedRegistryConfig string
40 buildOptions: types.ImageBuildOptions{
47 expectedQueryParams: map[string]string{
54 expectedTags: []string{},
55 expectedRegistryConfig: emptyRegistryConfig,
58 buildOptions: types.ImageBuildOptions{
59 SuppressOutput: false,
65 expectedQueryParams: map[string]string{
72 expectedTags: []string{},
73 expectedRegistryConfig: emptyRegistryConfig,
76 buildOptions: types.ImageBuildOptions{
77 RemoteContext: "remoteContext",
78 Isolation: container.Isolation("isolation"),
87 CgroupParent: "cgroup_parent",
88 Dockerfile: "Dockerfile",
90 expectedQueryParams: map[string]string{
91 "remote": "remoteContext",
92 "isolation": "isolation",
101 "cgroupparent": "cgroup_parent",
102 "dockerfile": "Dockerfile",
105 expectedTags: []string{},
106 expectedRegistryConfig: emptyRegistryConfig,
109 buildOptions: types.ImageBuildOptions{
110 BuildArgs: map[string]*string{
116 expectedQueryParams: map[string]string{
117 "buildargs": `{"ARG1":"value1","ARG2":"value2","ARG3":null}`,
120 expectedTags: []string{},
121 expectedRegistryConfig: emptyRegistryConfig,
124 buildOptions: types.ImageBuildOptions{
125 Ulimits: []*units.Ulimit{
138 expectedQueryParams: map[string]string{
139 "ulimits": `[{"Name":"nproc","Hard":65557,"Soft":65557},{"Name":"nofile","Hard":20000,"Soft":40000}]`,
142 expectedTags: []string{},
143 expectedRegistryConfig: emptyRegistryConfig,
146 buildOptions: types.ImageBuildOptions{
147 AuthConfigs: map[string]types.AuthConfig{
148 "https://index.docker.io/v1/": {
153 expectedQueryParams: map[string]string{
156 expectedTags: []string{},
157 expectedRegistryConfig: "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289In19",
160 for _, buildCase := range buildCases {
161 expectedURL := "/build"
163 client: newMockClient(func(r *http.Request) (*http.Response, error) {
164 if !strings.HasPrefix(r.URL.Path, expectedURL) {
165 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
167 // Check request headers
168 registryConfig := r.Header.Get("X-Registry-Config")
169 if registryConfig != buildCase.expectedRegistryConfig {
170 return nil, fmt.Errorf("X-Registry-Config header not properly set in the request. Expected '%s', got %s", buildCase.expectedRegistryConfig, registryConfig)
172 contentType := r.Header.Get("Content-Type")
173 if contentType != "application/x-tar" {
174 return nil, fmt.Errorf("Content-type header not properly set in the request. Expected 'application/x-tar', got %s", contentType)
177 // Check query parameters
178 query := r.URL.Query()
179 for key, expected := range buildCase.expectedQueryParams {
180 actual := query.Get(key)
181 if actual != expected {
182 return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
187 if len(buildCase.expectedTags) > 0 {
189 if !reflect.DeepEqual(tags, buildCase.expectedTags) {
190 return nil, fmt.Errorf("t (tags) not set in URL query properly. Expected '%s', got %s", buildCase.expectedTags, tags)
194 headers := http.Header{}
195 headers.Add("Server", "Docker/v1.23 (MyOS)")
196 return &http.Response{
197 StatusCode: http.StatusOK,
198 Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
203 buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions)
207 if buildResponse.OSType != "MyOS" {
208 t.Fatalf("expected OSType to be 'MyOS', got %s", buildResponse.OSType)
210 response, err := ioutil.ReadAll(buildResponse.Body)
214 buildResponse.Body.Close()
215 if string(response) != "body" {
216 t.Fatalf("expected Body to contain 'body' string, got %s", response)
221 func TestGetDockerOS(t *testing.T) {
222 cases := map[string]string{
223 "Docker/v1.22 (linux)": "linux",
224 "Docker/v1.22 (windows)": "windows",
225 "Foo/v1.22 (bar)": "",
227 for header, os := range cases {
228 g := getDockerOS(header)
230 t.Fatalf("Expected %s, got %s", os, g)