From 53247a3170898920054db2f6525e43c94a932442 Mon Sep 17 00:00:00 2001 From: Zou Rui <21751189@zju.edu.cn> Date: Fri, 30 Mar 2018 10:43:14 +0800 Subject: [PATCH] test: add more mock test on client side Signed-off-by: Zou Rui <21751189@zju.edu.cn> --- client/registry_login.go | 22 ++++++++++++ client/registry_login_test.go | 64 +++++++++++++++++++++++++++++++++ client/system.go | 53 ---------------------------- client/system_info.go | 21 +++++++++++ client/system_info_test.go | 66 +++++++++++++++++++++++++++++++++++ client/system_ping.go | 22 ++++++++++++ client/system_ping_test.go | 44 +++++++++++++++++++++++ client/system_version_test.go | 2 +- 8 files changed, 240 insertions(+), 54 deletions(-) create mode 100644 client/registry_login.go create mode 100644 client/registry_login_test.go delete mode 100644 client/system.go create mode 100644 client/system_info.go create mode 100644 client/system_info_test.go create mode 100644 client/system_ping.go create mode 100644 client/system_ping_test.go diff --git a/client/registry_login.go b/client/registry_login.go new file mode 100644 index 0000000000..b948e1add1 --- /dev/null +++ b/client/registry_login.go @@ -0,0 +1,22 @@ +package client + +import ( + "context" + "net/http" + + "github.com/alibaba/pouch/apis/types" +) + +// RegistryLogin authenticates the server with a given registry to login. +func (client *APIClient) RegistryLogin(ctx context.Context, auth *types.AuthConfig) (*types.AuthResponse, error) { + resp, err := client.post(ctx, "/auth", nil, auth, nil) + if err != nil || resp.StatusCode == http.StatusUnauthorized { + return nil, err + } + + authResp := &types.AuthResponse{} + err = decodeBody(authResp, resp.Body) + ensureCloseReader(resp) + + return authResp, err +} diff --git a/client/registry_login_test.go b/client/registry_login_test.go new file mode 100644 index 0000000000..9ce079e938 --- /dev/null +++ b/client/registry_login_test.go @@ -0,0 +1,64 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/alibaba/pouch/apis/types" + "github.com/stretchr/testify/assert" +) + +func TestRegistryLoginError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + loginConfig := types.AuthConfig{} + _, err := client.RegistryLogin(context.Background(), &loginConfig) + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestRegistryLogin(t *testing.T) { + expectedURL := "/auth" + + 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) + } + if req.Header.Get("Content-Type") == "application/json" { + loginConfig := types.AuthConfig{} + if err := json.NewDecoder(req.Body).Decode(&loginConfig); err != nil { + return nil, fmt.Errorf("failed to parse json: %v", err) + } + } + auth, err := json.Marshal(types.AuthResponse{ + IdentityToken: "aaa", + Status: "bbb", + }) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(auth))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + res, err := client.RegistryLogin(context.Background(), &types.AuthConfig{}) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, res.IdentityToken, "aaa") + assert.Equal(t, res.Status, "bbb") +} diff --git a/client/system.go b/client/system.go deleted file mode 100644 index 9fe670b54a..0000000000 --- a/client/system.go +++ /dev/null @@ -1,53 +0,0 @@ -package client - -import ( - "context" - "io/ioutil" - "net/http" - - "github.com/alibaba/pouch/apis/types" -) - -// SystemPing shows whether server is ok. -func (client *APIClient) SystemPing(ctx context.Context) (string, error) { - resp, err := client.get(ctx, "/_ping", nil, nil) - if err != nil { - return "", err - } - - defer resp.Body.Close() - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - - return string(data), nil -} - -// SystemInfo requests daemon for system info. -func (client *APIClient) SystemInfo(ctx context.Context) (*types.SystemInfo, error) { - resp, err := client.get(ctx, "/info", nil, nil) - if err != nil { - return nil, err - } - - info := &types.SystemInfo{} - err = decodeBody(info, resp.Body) - ensureCloseReader(resp) - - return info, err -} - -// RegistryLogin requests a registry server to login. -func (client *APIClient) RegistryLogin(ctx context.Context, auth *types.AuthConfig) (*types.AuthResponse, error) { - resp, err := client.post(ctx, "/auth", nil, auth, nil) - if err != nil || resp.StatusCode == http.StatusUnauthorized { - return nil, err - } - - authResp := &types.AuthResponse{} - err = decodeBody(authResp, resp.Body) - ensureCloseReader(resp) - - return authResp, err -} diff --git a/client/system_info.go b/client/system_info.go new file mode 100644 index 0000000000..3f7a6e0897 --- /dev/null +++ b/client/system_info.go @@ -0,0 +1,21 @@ +package client + +import ( + "context" + + "github.com/alibaba/pouch/apis/types" +) + +// SystemInfo requests daemon for system info. +func (client *APIClient) SystemInfo(ctx context.Context) (*types.SystemInfo, error) { + resp, err := client.get(ctx, "/info", nil, nil) + if err != nil { + return nil, err + } + + info := &types.SystemInfo{} + err = decodeBody(info, resp.Body) + ensureCloseReader(resp) + + return info, err +} diff --git a/client/system_info_test.go b/client/system_info_test.go new file mode 100644 index 0000000000..3501ee4d2a --- /dev/null +++ b/client/system_info_test.go @@ -0,0 +1,66 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/alibaba/pouch/apis/types" + + "github.com/stretchr/testify/assert" +) + +func TestSystemInfoError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + _, err := client.SystemInfo(context.Background()) + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestSystemInfo(t *testing.T) { + expectedURL := "/info" + + 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) + } + info := types.SystemInfo{ + ContainersRunning: 2, + ContainersStopped: 3, + Debug: true, + Name: "my_host", + PouchRootDir: "/var/lib/pouch", + } + b, err := json.Marshal(info) + if err != nil { + return nil, err + } + + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + info, err := client.SystemInfo(context.Background()) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, info.PouchRootDir, "/var/lib/pouch") + assert.Equal(t, info.Name, "my_host") + assert.Equal(t, info.Debug, true) + assert.Equal(t, info.ContainersStopped, int64(3)) + assert.Equal(t, info.ContainersRunning, int64(2)) +} diff --git a/client/system_ping.go b/client/system_ping.go new file mode 100644 index 0000000000..f35e1c4295 --- /dev/null +++ b/client/system_ping.go @@ -0,0 +1,22 @@ +package client + +import ( + "context" + "io/ioutil" +) + +// SystemPing shows whether server is ok. +func (client *APIClient) SystemPing(ctx context.Context) (string, error) { + resp, err := client.get(ctx, "/_ping", nil, nil) + if err != nil { + return "", err + } + + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return string(data), nil +} diff --git a/client/system_ping_test.go b/client/system_ping_test.go new file mode 100644 index 0000000000..b37545f457 --- /dev/null +++ b/client/system_ping_test.go @@ -0,0 +1,44 @@ +package client + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" +) + +func TestSystemPingError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + _, err := client.SystemPing(context.Background()) + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestSystemPing(t *testing.T) { + expectedURL := "/_ping" + + 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) + } + + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + if _, err := client.SystemPing(context.Background()); err != nil { + t.Fatal(err) + } +} diff --git a/client/system_version_test.go b/client/system_version_test.go index c9b66a326c..2e557632f9 100644 --- a/client/system_version_test.go +++ b/client/system_version_test.go @@ -3,13 +3,13 @@ package client import ( "bytes" "context" + "encoding/json" "fmt" "io/ioutil" "net/http" "strings" "testing" - "encoding/json" "github.com/alibaba/pouch/apis/types" )