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 container rename client side mock test #1021

Merged
merged 1 commit into from
Mar 30, 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
11 changes: 0 additions & 11 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.
return &container, err
}

// ContainerRename renames a container.
func (client *APIClient) ContainerRename(ctx context.Context, id string, name string) error {
q := url.Values{}
q.Add("name", name)

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

return err
}

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

import (
"context"
"net/url"
)

// ContainerRename renames a container.
func (client *APIClient) ContainerRename(ctx context.Context, id string, name string) error {
q := url.Values{}
q.Add("name", name)

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

return err
}
50 changes: 50 additions & 0 deletions client/container_rename_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 TestContainerRenameError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRename(context.Background(), "nothing", "containerRename")
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

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

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 renameConfig interface{}
if err := json.NewDecoder(req.Body).Decode(&renameConfig); 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.ContainerRename(context.Background(), "container_id", "containerRename"); err != nil {
t.Fatal(err)
}
}