From 7ac4a45422375b989ff21a207ed7f4fac23f8de4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Fri, 17 Aug 2018 15:40:20 +0200 Subject: [PATCH] cache.clear: clearing root clears everything, not only the root zone Problem was caused by our lookup format where only the root zone starts with \0 and all other zones start differently. This caused cache_match('.') to match only data from root zone. --- .luacheckrc | 1 + daemon/cache.test/clear.test.lua | 26 +++++++++++++++++--------- daemon/lua/sandbox.lua | 3 ++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index db1471c44..644f69aa2 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -79,3 +79,4 @@ files['daemon/lua/kres-gen.lua'].ignore = {'631'} -- Allow overly long lines files['scripts'].ignore = {'111', '112', '113'} files['tests'].ignore = {'111', '112', '113'} files['modules/**/*.test.lua'].ignore = {'111', '112', '113', '121', '122'} +files['daemon/**/*.test.lua'].ignore = {'111', '112', '113', '121', '122'} diff --git a/daemon/cache.test/clear.test.lua b/daemon/cache.test/clear.test.lua index ff219d619..9a412d3e9 100644 --- a/daemon/cache.test/clear.test.lua +++ b/daemon/cache.test/clear.test.lua @@ -26,9 +26,9 @@ assert(net.interfaces() ~= nil) -- Self-checks on loaded stuff assert(#modules.list() > 0) -- Self-check timers -ev = event.recurrent(1 * sec, function (ev) return 1 end) +ev = event.recurrent(1 * sec, function () return 1 end) event.cancel(ev) -ev = event.after(0, function (ev) return 1 end) +ev = event.after(0, function () return 1 end) -- import fake root zone @@ -61,8 +61,8 @@ local function import_zone() worker.sleep(0.2) -- zimport is delayed by 100 ms from function call -- sanity checks - cache must be filled in ok(cache.count() > 0, 'cache is not empty after import') - check_answer('root apex is cache', - '.', kres.type.NS, kres.rcode.NOERROR) + check_answer('root apex is in cache', + '.', kres.type.NS, kres.rcode.NOERROR) check_answer('deep subdomain is in cache', 'a.b.subtree1.', kres.type.AAAA, kres.rcode.NOERROR) @@ -120,23 +120,22 @@ end local function test_callback() local test_name = '20r.subtree2.' - local test_maxcount = 1 local test_exactname = true local test_rrtype = nil - local test_maxcount = 1 + local test_chunksize = 1 local test_prev_state = { works = true } local function check_callback(name, exact_name, rr_type, chunk_size, callback, prev_state, errors) is(errors.count, 1, 'callback received correct # of removed records') is(test_name, name, 'callback received subtree name') is(test_exactname, exact_name, 'callback received exact_name') - is(test_rrtype, rrtype, 'callback received rr_type') - is(test_chunksize, chunksize, 'callback received maxcount') + is(test_rrtype, rr_type, 'callback received rr_type') + is(test_chunksize, chunk_size, 'callback received chunk_size') is(check_callback, callback, 'callback received reference to itself') is(type(errors), 'table', 'callback received table of errors') same(test_prev_state, prev_state, 'callback received previous state') return 666 end - same(cache.clear(test_name, test_exactname, test_rrtype, test_maxcount, check_callback, test_prev_state), + same(cache.clear(test_name, test_exactname, test_rrtype, test_chunksize, check_callback, test_prev_state), 666, 'first callback return value is passed to cache.clear() caller') local cnt_before_wait = cache.count() worker.sleep(0.2) @@ -173,6 +172,13 @@ local function test_apex() 'a.b.subtree1.', kres.type.NULL, kres.rcode.SERVFAIL) end +local function test_root() + local prev_count = cache.count() + res = cache.clear('.') + is(res.count, prev_count, 'full clear reports correct number of entries') + is(cache.count(), 0, 'clearing root clears everything') +end + local function test_complete_flush() local prev_count = cache.count() res = cache.clear() @@ -190,5 +196,7 @@ return { test_subtree_limit, test_apex, import_zone, + test_root, + import_zone, test_complete_flush, } diff --git a/daemon/lua/sandbox.lua b/daemon/lua/sandbox.lua index e7990aafa..22db6aac5 100644 --- a/daemon/lua/sandbox.lua +++ b/daemon/lua/sandbox.lua @@ -159,7 +159,8 @@ setmetatable(modules, { cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_state) - if name == nil then -- keep same output format as for 'standard' clear + if name == nil or (name == '.' and not exact_name) then + -- keep same output format as for 'standard' clear local total_count = cache.count() if not cache.clear_everything() then error('unable to clear everything') -- 2.47.2