mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
Use a more idiomatic name so that it can be used as `client.New()`. We should look if we want `New()` to have different / updated defaults i.e., enable `WithEnv` as default, and have an opt-out and have API- version negotiation enabled by default (with an opt-out option). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/plugin"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestPluginListError(t *testing.T) {
|
|
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.PluginList(context.Background(), PluginListOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestPluginList(t *testing.T) {
|
|
const expectedURL = "/plugins"
|
|
|
|
listCases := []struct {
|
|
filters Filters
|
|
expectedQueryParams map[string]string
|
|
}{
|
|
{
|
|
expectedQueryParams: map[string]string{
|
|
"all": "",
|
|
"filter": "",
|
|
"filters": "",
|
|
},
|
|
},
|
|
{
|
|
filters: make(Filters).Add("enabled", "true"),
|
|
expectedQueryParams: map[string]string{
|
|
"all": "",
|
|
"filter": "",
|
|
"filters": `{"enabled":{"true":true}}`,
|
|
},
|
|
},
|
|
{
|
|
filters: make(Filters).Add("capability", "volumedriver", "authz"),
|
|
expectedQueryParams: map[string]string{
|
|
"all": "",
|
|
"filter": "",
|
|
"filters": `{"capability":{"authz":true,"volumedriver":true}}`,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, listCase := range listCases {
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
query := req.URL.Query()
|
|
for key, expected := range listCase.expectedQueryParams {
|
|
actual := query.Get(key)
|
|
if actual != expected {
|
|
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
|
}
|
|
}
|
|
return mockJSONResponse(http.StatusOK, nil, []*plugin.Plugin{
|
|
{ID: "plugin_id1"},
|
|
{ID: "plugin_id2"},
|
|
})(req)
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
list, err := client.PluginList(context.Background(), PluginListOptions{
|
|
Filters: listCase.filters,
|
|
})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Len(list.Items, 2))
|
|
}
|
|
}
|