diff --git a/build/busted b/build/busted index 4c5299de2..a24dd4cae 100755 --- a/build/busted +++ b/build/busted @@ -1,3 +1,16 @@ #!/usr/bin/env resty +local ffi = require("ffi") + +-- without this we get errors such as "attempt to redefine XXX" +local old_cdef = ffi.cdef +local exists = {} +ffi.cdef = function(def) + if exists[def] then + return + end + exists[def] = true + return old_cdef(def) +end + require "busted.runner"({ standalone = false }) diff --git a/build/test-lua.sh b/build/test-lua.sh index 2786daa17..5a581a8f1 100755 --- a/build/test-lua.sh +++ b/build/test-lua.sh @@ -18,7 +18,11 @@ set -o errexit set -o nounset set -o pipefail -export LUA_PATH="/usr/local/lib/lua/?.lua;;" -export LUA_CPATH="/usr/local/lib/lua/?.so;/usr/lib/lua-platform-path/lua/5.1/?.so;;" - -resty ./build/busted ${BUSTED_ARGS} ./rootfs/etc/nginx/lua/test; +resty \ + -I ./rootfs/etc/nginx/lua \ + -I /usr/local/lib/lua \ + -I /usr/lib/lua-platform-path/lua/5.1 \ + --shdict "configuration_data 5M" \ + --shdict "balancer_ewma 1M" \ + --shdict "balancer_ewma_last_touched_at 1M" \ + ./build/busted ${BUSTED_ARGS} ./rootfs/etc/nginx/lua/test/ diff --git a/rootfs/etc/nginx/lua/test/balancer/chash_test.lua b/rootfs/etc/nginx/lua/test/balancer/chash_test.lua index a03060454..533565f53 100644 --- a/rootfs/etc/nginx/lua/test/balancer/chash_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer/chash_test.lua @@ -1,5 +1,3 @@ -package.path = "./rootfs/etc/nginx/lua/?.lua;./rootfs/etc/nginx/lua/test/mocks/?.lua;" .. package.path - describe("Balancer chash", function() local balancer_chash = require("balancer.chash") diff --git a/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua b/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua index 7107ad8c2..7919a6f44 100644 --- a/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua @@ -1,17 +1,5 @@ -package.path = "./rootfs/etc/nginx/lua/?.lua;./rootfs/etc/nginx/lua/test/mocks/?.lua;" .. package.path - local util = require("util") -local _ngx = { - shared = { - balancer_ewma = { flush_all = function() end }, - balancer_ewma_last_touched_at = { flush_all = function() end } - }, - log = function(...) end, - now = function() return os.time() end, -} -_G.ngx = _ngx - describe("Balancer ewma", function() local balancer_ewma = require("balancer.ewma") diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index 04e97d98a..51dbc9c74 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -1,12 +1,5 @@ -package.path = "./rootfs/etc/nginx/lua/?.lua;./rootfs/etc/nginx/lua/test/mocks/?.lua;" .. package.path _G._TEST = true -local _ngx = { - shared = {}, - log = function(...) end, -} -_G.ngx = _ngx - local balancer, expected_implementations, backends local function reset_balancer() @@ -92,10 +85,10 @@ describe("Balancer", function() local s_old = spy.on(implementation, "new") local s = spy.on(new_implementation, "new") - local s_ngx_log = spy.on(_G.ngx, "log") + local s_ngx_log = spy.on(ngx, "log") assert.has_no.errors(function() balancer.sync_backend(backend) end) - assert.spy(s_ngx_log).was_called_with(ngx.ERR, + assert.spy(s_ngx_log).was_called_with(ngx.INFO, "LB algorithm changed from round_robin to ewma, resetting the instance") -- TODO(elvinefendi) figure out why -- assert.spy(s).was_called_with(new_implementation, backend) does not work here diff --git a/rootfs/etc/nginx/lua/test/defer_test.lua b/rootfs/etc/nginx/lua/test/defer_test.lua index 6f4f94cc3..459a7c8bf 100644 --- a/rootfs/etc/nginx/lua/test/defer_test.lua +++ b/rootfs/etc/nginx/lua/test/defer_test.lua @@ -1,14 +1,6 @@ -package.path = "./rootfs/etc/nginx/lua/?.lua;./rootfs/etc/nginx/lua/test/mocks/?.lua;" .. package.path _G._TEST = true -local defer = require('util.defer') +local defer = require("util.defer") -local _ngx = { - shared = {}, - log = function(...) end, - get_phase = function() return "timer" end, -} -_G.ngx = _ngx - describe("Defer", function() describe("to_timer_phase", function() it("executes passed callback immediately if called on timer phase", function() diff --git a/rootfs/etc/nginx/lua/test/mocks/ngx/balancer.lua b/rootfs/etc/nginx/lua/test/mocks/ngx/balancer.lua deleted file mode 100644 index a56470754..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/ngx/balancer.lua +++ /dev/null @@ -1 +0,0 @@ -return {} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/chash.lua b/rootfs/etc/nginx/lua/test/mocks/resty/chash.lua deleted file mode 100644 index 3a1ab6ae4..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/chash.lua +++ /dev/null @@ -1,3 +0,0 @@ -return { - new = function(self, nodes) return {} end -} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/cookie.lua b/rootfs/etc/nginx/lua/test/mocks/resty/cookie.lua deleted file mode 100644 index a56470754..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/cookie.lua +++ /dev/null @@ -1 +0,0 @@ -return {} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/lock.lua b/rootfs/etc/nginx/lua/test/mocks/resty/lock.lua deleted file mode 100644 index 1285e486e..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/lock.lua +++ /dev/null @@ -1,3 +0,0 @@ -return { - new = function(self, name, ...) return {} end, -} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/md5.lua b/rootfs/etc/nginx/lua/test/mocks/resty/md5.lua deleted file mode 100644 index a56470754..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/md5.lua +++ /dev/null @@ -1 +0,0 @@ -return {} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/roundrobin.lua b/rootfs/etc/nginx/lua/test/mocks/resty/roundrobin.lua deleted file mode 100644 index 3a1ab6ae4..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/roundrobin.lua +++ /dev/null @@ -1,3 +0,0 @@ -return { - new = function(self, nodes) return {} end -} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/sha1.lua b/rootfs/etc/nginx/lua/test/mocks/resty/sha1.lua deleted file mode 100644 index a56470754..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/sha1.lua +++ /dev/null @@ -1 +0,0 @@ -return {} diff --git a/rootfs/etc/nginx/lua/test/mocks/resty/string.lua b/rootfs/etc/nginx/lua/test/mocks/resty/string.lua deleted file mode 100644 index a56470754..000000000 --- a/rootfs/etc/nginx/lua/test/mocks/resty/string.lua +++ /dev/null @@ -1 +0,0 @@ -return {} diff --git a/rootfs/etc/nginx/lua/test/monitor_test.lua b/rootfs/etc/nginx/lua/test/monitor_test.lua index d89bcef48..b67c449a2 100644 --- a/rootfs/etc/nginx/lua/test/monitor_test.lua +++ b/rootfs/etc/nginx/lua/test/monitor_test.lua @@ -1,25 +1,5 @@ -package.path = "./rootfs/etc/nginx/lua/?.lua;./rootfs/etc/nginx/lua/test/mocks/?.lua;" .. package.path _G._TEST = true -local cjson = require('cjson') - -local function udp_mock() - return { - setpeername = function(...) return true end, - send = function(payload) return payload end, - close = function(...) return true end - } -end - -local _ngx = { - shared = {}, - log = function(...) end, - socket = { - udp = udp_mock - }, - get_phase = function() return "timer" end, - var = {} -} -_G.ngx = _ngx +local cjson = require("cjson") describe("Monitor", function() local monitor = require("monitor")