github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/plugin_list_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/docker/docker/api/types/filters"
    15  	"github.com/docker/docker/errdefs"
    16  )
    17  
    18  func TestPluginListError(t *testing.T) {
    19  	client := &Client{
    20  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    21  	}
    22  
    23  	_, err := client.PluginList(context.Background(), filters.NewArgs())
    24  	if !errdefs.IsSystem(err) {
    25  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    26  	}
    27  }
    28  
    29  func TestPluginList(t *testing.T) {
    30  	expectedURL := "/plugins"
    31  
    32  	enabledFilters := filters.NewArgs()
    33  	enabledFilters.Add("enabled", "true")
    34  
    35  	capabilityFilters := filters.NewArgs()
    36  	capabilityFilters.Add("capability", "volumedriver")
    37  	capabilityFilters.Add("capability", "authz")
    38  
    39  	listCases := []struct {
    40  		filters             filters.Args
    41  		expectedQueryParams map[string]string
    42  	}{
    43  		{
    44  			filters: filters.NewArgs(),
    45  			expectedQueryParams: map[string]string{
    46  				"all":     "",
    47  				"filter":  "",
    48  				"filters": "",
    49  			},
    50  		},
    51  		{
    52  			filters: enabledFilters,
    53  			expectedQueryParams: map[string]string{
    54  				"all":     "",
    55  				"filter":  "",
    56  				"filters": `{"enabled":{"true":true}}`,
    57  			},
    58  		},
    59  		{
    60  			filters: capabilityFilters,
    61  			expectedQueryParams: map[string]string{
    62  				"all":     "",
    63  				"filter":  "",
    64  				"filters": `{"capability":{"authz":true,"volumedriver":true}}`,
    65  			},
    66  		},
    67  	}
    68  
    69  	for _, listCase := range listCases {
    70  		client := &Client{
    71  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    72  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    73  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    74  				}
    75  				query := req.URL.Query()
    76  				for key, expected := range listCase.expectedQueryParams {
    77  					actual := query.Get(key)
    78  					if actual != expected {
    79  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    80  					}
    81  				}
    82  				content, err := json.Marshal([]*types.Plugin{
    83  					{
    84  						ID: "plugin_id1",
    85  					},
    86  					{
    87  						ID: "plugin_id2",
    88  					},
    89  				})
    90  				if err != nil {
    91  					return nil, err
    92  				}
    93  				return &http.Response{
    94  					StatusCode: http.StatusOK,
    95  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
    96  				}, nil
    97  			}),
    98  		}
    99  
   100  		plugins, err := client.PluginList(context.Background(), listCase.filters)
   101  		if err != nil {
   102  			t.Fatal(err)
   103  		}
   104  		if len(plugins) != 2 {
   105  			t.Fatalf("expected 2 plugins, got %v", plugins)
   106  		}
   107  	}
   108  }