From ad69ecc7787ffe110f752b129118e9609be21a35 Mon Sep 17 00:00:00 2001 From: Abdullah Barrak Date: Fri, 11 Feb 2022 02:17:00 +0300 Subject: [PATCH] test: cover both fail/recover redis connect cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` $ make busted-util /usr/local/openresty/luajit/bin/rover exec bin/busted "spec/threescale_utils_spec.lua" ●●● 3 successes / 0 failures / 0 errors / 0 pending : 0.022514 seconds ``` --- gateway/src/apicast/threescale_utils.lua | 2 +- spec/threescale_utils_spec.lua | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gateway/src/apicast/threescale_utils.lua b/gateway/src/apicast/threescale_utils.lua index f8f65d70a..268d50870 100644 --- a/gateway/src/apicast/threescale_utils.lua +++ b/gateway/src/apicast/threescale_utils.lua @@ -194,7 +194,7 @@ function _M.error(resilient, ...) else ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR ngx.say(...) - if not resilient then + if not resilient or resilient == nil then ngx.exit(ngx.status) end end diff --git a/spec/threescale_utils_spec.lua b/spec/threescale_utils_spec.lua index b90904c8c..be7c8bb40 100644 --- a/spec/threescale_utils_spec.lua +++ b/spec/threescale_utils_spec.lua @@ -11,13 +11,26 @@ describe('3scale utils', function() assert.equal('one two three', error) end) + it('fails nginx chain when redis cannot be connected', function() + stub(ngx, 'get_phase', function() return 'init' end) + stub(ngx, 'say', function(...) return nil end) + local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end) + + local error = _M.error(false, 'cache issue ' .. 'host:' .. 6379) + + assert.spy(ngx.exit).was_called(1) + assert.spy(ngx.say).was.called_with('cache issue ' .. 'host:' .. 6379) + end) + it('does not terminate with nginx.exit if resilient flag is set', function() - local exit = spy.on(ngx, 'exit', function() return 'exited!' end) - local error = _M.error(true, 'redis is not reachable') + stub(ngx, 'get_phase', function() return 'init' end) + stub(ngx, 'say', function(...) return nil end) - assert.spy(exit).was_not_called() + local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end) + local error = _M.error(true, 'redis is not reachable') - assert.equal('redis is not reachable', error) + assert.spy(ngx.exit).was_not_called() + assert.spy(ngx.say).was.called_with('redis is not reachable') end) end) end)