diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-10-05 14:29:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-05 14:29:59 +0100 |
commit | 14c84068b6dc269435cd76373b794e1a906b3e47 (patch) | |
tree | 1c2a55b42af3425ca279e2cf04efa7b80f5f03cb | |
parent | 315ecdf30dfefa0316b608cc37d867795499419e (diff) | |
parent | ee9041edc60ce2ef35760d70f9de5816bab2760a (diff) | |
download | rspamd-14c84068b6dc269435cd76373b794e1a906b3e47.tar.gz rspamd-14c84068b6dc269435cd76373b794e1a906b3e47.zip |
Merge pull request #2570 from negram/lua-coverage-collector
Lua coverage collector
-rw-r--r-- | .luacheckrc | 4 | ||||
-rw-r--r-- | test/functional/configs/clickhouse.conf | 1 | ||||
-rw-r--r-- | test/functional/configs/lua_test.conf | 2 | ||||
-rw-r--r-- | test/functional/configs/plugins.conf | 1 | ||||
-rw-r--r-- | test/functional/lua/test_coverage.lua | 46 |
5 files changed, 53 insertions, 1 deletions
diff --git a/.luacheckrc b/.luacheckrc index 9b899c216..39b820b70 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -63,3 +63,7 @@ files['/**/src/rspamadm/*'].globals = { 'ansicolors', 'getopt', } + +files['test/functional/lua/test_coverage.lua'].globals = { + '__GLOBAL_COVERAGE_WATCHDOG' +} diff --git a/test/functional/configs/clickhouse.conf b/test/functional/configs/clickhouse.conf index 8f20b4dba..08a3614a5 100644 --- a/test/functional/configs/clickhouse.conf +++ b/test/functional/configs/clickhouse.conf @@ -56,6 +56,7 @@ worker { secure_ip = ["127.0.0.1", "::1"]; stats_path = "${TMPDIR}/stats.ucl" } +lua = "${TESTDIR}/lua/test_coverage.lua"; modules { path = "${TESTDIR}/../../src/plugins/lua/" } diff --git a/test/functional/configs/lua_test.conf b/test/functional/configs/lua_test.conf index 949fd00f2..67e1e8103 100644 --- a/test/functional/configs/lua_test.conf +++ b/test/functional/configs/lua_test.conf @@ -47,5 +47,5 @@ worker { secure_ip = ["127.0.0.1", "::1"]; stats_path = "${TMPDIR}/stats.ucl" } - +lua = "${TESTDIR}/lua/test_coverage.lua"; lua = ${LUA_SCRIPT}; diff --git a/test/functional/configs/plugins.conf b/test/functional/configs/plugins.conf index 6d4fff803..ecff3eabf 100644 --- a/test/functional/configs/plugins.conf +++ b/test/functional/configs/plugins.conf @@ -507,5 +507,6 @@ worker { modules { path = "${TESTDIR}/../../src/plugins/lua/" } +lua = "${TESTDIR}/lua/test_coverage.lua"; lua = "${INSTALLROOT}/share/rspamd/rules/rspamd.lua" ${PLUGIN_CONFIG} diff --git a/test/functional/lua/test_coverage.lua b/test/functional/lua/test_coverage.lua new file mode 100644 index 000000000..68f2545ce --- /dev/null +++ b/test/functional/lua/test_coverage.lua @@ -0,0 +1,46 @@ +--[[ +-- This should be the very first file executed during a test +-- otherwise coverage will be partly missed +--]] +local logger = require "rspamd_logger" +local mempool = require "rspamd_mempool" +local loaded, luacov = pcall(require, 'luacov.runner') +if not loaded then + logger.errx('luacov is not loaded, will not collect coverage') + return +end + +luacov.init() + +local pool = mempool.create() +-- we don't need the pool, we need userdata to put __gc() on it +-- __gc() is not called for tables, that't why there is such trick +-- so, we are free to clean memory, let's do this :) +pool:destroy() + +local woker_name + +rspamd_config:add_on_load(function(cfg, ev_base, worker) + woker_name = worker:get_name() + local stats_path = rspamd_paths["DBDIR"] .. '/' .. woker_name .. '.luacov.stats.out' + local config = luacov.load_config() + config.statsfile = stats_path +end) + +-- use global variable to prevent the object from being GC'ed too early +__GLOBAL_COVERAGE_WATCHDOG = {pool = pool} + +local mt = { + __gc = function() + --[[ + -- We could've used finish_script but in that case some coverage would be missed: + -- pool destructors are executed after finish_scripts (when Lua state is terminated and that's + -- how we can collect coverage of cove executed there + --]] + if woker_name then + luacov.shutdown() + end + end +} + +debug.setmetatable(__GLOBAL_COVERAGE_WATCHDOG.pool, mt) |