本文整理汇总了Golang中github.com/docker/engine-api/client/transport.NewMockClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMockClient函数的具体用法?Golang NewMockClient怎么用?Golang NewMockClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMockClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestImageHistory
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)
}
}
示例2: TestContainerDiff
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)
}
}
示例3: TestContainerExecStart
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)
}
}
示例4: TestImageSave
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))
}
}
示例5: TestContainerUpdate
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)
}
}
示例6: TestContainerExecInspect
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)
}
}
示例7: TestContainerInspect
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)
}
}
示例8: TestContainerCreateWithName
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)
}
}
示例9: TestContainerWait
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)
}
}
示例10: TestImageInspect
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)
}
}
}
示例11: TestImageBuildError
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)
}
}
示例12: TestContainerExecCreateError
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)
}
}
示例13: TestContainerUnpauseError
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)
}
}
示例14: TestContainerStatsError
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)
}
}
示例15: TestContainerCreateImageNotFound
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)
}
}