]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Test] Added more test for rspamadm and test for lua tcp client invoked via rspamadm
authorMikhail Galanin <mgalanin@mimecast.com>
Wed, 5 Sep 2018 12:43:21 +0000 (13:43 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Wed, 5 Sep 2018 12:43:21 +0000 (13:43 +0100)
.luacheckrc
test/functional/cases/150_rspamadm.robot
test/functional/cases/151_rspamadm_async.robot [new file with mode: 0644]
test/functional/lib/rspamd.py
test/functional/lua/rspamadm/test_batch.lua [new file with mode: 0644]
test/functional/lua/rspamadm/test_message_callback.lua [new file with mode: 0644]
test/functional/lua/rspamadm/test_tcp_client.lua [new file with mode: 0644]

index 324c45985931043aeeaaa598a435408dc9c28fb7..a5819a46ba2509eeb0d976e23980cc4eebc1e8d4 100644 (file)
@@ -5,6 +5,7 @@ exclude_files = {
   '/**/contrib/**',
   '/**/test/lua/**',
   '/**/test/functional/lua/miltertest/**',
+  '/**/test/functional/lua/rspamadm/**',
 }
 
 globals = {
index 4e7b3c8aa525485d4f5343cbac8562e568f13f3a..82532ddab0df48563b55519f251b08f66b7cce90 100644 (file)
@@ -1,5 +1,8 @@
 *** Settings ***
 Library         Process
+Library         ../lib/rspamd.py
+
+Suite Teardown    Terminate All Processes    kill=True
 
 *** Test Cases ***
 Config Test
@@ -12,3 +15,26 @@ Config Help
   ${result} =  Run Process  ${RSPAMADM}  confighelp
   Should Match Regexp  ${result.stderr}  ^$
   Should Be Equal As Integers  ${result.rc}  0
+
+Simple interpreter
+  ${handle} =  Start Process  ${RSPAMADM}  lua
+  ${result} =  Write to stdin  ${handle}  1+1
+  Should Be Equal As Strings  ${result}  2\n
+
+Simple interpreter, two results
+  ${handle} =  Start Process  ${RSPAMADM}  lua
+  ${result} =  Write to stdin  ${handle}  1+1, 2 * 5
+  Should Be Equal  ${result}  2\n10\n
+
+Process message callback
+  ${handle} =  Start Process  ${RSPAMADM}  lua
+  ${result} =  Write to stdin  ${handle}  .load ${TESTDIR}/lua/rspamadm/test_message_callback.lua\n.message message_callback ${TESTDIR}/messages/empty_part.eml
+  Should Contain  ${result}  n parts = 2
+  Should Contain  ${result}  1\n2\n4\n6
+
+Lua batch mode
+  ${result} =  Run Process  ${RSPAMADM}  lua  -b  ${TESTDIR}/lua/rspamadm/test_batch.lua
+  Should Match Regexp  ${result.stderr}  ^$
+  Should Be Equal As Integers  ${result.rc}  0
+  Should Be Equal  ${result.stdout}  hello world
+
diff --git a/test/functional/cases/151_rspamadm_async.robot b/test/functional/cases/151_rspamadm_async.robot
new file mode 100644 (file)
index 0000000..ec97292
--- /dev/null
@@ -0,0 +1,36 @@
+*** Settings ***
+Test Setup      Http Setup
+Test Teardown   Http 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
+
+
+*** Test Cases ***
+Tcp client
+  ${result} =  Run Process  ${RSPAMADM}  lua  -b  ${TESTDIR}/lua/rspamadm/test_tcp_client.lua
+  Should Match Regexp  ${result.stderr}  ^$
+  Should Be Equal As Integers  ${result.rc}  0
+  Should Be Equal  ${result.stdout}  hello post
+
+*** Keywords ***
+
+Http Setup
+  Run Dummy Http
+  Run Redis
+
+Http 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]
+  ${result} =  Start Process  ${TESTDIR}/util/dummy_http.py
+  Wait Until Created  /tmp/dummy_http.pid
index 97ab516c155392195af7152b0b3e6a1677c4a4a5..1c4b428a27d9f011b060ff713d57915f60f46434 100644 (file)
@@ -212,3 +212,12 @@ def shutdown_process_with_children(pid):
         except:
             pass
 
+
+def write_to_stdin(process_handle, text):
+    lib = BuiltIn().get_library_instance('Process')
+    obj = lib.get_process_object()
+    obj.stdin.write(text + "\n")
+    obj.stdin.flush()
+    obj.stdin.close()
+    out = obj.stdout.read(4096)
+    return out
diff --git a/test/functional/lua/rspamadm/test_batch.lua b/test/functional/lua/rspamadm/test_batch.lua
new file mode 100644 (file)
index 0000000..e75154b
--- /dev/null
@@ -0,0 +1 @@
+print("hello world")
\ No newline at end of file
diff --git a/test/functional/lua/rspamadm/test_message_callback.lua b/test/functional/lua/rspamadm/test_message_callback.lua
new file mode 100644 (file)
index 0000000..6be512a
--- /dev/null
@@ -0,0 +1,5 @@
+function message_callback(task)
+  local parts = task:get_text_parts()
+  print("n parts = " .. tostring(#parts))
+  return 1,2,4,6
+end
diff --git a/test/functional/lua/rspamadm/test_tcp_client.lua b/test/functional/lua/rspamadm/test_tcp_client.lua
new file mode 100644 (file)
index 0000000..796fe91
--- /dev/null
@@ -0,0 +1,60 @@
+local logger = require "rspamd_logger"
+local tcp_sync = require "lua_tcp_sync"
+
+local is_ok, connection = tcp_sync.connect {
+  config = rspamd_config,
+  ev_base = rspamadm_ev_base,
+  session = rspamadm_session,
+  host = '127.0.0.1',
+  timeout = 20,
+  port = 18080,
+}
+local err
+is_ok, err = connection:write(string.format('POST /request HTTP/1.1\r\nConnection: close\r\n\r\n'))
+
+logger.info('write %1, %2', is_ok, err)
+if not is_ok then
+  logger.errx(rspamd_config, 'write error: %1', err)
+  return
+end
+
+local content_length, content
+
+while true do
+  local header_line
+  is_ok, header_line = connection:read_until("\r\n")
+  if not is_ok then
+    logger.errx(rspamd_config, 'failed to get header: %1', header_line)
+    return
+  end
+
+  if header_line == "" then
+    logger.info('headers done')
+    break
+  end
+
+  local value
+  local header = header_line:gsub("([%w-]+): (.*)", 
+      function (h, v) value = v; return h:lower() end)
+
+  logger.info('parsed header: %1 -> "%2"', header, value)
+
+  if header == "content-length" then
+    content_length = tonumber(value)
+  end
+
+end
+
+if content_length then
+  is_ok, content = connection:read_bytes(content_length)
+  if is_ok then
+  end
+else
+  is_ok, content = connection:read_until_eof()
+  if is_ok then
+  end
+end
+logger.info('(is_ok: %1) content [%2 bytes] %3', is_ok, content_length, content)
+
+
+print(content)