github.com/rish1988/moby@v25.0.2+incompatible/client/volume_inspect_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/volume"
    14  	"github.com/docker/docker/errdefs"
    15  	"github.com/pkg/errors"
    16  	"gotest.tools/v3/assert"
    17  	is "gotest.tools/v3/assert/cmp"
    18  )
    19  
    20  func TestVolumeInspectError(t *testing.T) {
    21  	client := &Client{
    22  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    23  	}
    24  
    25  	_, err := client.VolumeInspect(context.Background(), "nothing")
    26  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    27  }
    28  
    29  func TestVolumeInspectNotFound(t *testing.T) {
    30  	client := &Client{
    31  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    32  	}
    33  
    34  	_, err := client.VolumeInspect(context.Background(), "unknown")
    35  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    36  }
    37  
    38  func TestVolumeInspectWithEmptyID(t *testing.T) {
    39  	client := &Client{
    40  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    41  			return nil, errors.New("should not make request")
    42  		}),
    43  	}
    44  	_, _, err := client.VolumeInspectWithRaw(context.Background(), "")
    45  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    46  }
    47  
    48  func TestVolumeInspect(t *testing.T) {
    49  	expectedURL := "/volumes/volume_id"
    50  	expected := volume.Volume{
    51  		Name:       "name",
    52  		Driver:     "driver",
    53  		Mountpoint: "mountpoint",
    54  	}
    55  
    56  	client := &Client{
    57  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    58  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    59  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    60  			}
    61  			if req.Method != http.MethodGet {
    62  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    63  			}
    64  			content, err := json.Marshal(expected)
    65  			if err != nil {
    66  				return nil, err
    67  			}
    68  			return &http.Response{
    69  				StatusCode: http.StatusOK,
    70  				Body:       io.NopCloser(bytes.NewReader(content)),
    71  			}, nil
    72  		}),
    73  	}
    74  
    75  	vol, err := client.VolumeInspect(context.Background(), "volume_id")
    76  	assert.NilError(t, err)
    77  	assert.Check(t, is.DeepEqual(expected, vol))
    78  }