github.com/kinvolk/docker@v1.13.1/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  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestVolumeInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, err := client.VolumeInspect(context.Background(), "nothing")
    22  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    23  		t.Fatalf("expected a Server Error, got %v", err)
    24  	}
    25  }
    26  
    27  func TestVolumeInspectNotFound(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    30  	}
    31  
    32  	_, err := client.VolumeInspect(context.Background(), "unknown")
    33  	if err == nil || !IsErrVolumeNotFound(err) {
    34  		t.Fatalf("expected a volumeNotFound error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestVolumeInspect(t *testing.T) {
    39  	expectedURL := "/volumes/volume_id"
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    43  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    44  			}
    45  			if req.Method != "GET" {
    46  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    47  			}
    48  			content, err := json.Marshal(types.Volume{
    49  				Name:       "name",
    50  				Driver:     "driver",
    51  				Mountpoint: "mountpoint",
    52  			})
    53  			if err != nil {
    54  				return nil, err
    55  			}
    56  			return &http.Response{
    57  				StatusCode: http.StatusOK,
    58  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    59  			}, nil
    60  		}),
    61  	}
    62  
    63  	v, err := client.VolumeInspect(context.Background(), "volume_id")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if v.Name != "name" {
    68  		t.Fatalf("expected `name`, got %s", v.Name)
    69  	}
    70  	if v.Driver != "driver" {
    71  		t.Fatalf("expected `driver`, got %s", v.Driver)
    72  	}
    73  	if v.Mountpoint != "mountpoint" {
    74  		t.Fatalf("expected `mountpoint`, got %s", v.Mountpoint)
    75  	}
    76  }