github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/client/container_remove_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/stretchr/testify/assert"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestContainerRemoveError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  	err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
    21  	assert.EqualError(t, err, "Error response from daemon: Server error")
    22  }
    23  
    24  func TestContainerRemoveNotFoundError(t *testing.T) {
    25  	client := &Client{
    26  		client: newMockClient(errorMock(http.StatusNotFound, "missing")),
    27  	}
    28  	err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
    29  	assert.EqualError(t, err, "Error: No such container: container_id")
    30  	assert.True(t, IsErrNotFound(err))
    31  }
    32  
    33  func TestContainerRemove(t *testing.T) {
    34  	expectedURL := "/containers/container_id"
    35  	client := &Client{
    36  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    37  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    38  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    39  			}
    40  			query := req.URL.Query()
    41  			volume := query.Get("v")
    42  			if volume != "1" {
    43  				return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume)
    44  			}
    45  			force := query.Get("force")
    46  			if force != "1" {
    47  				return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force)
    48  			}
    49  			link := query.Get("link")
    50  			if link != "" {
    51  				return nil, fmt.Errorf("link should have not be present in query, go %s", link)
    52  			}
    53  			return &http.Response{
    54  				StatusCode: http.StatusOK,
    55  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    56  			}, nil
    57  		}),
    58  	}
    59  
    60  	err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{
    61  		RemoveVolumes: true,
    62  		Force:         true,
    63  	})
    64  	assert.NoError(t, err)
    65  }