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 pause and unpause test #1074

Merged
merged 1 commit into from
Apr 9, 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
16 changes: 0 additions & 16 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions client/container_pause.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions client/container_pause_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 11 additions & 0 deletions client/container_unpause.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions client/container_unpause_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}