mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"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 TestServiceInspectError(t *testing.T) {
|
|
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ServiceInspect(context.Background(), "nothing", ServiceInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
}
|
|
|
|
func TestServiceInspectServiceNotFound(t *testing.T) {
|
|
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusNotFound, "Server error")))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ServiceInspect(context.Background(), "unknown", ServiceInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
|
|
}
|
|
|
|
func TestServiceInspectWithEmptyID(t *testing.T) {
|
|
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
return nil, errors.New("should not make request")
|
|
}))
|
|
assert.NilError(t, err)
|
|
_, err = client.ServiceInspect(context.Background(), "", ServiceInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
|
|
_, err = client.ServiceInspect(context.Background(), " ", ServiceInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
}
|
|
|
|
func TestServiceInspect(t *testing.T) {
|
|
const expectedURL = "/services/service_id"
|
|
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
content, err := json.Marshal(swarm.Service{
|
|
ID: "service_id",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
|
}, nil
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
inspect, err := client.ServiceInspect(context.Background(), "service_id", ServiceInspectOptions{})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(inspect.Service.ID, "service_id"))
|
|
}
|