github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/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/pkg/errors"
    15  	"gotest.tools/assert"
    16  	is "gotest.tools/assert/cmp"
    17  )
    18  
    19  func TestVolumeInspectError(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    22  	}
    23  
    24  	_, err := client.VolumeInspect(context.Background(), "nothing")
    25  	assert.Check(t, is.ErrorContains(err, "Error response from daemon: Server error"))
    26  }
    27  
    28  func TestVolumeInspectNotFound(t *testing.T) {
    29  	client := &Client{
    30  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    31  	}
    32  
    33  	_, err := client.VolumeInspect(context.Background(), "unknown")
    34  	assert.Check(t, IsErrNotFound(err))
    35  }
    36  
    37  func TestVolumeInspectWithEmptyID(t *testing.T) {
    38  	client := &Client{
    39  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    40  			return nil, errors.New("should not make request")
    41  		}),
    42  	}
    43  	_, _, err := client.VolumeInspectWithRaw(context.Background(), "")
    44  	if !IsErrNotFound(err) {
    45  		t.Fatalf("Expected NotFoundError, got %v", err)
    46  	}
    47  }
    48  
    49  func TestVolumeInspect(t *testing.T) {
    50  	expectedURL := "/volumes/volume_id"
    51  	expected := types.Volume{
    52  		Name:       "name",
    53  		Driver:     "driver",
    54  		Mountpoint: "mountpoint",
    55  	}
    56  
    57  	client := &Client{
    58  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    59  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    60  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    61  			}
    62  			if req.Method != "GET" {
    63  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    64  			}
    65  			content, err := json.Marshal(expected)
    66  			if err != nil {
    67  				return nil, err
    68  			}
    69  			return &http.Response{
    70  				StatusCode: http.StatusOK,
    71  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    72  			}, nil
    73  		}),
    74  	}
    75  
    76  	volume, err := client.VolumeInspect(context.Background(), "volume_id")
    77  	assert.NilError(t, err)
    78  	assert.Check(t, is.DeepEqual(expected, volume))
    79  }