Files
moby/client/service_inspect_test.go
Sebastiaan van Stijn b29990916d client: fix unused imports
this was introduced in c950796596, but
likely due to the "replace" rules not being present, CI tested the
current version of the module instead of the code in the repository.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-12 17:53:36 +01:00

60 lines
2.0 KiB
Go

package client
import (
"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 TestServiceInspectError(t *testing.T) {
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ServiceInspect(t.Context(), "nothing", ServiceInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestServiceInspectServiceNotFound(t *testing.T) {
client, err := New(WithMockClient(errorMock(http.StatusNotFound, "Server error")))
assert.NilError(t, err)
_, err = client.ServiceInspect(t.Context(), "unknown", ServiceInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
func TestServiceInspectWithEmptyID(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.ServiceInspect(t.Context(), "", ServiceInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.ServiceInspect(t.Context(), " ", 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 := 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.Service{
ID: "service_id",
})(req)
}))
assert.NilError(t, err)
inspect, err := client.ServiceInspect(t.Context(), "service_id", ServiceInspectOptions{})
assert.NilError(t, err)
assert.Check(t, is.Equal(inspect.Service.ID, "service_id"))
}