2017-11-20 09:30:52 -05:00
|
|
|
package swarm
|
2017-02-22 15:43:13 -05:00
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-02-22 15:43:13 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-07-26 09:33:57 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-02-22 15:43:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPruneServices(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
namespace := convert.NewNamespace("foo")
|
|
|
|
services := map[string]struct{}{
|
|
|
|
"new": {},
|
|
|
|
"keep": {},
|
|
|
|
}
|
2017-03-26 02:23:24 -04:00
|
|
|
client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}}
|
2017-07-05 14:43:39 -04:00
|
|
|
dockerCli := test.NewFakeCli(client)
|
2017-02-22 15:43:13 -05:00
|
|
|
|
|
|
|
pruneServices(ctx, dockerCli, namespace, services)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices))
|
2017-02-22 15:43:13 -05:00
|
|
|
}
|
2017-07-26 09:33:57 -04:00
|
|
|
|
|
|
|
// TestServiceUpdateResolveImageChanged tests that the service's
|
2018-03-26 09:24:28 -04:00
|
|
|
// image digest, and "ForceUpdate" is preserved if the image did not change in
|
|
|
|
// the compose file
|
2017-07-26 09:33:57 -04:00
|
|
|
func TestServiceUpdateResolveImageChanged(t *testing.T) {
|
|
|
|
namespace := convert.NewNamespace("mystack")
|
|
|
|
|
|
|
|
var (
|
|
|
|
receivedOptions types.ServiceUpdateOptions
|
|
|
|
receivedService swarm.ServiceSpec
|
|
|
|
)
|
|
|
|
|
|
|
|
client := test.NewFakeCli(&fakeClient{
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
|
|
|
{
|
|
|
|
Spec: swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: namespace.Name() + "_myservice",
|
|
|
|
Labels: map[string]string{"com.docker.stack.image": "foobar:1.2.3"},
|
|
|
|
},
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
2017-08-07 05:52:40 -04:00
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
2017-07-26 09:33:57 -04:00
|
|
|
Image: "foobar:1.2.3@sha256:deadbeef",
|
|
|
|
},
|
2018-03-26 09:24:28 -04:00
|
|
|
ForceUpdate: 123,
|
2017-07-26 09:33:57 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2023-10-13 14:34:32 -04:00
|
|
|
serviceUpdateFunc: func(serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (swarm.ServiceUpdateResponse, error) {
|
2017-07-26 09:33:57 -04:00
|
|
|
receivedOptions = options
|
|
|
|
receivedService = service
|
2023-10-13 14:34:32 -04:00
|
|
|
return swarm.ServiceUpdateResponse{}, nil
|
2017-07-26 09:33:57 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-07-26 09:33:57 -04:00
|
|
|
image string
|
|
|
|
expectedQueryRegistry bool
|
|
|
|
expectedImage string
|
2018-03-26 09:24:28 -04:00
|
|
|
expectedForceUpdate uint64
|
2017-07-26 09:33:57 -04:00
|
|
|
}{
|
|
|
|
// Image not changed
|
|
|
|
{
|
2018-10-10 02:37:35 -04:00
|
|
|
image: "foobar:1.2.3",
|
2017-07-26 09:33:57 -04:00
|
|
|
expectedQueryRegistry: false,
|
|
|
|
expectedImage: "foobar:1.2.3@sha256:deadbeef",
|
2018-03-26 09:24:28 -04:00
|
|
|
expectedForceUpdate: 123,
|
2017-07-26 09:33:57 -04:00
|
|
|
},
|
|
|
|
// Image changed
|
|
|
|
{
|
2018-10-10 02:37:35 -04:00
|
|
|
image: "foobar:1.2.4",
|
2017-07-26 09:33:57 -04:00
|
|
|
expectedQueryRegistry: true,
|
|
|
|
expectedImage: "foobar:1.2.4",
|
2018-03-26 09:24:28 -04:00
|
|
|
expectedForceUpdate: 123,
|
2017-07-26 09:33:57 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-07-06 08:58:40 -04:00
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.image, func(t *testing.T) {
|
|
|
|
spec := map[string]swarm.ServiceSpec{
|
|
|
|
"myservice": {
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
|
|
|
Image: tc.image,
|
|
|
|
},
|
2017-07-26 09:33:57 -04:00
|
|
|
},
|
|
|
|
},
|
2020-07-06 08:58:40 -04:00
|
|
|
}
|
2023-05-05 10:00:30 -04:00
|
|
|
_, err := deployServices(ctx, client, spec, namespace, false, ResolveImageChanged)
|
2020-07-06 08:58:40 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(receivedOptions.QueryRegistry, tc.expectedQueryRegistry))
|
|
|
|
assert.Check(t, is.Equal(receivedService.TaskTemplate.ContainerSpec.Image, tc.expectedImage))
|
|
|
|
assert.Check(t, is.Equal(receivedService.TaskTemplate.ForceUpdate, tc.expectedForceUpdate))
|
2017-07-26 09:33:57 -04:00
|
|
|
|
2020-07-06 08:58:40 -04:00
|
|
|
receivedService = swarm.ServiceSpec{}
|
|
|
|
receivedOptions = types.ServiceUpdateOptions{}
|
|
|
|
})
|
2017-07-26 09:33:57 -04:00
|
|
|
}
|
|
|
|
}
|