diff --git a/client/system.go b/client/system.go index b7d28389e..9fe670b54 100644 --- a/client/system.go +++ b/client/system.go @@ -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) diff --git a/client/system_version.go b/client/system_version.go new file mode 100644 index 000000000..1413220b4 --- /dev/null +++ b/client/system_version.go @@ -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 +} diff --git a/client/system_version_test.go b/client/system_version_test.go new file mode 100644 index 000000000..c9b66a326 --- /dev/null +++ b/client/system_version_test.go @@ -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) + } +}