Skip to content

Commit 8818275

Browse files
authored
Merge pull request #1093 from zhuangqh/enhance-mock-test
test:add container_top test
2 parents 6678f07 + 860aac5 commit 8818275

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

client/container.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net"
88
"net/url"
9-
"strings"
109

1110
"github.com/alibaba/pouch/apis/types"
1211
)
@@ -100,24 +99,6 @@ func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, conf
10099
return err
101100
}
102101

103-
// ContainerTop shows process information from within a container.
104-
func (client *APIClient) ContainerTop(ctx context.Context, name string, arguments []string) (types.ContainerProcessList, error) {
105-
response := types.ContainerProcessList{}
106-
query := url.Values{}
107-
if len(arguments) > 0 {
108-
query.Set("ps_args", strings.Join(arguments, " "))
109-
}
110-
111-
resp, err := client.get(ctx, "/containers/"+name+"/top", query, nil)
112-
if err != nil {
113-
return response, err
114-
}
115-
116-
err = decodeBody(&response, resp.Body)
117-
ensureCloseReader(resp)
118-
return response, err
119-
}
120-
121102
// ContainerLogs return the logs generated by a container in an io.ReadCloser.
122103
func (client *APIClient) ContainerLogs(ctx context.Context, name string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
123104
query := url.Values{}

client/container_top.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"strings"
7+
8+
"github.com/alibaba/pouch/apis/types"
9+
)
10+
11+
// ContainerTop shows process information from within a container.
12+
func (client *APIClient) ContainerTop(ctx context.Context, name string, arguments []string) (types.ContainerProcessList, error) {
13+
response := types.ContainerProcessList{}
14+
query := url.Values{}
15+
if len(arguments) > 0 {
16+
query.Set("ps_args", strings.Join(arguments, " "))
17+
}
18+
19+
resp, err := client.get(ctx, "/containers/"+name+"/top", query, nil)
20+
if err != nil {
21+
return response, err
22+
}
23+
24+
err = decodeBody(&response, resp.Body)
25+
ensureCloseReader(resp)
26+
return response, err
27+
}

client/container_top_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 TestContainerTopError(t *testing.T) {
17+
client := &APIClient{
18+
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
19+
}
20+
_, err := client.ContainerTop(context.Background(), "nothing", []string{})
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 TestContainerTop(t *testing.T) {
27+
expectedURL := "/containers/container_id/top"
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+
query := req.URL.Query()
34+
args := query.Get("ps_args")
35+
if args != "arg1 arg2" {
36+
return nil, fmt.Errorf("args not set in URL properly. Expected 'arg1 arg2', got %s", args)
37+
}
38+
psListJSON := types.ContainerProcessList{
39+
Processes: [][]string{{"bar11", "bar12"}, {"bar21", "bar22"}},
40+
Titles: []string{"foo1", "foo2"},
41+
}
42+
b, err := json.Marshal(psListJSON)
43+
if err != nil {
44+
return nil, err
45+
}
46+
return &http.Response{
47+
StatusCode: http.StatusOK,
48+
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
49+
}, nil
50+
})
51+
client := &APIClient{
52+
HTTPCli: httpClient,
53+
}
54+
psList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"})
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
if len(psList.Titles) != 2 {
60+
t.Fatalf("expected 2 titles, got %v", len(psList.Titles))
61+
}
62+
for _, ps := range psList.Processes {
63+
if len(ps) != len(psList.Titles) {
64+
t.Fatalf("expected 2 values, got %v", len(ps))
65+
break
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)