add e2e test for cookie annotations

This commit is contained in:
liuwei 2018-10-30 19:27:21 +08:00
parent 7de718f359
commit 38279366a5
3 changed files with 24 additions and 35 deletions

View file

@ -74,7 +74,7 @@ local function set_cookie(self, value)
}
local expires
if self.cookie_expires then
if self.cookie_expires and self.cookie_expires ~= "" then
expires, err = parse_cookie_expires(self.cookie_expires)
if err then
ngx.log(ngx.WARN, string.format("error when parsing cookie expires: %s, ignoring it", tostring(err)))
@ -83,7 +83,7 @@ local function set_cookie(self, value)
end
end
if self.cookie_max_age then
if self.cookie_max_age and self.cookie_max_age ~= "" then
cookie_data.max_age = tonumber(self.cookie_max_age)
end

View file

@ -88,38 +88,6 @@ describe("Sticky", function()
assert.equal(sticky_balancer_instance.digest_func, default_hash_implementation)
end)
end)
context("when backend specifies cookie expires", function()
it("returns an instance containing the corresponding cookie expires", function()
local temp_backend = util.deepcopy(test_backend)
temp_backend.sessionAffinityConfig.cookieSessionAffinity.expires = "1y"
local sticky_balancer_instance = sticky:new(temp_backend)
assert.equal(sticky_balancer_instance.cookie_expires, "1y")
end)
end)
context("when backend does not specify cookie expires", function()
it("returns an instance without cookie expires", function()
local sticky_balancer_instance = sticky:new(test_backend)
assert.equal(sticky_balancer_instance.cookie_expires, nil)
end)
end)
context("when backend specifies cookie max-age", function()
it("returns an instance containing the corresponding cookie max-age", function()
local temp_backend = util.deepcopy(test_backend)
temp_backend.sessionAffinityConfig.cookieSessionAffinity.maxage = 1000
local sticky_balancer_instance = sticky:new(temp_backend)
assert.equal(sticky_balancer_instance.cookie_max_age, 1000)
end)
end)
context("when backend does not specify cookie max-age", function()
it("returns an instance without cookie max-age", function()
local sticky_balancer_instance = sticky:new(test_backend)
assert.equal(sticky_balancer_instance.cookie_max_age, nil)
end)
end)
end)
describe("balance()", function()

View file

@ -156,11 +156,32 @@ var _ = framework.IngressNginxDescribe("Dynamic Configuration", func() {
Expect(nginxConfig).ShouldNot(Equal(newNginxConfig))
})
It("should set sticky cookie correctly", func() {
resp, _, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", "foo.com").
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
//fmt.Println("--------->")
//fmt.Println(resp.Header.Get("Set-Cookie"))
//fmt.Println("--------->")
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID="))
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Expires="))
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Max-Age=172800"))
})
})
func ensureIngress(f *framework.Framework, host string) *extensions.Ingress {
ing, err := f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "http-svc", 80,
&map[string]string{"nginx.ingress.kubernetes.io/load-balance": "ewma"}))
&map[string]string{
"nginx.ingress.kubernetes.io/load-balance": "ewma",
"nginx.ingress.kubernetes.io/affinity": "cookie",
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
"nginx.ingress.kubernetes.io/session-cookie-expires": "2d",
"nginx.ingress.kubernetes.io/session-cookie-max-age": "172800"}))
Expect(err).NotTo(HaveOccurred())
Expect(ing).NotTo(BeNil())
err = f.WaitForNginxServer(host,