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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-10-28 01:54:22 +01:00
parent 3fa5aef0f9
commit 962015b057
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 29 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/internal/test/builders" . "github.com/docker/cli/internal/test/builders"
) )
@ -18,9 +19,13 @@ type fakeClient struct {
taskListFunc func(context.Context, types.TaskListOptions) ([]swarm.Task, error) taskListFunc func(context.Context, types.TaskListOptions) ([]swarm.Task, error)
infoFunc func(ctx context.Context) (types.Info, error) infoFunc func(ctx context.Context) (types.Info, error)
networkInspectFunc func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, 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) { 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 return nil, nil
} }
@ -69,9 +74,6 @@ func (f *fakeClient) NetworkInspect(ctx context.Context, networkID string, optio
return types.NetworkResource{}, nil return types.NetworkResource{}, nil
} }
func newService(id string, name string) swarm.Service { func newService(id string, name string, opts ...func(*swarm.Service)) swarm.Service {
return swarm.Service{ return *Service(append(opts, ServiceID(id), ServiceName(name))...)
ID: id,
Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: name}},
}
} }

View File

@ -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) { func ReplicatedService(replicas uint64) func(*swarm.Service) {
return func(service *swarm.Service) { return func(service *swarm.Service) {
service.Spec.Mode = swarm.ServiceMode{Replicated: &swarm.ReplicatedService{Replicas: &replicas}} 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,
}
} }
} }