diff --git a/client/container.go b/client/container.go index e15dad477..df4626875 100644 --- a/client/container.go +++ b/client/container.go @@ -76,17 +76,6 @@ func (client *APIClient) ContainerStartExec(ctx context.Context, execid string, return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header) } -// ContainerRestart restarts a running container. -func (client *APIClient) ContainerRestart(ctx context.Context, name string, timeout string) error { - q := url.Values{} - q.Add("t", timeout) - - resp, err := client.post(ctx, "/containers/"+name+"/restart", q, nil, nil) - ensureCloseReader(resp) - - return err -} - // ContainerUpgrade upgrade a container with new image and args. func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error { // TODO @@ -157,15 +146,3 @@ func (client *APIClient) ContainerLogs(ctx context.Context, name string, options ensureCloseReader(resp) return resp.Body, nil } - -// ContainerResize resizes the size of container tty. -func (client *APIClient) ContainerResize(ctx context.Context, name, height, width string) error { - query := url.Values{} - query.Set("h", height) - query.Set("w", width) - - resp, err := client.post(ctx, "/containers/"+name+"/resize", query, nil, nil) - ensureCloseReader(resp) - - return err -} diff --git a/client/container_get_test.go b/client/container_get_test.go index 7cec43e4b..c9e4725bb 100644 --- a/client/container_get_test.go +++ b/client/container_get_test.go @@ -28,7 +28,7 @@ func TestContainerGet(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } containerJSON := types.ContainerJSON{ Driver: "Driver", diff --git a/client/container_list_test.go b/client/container_list_test.go index 832566042..64292f1a7 100644 --- a/client/container_list_test.go +++ b/client/container_list_test.go @@ -28,12 +28,12 @@ func TestContainerList(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } query := req.URL.Query() all := query.Get("all") if all != "true" { - return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all) + return nil, fmt.Errorf("all not set in URL query properly. Expected 'true', got %s", all) } containersJSON := []types.ContainerJSON{ { @@ -41,8 +41,8 @@ func TestContainerList(t *testing.T) { Image: "Image1", }, { - Name: "container1", - Image: "Image1", + Name: "container2", + Image: "Image2", }, } b, err := json.Marshal(containersJSON) diff --git a/client/container_pause_test.go b/client/container_pause_test.go index e182556a8..d5ca2c479 100644 --- a/client/container_pause_test.go +++ b/client/container_pause_test.go @@ -25,7 +25,7 @@ func TestContainerPause(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } return &http.Response{ StatusCode: http.StatusOK, diff --git a/client/container_remove_test.go b/client/container_remove_test.go index f664edef0..635facf07 100644 --- a/client/container_remove_test.go +++ b/client/container_remove_test.go @@ -35,7 +35,7 @@ func TestContainerRemove(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } force := req.URL.Query().Get("force") if force != "true" { diff --git a/client/container_rename_test.go b/client/container_rename_test.go index caa1d0bdc..aa4961497 100644 --- a/client/container_rename_test.go +++ b/client/container_rename_test.go @@ -26,7 +26,7 @@ func TestContainerRename(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { var renameConfig interface{} diff --git a/client/container_resize.go b/client/container_resize.go new file mode 100644 index 000000000..bc4d6afb6 --- /dev/null +++ b/client/container_resize.go @@ -0,0 +1,18 @@ +package client + +import ( + "context" + "net/url" +) + +// ContainerResize resizes the size of container tty. +func (client *APIClient) ContainerResize(ctx context.Context, name, height, width string) error { + query := url.Values{} + query.Set("h", height) + query.Set("w", width) + + resp, err := client.post(ctx, "/containers/"+name+"/resize", query, nil, nil) + ensureCloseReader(resp) + + return err +} diff --git a/client/container_resize_test.go b/client/container_resize_test.go new file mode 100644 index 000000000..cace76ab3 --- /dev/null +++ b/client/container_resize_test.go @@ -0,0 +1,52 @@ +package client + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" +) + +func TestContainerResizeError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + err := client.ContainerResize(context.Background(), "nothing", "", "") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerResize(t *testing.T) { + expectedURL := "/containers/container_id/resize" + + 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) + } + height := req.URL.Query().Get("h") + if height != "200" { + return nil, fmt.Errorf("height not set in URL query properly. Expected '200', got %s", height) + } + width := req.URL.Query().Get("w") + if width != "300" { + return nil, fmt.Errorf("width not set in URL query properly. Expected '300', got %s", width) + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + err := client.ContainerResize(context.Background(), "container_id", "200", "300") + if err != nil { + t.Fatal(err) + } +} diff --git a/client/container_restart.go b/client/container_restart.go new file mode 100644 index 000000000..63c536aa7 --- /dev/null +++ b/client/container_restart.go @@ -0,0 +1,17 @@ +package client + +import ( + "context" + "net/url" +) + +// ContainerRestart restarts a running container. +func (client *APIClient) ContainerRestart(ctx context.Context, name string, timeout string) error { + q := url.Values{} + q.Add("t", timeout) + + resp, err := client.post(ctx, "/containers/"+name+"/restart", q, nil, nil) + ensureCloseReader(resp) + + return err +} diff --git a/client/container_restart_test.go b/client/container_restart_test.go new file mode 100644 index 000000000..4d1686dd5 --- /dev/null +++ b/client/container_restart_test.go @@ -0,0 +1,48 @@ +package client + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" +) + +func TestContainerRestartError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + err := client.ContainerRestart(context.Background(), "nothing", "") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerRestart(t *testing.T) { + expectedURL := "/containers/container_id/restart" + + 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) + } + timeout := req.URL.Query().Get("t") + if timeout != "100" { + return nil, fmt.Errorf("timeout not set in URL query properly. Expected '100', got %s", timeout) + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + err := client.ContainerRestart(context.Background(), "container_id", "100") + if err != nil { + t.Fatal(err) + } +} diff --git a/client/container_start_test.go b/client/container_start_test.go index 3964974a1..8513487f2 100644 --- a/client/container_start_test.go +++ b/client/container_start_test.go @@ -26,7 +26,7 @@ func TestContainerStart(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { var startConfig interface{} diff --git a/client/container_stop_test.go b/client/container_stop_test.go index 14c7dce71..3ac337ca9 100644 --- a/client/container_stop_test.go +++ b/client/container_stop_test.go @@ -25,7 +25,7 @@ func TestContainerStop(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } timeout := req.URL.Query().Get("t") if timeout != "10" { diff --git a/client/container_unpause_test.go b/client/container_unpause_test.go index 15be9cfc2..f50fb53c6 100644 --- a/client/container_unpause_test.go +++ b/client/container_unpause_test.go @@ -25,7 +25,7 @@ func TestContainerUnpause(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } return &http.Response{ StatusCode: http.StatusOK, diff --git a/client/container_update_test.go b/client/container_update_test.go index 524adbe2b..a47a615bc 100644 --- a/client/container_update_test.go +++ b/client/container_update_test.go @@ -28,7 +28,7 @@ func TestContainerUpdate(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { var updateConfig interface{} diff --git a/client/image_inspect_test.go b/client/image_inspect_test.go index ce174a235..e2f4b5cce 100644 --- a/client/image_inspect_test.go +++ b/client/image_inspect_test.go @@ -40,7 +40,7 @@ func TestImageInspect(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/image_list_test.go b/client/image_list_test.go index 1cfa6fc35..04544921a 100644 --- a/client/image_list_test.go +++ b/client/image_list_test.go @@ -30,7 +30,7 @@ func TestImageList(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/image_pull_test.go b/client/image_pull_test.go index 011c03f70..f2dd06445 100644 --- a/client/image_pull_test.go +++ b/client/image_pull_test.go @@ -35,11 +35,11 @@ func TestImagePull(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "POST" { - return nil, fmt.Errorf("Expected POST method, got %s", req.Method) + return nil, fmt.Errorf("expected POST method, got %s", req.Method) } return &http.Response{ diff --git a/client/image_remove_test.go b/client/image_remove_test.go index 77302928b..33a3b0a6c 100644 --- a/client/image_remove_test.go +++ b/client/image_remove_test.go @@ -25,7 +25,7 @@ func TestImageRemove(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "DELETE" { return nil, fmt.Errorf("expected DELETE method, got %s", req.Method) diff --git a/client/network_create_test.go b/client/network_create_test.go index f783796e5..5d7c4ef6d 100644 --- a/client/network_create_test.go +++ b/client/network_create_test.go @@ -40,7 +40,7 @@ func TestNetworkCreate(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { createConfig := types.NetworkCreateConfig{} diff --git a/client/network_inspect_test.go b/client/network_inspect_test.go index 57ccdeacd..0cff43be9 100644 --- a/client/network_inspect_test.go +++ b/client/network_inspect_test.go @@ -40,7 +40,7 @@ func TestNetworkInspect(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/network_list_test.go b/client/network_list_test.go index 99ec781ec..50fa2ffef 100644 --- a/client/network_list_test.go +++ b/client/network_list_test.go @@ -30,7 +30,7 @@ func TestNetworkList(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/network_remove_test.go b/client/network_remove_test.go index 161e3d883..5196a923f 100644 --- a/client/network_remove_test.go +++ b/client/network_remove_test.go @@ -25,7 +25,7 @@ func TestNetworkRemove(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "DELETE" { return nil, fmt.Errorf("expected DELETE method, got %s", req.Method) diff --git a/client/registry_login_test.go b/client/registry_login_test.go index 9ce079e93..737de4ccd 100644 --- a/client/registry_login_test.go +++ b/client/registry_login_test.go @@ -30,7 +30,7 @@ func TestRegistryLogin(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { loginConfig := types.AuthConfig{} diff --git a/client/system_info_test.go b/client/system_info_test.go index 3501ee4d2..e0057bd7d 100644 --- a/client/system_info_test.go +++ b/client/system_info_test.go @@ -30,7 +30,7 @@ func TestSystemInfo(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } info := types.SystemInfo{ ContainersRunning: 2, diff --git a/client/system_ping_test.go b/client/system_ping_test.go index b37545f45..d2c2a8c3f 100644 --- a/client/system_ping_test.go +++ b/client/system_ping_test.go @@ -25,7 +25,7 @@ func TestSystemPing(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } return &http.Response{ diff --git a/client/system_version_test.go b/client/system_version_test.go index 2e557632f..f8a39a4a9 100644 --- a/client/system_version_test.go +++ b/client/system_version_test.go @@ -28,7 +28,7 @@ func TestSystemVersion(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } version := types.SystemVersion{ GoVersion: "go_version", diff --git a/client/volume_create_test.go b/client/volume_create_test.go index 5579afba0..e9c8b5f27 100644 --- a/client/volume_create_test.go +++ b/client/volume_create_test.go @@ -40,7 +40,7 @@ func TestVolumeCreate(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Header.Get("Content-Type") == "application/json" { var createConfig interface{} diff --git a/client/volume_inspect_test.go b/client/volume_inspect_test.go index 11a6147cb..bf4293dfd 100644 --- a/client/volume_inspect_test.go +++ b/client/volume_inspect_test.go @@ -40,7 +40,7 @@ func TestVolumeInspect(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/volume_list_test.go b/client/volume_list_test.go index 1c2d503f1..a1a37a7e7 100644 --- a/client/volume_list_test.go +++ b/client/volume_list_test.go @@ -30,7 +30,7 @@ func TestVolumeList(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "GET" { return nil, fmt.Errorf("expected GET method, got %s", req.Method) diff --git a/client/volume_remove_test.go b/client/volume_remove_test.go index b31f07189..333f1c0ed 100644 --- a/client/volume_remove_test.go +++ b/client/volume_remove_test.go @@ -25,7 +25,7 @@ func TestVolumeRemove(t *testing.T) { 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 nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } if req.Method != "DELETE" { return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)