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>
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestNodeRemoveError(t *testing.T) {
|
|
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: false})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
|
|
_, err = client.NodeRemove(context.Background(), "", NodeRemoveOptions{Force: false})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
|
|
_, err = client.NodeRemove(context.Background(), " ", NodeRemoveOptions{Force: false})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
}
|
|
|
|
func TestNodeRemove(t *testing.T) {
|
|
const expectedURL = "/nodes/node_id"
|
|
|
|
removeCases := []struct {
|
|
force bool
|
|
expectedForce string
|
|
}{
|
|
{
|
|
expectedForce: "",
|
|
},
|
|
{
|
|
force: true,
|
|
expectedForce: "1",
|
|
},
|
|
}
|
|
|
|
for _, removeCase := range removeCases {
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
force := req.URL.Query().Get("force")
|
|
if force != removeCase.expectedForce {
|
|
return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", removeCase.expectedForce, force)
|
|
}
|
|
|
|
return mockResponse(http.StatusOK, nil, "body")(req)
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: removeCase.force})
|
|
assert.NilError(t, err)
|
|
}
|
|
}
|