summaryrefslogtreecommitdiffstats
path: root/test/functional/lib
diff options
context:
space:
mode:
authorAndrew Lewis <nerf@judo.za.org>2016-06-27 21:11:07 +0200
committerAndrew Lewis <nerf@judo.za.org>2016-06-27 23:27:26 +0200
commite8a7a6327950349aa897ce84e507d4fd94f43834 (patch)
tree1185e854673c7e220b56ada9fd4ef714f40bdcaf /test/functional/lib
parent3bb4e1cd86c65a854bcfe4e7d0bb0a1c64347fc7 (diff)
downloadrspamd-e8a7a6327950349aa897ce84e507d4fd94f43834.tar.gz
rspamd-e8a7a6327950349aa897ce84e507d4fd94f43834.zip
[Test] New functional test framework
Diffstat (limited to 'test/functional/lib')
-rw-r--r--test/functional/lib/.gitignore1
-rw-r--r--test/functional/lib/rspamd.py87
-rw-r--r--test/functional/lib/rspamd.robot65
-rw-r--r--test/functional/lib/vars.py7
4 files changed, 160 insertions, 0 deletions
diff --git a/test/functional/lib/.gitignore b/test/functional/lib/.gitignore
new file mode 100644
index 000000000..0d20b6487
--- /dev/null
+++ b/test/functional/lib/.gitignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/test/functional/lib/rspamd.py b/test/functional/lib/rspamd.py
new file mode 100644
index 000000000..998eb6960
--- /dev/null
+++ b/test/functional/lib/rspamd.py
@@ -0,0 +1,87 @@
+import grp
+import os
+import os.path
+import pwd
+import shutil
+import signal
+import socket
+import string
+import tempfile
+import time
+import urllib2
+
+def cleanup_temporary_directory(directory):
+ shutil.rmtree(directory)
+
+def encode_filename(filename):
+ return "".join(['%%%0X' % ord(b) for b in filename])
+
+def get_test_directory():
+ return os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "../..")
+
+def make_temporary_directory():
+ return tempfile.mkdtemp()
+
+def populate_rspamd_config(template_file, temporary_dir, **config):
+ t = string.Template(open(template_file).read())
+ f = open("%s/rspamd.conf" % temporary_dir, "w")
+ f.write(t.safe_substitute(config))
+ f.close()
+
+def process_should_exist(pid):
+ pid = int(pid)
+ os.kill(pid, 0)
+
+def read_log_from_position(filename, offset):
+ offset = long(offset)
+ f = open(filename, 'rb')
+ f.seek(offset)
+ goo = f.read()
+ size = len(goo)
+ return [goo, size+offset]
+
+def scan_file(addr, port, filename):
+ req = urllib2.Request("http://%s:%s/symbols?%s" % (addr, port, filename))
+ response = urllib2.urlopen(req)
+ return response.read()
+
+def Send_SIGUSR1(pid):
+ pid = int(pid)
+ os.kill(pid, signal.SIGUSR1)
+
+def set_directory_ownership(path, username, groupname):
+ uid=pwd.getpwnam(username).pw_uid
+ gid=grp.getgrnam(groupname).gr_gid
+ os.chown(path, uid, gid)
+
+def spamc(addr, port, filename):
+ goo = open(filename, 'rb').read()
+ length = len(goo)
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.connect((addr, port))
+ s.send("SYMBOLS SPAMC/1.0\r\nContent-length: %s\r\n\r\n%s" % (length, goo))
+ s.shutdown(socket.SHUT_WR)
+ r = s.recv(2048)
+ return r
+
+def update_dictionary(a, b):
+ a.update(b)
+ return a
+
+def shutdown_rspamd(pid):
+ pid = int(pid)
+ process_should_exist(pid)
+ i = 0
+ while i < 5:
+ try:
+ os.kill(pid, signal.SIGTERM)
+ time.sleep(0.1)
+ except:
+ break
+ if i >= 5:
+ while True:
+ try:
+ os.kill(pid, signal.SIGTERM)
+ time.sleep(0.1)
+ except:
+ break
diff --git a/test/functional/lib/rspamd.robot b/test/functional/lib/rspamd.robot
new file mode 100644
index 000000000..a5ddca20e
--- /dev/null
+++ b/test/functional/lib/rspamd.robot
@@ -0,0 +1,65 @@
+*** Settings ***
+Library Collections
+Library OperatingSystem
+Library Process
+
+*** Keywords ***
+Export Rspamd Vars To Suite
+ [Arguments] ${TMPDIR} ${RSPAMD_LOGPOS} ${RSPAMD_PID}
+ Set Suite Variable ${TMPDIR}
+ Set Suite Variable ${RSPAMD_LOGPOS}
+ Set Suite Variable ${RSPAMD_PID}
+
+Export Rspamd Vars To Test
+ [Arguments] ${TMPDIR} ${RSPAMD_LOGPOS} ${RSPAMD_PID}
+ Set Test Variable ${TMPDIR}
+ Set Test Variable ${RSPAMD_LOGPOS}
+ Set Test Variable ${RSPAMD_PID}
+
+Follow Rspamd Log
+ ${RSPAMD_LOGPOS} = Log Logs ${TMPDIR}/rspamd.log ${RSPAMD_LOGPOS}
+ Run Keyword If '${RSPAMD_SCOPE}' == 'Test' Set Test Variable ${RSPAMD_LOGPOS}
+ ... ELSE IF '${RSPAMD_SCOPE}' == 'Suite' Set Suite Variable ${RSPAMD_LOGPOS}
+ ... ELSE Fail 'RSPAMD_SCOPE must be Test or Suite'
+
+Generic Setup
+ ${TMPDIR} ${RSPAMD_PID} ${RSPAMD_LOGPOS} = Run Rspamd
+ Run Keyword If '${RSPAMD_SCOPE}' == 'Test' Export Rspamd Vars To Test ${TMPDIR} ${RSPAMD_LOGPOS} ${RSPAMD_PID}
+ ... ELSE IF '${RSPAMD_SCOPE}' == 'Suite' Export Rspamd Vars To Suite ${TMPDIR} ${RSPAMD_LOGPOS} ${RSPAMD_PID}
+ ... ELSE Fail 'RSPAMD_SCOPE must be Test or Suite'
+
+Generic Teardown
+ Shutdown Rspamd ${RSPAMD_PID}
+ Cleanup Temporary Directory ${TMPDIR}
+
+Log Logs
+ [Arguments] ${logfile} ${position}
+ ${the_log} ${position} = Read Log From Position ${logfile} ${position}
+ Log ${the_log}
+ [Return] ${position}
+
+Run Rspamc
+ [Arguments] @{args}
+ ${result} = Run Process ${RSPAMC} @{args}
+ Should Be Equal As Integers ${result.rc} 0
+ [Return] ${result}
+
+Run Rspamd
+ [Arguments] @{args} &{kw}
+ ${tmpdir} = Make Temporary Directory
+ Set Directory Ownership ${tmpdir} ${RSPAMD_USER} ${RSPAMD_GROUP}
+ Set To Dictionary ${RSPAMD_KEYWORDS} TMPDIR=${tmpdir}
+ Update Dictionary ${RSPAMD_KEYWORDS} ${kw}
+ :FOR ${i} IN @{args}
+ \ Set To Dictionary ${RSPAMD_KEYWORDS} ${i} ${tmpdir}
+ Populate Rspamd Config ${CONFIG} ${tmpdir} &{RSPAMD_KEYWORDS}
+ ${result} = Run Process ${RSPAMD} -u ${RSPAMD_USER} -g ${RSPAMD_GROUP} -c ${tmpdir}/rspamd.conf
+ ${rspamd_logpos} = Log Logs ${tmpdir}/rspamd.log 0
+ Should Be Equal As Integers ${result.rc} 0
+ ${rspamd_pid} = Get File ${tmpdir}/rspamd.pid
+ [Return] ${tmpdir} ${rspamd_pid} ${rspamd_logpos}
+
+Scan Message With Rspamc
+ [Arguments] ${msg_file}
+ ${result} = Run Rspamc -p -h ${LOCAL_ADDR}:${PORT_NORMAL} ${msg_file}
+ [Return] ${result}
diff --git a/test/functional/lib/vars.py b/test/functional/lib/vars.py
new file mode 100644
index 000000000..78151bd37
--- /dev/null
+++ b/test/functional/lib/vars.py
@@ -0,0 +1,7 @@
+KEY_PVT1 = 'ekd3x36tfa5gd76t6pa8hqif3ott7n1siuux68exbkk7ukscte9y'
+KEY_PUB1 = 'm8kneubpcjsb8sbsoj7jy7azj9fdd3xmj63txni86a8ye9ncomny'
+LOCAL_ADDR = 'localhost'
+PORT_CONTROLLER = 56790
+PORT_NORMAL = 56789
+RSPAMD_GROUP = 'nogroup'
+RSPAMD_USER = 'nobody'