github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/client/volume_remove_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/errdefs"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func TestVolumeRemoveError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	err := client.VolumeRemove(context.Background(), "volume_id", false)
    23  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    24  }
    25  
    26  // TestVolumeRemoveConnectionError verifies that connection errors occurring
    27  // during API-version negotiation are not shadowed by API-version errors.
    28  //
    29  // Regression test for https://github.com/docker/cli/issues/4890
    30  func TestVolumeRemoveConnectionError(t *testing.T) {
    31  	client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
    32  	assert.NilError(t, err)
    33  
    34  	err = client.VolumeRemove(context.Background(), "volume_id", false)
    35  	assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
    36  }
    37  
    38  func TestVolumeRemove(t *testing.T) {
    39  	expectedURL := "/volumes/volume_id"
    40  
    41  	client := &Client{
    42  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    43  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    44  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    45  			}
    46  			if req.Method != http.MethodDelete {
    47  				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
    48  			}
    49  			return &http.Response{
    50  				StatusCode: http.StatusOK,
    51  				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
    52  			}, nil
    53  		}),
    54  	}
    55  
    56  	err := client.VolumeRemove(context.Background(), "volume_id", false)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  }