mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
The WithMockClient option was explicitly resetting the client's API version (see [1]), which differs from the regular client, which is initialized with the current API version used by the client (see [2]). This patch: - reduces the `WithMockClient` to only set the custom HTTP client, leaving other fields un-touched. - adds a test utility and updates tests to handle the API-version prefix - removes redundant uses of `WithVersion()` in tests; for most test-cases it was used to make sure a current API version is used that supports the feature being tested, but there was no test to verify the behavior for lower API versions, so we may as well test against "latest". [1]:5a582729d8/client/client_mock_test.go (L22-L36)[2]:5a582729d8/client/client.go (L167-L190)Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/filters"
|
|
"github.com/moby/moby/api/types/network"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestNetworkListError(t *testing.T) {
|
|
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.NetworkList(context.Background(), NetworkListOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestNetworkList(t *testing.T) {
|
|
const expectedURL = "/networks"
|
|
|
|
listCases := []struct {
|
|
options NetworkListOptions
|
|
expectedFilters string
|
|
}{
|
|
{
|
|
options: NetworkListOptions{},
|
|
expectedFilters: "",
|
|
},
|
|
{
|
|
options: NetworkListOptions{
|
|
Filters: filters.NewArgs(filters.Arg("dangling", "false")),
|
|
},
|
|
expectedFilters: `{"dangling":{"false":true}}`,
|
|
},
|
|
{
|
|
options: NetworkListOptions{
|
|
Filters: filters.NewArgs(filters.Arg("dangling", "true")),
|
|
},
|
|
expectedFilters: `{"dangling":{"true":true}}`,
|
|
},
|
|
{
|
|
options: NetworkListOptions{
|
|
Filters: filters.NewArgs(
|
|
filters.Arg("label", "label1"),
|
|
filters.Arg("label", "label2"),
|
|
),
|
|
},
|
|
expectedFilters: `{"label":{"label1":true,"label2":true}}`,
|
|
},
|
|
}
|
|
|
|
for _, listCase := range listCases {
|
|
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
query := req.URL.Query()
|
|
actualFilters := query.Get("filters")
|
|
if actualFilters != listCase.expectedFilters {
|
|
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
|
|
}
|
|
content, err := json.Marshal([]network.Summary{
|
|
{
|
|
Network: network.Network{
|
|
Name: "network",
|
|
Driver: "bridge",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
|
}, nil
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
networkResources, err := client.NetworkList(context.Background(), listCase.options)
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Len(networkResources, 1))
|
|
}
|
|
}
|