Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
21 changes: 21 additions & 0 deletions client/container_get.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions client/container_get_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}