Skip to content

Commit 0dfe9ee

Browse files
committed
test:add container get test
Signed-off-by: Dewey-Ding <[email protected]>
1 parent 282cf37 commit 0dfe9ee

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

client/container.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,6 @@ func (client *APIClient) ContainerStartExec(ctx context.Context, execid string,
121121
return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header)
122122
}
123123

124-
// ContainerGet returns the detailed information of container.
125-
func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) {
126-
resp, err := client.get(ctx, "/containers/"+name+"/json", nil, nil)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
container := types.ContainerJSON{}
132-
err = decodeBody(&container, resp.Body)
133-
ensureCloseReader(resp)
134-
135-
return &container, err
136-
}
137-
138124
// ContainerRestart restarts a running container.
139125
func (client *APIClient) ContainerRestart(ctx context.Context, name string, timeout string) error {
140126
q := url.Values{}

client/container_get.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/alibaba/pouch/apis/types"
7+
)
8+
9+
// ContainerGet returns the detailed information of container.
10+
func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) {
11+
resp, err := client.get(ctx, "/containers/"+name+"/json", nil, nil)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
container := types.ContainerJSON{}
17+
err = decodeBody(&container, resp.Body)
18+
ensureCloseReader(resp)
19+
20+
return &container, err
21+
}

client/container_get_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"strings"
11+
"testing"
12+
13+
"github.com/alibaba/pouch/apis/types"
14+
)
15+
16+
func TestContainerGetError(t *testing.T) {
17+
client := &APIClient{
18+
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
19+
}
20+
_, err := client.ContainerGet(context.Background(), "nothing")
21+
if err == nil || !strings.Contains(err.Error(), "Server error") {
22+
t.Fatalf("expected a Server Error, got %v", err)
23+
}
24+
}
25+
26+
func TestContainerGet(t *testing.T) {
27+
expectedURL := "/containers/container_id/json"
28+
29+
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
30+
if !strings.HasPrefix(req.URL.Path, expectedURL) {
31+
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
32+
}
33+
containerJSON := types.ContainerJSON{
34+
Driver: "Driver",
35+
Image: "Image",
36+
}
37+
b, err := json.Marshal(containerJSON)
38+
if err != nil {
39+
return nil, err
40+
}
41+
return &http.Response{
42+
StatusCode: http.StatusOK,
43+
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
44+
}, nil
45+
})
46+
client := &APIClient{
47+
HTTPCli: httpClient,
48+
}
49+
if _, err := client.ContainerGet(context.Background(), "container_id"); err != nil {
50+
t.Fatal(err)
51+
}
52+
}

0 commit comments

Comments
 (0)