traefik/provider/kubernetes/builder_service_test.go

167 lines
3.1 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) {
return func(i *corev1.Service) {
spec := &corev1.ServiceSpec{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(spec)
}
i.Spec = *spec
}
}
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(
sName("service3"),
sNamespace("testing"),
sUID("3"),
sSpec(
clusterIP("10.0.0.3"),
sType("ExternalName"),
sExternalName("example.com"),
sPorts(
sPort(80, "http"),
sPort(443, "https"),
),
),
)
assert.EqualValues(t, sampleService2(), actual2)
}
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 {
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",
Port: 80,
},
{
Name: "https",
Port: 443,
},
},
},
}
}