diff --git a/client/container.go b/client/container.go index bbc00f853..290add55a 100644 --- a/client/container.go +++ b/client/container.go @@ -121,20 +121,6 @@ func (client *APIClient) ContainerStartExec(ctx context.Context, execid string, return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header) } -// ContainerGet returns the detailed information of container. -func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) { - resp, err := client.get(ctx, "/containers/"+name+"/json", nil, nil) - if err != nil { - return nil, err - } - - container := types.ContainerJSON{} - err = decodeBody(&container, resp.Body) - ensureCloseReader(resp) - - return &container, err -} - // ContainerRestart restarts a running container. func (client *APIClient) ContainerRestart(ctx context.Context, name string, timeout string) error { q := url.Values{} diff --git a/client/container_get.go b/client/container_get.go new file mode 100644 index 000000000..3350a7b72 --- /dev/null +++ b/client/container_get.go @@ -0,0 +1,21 @@ +package client + +import ( + "context" + + "github.com/alibaba/pouch/apis/types" +) + +// ContainerGet returns the detailed information of container. +func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) { + resp, err := client.get(ctx, "/containers/"+name+"/json", nil, nil) + if err != nil { + return nil, err + } + + container := types.ContainerJSON{} + err = decodeBody(&container, resp.Body) + ensureCloseReader(resp) + + return &container, err +} diff --git a/client/container_get_test.go b/client/container_get_test.go new file mode 100644 index 000000000..7cec43e4b --- /dev/null +++ b/client/container_get_test.go @@ -0,0 +1,52 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/alibaba/pouch/apis/types" +) + +func TestContainerGetError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + _, err := client.ContainerGet(context.Background(), "nothing") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerGet(t *testing.T) { + expectedURL := "/containers/container_id/json" + + httpClient := newMockClient(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) + } + containerJSON := types.ContainerJSON{ + Driver: "Driver", + Image: "Image", + } + b, err := json.Marshal(containerJSON) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), + }, nil + }) + client := &APIClient{ + HTTPCli: httpClient, + } + if _, err := client.ContainerGet(context.Background(), "container_id"); err != nil { + t.Fatal(err) + } +}