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>
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/volume"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestVolumeCreateError(t *testing.T) {
|
|
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.VolumeCreate(context.Background(), VolumeCreateOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestVolumeCreate(t *testing.T) {
|
|
const expectedURL = "/volumes/create"
|
|
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
return mockJSONResponse(http.StatusOK, nil, volume.Volume{
|
|
Name: "volume",
|
|
Driver: "local",
|
|
Mountpoint: "mountpoint",
|
|
})(req)
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
res, err := client.VolumeCreate(context.Background(), VolumeCreateOptions{
|
|
Name: "myvolume",
|
|
Driver: "mydriver",
|
|
DriverOpts: map[string]string{
|
|
"opt-key": "opt-value",
|
|
},
|
|
})
|
|
assert.NilError(t, err)
|
|
v := res.Volume
|
|
assert.Check(t, is.Equal(v.Name, "volume"))
|
|
assert.Check(t, is.Equal(v.Driver, "local"))
|
|
assert.Check(t, is.Equal(v.Mountpoint, "mountpoint"))
|
|
}
|