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