Skip to content

Commit

Permalink
Merge pull request #1064 from Dewey-Ding/master
Browse files Browse the repository at this point in the history
test:add container remove and stop test
  • Loading branch information
allencloud authored Apr 8, 2018
2 parents b04c454 + e44a897 commit f3325f0
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 26 deletions.
26 changes: 0 additions & 26 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,6 @@ func (client *APIClient) ContainerCreate(ctx context.Context, config types.Conta
return container, err
}

// ContainerStop stops a container.
func (client *APIClient) ContainerStop(ctx context.Context, name string, timeout string) error {
q := url.Values{}
q.Add("t", timeout)

resp, err := client.post(ctx, "/containers/"+name+"/stop", q, nil, nil)
ensureCloseReader(resp)

return err
}

// ContainerRemove removes a container.
func (client *APIClient) ContainerRemove(ctx context.Context, name string, force bool) error {
q := url.Values{}
if force {
q.Set("force", "true")
}

resp, err := client.delete(ctx, "/containers/"+name, q, nil)
if err != nil {
return err
}
ensureCloseReader(resp)
return nil
}

// ContainerAttach attach a container
func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) {
q := url.Values{}
Expand Down
21 changes: 21 additions & 0 deletions client/container_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import (
"context"
"net/url"
)

// ContainerRemove removes a container.
func (client *APIClient) ContainerRemove(ctx context.Context, name string, force bool) error {
q := url.Values{}
if force {
q.Set("force", "true")
}

resp, err := client.delete(ctx, "/containers/"+name, q, nil)
if err != nil {
return err
}
ensureCloseReader(resp)
return nil
}
56 changes: 56 additions & 0 deletions client/container_remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)

func TestContainerRemoveError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRemove(context.Background(), "nothing", true)
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerRemoveNotFoundError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")),
}
err := client.ContainerRemove(context.Background(), "no contaienr", true)
if err == nil || !strings.Contains(err.Error(), "Not Found") {
t.Fatalf("expected a Not Found Error, got %v", err)
}
}

func TestContainerRemove(t *testing.T) {
expectedURL := "/containers/container_id"

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)
}
force := req.URL.Query().Get("force")
if force != "true" {
return nil, fmt.Errorf("force not set in URL properly. Expected 'true', got %s", force)
}
return &http.Response{
StatusCode: http.StatusNoContent,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
})
client := &APIClient{
HTTPCli: httpClient,
}
err := client.ContainerRemove(context.Background(), "container_id", true)
if err != nil {
t.Fatal(err)
}
}
17 changes: 17 additions & 0 deletions client/container_stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package client

import (
"context"
"net/url"
)

// ContainerStop stops a container.
func (client *APIClient) ContainerStop(ctx context.Context, name string, timeout string) error {
q := url.Values{}
q.Add("t", timeout)

resp, err := client.post(ctx, "/containers/"+name+"/stop", q, nil, nil)
ensureCloseReader(resp)

return err
}
46 changes: 46 additions & 0 deletions client/container_stop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)

func TestContainerStopError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerStop(context.Background(), "nothing", "10")
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerStop(t *testing.T) {
expectedURL := "/containers/container_id/stop"

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 != "10" {
return nil, fmt.Errorf("timeout not set in URL properly. Expected '10', got %s", timeout)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
})
client := &APIClient{
HTTPCli: httpClient,
}
err := client.ContainerStop(context.Background(), "container_id", "10")
if err != nil {
t.Fatal(err)
}
}

0 comments on commit f3325f0

Please sign in to comment.