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 resize and restart test #1090

Merged
merged 1 commit into from
Apr 11, 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
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
}
2 changes: 1 addition & 1 deletion client/container_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions client/container_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ 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{
{
Name: "container1",
Image: "Image1",
},
{
Name: "container1",
Image: "Image1",
Name: "container2",
Image: "Image2",
},
}
b, err := json.Marshal(containersJSON)
Expand Down
2 changes: 1 addition & 1 deletion client/container_pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion client/container_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion client/container_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
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)
}
}
2 changes: 1 addition & 1 deletion client/container_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion client/container_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion client/container_unpause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion client/container_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion client/image_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/image_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions client/image_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion client/image_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion client/network_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/network_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/network_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/registry_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion client/system_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion client/system_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion client/system_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion client/volume_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Loading