-
Notifications
You must be signed in to change notification settings - Fork 950
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 operations on client side #1015
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
// RegistryLogin authenticates the server with a given registry to login. | ||
func (client *APIClient) RegistryLogin(ctx context.Context, auth *types.AuthConfig) (*types.AuthResponse, error) { | ||
resp, err := client.post(ctx, "/auth", nil, auth, nil) | ||
if err != nil || resp.StatusCode == http.StatusUnauthorized { | ||
return nil, err | ||
} | ||
|
||
authResp := &types.AuthResponse{} | ||
err = decodeBody(authResp, resp.Body) | ||
ensureCloseReader(resp) | ||
|
||
return authResp, 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,64 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRegistryLoginError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
loginConfig := types.AuthConfig{} | ||
_, err := client.RegistryLogin(context.Background(), &loginConfig) | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestRegistryLogin(t *testing.T) { | ||
expectedURL := "/auth" | ||
|
||
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" { | ||
loginConfig := types.AuthConfig{} | ||
if err := json.NewDecoder(req.Body).Decode(&loginConfig); err != nil { | ||
return nil, fmt.Errorf("failed to parse json: %v", err) | ||
} | ||
} | ||
auth, err := json.Marshal(types.AuthResponse{ | ||
IdentityToken: "aaa", | ||
Status: "bbb", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(auth))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
res, err := client.RegistryLogin(context.Background(), &types.AuthConfig{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, res.IdentityToken, "aaa") | ||
assert.Equal(t, res.Status, "bbb") | ||
} |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
// SystemInfo requests daemon for system info. | ||
func (client *APIClient) SystemInfo(ctx context.Context) (*types.SystemInfo, error) { | ||
resp, err := client.get(ctx, "/info", nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
info := &types.SystemInfo{} | ||
err = decodeBody(info, resp.Body) | ||
ensureCloseReader(resp) | ||
|
||
return info, 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,66 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSystemInfoError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
_, err := client.SystemInfo(context.Background()) | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestSystemInfo(t *testing.T) { | ||
expectedURL := "/info" | ||
|
||
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) | ||
} | ||
info := types.SystemInfo{ | ||
ContainersRunning: 2, | ||
ContainersStopped: 3, | ||
Debug: true, | ||
Name: "my_host", | ||
PouchRootDir: "/var/lib/pouch", | ||
} | ||
b, err := json.Marshal(info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
info, err := client.SystemInfo(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, info.PouchRootDir, "/var/lib/pouch") | ||
assert.Equal(t, info.Name, "my_host") | ||
assert.Equal(t, info.Debug, true) | ||
assert.Equal(t, info.ContainersStopped, int64(3)) | ||
assert.Equal(t, info.ContainersRunning, int64(2)) | ||
} |
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,22 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
) | ||
|
||
// SystemPing shows whether server is ok. | ||
func (client *APIClient) SystemPing(ctx context.Context) (string, error) { | ||
resp, err := client.get(ctx, "/_ping", nil, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer resp.Body.Close() | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(data), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSystemPingError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
_, err := client.SystemPing(context.Background()) | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestSystemPing(t *testing.T) { | ||
expectedURL := "/_ping" | ||
|
||
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) | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
if _, err := client.SystemPing(context.Background()); err != nil { | ||
t.Fatal(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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part has a potential logic bug which is already described in #1030.