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>
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/swarm"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestTaskInspectError(t *testing.T) {
|
|
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.TaskInspect(context.Background(), "nothing", TaskInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestTaskInspectWithEmptyID(t *testing.T) {
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
return nil, errors.New("should not make request")
|
|
}))
|
|
assert.NilError(t, err)
|
|
_, err = client.TaskInspect(context.Background(), "", TaskInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
|
|
_, err = client.TaskInspect(context.Background(), " ", TaskInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
}
|
|
|
|
func TestTaskInspect(t *testing.T) {
|
|
const expectedURL = "/tasks/task_id"
|
|
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
return mockJSONResponse(http.StatusOK, nil, swarm.Task{
|
|
ID: "task_id",
|
|
})(req)
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
result, err := client.TaskInspect(context.Background(), "task_id", TaskInspectOptions{})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(result.Task.ID, "task_id"))
|
|
}
|