github.com/hms58/moby@v1.13.1/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  	"golang.org/x/net/context"
    13  )
    14  
    15  func TestContainerRemoveError(t *testing.T) {
    16  	client := &Client{
    17  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    18  	}
    19  	err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
    20  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    21  		t.Fatalf("expected a Server Error, got %v", err)
    22  	}
    23  }
    24  
    25  func TestContainerRemove(t *testing.T) {
    26  	expectedURL := "/containers/container_id"
    27  	client := &Client{
    28  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    29  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    30  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    31  			}
    32  			query := req.URL.Query()
    33  			volume := query.Get("v")
    34  			if volume != "1" {
    35  				return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume)
    36  			}
    37  			force := query.Get("force")
    38  			if force != "1" {
    39  				return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force)
    40  			}
    41  			link := query.Get("link")
    42  			if link != "" {
    43  				return nil, fmt.Errorf("link should have not be present in query, go %s", link)
    44  			}
    45  			return &http.Response{
    46  				StatusCode: http.StatusOK,
    47  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    48  			}, nil
    49  		}),
    50  	}
    51  
    52  	err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{
    53  		RemoveVolumes: true,
    54  		Force:         true,
    55  	})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  }