Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.

Add unit tests for sticky lua module #72

Closed
wants to merge 8 commits into from

Conversation

fmejia97
Copy link

What this PR does / why we need it:
This PR adds unit tests to sticky lua module. We need this because we don't have unit test coverage for this module.

Issue: https://github.com/Shopify/edgescale/issues/435

@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch 2 times, most recently from c05f004 to 47c4376 Compare July 10, 2018 21:18

describe("balance()", function()
setup(function()
util.sha1_digest = function(msg) return msg, false end -- Don't hash for simplification

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment is not adding any value here imo

}
end
local sticky_balancer_instance = sticky:new(test_backend)
local s = spy.on(sticky_balancer_instance.instance, "next")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid this? This test should not know the implementation details of balance function. Why don't you assert on the return value of :balance?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it currently feels a bit hacky. I like the approach you suggested 👍

end
local sticky_balancer_instance = sticky:new(get_test_backend())
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
assert.spy(s).was_called()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also assert on the parameters of cookie? i.e what key, value, path, domain it is setting.

end)
end)

context("when client has a cookie set", function()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing a crucial test here. We should make sure :balance returns the correct endpoint for given cookie value.

@fmejia97
Copy link
Author

@ElvinEfendi Addressed your comments

local sticky_balancer_instance = sticky:new(test_backend)
local ip, port = sticky_balancer_instance:balance()
assert.equal(ip .. ":" .. port, test_backend_endpoint)
end)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't covering the logic properly:

> ingress (add-sticky-module-unit-tests)$ git diff
diff --git a/rootfs/etc/nginx/lua/balancer/sticky.lua b/rootfs/etc/nginx/lua/balancer/sticky.lua
index 8af93a98..348cb94b 100644
--- a/rootfs/etc/nginx/lua/balancer/sticky.lua
+++ b/rootfs/etc/nginx/lua/balancer/sticky.lua
@@ -70,7 +70,8 @@ local function sticky_endpoint_string(self)
     set_cookie(self, key)
   end

-  return self.instance:find(key)
+  return key
+  --return self.instance:find(key)
 end

 function _M.balance(self)
> ingress (add-sticky-module-unit-tests)$ make lua-test
Makefile:160: warning: overriding commands for target `verify-all'
Makefile:156: warning: ignoring old commands for target `verify-all'
●●●●●●●●●●●●●●●●●●●●●
21 successes / 0 failures / 0 errors / 0 pending : 0.02952 seconds


it("should return the correct endpoint for the client", function()
local cookie = require("resty.cookie")
local s = {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this variable is unused

@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch from 06e6cf2 to 4bcc9e6 Compare August 9, 2018 19:03
@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch from 4bcc9e6 to 15cad2a Compare August 9, 2018 19:04
@fmejia97
Copy link
Author

fmejia97 commented Aug 9, 2018

@ElvinEfendi Updated the tests. Removed resty.chash mock since we now have direct access to it.

@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch from 6e77f19 to d9bed56 Compare August 9, 2018 19:16
@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch 2 times, most recently from d411ccd to 5085291 Compare August 21, 2018 18:14
@fmejia97 fmejia97 force-pushed the add-sticky-module-unit-tests branch from 5085291 to a570797 Compare August 21, 2018 18:18
@ElvinEfendi
Copy link

this was completed at kubernetes#2966

@ElvinEfendi ElvinEfendi deleted the add-sticky-module-unit-tests branch October 12, 2018 17:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants