From 042490e8dc102635c02d2caeb195bddf83f58054 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 13 Aug 2024 15:07:31 +0100 Subject: [Test] Add unit tests for ucl functions --- test/lua/unit/ucl.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/lua/unit/ucl.lua (limited to 'test/lua/unit') diff --git a/test/lua/unit/ucl.lua b/test/lua/unit/ucl.lua new file mode 100644 index 000000000..991afc979 --- /dev/null +++ b/test/lua/unit/ucl.lua @@ -0,0 +1,60 @@ +-- Test some UCL stuff + +context("UCL manipulation", function() + local ucl = require "ucl" + + test("UCL transparent test", function() + local parser = ucl.parser() + local res, err = parser:parse_string('{"key":"val"}') + assert(res) + + local reply = parser:get_object_wrapped() + + assert_equal(tostring(reply), '{"key":"val"}') + assert_equal(reply:type(), 'object') + assert_equal(reply:at('key'):unwrap(), 'val') + reply.ololo = 'ohlol' + reply.ololo = 'ohlol' + reply.key = 'ohlol' + assert_equal(reply:at('key'):unwrap(), 'ohlol') + local expected = { + key = 'ohlol', + ololo = 'ohlol' + } + for k, v in reply:pairs() do + assert_equal(expected[k], v:unwrap()) + end + + parser = ucl.parser() + res, err = parser:parse_string('["e1","e2"]') + assert(res) + local ireply = parser:get_object_wrapped() + + assert_equal(tostring(ireply), '["e1","e2"]') + assert_equal(ireply:type(), 'array') + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[#ireply + 1] = 100500 + local iexpected = { 1, 1, 1, 1, 1, "e1", "e2", 100500 } + for k, v in ireply:ipairs() do + assert_equal(iexpected[k], v:unwrap()) + end + + reply.tbl = ireply + expected.tbl = iexpected + for k, v in reply:pairs() do + if type(expected[k]) == 'table' then + for kk, vv in v:ipairs() do + assert_equal(expected[k][kk], vv:unwrap()) + end + else + assert_equal(expected[k], v:unwrap()) + end + end + + collectgarbage() -- To ensure we don't crash with asan + end) +end) \ No newline at end of file -- cgit v1.2.3 From e33a823ac8d8242fb606b3887c2eab2c3c99c768 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 15 Aug 2024 17:37:46 +0100 Subject: [Minor] Slight cleanup --- lualib/lua_cfg_transform.lua | 32 +++++++++++++++++++++++--------- test/lua/unit/ucl.lua | 6 +++--- 2 files changed, 26 insertions(+), 12 deletions(-) (limited to 'test/lua/unit') diff --git a/lualib/lua_cfg_transform.lua b/lualib/lua_cfg_transform.lua index 5b1ab2e1c..b928cd88a 100644 --- a/lualib/lua_cfg_transform.lua +++ b/lualib/lua_cfg_transform.lua @@ -99,10 +99,12 @@ local function convert_metric(cfg, metric) end if metric:at('actions') then - cfg.actions = lua_util.override_defaults(cfg:at('actions'):unwrap(), metric:at('actions'):unwrap()) + local existing_actions = cfg:at('actions') and cfg:at('actions'):unwrap() or {} + cfg.actions = lua_util.override_defaults(existing_actions, metric:at('actions'):unwrap()) logger.infox("overriding actions from the legacy metric settings") end if metric:at('unknown_weight') then + logger.infox("overriding unknown weight from the legacy metric settings") cfg:at('actions').unknown_weight = metric:at('unknown_weight'):unwrap() end @@ -143,9 +145,19 @@ return function(cfg) local ret = false if cfg:at('metric') then - for _, v in cfg:at('metric'):pairs() do - if v:type() == 'object' then - convert_metric(cfg, v) + local metric = cfg:at('metric') + + -- There are two things that we can have (old `metric_pairs` logic) + -- 1. A metric is a single metric definition like: metric { name = "default", ... } + -- 2. A metric is a list of metrics like: metric { "default": ... } + if metric:at('actions') or metric:at('name') then + convert_metric(cfg, metric) + else + for _, v in cfg:at('metric'):pairs() do + if v:type() == 'object' then + logger.infox('converting metric element %s', v) + convert_metric(cfg, v) + end end end ret = true @@ -169,8 +181,8 @@ return function(cfg) 'reject', 'discard' } local actions = cfg:at('actions') - if actions and (not actions:at('no action') and not actions:at('no_action') and - not actions:at('accept')) then + if not actions:at('no action') and not actions:at('no_action') and + not actions:at('accept') then for _, d in ipairs(actions_defs) do if actions:at(d) then @@ -263,9 +275,11 @@ return function(cfg) -- If neural network is enabled we MUST have `check_all_filters` flag if cfg:at('neural') then - if not cfg:at('options'):at('check_all_filters') then - logger.infox(rspamd_config, 'enable `options.check_all_filters` for neural network') - cfg:at('options')['check_all_filters'] = true + if cfg:at('options') then + if not cfg:at('options'):at('check_all_filters') then + logger.infox(rspamd_config, 'enable `options.check_all_filters` for neural network') + cfg:at('options')['check_all_filters'] = true + end end end diff --git a/test/lua/unit/ucl.lua b/test/lua/unit/ucl.lua index 991afc979..9bbf7225c 100644 --- a/test/lua/unit/ucl.lua +++ b/test/lua/unit/ucl.lua @@ -37,10 +37,10 @@ context("UCL manipulation", function() ireply[1] = 1 ireply[1] = 1 ireply[1] = 1 - ireply[#ireply + 1] = 100500 - local iexpected = { 1, 1, 1, 1, 1, "e1", "e2", 100500 } + ireply[ireply:len() + 1] = 100500 + local iexpected = { 1, "e2", 100500 } for k, v in ireply:ipairs() do - assert_equal(iexpected[k], v:unwrap()) + assert_equal(v:unwrap(), iexpected[k]) end reply.tbl = ireply -- cgit v1.2.3 From 089746e704a325bf429dcb1e7caa554253c47063 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 16 Aug 2024 13:25:41 +0100 Subject: [Test] Add more unit tests --- test/lua/unit/ucl.lua | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'test/lua/unit') diff --git a/test/lua/unit/ucl.lua b/test/lua/unit/ucl.lua index 9bbf7225c..1b975d390 100644 --- a/test/lua/unit/ucl.lua +++ b/test/lua/unit/ucl.lua @@ -3,13 +3,17 @@ context("UCL manipulation", function() local ucl = require "ucl" - test("UCL transparent test", function() - local parser = ucl.parser() - local res, err = parser:parse_string('{"key":"val"}') - assert(res) + local parser = ucl.parser() + local res, err = parser:parse_string('{"key":"val"}') + assert(res) - local reply = parser:get_object_wrapped() + local reply = parser:get_object_wrapped() + local expected = { + key = 'ohlol', + ololo = 'ohlol' + } + test("UCL transparent test: object", function() assert_equal(tostring(reply), '{"key":"val"}') assert_equal(reply:type(), 'object') assert_equal(reply:at('key'):unwrap(), 'val') @@ -17,14 +21,13 @@ context("UCL manipulation", function() reply.ololo = 'ohlol' reply.key = 'ohlol' assert_equal(reply:at('key'):unwrap(), 'ohlol') - local expected = { - key = 'ohlol', - ololo = 'ohlol' - } + for k, v in reply:pairs() do assert_equal(expected[k], v:unwrap()) end + end) + test("UCL transparent test: array", function() parser = ucl.parser() res, err = parser:parse_string('["e1","e2"]') assert(res) @@ -42,7 +45,9 @@ context("UCL manipulation", function() for k, v in ireply:ipairs() do assert_equal(v:unwrap(), iexpected[k]) end + end) + test("UCL transparent test: concat", function() reply.tbl = ireply expected.tbl = iexpected for k, v in reply:pairs() do @@ -54,7 +59,17 @@ context("UCL manipulation", function() assert_equal(expected[k], v:unwrap()) end end + end) - collectgarbage() -- To ensure we don't crash with asan + test("UCL transparent test: implicit conversion array->object", function() + -- Assign empty table, so it'll be an array + reply.t = {} + assert_equal(reply.t:type(), 'array') + -- We can convert empty table to object + reply.t.test = 'test' + assert_equal(reply.t:type(), 'object') + assert_equal(reply.t.test:unwrap(), 'test') end) + + collectgarbage() -- To ensure we don't crash with asan end) \ No newline at end of file -- cgit v1.2.3