Esempio n. 1
0
func TestImageHistory(t *testing.T) {
	expectedURL := "/images/image_id/history"
	client := &Client{
		transport: transport.NewMockClient(nil, func(r *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(r.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
			}
			b, err := json.Marshal([]types.ImageHistory{
				{
					ID:   "image_id1",
					Tags: []string{"tag1", "tag2"},
				},
				{
					ID:   "image_id2",
					Tags: []string{"tag1", "tag2"},
				},
			})
			if err != nil {
				return nil, err
			}

			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(b)),
			}, nil
		}),
	}
	imageHistories, err := client.ImageHistory("image_id")
	if err != nil {
		t.Fatal(err)
	}
	if len(imageHistories) != 2 {
		t.Fatalf("expected 2 containers, got %v", imageHistories)
	}
}
Esempio n. 2
0
func TestContainerDiff(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			b, err := json.Marshal([]types.ContainerChange{
				{
					Kind: 0,
					Path: "/path/1",
				},
				{
					Kind: 1,
					Path: "/path/2",
				},
			})
			if err != nil {
				return nil, err
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(b)),
			}, nil
		}),
	}

	changes, err := client.ContainerDiff("container_id")
	if err != nil {
		t.Fatal(err)
	}
	if len(changes) != 2 {
		t.Fatalf("expected an array of 2 changes, got %v", changes)
	}
}
Esempio n. 3
0
func TestContainerExecStart(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			if err := req.ParseForm(); err != nil {
				return nil, err
			}
			execStartCheck := &types.ExecStartCheck{}
			if err := json.NewDecoder(req.Body).Decode(execStartCheck); err != nil {
				return nil, err
			}
			if execStartCheck.Tty || !execStartCheck.Detach {
				return nil, fmt.Errorf("expected execStartCheck{Detach:true,Tty:false}, got %v", execStartCheck)
			}

			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
			}, nil
		}),
	}

	err := client.ContainerExecStart("exec_id", types.ExecStartCheck{
		Detach: true,
		Tty:    false,
	})
	if err != nil {
		t.Fatal(err)
	}
}
Esempio n. 4
0
func TestImageSave(t *testing.T) {
	expectedURL := "/images/get"
	client := &Client{
		transport: transport.NewMockClient(nil, func(r *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(r.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
			}
			query := r.URL.Query()
			names := query["names"]
			expectedNames := []string{"image_id1", "image_id2"}
			if !reflect.DeepEqual(names, expectedNames) {
				return nil, fmt.Errorf("names not set in URL query properly. Expected %v, got %v", names, expectedNames)
			}

			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte("response"))),
			}, nil
		}),
	}
	saveResponse, err := client.ImageSave(context.Background(), []string{"image_id1", "image_id2"})
	if err != nil {
		t.Fatal(err)
	}
	response, err := ioutil.ReadAll(saveResponse)
	if err != nil {
		t.Fatal(err)
	}
	saveResponse.Close()
	if string(response) != "response" {
		t.Fatalf("expected response to contain 'response', got %s", string(response))
	}
}
func TestContainerUpdate(t *testing.T) {
	expectedURL := "/containers/container_id/update"
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(req.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
			}, nil
		}),
	}

	err := client.ContainerUpdate("container_id", container.UpdateConfig{
		Resources: container.Resources{
			CPUPeriod: 1,
		},
		RestartPolicy: container.RestartPolicy{
			Name: "always",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
}
Esempio n. 6
0
func TestContainerExecInspect(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			b, err := json.Marshal(types.ContainerExecInspect{
				ExecID:      "exec_id",
				ContainerID: "container_id",
			})
			if err != nil {
				return nil, err
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(b)),
			}, nil
		}),
	}

	inspect, err := client.ContainerExecInspect("exec_id")
	if err != nil {
		t.Fatal(err)
	}
	if inspect.ExecID != "exec_id" {
		t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID)
	}
	if inspect.ContainerID != "container_id" {
		t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID)
	}
}
func TestContainerInspect(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			content, err := json.Marshal(types.ContainerJSON{
				ContainerJSONBase: &types.ContainerJSONBase{
					ID:    "container_id",
					Image: "image",
					Name:  "name",
				},
			})
			if err != nil {
				return nil, err
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(content)),
			}, nil
		}),
	}

	r, err := client.ContainerInspect("container_id")
	if err != nil {
		t.Fatal(err)
	}
	if r.ID != "container_id" {
		t.Fatalf("expected `container_id`, got %s", r.ID)
	}
	if r.Image != "image" {
		t.Fatalf("expected `image`, got %s", r.ID)
	}
	if r.Name != "name" {
		t.Fatalf("expected `name`, got %s", r.ID)
	}
}
func TestContainerCreateWithName(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			name := req.URL.Query().Get("name")
			if name != "container_name" {
				return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
			}
			b, err := json.Marshal(types.ContainerCreateResponse{
				ID: "container_id",
			})
			if err != nil {
				return nil, err
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(b)),
			}, nil
		}),
	}

	r, err := client.ContainerCreate(nil, nil, nil, "container_name")
	if err != nil {
		t.Fatal(err)
	}
	if r.ID != "container_id" {
		t.Fatalf("expected `container_id`, got %s", r.ID)
	}
}
Esempio n. 9
0
func TestContainerWait(t *testing.T) {
	expectedURL := "/containers/container_id/wait"
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(req.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
			}
			b, err := json.Marshal(types.ContainerWaitResponse{
				StatusCode: 15,
			})
			if err != nil {
				return nil, err
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader(b)),
			}, nil
		}),
	}

	code, err := client.ContainerWait(context.Background(), "container_id")
	if err != nil {
		t.Fatal(err)
	}
	if code != 15 {
		t.Fatalf("expected a status code equal to '15', got %d", code)
	}
}
Esempio n. 10
0
func TestImageInspect(t *testing.T) {
	expectedURL := "/images/image_id/json"
	expectedTags := []string{"tag1", "tag2"}
	inspectCases := []struct {
		size                bool
		expectedQueryParams map[string]string
	}{
		{
			size: true,
			expectedQueryParams: map[string]string{
				"size": "1",
			},
		},
		{
			size: false,
			expectedQueryParams: map[string]string{
				"size": "",
			},
		},
	}
	for _, inspectCase := range inspectCases {
		client := &Client{
			transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
				if !strings.HasPrefix(req.URL.Path, expectedURL) {
					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
				}
				query := req.URL.Query()
				for key, expected := range inspectCase.expectedQueryParams {
					actual := query.Get(key)
					if actual != expected {
						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
					}
				}
				content, err := json.Marshal(types.ImageInspect{
					ID:       "image_id",
					RepoTags: expectedTags,
				})
				if err != nil {
					return nil, err
				}
				return &http.Response{
					StatusCode: http.StatusOK,
					Body:       ioutil.NopCloser(bytes.NewReader(content)),
				}, nil
			}),
		}

		imageInspect, _, err := client.ImageInspectWithRaw("image_id", inspectCase.size)
		if err != nil {
			t.Fatal(err)
		}
		if imageInspect.ID != "image_id" {
			t.Fatalf("expected `image_id`, got %s", imageInspect.ID)
		}
		if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) {
			t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags)
		}
	}
}
Esempio n. 11
0
func TestImageBuildError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	_, err := client.ImageBuild(context.Background(), types.ImageBuildOptions{})
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 12
0
func TestContainerExecCreateError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	_, err := client.ContainerExecCreate(types.ExecConfig{})
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 13
0
func TestContainerUnpauseError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	err := client.ContainerUnpause("nothing")
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 14
0
func TestContainerStatsError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	_, err := client.ContainerStats(context.Background(), "nothing", false)
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 15
0
func TestContainerCreateImageNotFound(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusNotFound, "No such image")),
	}
	_, err := client.ContainerCreate(&container.Config{Image: "unknown_image"}, nil, nil, "unknown")
	if err == nil || !IsErrImageNotFound(err) {
		t.Fatalf("expected a imageNotFound error, got %v", err)
	}
}
Esempio n. 16
0
func TestContainerCreateError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	_, err := client.ContainerCreate(nil, nil, nil, "nothing")
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}

	// 404 doesn't automagitally means an unknown image
	client = &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusNotFound, "Server error")),
	}
	_, err = client.ContainerCreate(nil, nil, nil, "nothing")
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 17
0
func TestImageImport(t *testing.T) {
	expectedURL := "/images/create"
	client := &Client{
		transport: transport.NewMockClient(nil, func(r *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(r.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
			}
			query := r.URL.Query()
			fromSrc := query.Get("fromSrc")
			if fromSrc != "image_source" {
				return nil, fmt.Errorf("fromSrc not set in URL query properly. Expected 'image_source', got %s", fromSrc)
			}
			repo := query.Get("repo")
			if repo != "repository_name" {
				return nil, fmt.Errorf("repo not set in URL query properly. Expected 'repository_name', got %s", repo)
			}
			tag := query.Get("tag")
			if tag != "imported" {
				return nil, fmt.Errorf("tag not set in URL query properly. Expected 'imported', got %s", tag)
			}
			message := query.Get("message")
			if message != "A message" {
				return nil, fmt.Errorf("message not set in URL query properly. Expected 'A message', got %s", message)
			}
			changes := query["changes"]
			expectedChanges := []string{"change1", "change2"}
			if !reflect.DeepEqual(expectedChanges, changes) {
				return nil, fmt.Errorf("changes not set in URL query properly. Expected %v, got %v", expectedChanges, changes)
			}

			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte("response"))),
			}, nil
		}),
	}
	importResponse, err := client.ImageImport(context.Background(), types.ImageImportOptions{
		Source:         strings.NewReader("source"),
		SourceName:     "image_source",
		RepositoryName: "repository_name",
		Message:        "A message",
		Tag:            "imported",
		Changes:        []string{"change1", "change2"},
	})
	if err != nil {
		t.Fatal(err)
	}
	response, err := ioutil.ReadAll(importResponse)
	if err != nil {
		t.Fatal(err)
	}
	importResponse.Close()
	if string(response) != "response" {
		t.Fatalf("expected response to contain 'response', got %s", string(response))
	}
}
Esempio n. 18
0
func TestImageInspectImageNotFound(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusNotFound, "Server error")),
	}

	_, _, err := client.ImageInspectWithRaw("unknown", true)
	if err == nil || !IsErrImageNotFound(err) {
		t.Fatalf("expected a imageNotFound error, got %v", err)
	}
}
Esempio n. 19
0
func TestImageInspectError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}

	_, _, err := client.ImageInspectWithRaw("nothing", true)
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 20
0
func TestContainerInspectContainerNotFound(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusNotFound, "Server error")),
	}

	_, err := client.ContainerInspect("unknown")
	if err == nil || !IsErrContainerNotFound(err) {
		t.Fatalf("expected a containerNotFound error, got %v", err)
	}
}
Esempio n. 21
0
func TestVolumeListError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}

	_, err := client.VolumeList(filters.NewArgs())
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 22
0
func TestContainerWaitError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, transport.ErrorMock(http.StatusInternalServerError, "Server error")),
	}
	code, err := client.ContainerWait(context.Background(), "nothing")
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
	if code != -1 {
		t.Fatalf("expected a status code equal to '-1', got %d", code)
	}
}
Esempio n. 23
0
func TestImageCreate(t *testing.T) {
	expectedURL := "/images/create"
	expectedImage := "my_image"
	expectedTag := "another:image"
	expectedRegistryAuth := "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0="
	client := &Client{
		transport: transport.NewMockClient(nil, func(r *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(r.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
			}
			registryAuth := r.Header.Get("X-Registry-Auth")
			if registryAuth != expectedRegistryAuth {
				return nil, fmt.Errorf("X-Registry-Auth header not properly set in the request. Expected '%s', got %s", expectedRegistryAuth, registryAuth)
			}

			query := r.URL.Query()
			fromImage := query.Get("fromImage")
			if fromImage != expectedImage {
				return nil, fmt.Errorf("fromImage not set in URL query properly. Expected '%s', got %s", expectedImage, fromImage)
			}

			tag := query.Get("tag")
			if tag != expectedTag {
				return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", expectedTag, tag)
			}

			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
			}, nil
		}),
	}

	createResponse, err := client.ImageCreate(context.Background(), types.ImageCreateOptions{
		Parent:       expectedImage,
		Tag:          expectedTag,
		RegistryAuth: expectedRegistryAuth,
	})
	if err != nil {
		t.Fatal(err)
	}
	response, err := ioutil.ReadAll(createResponse)
	if err != nil {
		t.Fatal(err)
	}
	if err = createResponse.Close(); err != nil {
		t.Fatal(err)
	}
	if string(response) != "body" {
		t.Fatalf("expected Body to contain 'body' string, got %s", response)
	}
}
Esempio n. 24
0
func TestContainerCreateWithName(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, containerCreateWithNameMock),
	}

	r, err := client.ContainerCreate(nil, nil, nil, "container_name")
	if err != nil {
		t.Fatal(err)
	}
	if r.ID != "container_id" {
		t.Fatalf("expected `container_id`, got %s", r.ID)
	}
}
Esempio n. 25
0
func TestContainerExecResize(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, resizeTransport("/exec/exec_id/resize")),
	}

	err := client.ContainerExecResize(types.ResizeOptions{
		ID:     "exec_id",
		Height: 500,
		Width:  600,
	})
	if err != nil {
		t.Fatal(err)
	}
}
func TestContainerInspectContainerNotFound(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			return &http.Response{
				StatusCode: http.StatusNotFound,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
			}, nil
		}),
	}

	_, err := client.ContainerInspect("unknown")
	if err == nil || !IsErrContainerNotFound(err) {
		t.Fatalf("expected a containerNotFound error, got %v", err)
	}
}
func TestContainerInspectError(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			return &http.Response{
				StatusCode: http.StatusInternalServerError,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte("Server error"))),
			}, nil
		}),
	}

	_, err := client.ContainerInspect("nothing")
	if err == nil || err.Error() != "Error response from daemon: Server error" {
		t.Fatalf("expected a Server Error, got %v", err)
	}
}
Esempio n. 28
0
func TestContainerStart(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
			}, nil
		}),
	}

	err := client.ContainerStart("container_id")
	if err != nil {
		t.Fatal(err)
	}
}
Esempio n. 29
0
func TestContainerUnpause(t *testing.T) {
	expectedURL := "/containers/container_id/unpause"
	client := &Client{
		transport: transport.NewMockClient(nil, func(req *http.Request) (*http.Response, error) {
			if !strings.HasPrefix(req.URL.Path, expectedURL) {
				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
			}
			return &http.Response{
				StatusCode: http.StatusOK,
				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
			}, nil
		}),
	}
	err := client.ContainerUnpause("container_id")
	if err != nil {
		t.Fatal(err)
	}
}
Esempio n. 30
0
func TestInfo(t *testing.T) {
	client := &Client{
		transport: transport.NewMockClient(nil, infoMock),
	}

	info, err := client.Info()
	if err != nil {
		t.Fatal(err)
	}

	if info.ID != "daemonID" {
		t.Fatalf("expected daemonID, got %s", info.ID)
	}

	if info.Containers != 3 {
		t.Fatalf("expected 3 containers, got %d", info.Containers)
	}
}