github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/stack/swarm/deploy_test.go (about) 1 package swarm 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/docker/cli/cli/compose/convert" 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/swarm" 11 "gotest.tools/v3/assert" 12 is "gotest.tools/v3/assert/cmp" 13 ) 14 15 func TestPruneServices(t *testing.T) { 16 ctx := context.Background() 17 namespace := convert.NewNamespace("foo") 18 services := map[string]struct{}{ 19 "new": {}, 20 "keep": {}, 21 } 22 client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}} 23 dockerCli := test.NewFakeCli(client) 24 25 pruneServices(ctx, dockerCli, namespace, services) 26 assert.Check(t, is.DeepEqual(buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices)) 27 } 28 29 // TestServiceUpdateResolveImageChanged tests that the service's 30 // image digest, and "ForceUpdate" is preserved if the image did not change in 31 // the compose file 32 func TestServiceUpdateResolveImageChanged(t *testing.T) { 33 namespace := convert.NewNamespace("mystack") 34 35 var ( 36 receivedOptions types.ServiceUpdateOptions 37 receivedService swarm.ServiceSpec 38 ) 39 40 client := test.NewFakeCli(&fakeClient{ 41 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 42 return []swarm.Service{ 43 { 44 Spec: swarm.ServiceSpec{ 45 Annotations: swarm.Annotations{ 46 Name: namespace.Name() + "_myservice", 47 Labels: map[string]string{"com.docker.stack.image": "foobar:1.2.3"}, 48 }, 49 TaskTemplate: swarm.TaskSpec{ 50 ContainerSpec: &swarm.ContainerSpec{ 51 Image: "foobar:1.2.3@sha256:deadbeef", 52 }, 53 ForceUpdate: 123, 54 }, 55 }, 56 }, 57 }, nil 58 }, 59 serviceUpdateFunc: func(serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) { 60 receivedOptions = options 61 receivedService = service 62 return types.ServiceUpdateResponse{}, nil 63 }, 64 }) 65 66 var testcases = []struct { 67 image string 68 expectedQueryRegistry bool 69 expectedImage string 70 expectedForceUpdate uint64 71 }{ 72 // Image not changed 73 { 74 image: "foobar:1.2.3", 75 expectedQueryRegistry: false, 76 expectedImage: "foobar:1.2.3@sha256:deadbeef", 77 expectedForceUpdate: 123, 78 }, 79 // Image changed 80 { 81 image: "foobar:1.2.4", 82 expectedQueryRegistry: true, 83 expectedImage: "foobar:1.2.4", 84 expectedForceUpdate: 123, 85 }, 86 } 87 88 ctx := context.Background() 89 90 for _, tc := range testcases { 91 tc := tc 92 t.Run(tc.image, func(t *testing.T) { 93 spec := map[string]swarm.ServiceSpec{ 94 "myservice": { 95 TaskTemplate: swarm.TaskSpec{ 96 ContainerSpec: &swarm.ContainerSpec{ 97 Image: tc.image, 98 }, 99 }, 100 }, 101 } 102 err := deployServices(ctx, client, spec, namespace, false, ResolveImageChanged) 103 assert.NilError(t, err) 104 assert.Check(t, is.Equal(receivedOptions.QueryRegistry, tc.expectedQueryRegistry)) 105 assert.Check(t, is.Equal(receivedService.TaskTemplate.ContainerSpec.Image, tc.expectedImage)) 106 assert.Check(t, is.Equal(receivedService.TaskTemplate.ForceUpdate, tc.expectedForceUpdate)) 107 108 receivedService = swarm.ServiceSpec{} 109 receivedOptions = types.ServiceUpdateOptions{} 110 }) 111 } 112 }