Skip to content

Commit

Permalink
Merge pull request #1049 from Dewey-Ding/master
Browse files Browse the repository at this point in the history
test:add container list test
  • Loading branch information
allencloud authored Apr 3, 2018
2 parents b1b2207 + c37a92c commit 97fbd00
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 19 deletions.
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)
}
}

0 comments on commit 97fbd00

Please sign in to comment.