Skip to content

Commit

Permalink
test:add container rename
Browse files Browse the repository at this point in the history
Signed-off-by: Dewey-Ding <[email protected]>
  • Loading branch information
Dewey-Ding committed Mar 30, 2018
1 parent 12435f4 commit 8d0ccec
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
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 (
"net/url"
"context"
)

// 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 (
"testing"
"net/http"
"context"
"strings"
"fmt"
"bytes"
"encoding/json"
"io/ioutil"
)

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)
}
}

0 comments on commit 8d0ccec

Please sign in to comment.