Skip to content

Commit

Permalink
Merge branch 'tarantool:master' into bump-luatest-to-add-more-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaplashkin authored Jun 10, 2024
2 parents c99a874 + dd00063 commit 0a477cf
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/luatest
93 changes: 80 additions & 13 deletions lib/luatest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys

from subprocess import PIPE
from subprocess import Popen
from threading import Timer

Expand All @@ -14,6 +15,8 @@
from lib.tarantool_server import Test
from lib.tarantool_server import TestExecutionError
from lib.tarantool_server import TarantoolServer
from lib.utils import bytes_to_str
from lib.utils import find_tags


def timeout_handler(process, test_timeout):
Expand Down Expand Up @@ -54,6 +57,10 @@ def execute(self, server):
unified_logfile_path = os.path.join(server.vardir, 'run.log')
command.extend(['-l', unified_logfile_path])

# Run a specific test case. See find_tests() for details.
if 'test_case' in self.run_params:
command.extend(['--run-test-case', self.run_params['test_case']])

# We start luatest from the project source directory, it
# is the usual way to use luatest.
#
Expand Down Expand Up @@ -139,22 +146,82 @@ def verify_luatest_exe(cls):
# those cases, which are childs of OSError anyway.
raise TestRunInitError('Unable to find luatest executable', e)

@classmethod
def test_cases(cls, test_name):
p = Popen([cls.luatest, test_name, '--list-test-cases'], stdout=PIPE)
output = bytes_to_str(p.stdout.read()).rstrip()
p.wait()

# Exclude the first line if it is a tarantool version
# report.
res = output.split('\n')
if len(res) > 0 and res[0].startswith('Tarantool version is'):
return res[1:]

return res

@staticmethod
def find_tests(test_suite, suite_path):
"""Looking for *_test.lua, which are can be executed by luatest."""

def patterned(test, patterns):
answer = []
for i in patterns:
if test.name.find(i) != -1:
answer.append(test)
return answer

# TODO: Investigate why this old hack is needed and drop
# it if possible (move the assignment to test_suite.py).
#
# cdc70f94701f suggests that it is related to the out of
# source build.
test_suite.ini['suite'] = suite_path
tests = glob.glob(os.path.join(suite_path, '*_test.lua'))

tests = Server.exclude_tests(tests, test_suite.args.exclude)
test_suite.tests = [LuatestTest(k, test_suite.args, test_suite.ini)
for k in sorted(tests)]
test_suite.tests = sum([patterned(x, test_suite.args.tests)
for x in test_suite.tests], [])
# A pattern here means just a substring to find in a test
# name.
include_patterns = Options().args.tests
exclude_patterns = Options().args.exclude

accepted_tags = Options().args.tags

tests = []
for test_name in glob.glob(os.path.join(suite_path, '*_test.lua')):
# If neither of the include patterns are substrings of
# the given test name, skip the test.
if not any(p in test_name for p in include_patterns):
continue

# If at least one of the exclude patterns is a
# substring of the given test name, skip the test.
if any(p in test_name for p in exclude_patterns):
continue

tags = find_tags(test_name)

# If --tags <...> CLI option is provided...
if accepted_tags:
# ...and the test has neither of the given tags,
# skip the test.
if not any(t in accepted_tags for t in tags):
continue

# Add the test to the execution list otherwise.
if 'parallel' in tags:
# If the test has the 'parallel' tag, split the
# test to test cases to run in separate tasks in
# parallel.
test_cases = LuatestServer.test_cases(test_name)

# Display shorter test case names on the screen:
# strip the common prefix.
prefix_len = len(os.path.commonprefix(test_cases))

for test_case in test_cases:
tests.append(LuatestTest(test_name, test_suite.args, test_suite.ini,
params={"test_case": test_case},
conf_name=test_case[prefix_len:]))
else:
# If the test has no 'parallel' tag, run all the
# test cases as one task.
tests.append(LuatestTest(test_name, test_suite.args, test_suite.ini))

tests.sort(key=lambda t: t.name)

# TODO: Don't modify a test suite object's field from
# another object directly. It is much better to just
# return a list of tests from this method.
test_suite.tests = tests
26 changes: 26 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import json
import subprocess
import multiprocessing
from lib.colorer import color_stdout

try:
Expand All @@ -31,6 +32,12 @@
# Python 2.7.
get_terminal_size = None

try:
# Python 3.3+
from os import sched_getaffinity
except ImportError:
sched_getaffinity = None

UNIX_SOCKET_LEN_LIMIT = 107

# Useful for very coarse version differentiation.
Expand Down Expand Up @@ -384,3 +391,22 @@ def terminal_columns():
if get_terminal_size:
return get_terminal_size().columns
return 80


def cpu_count():
"""
Return available CPU count available for the current process.
The result is the same as one from the `nproc` command.
It may be smaller than all the online CPUs count. For example,
an LXD container may have limited available CPUs or it may be
reduced by `taskset` or `numactl` commands.
If it is impossible to determine the available CPUs count (for
example on Python < 3.3), fallback to the all online CPUs
count.
"""
if sched_getaffinity:
return len(sched_getaffinity(0))
return multiprocessing.cpu_count()
3 changes: 2 additions & 1 deletion test-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from lib.colorer import color_stdout
from lib.colorer import separator
from lib.colorer import test_line
from lib.utils import cpu_count
from lib.utils import find_tags
from lib.utils import shlex_quote
from lib.error import TestRunInitError
Expand Down Expand Up @@ -86,7 +87,7 @@ def main_loop_parallel():
jobs = args.jobs
if jobs < 1:
# faster result I got was with 2 * cpu_count
jobs = 2 * multiprocessing.cpu_count()
jobs = 2 * cpu_count()

if jobs > 0:
color_stdout("Running in parallel with %d workers\n\n" % jobs,
Expand Down

0 comments on commit 0a477cf

Please sign in to comment.