diff --git a/client/container.go b/client/container.go index eb2dd0e16..e15dad477 100644 --- a/client/container.go +++ b/client/container.go @@ -87,22 +87,6 @@ func (client *APIClient) ContainerRestart(ctx context.Context, name string, time return err } -// ContainerPause pauses a container. -func (client *APIClient) ContainerPause(ctx context.Context, name string) error { - resp, err := client.post(ctx, "/containers/"+name+"/pause", nil, nil, nil) - ensureCloseReader(resp) - - return err -} - -// ContainerUnpause unpauses a container. -func (client *APIClient) ContainerUnpause(ctx context.Context, name string) error { - resp, err := client.post(ctx, "/containers/"+name+"/unpause", nil, 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 diff --git a/client/container_pause.go b/client/container_pause.go new file mode 100644 index 000000000..76b1f961a --- /dev/null +++ b/client/container_pause.go @@ -0,0 +1,11 @@ +package client + +import "context" + +// ContainerPause pauses a container. +func (client *APIClient) ContainerPause(ctx context.Context, name string) error { + resp, err := client.post(ctx, "/containers/"+name+"/pause", nil, nil, nil) + ensureCloseReader(resp) + + return err +} diff --git a/client/container_pause_test.go b/client/container_pause_test.go new file mode 100644 index 000000000..e182556a8 --- /dev/null +++ b/client/container_pause_test.go @@ -0,0 +1,44 @@ +package client + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" +) + +func TestContainerPauseError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + err := client.ContainerPause(context.Background(), "nothing") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerPause(t *testing.T) { + expectedURL := "/containers/container_id/pause" + + 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, + } + + err := client.ContainerPause(context.Background(), "container_id") + if err != nil { + t.Fatal(err) + } +} diff --git a/client/container_unpause.go b/client/container_unpause.go new file mode 100644 index 000000000..2d4a7459c --- /dev/null +++ b/client/container_unpause.go @@ -0,0 +1,11 @@ +package client + +import "context" + +// ContainerUnpause unpauses a container. +func (client *APIClient) ContainerUnpause(ctx context.Context, name string) error { + resp, err := client.post(ctx, "/containers/"+name+"/unpause", nil, nil, nil) + ensureCloseReader(resp) + + return err +} diff --git a/client/container_unpause_test.go b/client/container_unpause_test.go new file mode 100644 index 000000000..15be9cfc2 --- /dev/null +++ b/client/container_unpause_test.go @@ -0,0 +1,44 @@ +package client + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" +) + +func TestContainerUnpauseError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + err := client.ContainerUnpause(context.Background(), "nothing") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerUnpause(t *testing.T) { + expectedURL := "/containers/container_id/unpause" + + 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, + } + + err := client.ContainerUnpause(context.Background(), "container_id") + if err != nil { + t.Fatal(err) + } +}