Skip to content

Commit 10ca249

Browse files
committed
Add luatest server
Provide luatest server, which can find tests by luatest pattern *_test.lua and execute them luatest -v <name>_test.lua -o tap --shuffle none. Was changed get_filename_by_test for right working not only with *.test.*, but also with *_test.*. Part of: #304
1 parent b15160f commit 10ca249

File tree

8 files changed

+159
-4
lines changed

8 files changed

+159
-4
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ jobs:
3535
sudo apt update -y
3636
sudo apt-get -y install lua5.1 luarocks
3737
sudo luarocks install luacheck
38+
cd ${{ env.GITHUB_WORKSPACE }}
39+
sudo tarantoolctl rocks install luatest
40+
cd -
3841
- name: set default pip to pip3
3942
run: |
4043
sudo rm -f /usr/local/bin/pip

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ include_files = {
2727
exclude_files = {
2828
"lib/tarantool-python",
2929
"test/test-tarantool/*.test.lua",
30+
".rocks/**"
3031
}

lib/__init__.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from lib.tarantool_server import TarantoolServer
77
from lib.unittest_server import UnittestServer
88
from lib.app_server import AppServer
9+
from lib.luatest_server import LuatestServer
910
from lib.utils import warn_unix_sockets_at_start
1011

11-
1212
__all__ = ['Options']
1313

1414

@@ -56,14 +56,44 @@ def module_init():
5656
os.environ["SOURCEDIR"] = SOURCEDIR
5757
os.environ["BUILDDIR"] = BUILDDIR
5858
soext = sys.platform == 'darwin' and 'dylib' or 'so'
59-
os.environ["LUA_PATH"] = SOURCEDIR+"/?.lua;"+SOURCEDIR+"/?/init.lua;;"
60-
os.environ["LUA_CPATH"] = BUILDDIR+"/?."+soext+";;"
59+
60+
def find_dir(path, dir_name, level=2):
61+
"""Check directory exists at the path or on levels above.
62+
63+
For example,
64+
path = 'foo/bar',
65+
dir_name = 'baz',
66+
level = 2 (default)
67+
Return True if baz exists by foo/bar/baz or foo/baz path.
68+
"""
69+
level -= 1
70+
print(os.path.join(path, dir_name))
71+
if os.path.isdir(os.path.join(path, dir_name)):
72+
return os.path.join(path, dir_name)
73+
if level:
74+
return find_dir(os.path.split(path)[0], dir_name, level)
75+
76+
def is_core_in_suite(core):
77+
"""Check there is core in current tests."""
78+
pass
79+
# return core in [suite.ini["core"] for suite in find_suites()]
80+
81+
ROCKS_DIR = find_dir(SOURCEDIR, '.rocks') or find_dir(BUILDDIR, '.rocks')
82+
if not ROCKS_DIR:
83+
raise Exception('.rocks was not found in source dir and build dir')
84+
os.environ["PATH"] += ":" + ROCKS_DIR
85+
os.environ["LUA_PATH"] = (SOURCEDIR + "/test/?.lua;"
86+
+ SOURCEDIR + "/?.lua;"
87+
+ SOURCEDIR + "/?/init.lua;;")
88+
os.environ['LUATEST_BIN'] = os.path.join(ROCKS_DIR, "bin/luatest")
89+
os.environ["LUA_CPATH"] = BUILDDIR + "/?." + soext + ";;"
6190
os.environ["REPLICATION_SYNC_TIMEOUT"] = str(args.replication_sync_timeout)
6291
os.environ['MEMTX_ALLOCATOR'] = args.memtx_allocator
6392

6493
TarantoolServer.find_exe(args.builddir)
6594
UnittestServer.find_exe(args.builddir)
6695
AppServer.find_exe(args.builddir)
96+
LuatestServer.find_exe(args.builddir)
6797

6898
Options().check_schema_upgrade_option(TarantoolServer.debug)
6999

lib/luatest_server.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import glob
2+
import os
3+
import re
4+
import sys
5+
6+
from subprocess import Popen, PIPE
7+
from subprocess import STDOUT
8+
9+
from lib.sampler import sampler
10+
from lib.server import Server
11+
from lib.tarantool_server import Test
12+
from lib.tarantool_server import TarantoolServer
13+
14+
15+
class LuatestTest(Test):
16+
""" Handle *_test.lua.
17+
18+
Provide method for executing luatest <name>_test.lua test.
19+
"""
20+
21+
def __init__(self, *args, **kwargs):
22+
super(LuatestTest, self).__init__(*args, **kwargs)
23+
self.valgrind = kwargs.get('valgrind', False)
24+
25+
def execute(self, server):
26+
"""Execute test by luatest command
27+
28+
Execute 'luatest -v <name>_test.lua -o tap --shuffle none'
29+
Provide a verbose output in the tap format.
30+
Use shuffle option in none mode for avoiding mixing tests.
31+
Use capture mode.
32+
"""
33+
server.current_test = self
34+
command = [os.environ['LUATEST_BIN'], '-v', '-c', self.name,
35+
'-o', 'tap',
36+
'--shuffle', 'none']
37+
proc = Popen(command, cwd=server.vardir, stdout=PIPE, stderr=STDOUT)
38+
sampler.register_process(proc.pid, self.id, server.name)
39+
sys.stdout.write_bytes(proc.communicate()[0])
40+
41+
42+
class LuatestServer(Server):
43+
"""A dummy server implementation for luatest server tests"""
44+
45+
def __new__(cls, ini=None, *args, **kwargs):
46+
cls = Server.get_mixed_class(cls, ini)
47+
return object.__new__(cls)
48+
49+
def __init__(self, _ini=None, test_suite=None):
50+
if _ini is None:
51+
_ini = {}
52+
ini = {'vardir': None}
53+
ini.update(_ini)
54+
Server.__init__(self, ini, test_suite)
55+
self.testdir = os.path.abspath(os.curdir)
56+
self.vardir = ini['vardir']
57+
self.builddir = ini['builddir']
58+
self.name = 'luatest_server'
59+
60+
@property
61+
def logfile(self):
62+
return self.current_test.tmp_result
63+
64+
@property
65+
def binary(self):
66+
return LuatestServer.prepare_args(self)[0]
67+
68+
def deploy(self, vardir=None, silent=True, wait=True):
69+
self.vardir = vardir
70+
if not os.access(self.vardir, os.F_OK):
71+
os.makedirs(self.vardir)
72+
73+
@classmethod
74+
def find_exe(cls, builddir):
75+
cls.builddir = builddir
76+
cls.binary = TarantoolServer.binary
77+
cls.debug = bool(re.findall(r'-Debug', str(cls.version()),
78+
re.I))
79+
80+
@staticmethod
81+
def find_tests(test_suite, suite_path):
82+
"""Looking for *_test.lua, which are can be executed by luatest."""
83+
84+
def patterned(test, patterns):
85+
answer = []
86+
for i in patterns:
87+
if test.name.find(i) != -1:
88+
answer.append(test)
89+
return answer
90+
91+
test_suite.ini['suite'] = suite_path
92+
tests = glob.glob(os.path.join(suite_path, "*_test.lua"))
93+
94+
tests = Server.exclude_tests(tests, test_suite.args.exclude)
95+
test_suite.tests = [LuatestTest(k, test_suite.args, test_suite.ini)
96+
for k in sorted(tests)]
97+
test_suite.tests = sum([patterned(x, test_suite.args.tests)
98+
for x in test_suite.tests], [])
99+
100+
def print_log(self, lines):
101+
pass

lib/test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ def flush(self):
9494

9595

9696
def get_filename_by_test(postfix, test_name):
97-
rg = re.compile(r'\.test.*')
97+
"""For <..>/<name>_test.* or <..>/<name>.test.* return <name> + postfix
98+
99+
Examples:
100+
postfix='.result', test_name='foo/bar.test.lua' => return 'bar.result'
101+
postfix='.reject', test_name='bar_test.lua' => return 'bar.reject'
102+
"""
103+
rg = re.compile(r'[._]test.*')
98104
return os.path.basename(rg.sub(postfix, test_name))
99105

100106

lib/test_suite.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from lib import Options
1515
from lib.app_server import AppServer
16+
from lib.luatest_server import LuatestServer
1617
from lib.colorer import color_stdout
1718
from lib.inspector import TarantoolInspector
1819
from lib.server import Server
@@ -153,6 +154,8 @@ def collect_tests(self):
153154

154155
if self.ini['core'] == 'tarantool':
155156
TarantoolServer.find_tests(self, self.suite_path)
157+
elif self.ini['core'] == 'luatest':
158+
LuatestServer.find_tests(self, self.suite_path)
156159
elif self.ini['core'] == 'app':
157160
AppServer.find_tests(self, self.suite_path)
158161
elif self.ini['core'] == 'unittest':
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local t = require('luatest')
2+
local g = t.group()
3+
4+
g.test_smoke = function()
5+
t.assert(true)
6+
t.assert_not(false)
7+
end

test/test-luatest/suite.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[default]
2+
core = luatest
3+
description = luatest
4+
disabled =

0 commit comments

Comments
 (0)