traefik/provider/kubernetes/builder_endpoint_test.go

159 lines
3 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 buildEndpoint(opts ...func(*corev1.Endpoints)) *corev1.Endpoints {
e := &corev1.Endpoints{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(e)
}
return e
}
2018-02-14 08:56:04 +00:00
func eNamespace(value string) func(*corev1.Endpoints) {
return func(i *corev1.Endpoints) {
2017-12-05 19:24:03 +00:00
i.Namespace = value
}
}
2018-02-14 08:56:04 +00:00
func eName(value string) func(*corev1.Endpoints) {
return func(i *corev1.Endpoints) {
2017-12-05 19:24:03 +00:00
i.Name = value
}
}
2018-02-14 08:56:04 +00:00
func eUID(value types.UID) func(*corev1.Endpoints) {
return func(i *corev1.Endpoints) {
2017-12-05 19:24:03 +00:00
i.UID = value
}
}
2018-02-14 08:56:04 +00:00
func subset(opts ...func(*corev1.EndpointSubset)) func(*corev1.Endpoints) {
return func(e *corev1.Endpoints) {
s := &corev1.EndpointSubset{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(s)
}
e.Subsets = append(e.Subsets, *s)
}
}
2018-02-14 08:56:04 +00:00
func eAddresses(opts ...func(*corev1.EndpointAddress)) func(*corev1.EndpointSubset) {
return func(subset *corev1.EndpointSubset) {
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
2018-04-16 12:44:04 +00:00
a := &corev1.EndpointAddress{}
2017-12-05 19:24:03 +00:00
opt(a)
2018-04-16 12:44:04 +00:00
subset.Addresses = append(subset.Addresses, *a)
2017-12-05 19:24:03 +00:00
}
}
}
2018-02-14 08:56:04 +00:00
func eAddress(ip string) func(*corev1.EndpointAddress) {
return func(address *corev1.EndpointAddress) {
2017-12-05 19:24:03 +00:00
address.IP = ip
}
}
2018-06-19 20:10:03 +00:00
func eAddressWithTargetRef(targetRef, ip string) func(*corev1.EndpointAddress) {
return func(address *corev1.EndpointAddress) {
address.TargetRef = &corev1.ObjectReference{Name: targetRef}
address.IP = ip
}
}
2018-02-14 08:56:04 +00:00
func ePorts(opts ...func(port *corev1.EndpointPort)) func(*corev1.EndpointSubset) {
return func(spec *corev1.EndpointSubset) {
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
2018-02-14 08:56:04 +00:00
p := &corev1.EndpointPort{}
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 ePort(port int32, name string) func(*corev1.EndpointPort) {
return func(sp *corev1.EndpointPort) {
2017-12-05 19:24:03 +00:00
sp.Port = port
sp.Name = name
}
}
// Test
func TestBuildEndpoint(t *testing.T) {
actual := buildEndpoint(
eNamespace("testing"),
eName("service3"),
eUID("3"),
subset(
eAddresses(eAddress("10.15.0.1")),
ePorts(
ePort(8080, "http"),
ePort(8443, "https"),
),
),
subset(
eAddresses(eAddress("10.15.0.2")),
ePorts(
ePort(9080, "http"),
ePort(9443, "https"),
),
),
)
assert.EqualValues(t, sampleEndpoint1(), actual)
}
2018-02-14 08:56:04 +00:00
func sampleEndpoint1() *corev1.Endpoints {
return &corev1.Endpoints{
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
Subsets: []corev1.EndpointSubset{
2017-12-05 19:24:03 +00:00
{
2018-02-14 08:56:04 +00:00
Addresses: []corev1.EndpointAddress{
2017-12-05 19:24:03 +00:00
{
IP: "10.15.0.1",
},
},
2018-02-14 08:56:04 +00:00
Ports: []corev1.EndpointPort{
2017-12-05 19:24:03 +00:00
{
Name: "http",
Port: 8080,
},
{
Name: "https",
Port: 8443,
},
},
},
{
2018-02-14 08:56:04 +00:00
Addresses: []corev1.EndpointAddress{
2017-12-05 19:24:03 +00:00
{
IP: "10.15.0.2",
},
},
2018-02-14 08:56:04 +00:00
Ports: []corev1.EndpointPort{
2017-12-05 19:24:03 +00:00
{
Name: "http",
Port: 9080,
},
{
Name: "https",
Port: 9443,
},
},
},
},
}
}