github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+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/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    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  	if !errdefs.IsSystem(err) {
    27  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    28  	}
    29  }
    30  
    31  func TestVolumeInspectNotFound(t *testing.T) {
    32  	client := &Client{
    33  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    34  	}
    35  
    36  	_, err := client.VolumeInspect(context.Background(), "unknown")
    37  	assert.Check(t, IsErrNotFound(err))
    38  }
    39  
    40  func TestVolumeInspectWithEmptyID(t *testing.T) {
    41  	client := &Client{
    42  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    43  			return nil, errors.New("should not make request")
    44  		}),
    45  	}
    46  	_, _, err := client.VolumeInspectWithRaw(context.Background(), "")
    47  	if !IsErrNotFound(err) {
    48  		t.Fatalf("Expected NotFoundError, got %v", err)
    49  	}
    50  }
    51  
    52  func TestVolumeInspect(t *testing.T) {
    53  	expectedURL := "/volumes/volume_id"
    54  	expected := types.Volume{
    55  		Name:       "name",
    56  		Driver:     "driver",
    57  		Mountpoint: "mountpoint",
    58  	}
    59  
    60  	client := &Client{
    61  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    62  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    63  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    64  			}
    65  			if req.Method != http.MethodGet {
    66  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    67  			}
    68  			content, err := json.Marshal(expected)
    69  			if err != nil {
    70  				return nil, err
    71  			}
    72  			return &http.Response{
    73  				StatusCode: http.StatusOK,
    74  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    75  			}, nil
    76  		}),
    77  	}
    78  
    79  	volume, err := client.VolumeInspect(context.Background(), "volume_id")
    80  	assert.NilError(t, err)
    81  	assert.Check(t, is.DeepEqual(expected, volume))
    82  }