Files
moby/client/config_list_test.go
Sebastiaan van Stijn d1f70d4f54 client: deprecate NewClientWithOpts in favor of New
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>
2025-10-30 18:09:44 +01:00

75 lines
1.8 KiB
Go

package client
import (
"context"
"fmt"
"net/http"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/swarm"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestConfigListError(t *testing.T) {
client, err := New(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ConfigList(context.Background(), ConfigListOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestConfigList(t *testing.T) {
const expectedURL = "/configs"
listCases := []struct {
options ConfigListOptions
expectedQueryParams map[string]string
}{
{
options: ConfigListOptions{},
expectedQueryParams: map[string]string{
"filters": "",
},
},
{
options: ConfigListOptions{
Filters: make(Filters).
Add("label", "label1").
Add("label", "label2"),
},
expectedQueryParams: map[string]string{
"filters": `{"label":{"label1":true,"label2":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, []swarm.Config{
{ID: "config_id1"},
{ID: "config_id2"},
})(req)
}),
)
assert.NilError(t, err)
result, err := client.ConfigList(context.Background(), listCase.options)
assert.NilError(t, err)
assert.Check(t, is.Len(result.Items, 2))
}
}