Skip to content

Commit 85d808d

Browse files
authored
Merge pull request #1015 from ZouRui89/mock
test: add mock test for system operations on client side
2 parents caf4ea8 + 53247a3 commit 85d808d

File tree

8 files changed

+240
-54
lines changed

8 files changed

+240
-54
lines changed

client/registry_login.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/alibaba/pouch/apis/types"
8+
)
9+
10+
// RegistryLogin authenticates the server with a given registry to login.
11+
func (client *APIClient) RegistryLogin(ctx context.Context, auth *types.AuthConfig) (*types.AuthResponse, error) {
12+
resp, err := client.post(ctx, "/auth", nil, auth, nil)
13+
if err != nil || resp.StatusCode == http.StatusUnauthorized {
14+
return nil, err
15+
}
16+
17+
authResp := &types.AuthResponse{}
18+
err = decodeBody(authResp, resp.Body)
19+
ensureCloseReader(resp)
20+
21+
return authResp, err
22+
}

client/registry_login_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestRegistryLoginError(t *testing.T) {
18+
client := &APIClient{
19+
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
20+
}
21+
loginConfig := types.AuthConfig{}
22+
_, err := client.RegistryLogin(context.Background(), &loginConfig)
23+
if err == nil || !strings.Contains(err.Error(), "Server error") {
24+
t.Fatalf("expected a Server Error, got %v", err)
25+
}
26+
}
27+
28+
func TestRegistryLogin(t *testing.T) {
29+
expectedURL := "/auth"
30+
31+
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
32+
if !strings.HasPrefix(req.URL.Path, expectedURL) {
33+
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
34+
}
35+
if req.Header.Get("Content-Type") == "application/json" {
36+
loginConfig := types.AuthConfig{}
37+
if err := json.NewDecoder(req.Body).Decode(&loginConfig); err != nil {
38+
return nil, fmt.Errorf("failed to parse json: %v", err)
39+
}
40+
}
41+
auth, err := json.Marshal(types.AuthResponse{
42+
IdentityToken: "aaa",
43+
Status: "bbb",
44+
})
45+
if err != nil {
46+
return nil, err
47+
}
48+
return &http.Response{
49+
StatusCode: http.StatusOK,
50+
Body: ioutil.NopCloser(bytes.NewReader([]byte(auth))),
51+
}, nil
52+
})
53+
54+
client := &APIClient{
55+
HTTPCli: httpClient,
56+
}
57+
58+
res, err := client.RegistryLogin(context.Background(), &types.AuthConfig{})
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
assert.Equal(t, res.IdentityToken, "aaa")
63+
assert.Equal(t, res.Status, "bbb")
64+
}

client/system.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

client/system_info.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+
// SystemInfo requests daemon for system info.
10+
func (client *APIClient) SystemInfo(ctx context.Context) (*types.SystemInfo, error) {
11+
resp, err := client.get(ctx, "/info", nil, nil)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
info := &types.SystemInfo{}
17+
err = decodeBody(info, resp.Body)
18+
ensureCloseReader(resp)
19+
20+
return info, err
21+
}

client/system_info_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestSystemInfoError(t *testing.T) {
19+
client := &APIClient{
20+
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
21+
}
22+
_, err := client.SystemInfo(context.Background())
23+
if err == nil || !strings.Contains(err.Error(), "Server error") {
24+
t.Fatalf("expected a Server Error, got %v", err)
25+
}
26+
}
27+
28+
func TestSystemInfo(t *testing.T) {
29+
expectedURL := "/info"
30+
31+
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
32+
if !strings.HasPrefix(req.URL.Path, expectedURL) {
33+
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
34+
}
35+
info := types.SystemInfo{
36+
ContainersRunning: 2,
37+
ContainersStopped: 3,
38+
Debug: true,
39+
Name: "my_host",
40+
PouchRootDir: "/var/lib/pouch",
41+
}
42+
b, err := json.Marshal(info)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return &http.Response{
48+
StatusCode: http.StatusOK,
49+
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
50+
}, nil
51+
})
52+
53+
client := &APIClient{
54+
HTTPCli: httpClient,
55+
}
56+
57+
info, err := client.SystemInfo(context.Background())
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
assert.Equal(t, info.PouchRootDir, "/var/lib/pouch")
62+
assert.Equal(t, info.Name, "my_host")
63+
assert.Equal(t, info.Debug, true)
64+
assert.Equal(t, info.ContainersStopped, int64(3))
65+
assert.Equal(t, info.ContainersRunning, int64(2))
66+
}

client/system_ping.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"io/ioutil"
6+
)
7+
8+
// SystemPing shows whether server is ok.
9+
func (client *APIClient) SystemPing(ctx context.Context) (string, error) {
10+
resp, err := client.get(ctx, "/_ping", nil, nil)
11+
if err != nil {
12+
return "", err
13+
}
14+
15+
defer resp.Body.Close()
16+
data, err := ioutil.ReadAll(resp.Body)
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
return string(data), nil
22+
}

client/system_ping_test.go

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

client/system_version_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package client
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"net/http"
910
"strings"
1011
"testing"
1112

12-
"encoding/json"
1313
"github.com/alibaba/pouch/apis/types"
1414
)
1515

0 commit comments

Comments
 (0)