Files
moby/client/service_update_test.go
Sebastiaan van Stijn d1f70d4f54 client: deprecate NewClientWithOpts in favor of New
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>
2025-10-30 18:09:44 +01:00

86 lines
2.5 KiB
Go

package client
import (
"fmt"
"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 TestServiceUpdateError(t *testing.T) {
client, err := New(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ServiceUpdate(t.Context(), "service_id", ServiceUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ServiceUpdate(t.Context(), "", ServiceUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.ServiceUpdate(t.Context(), " ", ServiceUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
// TestServiceUpdateConnectionError verifies that connection errors occurring
// during API-version negotiation are not shadowed by API-version errors.
//
// Regression test for https://github.com/docker/cli/issues/4890
func TestServiceUpdateConnectionError(t *testing.T) {
client, err := New(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
assert.NilError(t, err)
_, err = client.ServiceUpdate(t.Context(), "service_id", ServiceUpdateOptions{})
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
}
func TestServiceUpdate(t *testing.T) {
const expectedURL = "/services/service_id/update"
updateCases := []struct {
swarmVersion swarm.Version
expectedVersion string
}{
{
expectedVersion: "0",
},
{
swarmVersion: swarm.Version{
Index: 0,
},
expectedVersion: "0",
},
{
swarmVersion: swarm.Version{
Index: 10,
},
expectedVersion: "10",
},
}
for _, updateCase := range updateCases {
client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
version := req.URL.Query().Get("version")
if version != updateCase.expectedVersion {
return nil, fmt.Errorf("version not set in URL query properly, expected '%s', got %s", updateCase.expectedVersion, version)
}
return mockResponse(http.StatusOK, nil, "{}")(req)
}))
assert.NilError(t, err)
_, err = client.ServiceUpdate(t.Context(), "service_id", ServiceUpdateOptions{
Version: updateCase.swarmVersion,
Spec: swarm.ServiceSpec{},
})
assert.NilError(t, err)
}
}