mirror of https://github.com/docker/cli.git
Merge pull request #2180 from thaJeztah/refactor_conversion_test
kubernetes/conversion_test: use test-builders package
This commit is contained in:
commit
ab255f13b7
|
@ -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"
|
||||
|
@ -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)),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
func withPort(port swarm.PortConfig) func(*swarm.Service) {
|
||||
return func(service *swarm.Service) {
|
||||
if service.Endpoint.Ports == nil {
|
||||
|
@ -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...)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue