mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +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>
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/volume"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestVolumePrune(t *testing.T) {
|
|
const expectedURL = "/volumes/prune"
|
|
|
|
tests := []struct {
|
|
doc string
|
|
opts VolumePruneOptions
|
|
expectedError string
|
|
expectedFilters string
|
|
}{
|
|
{
|
|
doc: "empty options",
|
|
},
|
|
{
|
|
doc: "all filter",
|
|
opts: VolumePruneOptions{
|
|
Filters: make(Filters).Add("all", "true"),
|
|
},
|
|
expectedFilters: `{"all":{"true":true}}`,
|
|
},
|
|
{
|
|
doc: "all option",
|
|
opts: VolumePruneOptions{
|
|
All: true,
|
|
},
|
|
expectedFilters: `{"all":{"true":true}}`,
|
|
},
|
|
{
|
|
doc: "label filters",
|
|
opts: VolumePruneOptions{
|
|
Filters: make(Filters).Add("label", "label1", "label2"),
|
|
},
|
|
expectedFilters: `{"label":{"label1":true,"label2":true}}`,
|
|
},
|
|
{
|
|
doc: "all and label filters",
|
|
opts: VolumePruneOptions{
|
|
All: true,
|
|
Filters: make(Filters).Add("label", "label1", "label2"),
|
|
},
|
|
expectedFilters: `{"all":{"true":true},"label":{"label1":true,"label2":true}}`,
|
|
},
|
|
{
|
|
doc: "conflicting options",
|
|
opts: VolumePruneOptions{
|
|
All: true,
|
|
Filters: make(Filters).Add("all", "true"),
|
|
},
|
|
expectedError: `conflicting options: cannot specify both "all" and "all" filter`,
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
query := req.URL.Query()
|
|
actualFilters := query.Get("filters")
|
|
if actualFilters != tc.expectedFilters {
|
|
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", tc.expectedFilters, actualFilters)
|
|
}
|
|
return mockJSONResponse(http.StatusOK, nil, volume.PruneReport{
|
|
VolumesDeleted: []string{"volume"},
|
|
SpaceReclaimed: 12345,
|
|
})(req)
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.VolumesPrune(t.Context(), tc.opts)
|
|
if tc.expectedError != "" {
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidArgument))
|
|
assert.Check(t, is.Error(err, tc.expectedError))
|
|
} else {
|
|
assert.NilError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|