traefik/provider/kubernetes/builder_service_test.go

233 lines
4.4 KiB
Go
Raw Normal View History

2017-12-05 19:24:03 +00:00
package kubernetes
import (
"testing"
"github.com/stretchr/testify/assert"
2018-02-14 08:56:04 +00:00
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
2017-12-05 19:24:03 +00:00
)
2018-02-14 08:56:04 +00:00
func buildService(opts ...func(*corev1.Service)) *corev1.Service {
s := &corev1.Service{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(s)
}
return s
}
2018-02-14 08:56:04 +00:00
func sNamespace(value string) func(*corev1.Service) {
return func(i *corev1.Service) {
2017-12-05 19:24:03 +00:00
i.Namespace = value
}
}
2018-02-14 08:56:04 +00:00
func sName(value string) func(*corev1.Service) {
return func(i *corev1.Service) {
2017-12-05 19:24:03 +00:00
i.Name = value
}
}
2018-02-14 08:56:04 +00:00
func sUID(value types.UID) func(*corev1.Service) {
return func(i *corev1.Service) {
2017-12-05 19:24:03 +00:00
i.UID = value
}
}
2018-02-14 08:56:04 +00:00
func sAnnotation(name string, value string) func(*corev1.Service) {
return func(s *corev1.Service) {
2017-12-05 19:24:03 +00:00
if s.Annotations == nil {
s.Annotations = make(map[string]string)
}
s.Annotations[name] = value
}
}
2018-02-14 08:56:04 +00:00
func sSpec(opts ...func(*corev1.ServiceSpec)) func(*corev1.Service) {
2018-05-18 12:12:03 +00:00
return func(s *corev1.Service) {
2018-02-14 08:56:04 +00:00
spec := &corev1.ServiceSpec{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(spec)
}
2018-05-18 12:12:03 +00:00
s.Spec = *spec
}
}
func sLoadBalancerStatus(opts ...func(*corev1.LoadBalancerStatus)) func(service *corev1.Service) {
return func(s *corev1.Service) {
loadBalancer := &corev1.LoadBalancerStatus{}
for _, opt := range opts {
if opt != nil {
opt(loadBalancer)
}
}
s.Status = corev1.ServiceStatus{
LoadBalancer: *loadBalancer,
}
}
}
func sLoadBalancerIngress(ip string, hostname string) func(*corev1.LoadBalancerStatus) {
return func(status *corev1.LoadBalancerStatus) {
ingress := corev1.LoadBalancerIngress{
IP: ip,
Hostname: hostname,
}
status.Ingress = append(status.Ingress, ingress)
2017-12-05 19:24:03 +00:00
}
}
2018-02-14 08:56:04 +00:00
func clusterIP(ip string) func(*corev1.ServiceSpec) {
return func(spec *corev1.ServiceSpec) {
2017-12-05 19:24:03 +00:00
spec.ClusterIP = ip
}
}
2018-02-14 08:56:04 +00:00
func sType(value corev1.ServiceType) func(*corev1.ServiceSpec) {
return func(spec *corev1.ServiceSpec) {
2017-12-05 19:24:03 +00:00
spec.Type = value
}
}
2018-02-14 08:56:04 +00:00
func sExternalName(name string) func(*corev1.ServiceSpec) {
return func(spec *corev1.ServiceSpec) {
2017-12-05 19:24:03 +00:00
spec.ExternalName = name
}
}
2018-02-14 08:56:04 +00:00
func sPorts(opts ...func(*corev1.ServicePort)) func(*corev1.ServiceSpec) {
return func(spec *corev1.ServiceSpec) {
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
2018-02-14 08:56:04 +00:00
p := &corev1.ServicePort{}
2017-12-05 19:24:03 +00:00
opt(p)
spec.Ports = append(spec.Ports, *p)
}
}
}
2018-02-14 08:56:04 +00:00
func sPort(port int32, name string) func(*corev1.ServicePort) {
return func(sp *corev1.ServicePort) {
2017-12-05 19:24:03 +00:00
sp.Port = port
sp.Name = name
}
}
// Test
func TestBuildService(t *testing.T) {
actual1 := buildService(
sName("service1"),
sNamespace("testing"),
sUID("1"),
sSpec(
clusterIP("10.0.0.1"),
sPorts(sPort(80, "")),
),
)
assert.EqualValues(t, sampleService1(), actual1)
actual2 := buildService(
2018-05-17 16:46:03 +00:00
sName("service2"),
2017-12-05 19:24:03 +00:00
sNamespace("testing"),
2018-05-17 16:46:03 +00:00
sUID("2"),
2017-12-05 19:24:03 +00:00
sSpec(
2018-05-17 16:46:03 +00:00
clusterIP("10.0.0.2"),
2017-12-05 19:24:03 +00:00
sType("ExternalName"),
sExternalName("example.com"),
sPorts(
sPort(80, "http"),
sPort(443, "https"),
),
),
)
assert.EqualValues(t, sampleService2(), actual2)
2018-05-17 16:46:03 +00:00
actual3 := buildService(
sName("service3"),
sNamespace("testing"),
sUID("3"),
sSpec(
clusterIP("10.0.0.3"),
sType("ExternalName"),
sExternalName("example.com"),
sPorts(
sPort(8080, "http"),
sPort(8443, "https"),
),
),
)
assert.EqualValues(t, sampleService3(), actual3)
2017-12-05 19:24:03 +00:00
}
2018-02-14 08:56:04 +00:00
func sampleService1() *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
2017-12-05 19:24:03 +00:00
Name: "service1",
UID: "1",
Namespace: "testing",
},
2018-02-14 08:56:04 +00:00
Spec: corev1.ServiceSpec{
2017-12-05 19:24:03 +00:00
ClusterIP: "10.0.0.1",
2018-02-14 08:56:04 +00:00
Ports: []corev1.ServicePort{
2017-12-05 19:24:03 +00:00
{
Port: 80,
},
},
},
}
}
2018-02-14 08:56:04 +00:00
func sampleService2() *corev1.Service {
2018-05-17 16:46:03 +00:00
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service2",
UID: "2",
Namespace: "testing",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.0.0.2",
Type: "ExternalName",
ExternalName: "example.com",
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
},
{
Name: "https",
Port: 443,
},
},
},
}
}
func sampleService3() *corev1.Service {
2018-02-14 08:56:04 +00:00
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
2017-12-05 19:24:03 +00:00
Name: "service3",
UID: "3",
Namespace: "testing",
},
2018-02-14 08:56:04 +00:00
Spec: corev1.ServiceSpec{
2017-12-05 19:24:03 +00:00
ClusterIP: "10.0.0.3",
Type: "ExternalName",
ExternalName: "example.com",
2018-02-14 08:56:04 +00:00
Ports: []corev1.ServicePort{
2017-12-05 19:24:03 +00:00
{
Name: "http",
2018-05-17 16:46:03 +00:00
Port: 8080,
2017-12-05 19:24:03 +00:00
},
{
Name: "https",
2018-05-17 16:46:03 +00:00
Port: 8443,
2017-12-05 19:24:03 +00:00
},
},
},
}
}