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>
98 lines
2.6 KiB
Go
98 lines
2.6 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/container"
|
|
"github.com/moby/moby/api/types/filters"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestContainerListError(t *testing.T) {
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerList(context.Background(), ContainerListOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestContainerList(t *testing.T) {
|
|
const (
|
|
expectedURL = "/containers/json"
|
|
expectedFilters = `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
|
|
)
|
|
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()
|
|
all := query.Get("all")
|
|
if all != "1" {
|
|
return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
|
|
}
|
|
limit := query.Get("limit")
|
|
if limit != "" {
|
|
return nil, fmt.Errorf("limit should have not be present in query, got %s", limit)
|
|
}
|
|
since := query.Get("since")
|
|
if since != "container" {
|
|
return nil, fmt.Errorf("since not set in URL query properly. Expected 'container', got %s", since)
|
|
}
|
|
before := query.Get("before")
|
|
if before != "" {
|
|
return nil, fmt.Errorf("before should have not be present in query, got %s", before)
|
|
}
|
|
size := query.Get("size")
|
|
if size != "1" {
|
|
return nil, fmt.Errorf("size not set in URL query properly. Expected '1', got %s", size)
|
|
}
|
|
fltrs := query.Get("filters")
|
|
if fltrs != expectedFilters {
|
|
return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, fltrs)
|
|
}
|
|
|
|
b, err := json.Marshal([]container.Summary{
|
|
{
|
|
ID: "container_id1",
|
|
},
|
|
{
|
|
ID: "container_id2",
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
|
}, nil
|
|
}),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
containers, err := client.ContainerList(context.Background(), ContainerListOptions{
|
|
Size: true,
|
|
All: true,
|
|
Since: "container",
|
|
Filters: filters.NewArgs(
|
|
filters.Arg("label", "label1"),
|
|
filters.Arg("label", "label2"),
|
|
filters.Arg("before", "container"),
|
|
),
|
|
})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Len(containers, 2))
|
|
}
|