diff --git a/client/container.go b/client/container.go index f8ae608b6..729a9b86e 100644 --- a/client/container.go +++ b/client/container.go @@ -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{} diff --git a/client/container_rename.go b/client/container_rename.go new file mode 100644 index 000000000..a83992b03 --- /dev/null +++ b/client/container_rename.go @@ -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 +} diff --git a/client/container_rename_test.go b/client/container_rename_test.go new file mode 100644 index 000000000..caa1d0bdc --- /dev/null +++ b/client/container_rename_test.go @@ -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) + } +}