From 962015b05797194b94f8dbd79f60e442533d89db Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 28 Oct 2019 01:54:22 +0100 Subject: [PATCH] internal/builders: add GlobalService, ServiceStatus, NodeList() This patch: - Adds new GlobalService and ServiceStatus options - Makes the NodeList() function functional - Minor improvment to the `newService()` function to allow passing options Signed-off-by: Sebastiaan van Stijn --- cli/command/service/client_test.go | 12 +++++++----- internal/test/builders/service.go | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cli/command/service/client_test.go b/cli/command/service/client_test.go index 8d0d592cec..b6b9f99058 100644 --- a/cli/command/service/client_test.go +++ b/cli/command/service/client_test.go @@ -6,6 +6,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" + // Import builders to get the builder function as package function . "github.com/docker/cli/internal/test/builders" ) @@ -18,9 +19,13 @@ type fakeClient struct { taskListFunc func(context.Context, types.TaskListOptions) ([]swarm.Task, error) infoFunc func(ctx context.Context) (types.Info, error) networkInspectFunc func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error) + nodeListFunc func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) } func (f *fakeClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) { + if f.nodeListFunc != nil { + return f.nodeListFunc(ctx, options) + } return nil, nil } @@ -69,9 +74,6 @@ func (f *fakeClient) NetworkInspect(ctx context.Context, networkID string, optio return types.NetworkResource{}, nil } -func newService(id string, name string) swarm.Service { - return swarm.Service{ - ID: id, - Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: name}}, - } +func newService(id string, name string, opts ...func(*swarm.Service)) swarm.Service { + return *Service(append(opts, ServiceID(id), ServiceName(name))...) } diff --git a/internal/test/builders/service.go b/internal/test/builders/service.go index 2817e6e2eb..ea2c95a301 100644 --- a/internal/test/builders/service.go +++ b/internal/test/builders/service.go @@ -46,10 +46,31 @@ func ServiceLabels(labels map[string]string) func(*swarm.Service) { } } -// ReplicatedService sets the number of replicas for the service +// GlobalService sets the service to use "global" mode +func GlobalService() func(*swarm.Service) { + return func(service *swarm.Service) { + service.Spec.Mode = swarm.ServiceMode{Global: &swarm.GlobalService{}} + } +} + +// ReplicatedService sets the service to use "replicated" mode with the specified number of replicas func ReplicatedService(replicas uint64) func(*swarm.Service) { return func(service *swarm.Service) { service.Spec.Mode = swarm.ServiceMode{Replicated: &swarm.ReplicatedService{Replicas: &replicas}} + if service.ServiceStatus == nil { + service.ServiceStatus = &swarm.ServiceStatus{} + } + service.ServiceStatus.DesiredTasks = replicas + } +} + +// ServiceStatus sets the services' ServiceStatus (API v1.41 and above) +func ServiceStatus(desired, running uint64) func(*swarm.Service) { + return func(service *swarm.Service) { + service.ServiceStatus = &swarm.ServiceStatus{ + RunningTasks: running, + DesiredTasks: desired, + } } }