119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
![]() |
/*
|
||
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||
|
|
||
|
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 rewrite
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"k8s.io/kubernetes/pkg/api"
|
||
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||
|
"k8s.io/kubernetes/pkg/util/intstr"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
defRoute = "/demo"
|
||
|
)
|
||
|
|
||
|
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,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestAnnotations(t *testing.T) {
|
||
|
ing := buildIngress()
|
||
|
|
||
|
r := ingAnnotations(ing.GetAnnotations()).rewrite()
|
||
|
if r != "" {
|
||
|
t.Error("Expected no redirect")
|
||
|
}
|
||
|
|
||
|
f := ingAnnotations(ing.GetAnnotations()).fixUrls()
|
||
|
if f != false {
|
||
|
t.Error("Expected false as fix-urls but %v was returend", f)
|
||
|
}
|
||
|
|
||
|
data := map[string]string{}
|
||
|
data[rewrite] = defRoute
|
||
|
data[fixUrls] = "true"
|
||
|
ing.SetAnnotations(data)
|
||
|
|
||
|
r = ingAnnotations(ing.GetAnnotations()).rewrite()
|
||
|
if r != defRoute {
|
||
|
t.Error("Expected %v as rewrite but %v was returend", defRoute, r)
|
||
|
}
|
||
|
|
||
|
f = ingAnnotations(ing.GetAnnotations()).fixUrls()
|
||
|
if f != true {
|
||
|
t.Error("Expected true as fix-urls but %v was returend", f)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestWithoutAnnotations(t *testing.T) {
|
||
|
ing := buildIngress()
|
||
|
_, err := ParseAnnotations(ing)
|
||
|
if err == nil {
|
||
|
t.Error("Expected error with ingress without annotations")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRedirect(t *testing.T) {
|
||
|
ing := buildIngress()
|
||
|
|
||
|
data := map[string]string{}
|
||
|
data[rewrite] = defRoute
|
||
|
ing.SetAnnotations(data)
|
||
|
|
||
|
redirect, err := ParseAnnotations(ing)
|
||
|
if err != nil {
|
||
|
t.Errorf("Uxpected error with ingress: %v", err)
|
||
|
}
|
||
|
|
||
|
if redirect.To != defRoute {
|
||
|
t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.To)
|
||
|
}
|
||
|
}
|