traefik/old/provider/kubernetes/builder_ingress_test.go

224 lines
5.5 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
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
2017-12-05 19:24:03 +00:00
)
2018-02-14 08:56:04 +00:00
func buildIngress(opts ...func(*extensionsv1beta1.Ingress)) *extensionsv1beta1.Ingress {
i := &extensionsv1beta1.Ingress{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(i)
}
return i
}
2018-02-14 08:56:04 +00:00
func iNamespace(value string) func(*extensionsv1beta1.Ingress) {
return func(i *extensionsv1beta1.Ingress) {
2017-12-05 19:24:03 +00:00
i.Namespace = value
}
}
2018-02-14 08:56:04 +00:00
func iAnnotation(name string, value string) func(*extensionsv1beta1.Ingress) {
return func(i *extensionsv1beta1.Ingress) {
2017-12-05 19:24:03 +00:00
if i.Annotations == nil {
i.Annotations = make(map[string]string)
}
i.Annotations[name] = value
}
}
2018-02-14 08:56:04 +00:00
func iRules(opts ...func(*extensionsv1beta1.IngressSpec)) func(*extensionsv1beta1.Ingress) {
return func(i *extensionsv1beta1.Ingress) {
s := &extensionsv1beta1.IngressSpec{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(s)
}
i.Spec = *s
}
}
2018-07-03 16:58:03 +00:00
func iSpecBackends(opts ...func(*extensionsv1beta1.IngressSpec)) func(*extensionsv1beta1.Ingress) {
return func(i *extensionsv1beta1.Ingress) {
s := &extensionsv1beta1.IngressSpec{}
for _, opt := range opts {
opt(s)
}
i.Spec = *s
}
}
func iSpecBackend(opts ...func(*extensionsv1beta1.IngressBackend)) func(*extensionsv1beta1.IngressSpec) {
return func(s *extensionsv1beta1.IngressSpec) {
p := &extensionsv1beta1.IngressBackend{}
for _, opt := range opts {
opt(p)
}
s.Backend = p
}
}
func iIngressBackend(name string, port intstr.IntOrString) func(*extensionsv1beta1.IngressBackend) {
return func(p *extensionsv1beta1.IngressBackend) {
p.ServiceName = name
p.ServicePort = port
}
}
2018-02-14 08:56:04 +00:00
func iRule(opts ...func(*extensionsv1beta1.IngressRule)) func(*extensionsv1beta1.IngressSpec) {
return func(spec *extensionsv1beta1.IngressSpec) {
r := &extensionsv1beta1.IngressRule{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(r)
}
spec.Rules = append(spec.Rules, *r)
}
}
2018-02-14 08:56:04 +00:00
func iHost(name string) func(*extensionsv1beta1.IngressRule) {
return func(rule *extensionsv1beta1.IngressRule) {
2017-12-05 19:24:03 +00:00
rule.Host = name
}
}
2018-02-14 08:56:04 +00:00
func iPaths(opts ...func(*extensionsv1beta1.HTTPIngressRuleValue)) func(*extensionsv1beta1.IngressRule) {
return func(rule *extensionsv1beta1.IngressRule) {
rule.HTTP = &extensionsv1beta1.HTTPIngressRuleValue{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(rule.HTTP)
}
}
}
2018-02-14 08:56:04 +00:00
func onePath(opts ...func(*extensionsv1beta1.HTTPIngressPath)) func(*extensionsv1beta1.HTTPIngressRuleValue) {
return func(irv *extensionsv1beta1.HTTPIngressRuleValue) {
p := &extensionsv1beta1.HTTPIngressPath{}
2017-12-05 19:24:03 +00:00
for _, opt := range opts {
opt(p)
}
irv.Paths = append(irv.Paths, *p)
}
}
2018-02-14 08:56:04 +00:00
func iPath(name string) func(*extensionsv1beta1.HTTPIngressPath) {
return func(p *extensionsv1beta1.HTTPIngressPath) {
2017-12-05 19:24:03 +00:00
p.Path = name
}
}
2018-02-14 08:56:04 +00:00
func iBackend(name string, port intstr.IntOrString) func(*extensionsv1beta1.HTTPIngressPath) {
return func(p *extensionsv1beta1.HTTPIngressPath) {
p.Backend = extensionsv1beta1.IngressBackend{
2017-12-05 19:24:03 +00:00
ServiceName: name,
ServicePort: port,
}
}
}
2018-02-14 08:56:04 +00:00
func iTLSes(opts ...func(*extensionsv1beta1.IngressTLS)) func(*extensionsv1beta1.Ingress) {
return func(i *extensionsv1beta1.Ingress) {
for _, opt := range opts {
2018-02-14 08:56:04 +00:00
iTLS := extensionsv1beta1.IngressTLS{}
opt(&iTLS)
i.Spec.TLS = append(i.Spec.TLS, iTLS)
}
}
}
2018-02-14 08:56:04 +00:00
func iTLS(secret string, hosts ...string) func(*extensionsv1beta1.IngressTLS) {
return func(i *extensionsv1beta1.IngressTLS) {
i.SecretName = secret
i.Hosts = hosts
}
}
2017-12-05 19:24:03 +00:00
// Test
func TestBuildIngress(t *testing.T) {
i := buildIngress(
iNamespace("testing"),
iRules(
iRule(iHost("foo"), iPaths(
onePath(iPath("/bar"), iBackend("service1", intstr.FromInt(80))),
onePath(iPath("/namedthing"), iBackend("service4", intstr.FromString("https")))),
),
iRule(iHost("bar"), iPaths(
onePath(iBackend("service3", intstr.FromString("https"))),
onePath(iBackend("service2", intstr.FromInt(802))),
),
),
),
iTLSes(
iTLS("tls-secret", "foo"),
),
)
2017-12-05 19:24:03 +00:00
assert.EqualValues(t, sampleIngress(), i)
}
2018-02-14 08:56:04 +00:00
func sampleIngress() *extensionsv1beta1.Ingress {
return &extensionsv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
2017-12-05 19:24:03 +00:00
Namespace: "testing",
},
2018-02-14 08:56:04 +00:00
Spec: extensionsv1beta1.IngressSpec{
Rules: []extensionsv1beta1.IngressRule{
2017-12-05 19:24:03 +00:00
{
Host: "foo",
2018-02-14 08:56:04 +00:00
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
2017-12-05 19:24:03 +00:00
{
Path: "/bar",
2018-02-14 08:56:04 +00:00
Backend: extensionsv1beta1.IngressBackend{
2017-12-05 19:24:03 +00:00
ServiceName: "service1",
ServicePort: intstr.FromInt(80),
},
},
{
Path: "/namedthing",
2018-02-14 08:56:04 +00:00
Backend: extensionsv1beta1.IngressBackend{
2017-12-05 19:24:03 +00:00
ServiceName: "service4",
ServicePort: intstr.FromString("https"),
},
},
},
},
},
},
{
Host: "bar",
2018-02-14 08:56:04 +00:00
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
2017-12-05 19:24:03 +00:00
{
2018-02-14 08:56:04 +00:00
Backend: extensionsv1beta1.IngressBackend{
2017-12-05 19:24:03 +00:00
ServiceName: "service3",
ServicePort: intstr.FromString("https"),
},
},
{
2018-02-14 08:56:04 +00:00
Backend: extensionsv1beta1.IngressBackend{
2017-12-05 19:24:03 +00:00
ServiceName: "service2",
ServicePort: intstr.FromInt(802),
},
},
},
},
},
},
},
2018-02-14 08:56:04 +00:00
TLS: []extensionsv1beta1.IngressTLS{
{
Hosts: []string{"foo"},
SecretName: "tls-secret",
},
},
2017-12-05 19:24:03 +00:00
},
}
}