Skip to content

Commit

Permalink
Merge pull request #1004 from ZouRui89/mock_version
Browse files Browse the repository at this point in the history
test: add mock test for system_version
  • Loading branch information
allencloud authored Mar 29, 2018
2 parents ef1a4ed + 48b7ffd commit b06bc83
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
14 changes: 0 additions & 14 deletions client/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ func (client *APIClient) SystemPing(ctx context.Context) (string, error) {
return string(data), nil
}

// SystemVersion requests daemon for system version.
func (client *APIClient) SystemVersion(ctx context.Context) (*types.SystemVersion, error) {
resp, err := client.get(ctx, "/version", nil, nil)
if err != nil {
return nil, err
}

version := &types.SystemVersion{}
err = decodeBody(version, resp.Body)
ensureCloseReader(resp)

return version, err
}

// SystemInfo requests daemon for system info.
func (client *APIClient) SystemInfo(ctx context.Context) (*types.SystemInfo, error) {
resp, err := client.get(ctx, "/info", nil, nil)
Expand Down
21 changes: 21 additions & 0 deletions client/system_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import (
"context"

"github.com/alibaba/pouch/apis/types"
)

// SystemVersion requests daemon for system version.
func (client *APIClient) SystemVersion(ctx context.Context) (*types.SystemVersion, error) {
resp, err := client.get(ctx, "/version", nil, nil)
if err != nil {
return nil, err
}

version := &types.SystemVersion{}
err = decodeBody(version, resp.Body)
ensureCloseReader(resp)

return version, err
}
55 changes: 55 additions & 0 deletions client/system_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package client

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"encoding/json"
"github.com/alibaba/pouch/apis/types"
)

func TestSystemVersionError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
_, err := client.SystemVersion(context.Background())
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestSystemVersion(t *testing.T) {
expectedURL := "/version"

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)
}
version := types.SystemVersion{
GoVersion: "go_version",
APIVersion: "API_version",
}
b, err := json.Marshal(version)
if err != nil {
return nil, err
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
}, nil
})

client := &APIClient{
HTTPCli: httpClient,
}

if _, err := client.SystemVersion(context.Background()); err != nil {
t.Fatal(err)
}
}

0 comments on commit b06bc83

Please sign in to comment.