From 89e83602136cb2909d1717174c2bb3834897bda7 Mon Sep 17 00:00:00 2001 From: Canh Ngo Date: Wed, 22 Nov 2017 00:00:19 +0100 Subject: [PATCH] Added e2e test for sticky cookie and redirection --- test/e2e/annotations/affinity.go | 140 +++++++++++++++++++++++++++++++ test/e2e/framework/echo.go | 10 ++- 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 test/e2e/annotations/affinity.go diff --git a/test/e2e/annotations/affinity.go b/test/e2e/annotations/affinity.go new file mode 100644 index 000000000..823c4074b --- /dev/null +++ b/test/e2e/annotations/affinity.go @@ -0,0 +1,140 @@ +/* +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 annotations + +import ( + "fmt" + "net/http" + "strings" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/parnurzeal/gorequest" + + v1beta1 "k8s.io/api/extensions/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + + "k8s.io/ingress-nginx/test/e2e/framework" +) + +var _ = framework.IngressNginxDescribe("Annotations - Affinity", func() { + f := framework.NewDefaultFramework("affinity") + + BeforeEach(func() { + err := f.NewEchoDeploymentWithReplicas(1) + Expect(err).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + }) + + It("should set sticky cookie SERVERID", func() { + host := "sticky.foo.com" + + bi := buildIngress(host, f.Namespace.Name) + bi.Annotations["ingress.kubernetes.io/affinity"] = "cookie" + bi.Annotations["ingress.kubernetes.io/session-cookie-name"] = "SERVERID" + + ing, err := f.EnsureIngress(bi) + Expect(err).NotTo(HaveOccurred()) + Expect(ing).NotTo(BeNil()) + + stickyUpstreamName := "sticky-"+f.Namespace.Name+"-http-svc-80" + upstreamPrefix := fmt.Sprintf(` + upstream %v { + sticky hash=md5 name=SERVERID httponly;`, stickyUpstreamName) + err = f.WaitForNginxServer(host, + func(cfg string) bool { + return strings.Contains(cfg, "proxy_pass http://"+stickyUpstreamName+";") && + strings.Contains(cfg, upstreamPrefix) + }) + Expect(err).NotTo(HaveOccurred()) + + resp, _, errs := gorequest.New(). + Get(f.NginxHTTPURL). + Set("Host", host). + End() + + Expect(len(errs)).Should(BeNumerically("==", 0)) + Expect(resp.StatusCode).Should(Equal(http.StatusOK)) + Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID=")) + }) + + XIt("should redirect to '/something' with enabled affinity", func() { + host := "example.com" + + ing, err := f.EnsureIngress(&v1beta1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: host, + Namespace: f.Namespace.Name, + Annotations: map[string]string{ + "ingress.kubernetes.io/affinity": "cookie", + "ingress.kubernetes.io/session-cookie-name": "SERVERID", + "ingress.kubernetes.io/rewrite-target": "/something", + }, + }, + Spec: v1beta1.IngressSpec{ + Rules: []v1beta1.IngressRule{ + { + Host: host, + IngressRuleValue: v1beta1.IngressRuleValue{ + HTTP: &v1beta1.HTTPIngressRuleValue{ + Paths: []v1beta1.HTTPIngressPath{ + { + Path: "/", + Backend: v1beta1.IngressBackend{ + ServiceName: "http-svc", + ServicePort: intstr.FromInt(8080), + }, + }, + }, + }, + }, + }, + }, + }, + }) + + Expect(err).NotTo(HaveOccurred()) + Expect(ing).NotTo(BeNil()) + + stickyUpstreamName := "sticky-"+f.Namespace.Name+"-http-svc-8080;" + proxyPass := fmt.Sprintf(` + rewrite /(.*) /something/$1 break; + proxy_pass http://%v;`,stickyUpstreamName) + upstreamPrefix := fmt.Sprintf(` + upstream %v { + sticky hash=sha1 name=SERVERID httponly;`, stickyUpstreamName) + err = f.WaitForNginxServer(host, + func(cfg string) bool { + return strings.Contains(cfg, proxyPass) && + strings.Contains(cfg, upstreamPrefix) + }) + Expect(err).NotTo(HaveOccurred()) + + resp, body, errs := gorequest.New(). + Get(f.NginxHTTPURL). + Set("Host", host). + End() + + Expect(len(errs)).Should(BeNumerically("==", 0)) + Expect(resp.StatusCode).Should(Equal(http.StatusOK)) + Expect(body).Should(ContainSubstring("real path=/something")) + Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID=")) + }) +}) diff --git a/test/e2e/framework/echo.go b/test/e2e/framework/echo.go index 1868bd4d2..ef3e2d0e0 100644 --- a/test/e2e/framework/echo.go +++ b/test/e2e/framework/echo.go @@ -29,15 +29,21 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) -// NewEchoDeployment creates a new deployment of the echoserver image in a particular namespace +// NewEchoDeployment creates a new single replica deployment of the echoserver image in a particular namespace func (f *Framework) NewEchoDeployment() error { + return f.NewEchoDeploymentWithReplicas(1) +} + +// NewEchoDeploymentWithReplicas creates a new deployment of the echoserver image in a particular namespace. Number of +// replicas is configurable +func (f *Framework) NewEchoDeploymentWithReplicas(replicas int32) error { deployment := &extensions.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "http-svc", Namespace: f.Namespace.Name, }, Spec: extensions.DeploymentSpec{ - Replicas: NewInt32(1), + Replicas: NewInt32(replicas), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "app": "http-svc",