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 pouch package client #965

Merged
merged 1 commit into from
Mar 28, 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
42 changes: 42 additions & 0 deletions client/client_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package client

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"

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

type transportFunc func(*http.Request) (*http.Response, error)

func (transFunc transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return transFunc(req)
}

func newMockClient(handler func(*http.Request) (*http.Response, error)) *http.Client {
return &http.Client{
Transport: transportFunc(handler),
}
}

func errorMockResponse(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
header := http.Header{}
header.Set("Content-Type", "application/json")

body, err := json.Marshal(&types.Error{
Message: message,
})
if err != nil {
return nil, err
}

return &http.Response{
StatusCode: statusCode,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Header: header,
}, nil
}
}
13 changes: 0 additions & 13 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ func (client *APIClient) ContainerCreate(ctx context.Context, config types.Conta
return container, err
}

// ContainerStart starts a created container.
func (client *APIClient) ContainerStart(ctx context.Context, name, detachKeys string) error {
q := url.Values{}
if detachKeys != "" {
q.Set("detachKeys", detachKeys)
}

resp, err := client.post(ctx, "/containers/"+name+"/start", q, nil, nil)
ensureCloseReader(resp)

return err
}

// ContainerStop stops a container.
func (client *APIClient) ContainerStop(ctx context.Context, name string, timeout string) error {
q := url.Values{}
Expand Down
19 changes: 19 additions & 0 deletions client/container_start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

import (
"context"
"net/url"
)

// ContainerStart starts a created container.
func (client *APIClient) ContainerStart(ctx context.Context, name, detachKeys string) error {
q := url.Values{}
if detachKeys != "" {
q.Set("detachKeys", detachKeys)
}

resp, err := client.post(ctx, "/containers/"+name+"/start", q, nil, nil)
ensureCloseReader(resp)

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

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

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

func TestContainerStart(t *testing.T) {
expectedURL := "/containers/container_id/start"

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" {
var startConfig interface{}
if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
return nil, fmt.Errorf("failed to parse json: %v", err)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
})

client := &APIClient{
HTTPCli: httpClient,
}

if err := client.ContainerStart(context.Background(), "container_id", ""); err != nil {
t.Fatal(err)
}
}