github.com/olljanat/moby@v1.13.1/client/plugin_list_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 TestPluginListError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, err := client.PluginList(context.Background())
    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 TestPluginList(t *testing.T) {
    28  	expectedURL := "/plugins"
    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  				{
    36  					ID: "plugin_id1",
    37  				},
    38  				{
    39  					ID: "plugin_id2",
    40  				},
    41  			})
    42  			if err != nil {
    43  				return nil, err
    44  			}
    45  			return &http.Response{
    46  				StatusCode: http.StatusOK,
    47  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    48  			}, nil
    49  		}),
    50  	}
    51  
    52  	plugins, err := client.PluginList(context.Background())
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	if len(plugins) != 2 {
    57  		t.Fatalf("expected 2 plugins, got %v", plugins)
    58  	}
    59  }