Skip to content

Commit

Permalink
test:add container resize and restart test
Browse files Browse the repository at this point in the history
Signed-off-by: Dewey-Ding <[email protected]>
  • Loading branch information
Dewey-Ding committed Apr 10, 2018
1 parent 42cd312 commit 86c7f9a
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 23 deletions.
23 changes: 0 additions & 23 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions client/container_resize.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions client/container_resize_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions client/container_restart.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions client/container_restart_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 86c7f9a

Please sign in to comment.