-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add mock test for pouch package client
Signed-off-by: Allen Sun <allensun.shl@alibaba-inc.com>
- Loading branch information
1 parent
b9f3816
commit d2848bf
Showing
4 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |