Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test:add container list test #1049

Merged
merged 1 commit into from
Apr 3, 2018
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
19 changes: 0 additions & 19 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,6 @@ func (client *APIClient) ContainerRemove(ctx context.Context, name string, force
return nil
}

// ContainerList returns the list of containers.
func (client *APIClient) ContainerList(ctx context.Context, all bool) ([]*types.Container, error) {
q := url.Values{}
if all {
q.Set("all", "true")
}

resp, err := client.get(ctx, "/containers/json", q, nil)
if err != nil {
return nil, err
}

containers := []*types.Container{}
err = decodeBody(&containers, resp.Body)
ensureCloseReader(resp)

return containers, err
}

// ContainerAttach attach a container
func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) {
q := url.Values{}
Expand Down
27 changes: 27 additions & 0 deletions client/container_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"context"
"net/url"

"github.com/alibaba/pouch/apis/types"
)

// ContainerList returns the list of containers.
func (client *APIClient) ContainerList(ctx context.Context, all bool) ([]*types.Container, error) {
q := url.Values{}
if all {
q.Set("all", "true")
}

resp, err := client.get(ctx, "/containers/json", q, nil)
if err != nil {
return nil, err
}

containers := []*types.Container{}
err = decodeBody(&containers, resp.Body)
ensureCloseReader(resp)

return containers, err
}
67 changes: 67 additions & 0 deletions client/container_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/alibaba/pouch/apis/types"
)

func TestContainerListError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerList(context.Background(), true)
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerList(t *testing.T) {
expectedURL := "/containers/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)
}
query := req.URL.Query()
all := query.Get("all")
if all != "true" {
return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
}
containersJSON := []types.ContainerJSON{
{
Name: "container1",
Image: "Image1",
},
{
Name: "container1",
Image: "Image1",
},
}
b, err := json.Marshal(containersJSON)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
}, nil
})
client := &APIClient{
HTTPCli: httpClient,
}
containers, err := client.ContainerList(context.Background(), true)
if err != nil {
t.Fatal(err)
}
if len(containers) != 2 {
t.Fatalf("expected 2 containers, got %v", containers)
}
}