2016-12-29 20:02:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
|
|
|
|
|
|
|
"k8s.io/ingress/core/pkg/ingress/defaults"
|
2017-01-10 12:16:18 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/resolver"
|
2016-12-29 20:02:06 +00:00
|
|
|
)
|
|
|
|
|
2017-01-25 02:17:45 +00:00
|
|
|
const (
|
2017-02-02 11:11:19 +00:00
|
|
|
annotationSecureUpstream = "ingress.kubernetes.io/secure-backends"
|
|
|
|
annotationUpsMaxFails = "ingress.kubernetes.io/upstream-max-fails"
|
|
|
|
annotationUpsFailTimeout = "ingress.kubernetes.io/upstream-fail-timeout"
|
|
|
|
annotationPassthrough = "ingress.kubernetes.io/ssl-passthrough"
|
2017-02-10 03:00:17 +00:00
|
|
|
annotationStickyEnabled = "ingress.kubernetes.io/sticky-enabled"
|
|
|
|
annotationStickyName = "ingress.kubernetes.io/sticky-name"
|
|
|
|
annotationStickyHash = "ingress.kubernetes.io/sticky-hash"
|
2017-01-25 02:17:45 +00:00
|
|
|
)
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type mockCfg struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockCfg) GetDefaultBackend() defaults.Backend {
|
|
|
|
return defaults.Backend{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockCfg) GetSecret(string) (*api.Secret, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:16:18 +00:00
|
|
|
func (m mockCfg) GetAuthCertificate(string) (*resolver.AuthSSLCert, error) {
|
2016-12-29 20:02:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnnotationExtractor(t *testing.T) {
|
|
|
|
ec := newAnnotationExtractor(mockCfg{})
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
m := ec.Extract(ing)
|
|
|
|
// the map at least should contains HealthCheck and Proxy information (defaults)
|
|
|
|
if _, ok := m["HealthCheck"]; !ok {
|
|
|
|
t.Error("expected HealthCheck annotation")
|
|
|
|
}
|
|
|
|
if _, ok := m["Proxy"]; !ok {
|
|
|
|
t.Error("expected Proxy annotation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildIngress() *extensions.Ingress {
|
|
|
|
defaultBackend := extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &extensions.Ingress{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: extensions.IngressSpec{
|
|
|
|
Backend: &extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
Rules: []extensions.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo.bar.com",
|
|
|
|
IngressRuleValue: extensions.IngressRuleValue{
|
|
|
|
HTTP: &extensions.HTTPIngressRuleValue{
|
|
|
|
Paths: []extensions.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
Backend: defaultBackend,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 02:17:45 +00:00
|
|
|
|
|
|
|
func TestSecureUpstream(t *testing.T) {
|
|
|
|
ec := newAnnotationExtractor(mockCfg{})
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
fooAnns := []struct {
|
|
|
|
annotations map[string]string
|
|
|
|
er bool
|
|
|
|
}{
|
2017-02-02 11:11:19 +00:00
|
|
|
{map[string]string{annotationSecureUpstream: "true"}, true},
|
|
|
|
{map[string]string{annotationSecureUpstream: "false"}, false},
|
|
|
|
{map[string]string{annotationSecureUpstream + "_no": "true"}, false},
|
2017-01-25 02:17:45 +00:00
|
|
|
{map[string]string{}, false},
|
|
|
|
{nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foo := range fooAnns {
|
|
|
|
ing.SetAnnotations(foo.annotations)
|
|
|
|
r := ec.SecureUpstream(ing)
|
|
|
|
if r != foo.er {
|
|
|
|
t.Errorf("Returned %v but expected %v", r, foo.er)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthCheck(t *testing.T) {
|
|
|
|
ec := newAnnotationExtractor(mockCfg{})
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
fooAnns := []struct {
|
|
|
|
annotations map[string]string
|
|
|
|
eumf int
|
|
|
|
euft int
|
|
|
|
}{
|
2017-02-02 11:11:19 +00:00
|
|
|
{map[string]string{annotationUpsMaxFails: "3", annotationUpsFailTimeout: "10"}, 3, 10},
|
|
|
|
{map[string]string{annotationUpsMaxFails: "3"}, 3, 0},
|
|
|
|
{map[string]string{annotationUpsFailTimeout: "10"}, 0, 10},
|
2017-01-25 02:17:45 +00:00
|
|
|
{map[string]string{}, 0, 0},
|
|
|
|
{nil, 0, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foo := range fooAnns {
|
|
|
|
ing.SetAnnotations(foo.annotations)
|
|
|
|
r := ec.HealthCheck(ing)
|
|
|
|
if r == nil {
|
|
|
|
t.Errorf("Returned nil but expected a healthcheck.Upstream")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FailTimeout != foo.euft {
|
|
|
|
t.Errorf("Returned %d but expected %d for FailTimeout", r.FailTimeout, foo.euft)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.MaxFails != foo.eumf {
|
|
|
|
t.Errorf("Returned %d but expected %d for MaxFails", r.MaxFails, foo.eumf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSLPassthrough(t *testing.T) {
|
|
|
|
ec := newAnnotationExtractor(mockCfg{})
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
fooAnns := []struct {
|
|
|
|
annotations map[string]string
|
|
|
|
er bool
|
|
|
|
}{
|
2017-02-02 11:11:19 +00:00
|
|
|
{map[string]string{annotationPassthrough: "true"}, true},
|
|
|
|
{map[string]string{annotationPassthrough: "false"}, false},
|
|
|
|
{map[string]string{annotationPassthrough + "_no": "true"}, false},
|
2017-01-25 02:17:45 +00:00
|
|
|
{map[string]string{}, false},
|
|
|
|
{nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foo := range fooAnns {
|
|
|
|
ing.SetAnnotations(foo.annotations)
|
|
|
|
r := ec.SSLPassthrough(ing)
|
|
|
|
if r != foo.er {
|
|
|
|
t.Errorf("Returned %v but expected %v", r, foo.er)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
func TestStickySession(t *testing.T) {
|
|
|
|
ec := newAnnotationExtractor(mockCfg{})
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
fooAnns := []struct {
|
|
|
|
annotations map[string]string
|
|
|
|
enabled bool
|
|
|
|
hash string
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "md5", annotationStickyName: "route"}, true, "md5", "route"},
|
|
|
|
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "", annotationStickyName: "xpto"}, true, "md5", "xpto"},
|
|
|
|
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "", annotationStickyName: ""}, true, "md5", "route"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foo := range fooAnns {
|
|
|
|
ing.SetAnnotations(foo.annotations)
|
|
|
|
r := ec.StickySession(ing)
|
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
t.Errorf("Returned nil but expected a StickySesion.StickyConfig")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Hash != foo.hash {
|
|
|
|
t.Errorf("Returned %v but expected %v for Hash", r.Hash, foo.hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Name != foo.name {
|
|
|
|
t.Errorf("Returned %v but expected %v for Name", r.Name, foo.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|