2017-02-10 03:00:17 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
2017-02-12 23:13:39 +00:00
|
|
|
package sessionaffinity
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-04-01 14:39:42 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2017-11-23 16:46:23 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2017-02-10 03:00:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildIngress() *extensions.Ingress {
|
|
|
|
defaultBackend := extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &extensions.Ingress{
|
2017-04-01 14:39:42 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
2017-02-10 03:00:17 +00:00
|
|
|
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-02-12 23:13:39 +00:00
|
|
|
func TestIngressAffinityCookieConfig(t *testing.T) {
|
2017-02-10 03:00:17 +00:00
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
data := map[string]string{}
|
2017-11-23 16:46:23 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityType)] = "cookie"
|
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieHash)] = "sha123"
|
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieName)] = "INGRESSCOOKIE"
|
2018-11-02 09:05:38 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieExpires)] = "4500"
|
2018-10-29 07:21:10 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieMaxAge)] = "3000"
|
2018-11-19 14:15:24 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(annotationAffinityCookiePath)] = "/foo"
|
2017-02-10 03:00:17 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2017-11-08 20:58:57 +00:00
|
|
|
affin, _ := NewParser(&resolver.Mock{}).Parse(ing)
|
2017-11-07 16:36:51 +00:00
|
|
|
nginxAffinity, ok := affin.(*Config)
|
2017-02-10 03:00:17 +00:00
|
|
|
if !ok {
|
2017-02-12 23:13:39 +00:00
|
|
|
t.Errorf("expected a Config type")
|
2017-02-10 03:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
if nginxAffinity.Type != "cookie" {
|
2018-11-19 14:15:24 +00:00
|
|
|
t.Errorf("expected cookie as affinity but returned %v", nginxAffinity.Type)
|
2017-02-10 03:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
if nginxAffinity.Cookie.Hash != "md5" {
|
2018-11-19 14:15:24 +00:00
|
|
|
t.Errorf("expected md5 as session-cookie-hash but returned %v", nginxAffinity.Cookie.Hash)
|
2017-02-10 03:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
if nginxAffinity.Cookie.Name != "INGRESSCOOKIE" {
|
2018-11-19 14:15:24 +00:00
|
|
|
t.Errorf("expected route as session-cookie-name but returned %v", nginxAffinity.Cookie.Name)
|
2017-02-10 03:00:17 +00:00
|
|
|
}
|
2018-10-29 07:21:10 +00:00
|
|
|
|
2018-11-02 09:05:38 +00:00
|
|
|
if nginxAffinity.Cookie.Expires != "4500" {
|
2018-11-19 14:15:24 +00:00
|
|
|
t.Errorf("expected 1h as session-cookie-expires but returned %v", nginxAffinity.Cookie.Expires)
|
2018-10-29 07:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nginxAffinity.Cookie.MaxAge != "3000" {
|
2018-11-19 14:15:24 +00:00
|
|
|
t.Errorf("expected 3000 as session-cookie-max-age but returned %v", nginxAffinity.Cookie.MaxAge)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nginxAffinity.Cookie.Path != "/foo" {
|
|
|
|
t.Errorf("expected /foo as session-cookie-path but returned %v", nginxAffinity.Cookie.Path)
|
2018-10-29 07:21:10 +00:00
|
|
|
}
|
2017-02-10 03:00:17 +00:00
|
|
|
}
|