From 270defa1771d57c176b9813d12543dc0e8daa5f0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Oct 2019 10:52:26 +0100 Subject: [PATCH] internal/test/builders: don't set EndpointSpec by default Also rewrite `Service()` to use the available options Signed-off-by: Sebastiaan van Stijn --- internal/test/builders/service.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/internal/test/builders/service.go b/internal/test/builders/service.go index ea2c95a301..4a6b50baa6 100644 --- a/internal/test/builders/service.go +++ b/internal/test/builders/service.go @@ -6,20 +6,12 @@ import ( // Service creates a service with default values. // Any number of service builder functions can be passed to augment it. -// Currently, only ServiceName is implemented func Service(builders ...func(*swarm.Service)) *swarm.Service { - service := &swarm.Service{ - ID: "serviceID", - Spec: swarm.ServiceSpec{ - Annotations: swarm.Annotations{ - Name: "defaultServiceName", - }, - EndpointSpec: &swarm.EndpointSpec{}, - }, - } + service := &swarm.Service{} + defaults := []func(*swarm.Service){ServiceID("serviceID"), ServiceName("defaultServiceName")} - for _, builder := range builders { - builder(service) + for _, opt := range append(defaults, builders...) { + opt(service) } return service @@ -77,13 +69,19 @@ func ServiceStatus(desired, running uint64) func(*swarm.Service) { // ServiceImage sets the service's image func ServiceImage(image string) func(*swarm.Service) { return func(service *swarm.Service) { - service.Spec.TaskTemplate = swarm.TaskSpec{ContainerSpec: &swarm.ContainerSpec{Image: image}} + if service.Spec.TaskTemplate.ContainerSpec == nil { + service.Spec.TaskTemplate.ContainerSpec = &swarm.ContainerSpec{} + } + service.Spec.TaskTemplate.ContainerSpec.Image = image } } // ServicePort sets the service's port func ServicePort(port swarm.PortConfig) func(*swarm.Service) { return func(service *swarm.Service) { + if service.Spec.EndpointSpec == nil { + service.Spec.EndpointSpec = &swarm.EndpointSpec{} + } service.Spec.EndpointSpec.Ports = append(service.Spec.EndpointSpec.Ports, port) assignedPort := port