From a77f6d5515077c0b6912ce957d7c53bf0c526a58 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Mon, 10 Sep 2018 15:19:16 +0100 Subject: [PATCH] [Test] Added test for Redis API --- .luacheckrc | 4 + .../functional/cases/151_rspamadm_async.robot | 16 +++- test/functional/cases/240_redis.robot | 40 +++++++++ test/functional/cases/241_redis_is_dead.robot | 39 ++++++++ test/functional/configs/redis.conf | 5 ++ test/functional/lib/rspamd.robot | 15 +++- test/functional/lua/redis.lua | 88 +++++++++++++++++++ .../lua/rspamadm/test_redis_client.lua | 40 +++++++++ test/functional/lua/tcp.lua | 7 +- 9 files changed, 247 insertions(+), 7 deletions(-) create mode 100644 test/functional/cases/240_redis.robot create mode 100644 test/functional/cases/241_redis_is_dead.robot create mode 100644 test/functional/configs/redis.conf create mode 100644 test/functional/lua/redis.lua create mode 100644 test/functional/lua/rspamadm/test_redis_client.lua diff --git a/.luacheckrc b/.luacheckrc index a5819a46b..9af168474 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -52,6 +52,10 @@ files['/**/lualib/lua_util.lua'].globals = { 'unpack', } +files['/**/lualib/lua_redis.lua'].globals = { + 'rspamadm_ev_base', +} + files['/**/src/rspamadm/*'].globals = { 'ansicolors', 'getopt', diff --git a/test/functional/cases/151_rspamadm_async.robot b/test/functional/cases/151_rspamadm_async.robot index 5a848d274..31fa84245 100644 --- a/test/functional/cases/151_rspamadm_async.robot +++ b/test/functional/cases/151_rspamadm_async.robot @@ -1,6 +1,6 @@ *** Settings *** -Test Setup Http Setup -Test Teardown Http Teardown +Test Setup Rspamadm test Setup +Test Teardown Rspamadm test Teardown Library Process Library ${TESTDIR}/lib/rspamd.py Resource ${TESTDIR}/lib/rspamd.robot @@ -18,15 +18,23 @@ Tcp client Should Be Equal As Integers ${result.rc} 0 Should Be Equal ${result.stdout} hello post +Redis client + ${result} = Run Process ${RSPAMADM} lua -b ${TESTDIR}/lua/rspamadm/test_redis_client.lua + Should Match Regexp ${result.stderr} ^$ + Should Be Equal As Integers ${result.rc} 0 + Should Be Equal ${result.stdout} true\thello from lua on redis + *** Keywords *** -Http Setup +Rspamadm test Setup Run Dummy Http + Run Redis -Http Teardown +Rspamadm test Teardown ${http_pid} = Get File /tmp/dummy_http.pid Shutdown Process With Children ${http_pid} Remove file /tmp/dummy_http.pid + Shutdown Process With Children ${REDIS_PID} Run Dummy Http [Arguments] diff --git a/test/functional/cases/240_redis.robot b/test/functional/cases/240_redis.robot new file mode 100644 index 000000000..70798a96b --- /dev/null +++ b/test/functional/cases/240_redis.robot @@ -0,0 +1,40 @@ +*** Settings *** +Test Setup Redis Setup +Test Teardown Redis Teardown +Library Process +Library ${TESTDIR}/lib/rspamd.py +Resource ${TESTDIR}/lib/rspamd.robot +Variables ${TESTDIR}/lib/vars.py +Suite Teardown Terminate All Processes kill=True + + +*** Variables *** +${REDIS_SCOPE} Test +${RSPAMD_SCOPE} Test +${CONFIG} ${TESTDIR}/configs/plugins.conf +${URL_TLD} ${TESTDIR}/../lua/unit/test_tld.dat +${MESSAGE} ${TESTDIR}/messages/spam_message.eml + + +*** Test Cases *** +Redis client + Redis SET test_key test value + ${result} = Scan Message With Rspamc ${MESSAGE} + Check Rspamc ${result} REDIS (0.00)[hello from lua on redis] + Check Rspamc ${result} REDIS_ASYNC (0.00)[test value] + +*** Keywords *** +Lua Setup + [Arguments] ${LUA_SCRIPT} + Set Global Variable ${LUA_SCRIPT} + ${PLUGIN_CONFIG} = Get File ${TESTDIR}/configs/redis.conf + Set Suite Variable ${PLUGIN_CONFIG} + Generic Setup PLUGIN_CONFIG + +Redis Setup + Lua Setup ${TESTDIR}/lua/redis.lua + Run Redis + +Redis Teardown + Normal Teardown + Shutdown Process With Children ${REDIS_PID} diff --git a/test/functional/cases/241_redis_is_dead.robot b/test/functional/cases/241_redis_is_dead.robot new file mode 100644 index 000000000..e23a24120 --- /dev/null +++ b/test/functional/cases/241_redis_is_dead.robot @@ -0,0 +1,39 @@ +*** Settings *** +Documentation Test the case when trying to connect to nowhere +... (i.e. redis is not running) +Test Setup Redis Setup +Test Teardown Redis Teardown +Library Process +Library ${TESTDIR}/lib/rspamd.py +Resource ${TESTDIR}/lib/rspamd.robot +Variables ${TESTDIR}/lib/vars.py +Suite Teardown Terminate All Processes kill=True + + +*** Variables *** +${REDIS_SCOPE} Test +${RSPAMD_SCOPE} Test +${CONFIG} ${TESTDIR}/configs/plugins.conf +${URL_TLD} ${TESTDIR}/../lua/unit/test_tld.dat +${MESSAGE} ${TESTDIR}/messages/spam_message.eml + + +*** Test Cases *** +Dead Redis client + ${result} = Scan Message With Rspamc ${MESSAGE} + Check Rspamc ${result} REDIS_ERROR_3 (0.00)[Connection refused] + +*** Keywords *** +Lua Setup + [Arguments] ${LUA_SCRIPT} + Set Global Variable ${LUA_SCRIPT} + ${PLUGIN_CONFIG} = Get File ${TESTDIR}/configs/redis.conf + Set Suite Variable ${PLUGIN_CONFIG} + Generic Setup PLUGIN_CONFIG + +Redis Setup + Lua Setup ${TESTDIR}/lua/redis.lua + +Redis Teardown + Normal Teardown +# Shutdown Process With Children ${REDIS_PID} diff --git a/test/functional/configs/redis.conf b/test/functional/configs/redis.conf new file mode 100644 index 000000000..2da10faf7 --- /dev/null +++ b/test/functional/configs/redis.conf @@ -0,0 +1,5 @@ +lua = ${LUA_SCRIPT}; +redis { + servers = "${REDIS_ADDR}:${REDIS_PORT}"; + expand_keys = true; +} diff --git a/test/functional/lib/rspamd.robot b/test/functional/lib/rspamd.robot index 27cdf97fb..3dca630d7 100644 --- a/test/functional/lib/rspamd.robot +++ b/test/functional/lib/rspamd.robot @@ -70,11 +70,11 @@ Generic Teardown [Arguments] @{ports} Run Keyword If '${CONTROLLER_ERRORS}' == 'True' Check Controller Errors Shutdown Process With Children ${RSPAMD_PID} - Log do not contain segfault record + Log does not contain segfault record Save Run Results ${TMPDIR} rspamd.log redis.log rspamd.conf clickhouse-server.log clickhouse-server.err.log clickhouse-config.xml Cleanup Temporary Directory ${TMPDIR} -Log do not contain segfault record +Log does not contain segfault record ${log} = Get File ${TMPDIR}/rspamd.log Should not contain ${log} Segmentation fault: msg=Segmentation fault detected @@ -98,7 +98,18 @@ Redis HSET Log ${result.stdout} Should Be Equal As Integers ${result.rc} 0 +Redis SET + [Arguments] ${key} ${value} + ${result} = Run Process redis-cli -h ${REDIS_ADDR} -p ${REDIS_PORT} + ... SET ${key} ${value} + Run Keyword If ${result.rc} != 0 Log ${result.stderr} + Log ${result.stdout} + Should Be Equal As Integers ${result.rc} 0 + Run Redis + ${has_TMPDIR} = Evaluate 'TMPDIR' + ${tmpdir} = Run Keyword If '${has_TMPDIR}' == 'True' Set Variable &{kwargs}[TMPDIR] + ... ELSE Make Temporary Directory ${template} = Get File ${TESTDIR}/configs/redis-server.conf ${config} = Replace Variables ${template} Create File ${TMPDIR}/redis-server.conf ${config} diff --git a/test/functional/lua/redis.lua b/test/functional/lua/redis.lua new file mode 100644 index 000000000..841b20787 --- /dev/null +++ b/test/functional/lua/redis.lua @@ -0,0 +1,88 @@ +--[[[ +-- Just a test for Redis API +--]] + +local logger = require "rspamd_logger" +local redis_lua = require "lua_redis" +local lua_util = require "lua_util" + +local redis_params +local N = 'redis_test' + +local lua_script = [[ +local f = function() end +return "hello from lua on redis" +]] + +local function redis_simple_async_symbol(task) + local function redis_cb(err, data) + if err then + task:insert_result('REDIS_ASYNC_ERROR', 1.0, err) + else + task:insert_result('REDIS_ASYNC', 1.0, data) + end + end + + redis_lua.rspamd_redis_make_request( + task, + redis_params, + "test_key", + false, + redis_cb, + 'GET', + {'test_key'} + ) +end + +local function redis_symbol(task) + + local params = lua_util.deepcopy(redis_params) + params.task = task + local is_ok, connection = redis_lua.redis_connect_sync(params) + + logger.infox(task, "connect: %1, %2", is_ok, connection) + + if not is_ok then + task:insert_result('REDIS_ERROR', 1.0, connection) + return + end + + local err, data + + is_ok, err = connection:add_cmd('EVAL', {lua_script, 0}) + logger.infox(task, "add_cmd: %1, %2", is_ok, err) + + if not is_ok then + task:insert_result('REDIS_ERROR_2', 1.0, err) + return + end + + is_ok,data = connection:exec() + + logger.infox(task, "exec: %1, %2", is_ok, data) + + if not is_ok then + task:insert_result('REDIS_ERROR_3', 1.0, data) + return + end + + task:insert_result('REDIS', 1.0, data) + +end + +redis_params = rspamd_parse_redis_server(N) + +rspamd_config:register_symbol({ + name = 'SIMPLE_REDIS_ASYNC_TEST', + score = 1.0, + callback = redis_simple_async_symbol, + no_squeeze = true +}) + +rspamd_config:register_symbol({ + name = 'REDIS_TEST', + score = 1.0, + callback = redis_symbol, + no_squeeze = true +}) +-- ]] diff --git a/test/functional/lua/rspamadm/test_redis_client.lua b/test/functional/lua/rspamadm/test_redis_client.lua new file mode 100644 index 000000000..7de82cd96 --- /dev/null +++ b/test/functional/lua/rspamadm/test_redis_client.lua @@ -0,0 +1,40 @@ +local logger = require "rspamd_logger" +local redis = require "lua_redis" +local upstream_list = require "rspamd_upstream_list" + +local upstreams_write = upstream_list.create('127.0.0.1', 56379) +local upstreams_read = upstream_list.create('127.0.0.1', 56379) + +local is_ok, connection = redis.redis_connect_sync({ + write_servers = upstreams_write, + read_servers = upstreams_read, + config = rspamd_config, + ev_base = rspamadm_ev_base, + session = rspamadm_session, + timeout = 2 +}) + + +local lua_script = [[ +local f = function() end +--for k = 1,100000000 do +-- for i=1,100000000 do +-- f() +-- end +--end +return "hello from lua on redis" +]] + +local a,b = connection:add_cmd('EVAL', {lua_script, 0}) +local is_ok,ver = connection:exec() + +print(is_ok, ver) + +--[[ +a,b = connection:add_cmd('EVAL', {lua_script, 0}) +print(a,b) + +is_ok,ver = connection:exec() + +print(is_ok, ver) +]] \ No newline at end of file diff --git a/test/functional/lua/tcp.lua b/test/functional/lua/tcp.lua index 21bb0c6e7..30285c4f5 100644 --- a/test/functional/lua/tcp.lua +++ b/test/functional/lua/tcp.lua @@ -20,7 +20,7 @@ local function http_simple_tcp_async_symbol(task) local function http_read_cb(err, data, conn) logger.errx(task, 'http_read_cb: got reply: %s, error: %s, conn: %s', data, err, conn) conn:add_write(http_read_post_cb, "POST /request2 HTTP/1.1\r\n\r\n") - task:insert_result('HTTP_ASYNC_RESPONSE', 1.0, data) + task:insert_result('HTTP_ASYNC_RESPONSE', 1.0, data or err) end rspamd_tcp:request({ task = task, @@ -43,6 +43,11 @@ local function http_simple_tcp_symbol(task) port = 18080, } + if not is_ok then + task:insert_result('HTTP_SYNC_WRITE_ERROR', 1.0, connection) + logger.errx(task, 'write error: %1', connection) + end + logger.errx(task, 'connect_sync %1, %2', is_ok, tostring(connection)) is_ok, err = connection:write('GET /request_sync HTTP/1.1\r\nConnection: keep-alive\r\n\r\n') -- 2.39.5