]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Test] Added test for Redis API
authorMikhail Galanin <mgalanin@mimecast.com>
Mon, 10 Sep 2018 14:19:16 +0000 (15:19 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Mon, 10 Sep 2018 14:19:16 +0000 (15:19 +0100)
.luacheckrc
test/functional/cases/151_rspamadm_async.robot
test/functional/cases/240_redis.robot [new file with mode: 0644]
test/functional/cases/241_redis_is_dead.robot [new file with mode: 0644]
test/functional/configs/redis.conf [new file with mode: 0644]
test/functional/lib/rspamd.robot
test/functional/lua/redis.lua [new file with mode: 0644]
test/functional/lua/rspamadm/test_redis_client.lua [new file with mode: 0644]
test/functional/lua/tcp.lua

index a5819a46ba2509eeb0d976e23980cc4eebc1e8d4..9af168474c3059f04bf7bfe7b78efb9831a5fb6f 100644 (file)
@@ -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',
index 5a848d274c04c44ef45599b3bb786d09f0a7983b..31fa842459a5bd278e2a8a615a3b35f50ab4b934 100644 (file)
@@ -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 (file)
index 0000000..70798a9
--- /dev/null
@@ -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 (file)
index 0000000..e23a241
--- /dev/null
@@ -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 (file)
index 0000000..2da10fa
--- /dev/null
@@ -0,0 +1,5 @@
+lua = ${LUA_SCRIPT};
+redis {
+  servers = "${REDIS_ADDR}:${REDIS_PORT}";
+  expand_keys = true;
+}
index 27cdf97fbd99e6f378b93d54402b4dc395573a80..3dca630d78d430c08dcd337916d90f708efcf749 100644 (file)
@@ -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 (file)
index 0000000..841b207
--- /dev/null
@@ -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 (file)
index 0000000..7de82cd
--- /dev/null
@@ -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
index 21bb0c6e7291d708f7b9f5e0d128f001e00eae65..30285c4f5954d470e82da07bedb5aa5adbedd0d7 100644 (file)
@@ -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')