-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1049 from Dewey-Ding/master
test:add container list test
- Loading branch information
Showing
3 changed files
with
94 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |