github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_remove_test.go (about)

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