Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 13 additions & 52 deletions cli/command/stack/kubernetes/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"testing"

. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
"github.com/docker/compose-on-kubernetes/api/labels"
"github.com/docker/docker/api/types/swarm"
"gotest.tools/assert"
Expand Down Expand Up @@ -45,8 +46,8 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
},
},
expectedServices: []swarm.Service{
makeSwarmService(t, "stack_service1", "uid1", withMode("replicated", 5), withStatus(2, 5)),
makeSwarmService(t, "stack_service2", "uid2", withMode("replicated", 3), withStatus(3, 3)),
makeSwarmService(t, "stack_service1", "uid1", ReplicatedService(5), ServiceStatus(5, 2)),
makeSwarmService(t, "stack_service2", "uid2", ReplicatedService(3), ServiceStatus(3, 3)),
},
},
{
Expand All @@ -70,8 +71,9 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
},
expectedServices: []swarm.Service{
makeSwarmService(t, "stack_service", "uid1",
withMode("replicated", 1),
withStatus(1, 1), withPort(swarm.PortConfig{
ReplicatedService(1),
ServiceStatus(1, 1),
withPort(swarm.PortConfig{
PublishMode: swarm.PortConfigPublishModeIngress,
PublishedPort: 80,
TargetPort: 80,
Expand Down Expand Up @@ -101,8 +103,8 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
},
expectedServices: []swarm.Service{
makeSwarmService(t, "stack_service", "uid1",
withMode("replicated", 1),
withStatus(1, 1),
ReplicatedService(1),
ServiceStatus(1, 1),
withPort(swarm.PortConfig{
PublishMode: swarm.PortConfigPublishModeHost,
PublishedPort: 35666,
Expand Down Expand Up @@ -165,25 +167,7 @@ func makeKubeService(service, stack, uid string, serviceType apiv1.ServiceType,
}
}

func withMode(mode string, replicas uint64) func(*swarm.Service) {
return func(service *swarm.Service) {
switch mode {
case "global":
service.Spec.Mode = swarm.ServiceMode{
Global: &swarm.GlobalService{},
}
case "replicated":
service.Spec.Mode = swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{Replicas: &replicas},
}
withStatus(0, replicas)
default:
service.Spec.Mode = swarm.ServiceMode{}
withStatus(0, 0)
}
}
}

// TODO convertToServices currently doesn't set swarm.EndpointSpec.Ports
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a TODO for this; looks like the conversion doesn't return the ports in the spec for the service

func withPort(port swarm.PortConfig) func(*swarm.Service) {
return func(service *swarm.Service) {
if service.Endpoint.Ports == nil {
Expand All @@ -193,32 +177,9 @@ func withPort(port swarm.PortConfig) func(*swarm.Service) {
}
}

func withStatus(running, desired uint64) func(*swarm.Service) {
return func(service *swarm.Service) {
service.ServiceStatus = &swarm.ServiceStatus{
RunningTasks: running,
DesiredTasks: desired,
}
}
}

func makeSwarmService(t *testing.T, service, id string, opts ...func(*swarm.Service)) swarm.Service {
func makeSwarmService(t *testing.T, name, id string, opts ...func(*swarm.Service)) swarm.Service {
t.Helper()
s := swarm.Service{
ID: id,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: service,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: "image",
},
},
},
}
for _, o := range opts {
o(&s)
}
return s
options := []func(*swarm.Service){ServiceID(id), ServiceName(name), ServiceImage("image")}
options = append(options, opts...)
return *Service(options...)
}
24 changes: 11 additions & 13 deletions internal/test/builders/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking that we could even remove these defaults; looks like it's currently not used in other locations where the code depends on it being set (which means we could convert this package to something that can be used outside of testing as well


for _, builder := range builders {
builder(service)
for _, opt := range append(defaults, builders...) {
opt(service)
}

return service
Expand Down Expand Up @@ -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
Expand Down