github.com/kinvolk/docker@v1.13.1/client/plugin_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 TestPluginInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, _, err := client.PluginInspectWithRaw(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 TestPluginInspect(t *testing.T) {
    28  	expectedURL := "/plugins/plugin_name"
    29  	client := &Client{
    30  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    31  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    32  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    33  			}
    34  			content, err := json.Marshal(types.Plugin{
    35  				ID: "plugin_id",
    36  			})
    37  			if err != nil {
    38  				return nil, err
    39  			}
    40  			return &http.Response{
    41  				StatusCode: http.StatusOK,
    42  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    43  			}, nil
    44  		}),
    45  	}
    46  
    47  	pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if pluginInspect.ID != "plugin_id" {
    52  		t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID)
    53  	}
    54  }