Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add mock test for system_version #1004

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can assert the value returned by the function.

t.Fatal(err)
}
}