github.com/shishir-a412ed/docker@v1.3.2-0.20180103180333-fda904911d87/client/volume_inspect_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/internal/testutil"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  	"golang.org/x/net/context"
    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  	testutil.ErrorContains(t, 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.True(t, IsErrNotFound(err))
    35  }
    36  
    37  func TestVolumeInspectWithEmptyID(t *testing.T) {
    38  	expectedURL := "/volumes/"
    39  
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			assert.Equal(t, req.URL.Path, expectedURL)
    43  			return &http.Response{
    44  				StatusCode: http.StatusNotFound,
    45  				Body:       ioutil.NopCloser(bytes.NewReader(nil)),
    46  			}, nil
    47  		}),
    48  	}
    49  	_, err := client.VolumeInspect(context.Background(), "")
    50  	testutil.ErrorContains(t, err, "No such volume: ")
    51  
    52  }
    53  
    54  func TestVolumeInspect(t *testing.T) {
    55  	expectedURL := "/volumes/volume_id"
    56  	expected := types.Volume{
    57  		Name:       "name",
    58  		Driver:     "driver",
    59  		Mountpoint: "mountpoint",
    60  	}
    61  
    62  	client := &Client{
    63  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    64  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    65  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    66  			}
    67  			if req.Method != "GET" {
    68  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    69  			}
    70  			content, err := json.Marshal(expected)
    71  			if err != nil {
    72  				return nil, err
    73  			}
    74  			return &http.Response{
    75  				StatusCode: http.StatusOK,
    76  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    77  			}, nil
    78  		}),
    79  	}
    80  
    81  	volume, err := client.VolumeInspect(context.Background(), "volume_id")
    82  	require.NoError(t, err)
    83  	assert.Equal(t, expected, volume)
    84  }