github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/client/volume_update_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/api/types/swarm"
    13  	volumetypes "github.com/docker/docker/api/types/volume"
    14  	"github.com/docker/docker/errdefs"
    15  )
    16  
    17  func TestVolumeUpdateError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	err := client.VolumeUpdate(context.Background(), "", swarm.Version{}, volumetypes.UpdateOptions{})
    23  
    24  	if !errdefs.IsSystem(err) {
    25  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    26  	}
    27  }
    28  
    29  func TestVolumeUpdate(t *testing.T) {
    30  	expectedURL := "/volumes/test1"
    31  	expectedVersion := "version=10"
    32  
    33  	client := &Client{
    34  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    35  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    36  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    37  			}
    38  			if req.Method != http.MethodPut {
    39  				return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
    40  			}
    41  			if !strings.Contains(req.URL.RawQuery, expectedVersion) {
    42  				return nil, fmt.Errorf("expected query to contain '%s', got '%s'", expectedVersion, req.URL.RawQuery)
    43  			}
    44  			return &http.Response{
    45  				StatusCode: http.StatusOK,
    46  				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
    47  			}, nil
    48  		}),
    49  	}
    50  
    51  	err := client.VolumeUpdate(context.Background(), "test1", swarm.Version{Index: uint64(10)}, volumetypes.UpdateOptions{})
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  }