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