github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/plugin_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  )
    16  
    17  func TestPluginInspectError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	_, _, err := client.PluginInspectWithRaw(context.Background(), "nothing")
    23  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    24  		t.Fatalf("expected a Server Error, got %v", err)
    25  	}
    26  }
    27  
    28  func TestPluginInspectWithEmptyID(t *testing.T) {
    29  	client := &Client{
    30  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    31  			return nil, errors.New("should not make request")
    32  		}),
    33  	}
    34  	_, _, err := client.PluginInspectWithRaw(context.Background(), "")
    35  	if !IsErrNotFound(err) {
    36  		t.Fatalf("Expected NotFoundError, got %v", err)
    37  	}
    38  }
    39  
    40  func TestPluginInspect(t *testing.T) {
    41  	expectedURL := "/plugins/plugin_name"
    42  	client := &Client{
    43  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    44  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    45  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    46  			}
    47  			content, err := json.Marshal(types.Plugin{
    48  				ID: "plugin_id",
    49  			})
    50  			if err != nil {
    51  				return nil, err
    52  			}
    53  			return &http.Response{
    54  				StatusCode: http.StatusOK,
    55  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    56  			}, nil
    57  		}),
    58  	}
    59  
    60  	pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	if pluginInspect.ID != "plugin_id" {
    65  		t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID)
    66  	}
    67  }