diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-19 10:04:26 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-19 10:04:26 +0200 |
commit | a456db7e69e14e3fe6040b27c275efe714fdb907 (patch) | |
tree | f57eac73f23504a202e45e8d48794761d2f62586 | |
parent | README: update (diff) | |
download | cimple-a456db7e69e14e3fe6040b27c275efe714fdb907.tar.gz cimple-a456db7e69e14e3fe6040b27c275efe714fdb907.zip |
test: refactor command line options
-rw-r--r-- | test/CMakeLists.txt | 12 | ||||
-rw-r--r-- | test/py/conftest.py | 114 |
2 files changed, 48 insertions, 78 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index edbf8aa..dd7f6f6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,10 +6,10 @@ set(python_test_args --no-header -v --durations=0 --durations-min=1.0 "${CMAKE_CURRENT_SOURCE_DIR}/py" - --server-binary "$<TARGET_FILE:server>" - --worker-binary "$<TARGET_FILE:worker>" - --client-binary "$<TARGET_FILE:client>" - --sigsegv-binary "$<TARGET_FILE:sigsegv>" + --server "$<TARGET_FILE:server>" + --worker "$<TARGET_FILE:worker>" + --client "$<TARGET_FILE:client>" + --sigsegv "$<TARGET_FILE:sigsegv>" --project-version "${PROJECT_VERSION}") function(add_python_tests name) @@ -28,7 +28,7 @@ add_python_tests(python_tests_stress add_python_tests(python_tests_valgrind Python3::Interpreter -m pytest ${python_test_args} -m "not stress and not flame_graph" - --valgrind-binary "${CMAKE_CURRENT_SOURCE_DIR}/../src/valgrind.sh") + --valgrind "${CMAKE_CURRENT_SOURCE_DIR}/../src/valgrind.sh") if(NOT DEFINED FLAME_GRAPHS_DIR) set(FLAME_GRAPHS_DIR "${CMAKE_CURRENT_SOURCE_DIR}") @@ -36,5 +36,5 @@ endif() add_python_tests(python_tests_perf Python3::Interpreter -m pytest ${python_test_args} -m "flame_graph" - --flamegraph-binary "${CMAKE_CURRENT_SOURCE_DIR}/../scripts/flamegraph.sh" + --flamegraph "${CMAKE_CURRENT_SOURCE_DIR}/../scripts/flamegraph.sh" --flame-graphs-dir "${FLAME_GRAPHS_DIR}") diff --git a/test/py/conftest.py b/test/py/conftest.py index 84163a5..01e329e 100644 --- a/test/py/conftest.py +++ b/test/py/conftest.py @@ -29,39 +29,16 @@ class Param: parser.addoption(self.cmd_line, required=self.required, help=self.help_string) -class ParamBinary(Param): - def __init__(self, name, **kwargs): - self.name = name - super().__init__(self.get_code_name(), self.get_help_string(), **kwargs) - - def get_code_name(self): - return f'{self.name}_binary' - - @property - def basename(self): - return f'cimple-{self.name}' - - def get_help_string(self): - return f'{self.basename} binary path' - - -BINARY_PARAMS = [ - ParamBinary(name) for name in ('server', 'worker', 'client', 'sigsegv') +PARAMS = [ + Param(f'{name}', f'cimple-{name} binary path') + for name in ('server', 'worker', 'client') ] - -PARAM_VERSION = Param('project_version', 'project version') - -PARAM_VALGRIND = ParamBinary('valgrind', required=False) - -PARAM_FLAMEGRAPH = ParamBinary('flamegraph', required=False) -PARAM_FLAME_GRAPHS_DIR = Param('flame_graphs_dir', 'directory to store flame graphs', required=False) - -PARAMS = list(BINARY_PARAMS) PARAMS += [ - PARAM_VALGRIND, - PARAM_FLAMEGRAPH, - PARAM_FLAME_GRAPHS_DIR, - PARAM_VERSION, + Param('sigsegv', 'sigsegv binary path'), + Param('project_version', 'project version'), + Param('valgrind', 'path to valgrind.sh', required=False), + Param('flamegraph', 'path to flamegraph.sh', required=False), + Param('flame_graphs_dir', 'directory to store flame graphs', required=False), ] @@ -70,27 +47,21 @@ def pytest_addoption(parser): opt.add_to_parser(parser) -def pytest_generate_tests(metafunc): - for opt in PARAMS: - if opt.codename in metafunc.fixturenames: - metafunc.parametrize(opt.codename, metafunc.config.getoption(opt.codename)) - - -class Paths: +class Params: def __init__(self, pytestconfig): - for opt in BINARY_PARAMS: + for opt in PARAMS: setattr(self, opt.codename, None) - for opt in BINARY_PARAMS: + for opt in PARAMS: path = pytestconfig.getoption(opt.codename) if path is None: continue - logging.info('%s path: %s', opt.basename, path) + logging.info("'%s' parameter value: %s", opt.codename, path) setattr(self, opt.codename, path) @fixture(scope='session') -def paths(pytestconfig): - return Paths(pytestconfig) +def params(pytestconfig): + return Params(pytestconfig) class CmdLineValgrind(CmdLine): @@ -101,17 +72,17 @@ class CmdLineValgrind(CmdLine): @fixture(scope='session') -def base_cmd_line(pytestconfig): +def base_cmd_line(params): cmd_line = CmdLine.unbuffered() - valgrind = pytestconfig.getoption(PARAM_VALGRIND.codename) + valgrind = params.valgrind if valgrind is not None: cmd_line = CmdLine.wrap(CmdLineValgrind(valgrind), cmd_line) return cmd_line @fixture(scope='session') -def version(pytestconfig): - return pytestconfig.getoption(PARAM_VERSION.codename) +def version(params): + return params.project_version @fixture(scope='session') @@ -140,41 +111,41 @@ class CmdLineWorker(CmdLine): @fixture -def server_exe(paths): - return CmdLineServer(paths.server_binary) +def server_exe(params): + return CmdLineServer(params.server) @fixture -def worker_exe(paths): - return CmdLineWorker(paths.worker_binary) +def worker_exe(params): + return CmdLineWorker(params.worker) @fixture -def client_exe(paths): - return CmdLine(paths.client_binary) +def client_exe(params): + return CmdLine(params.client) @fixture -def server_cmd(base_cmd_line, paths, server_port, sqlite_path): +def server_cmd(base_cmd_line, params, server_port, sqlite_path): args = ['--port', server_port, '--sqlite', sqlite_path] - return CmdLineServer.wrap(base_cmd_line, CmdLine(paths.server_binary, *args)) + return CmdLineServer.wrap(base_cmd_line, CmdLine(params.server, *args)) @fixture -def worker_cmd(base_cmd_line, paths, server_port): +def worker_cmd(base_cmd_line, params, server_port): args = ['--host', '127.0.0.1', '--port', server_port] - return CmdLineWorker.wrap(base_cmd_line, CmdLine(paths.worker_binary, *args)) + return CmdLineWorker.wrap(base_cmd_line, CmdLine(params.worker, *args)) @fixture -def client(base_cmd_line, paths, server_port): +def client(base_cmd_line, params, server_port): args = ['--host', '127.0.0.1', '--port', server_port] - return CmdLine.wrap(base_cmd_line, CmdLine(paths.client_binary, *args)) + return CmdLine.wrap(base_cmd_line, CmdLine(params.client, *args)) @fixture -def sigsegv(paths): - return CmdLine(paths.sigsegv_binary) +def sigsegv(params): + return CmdLine(params.sigsegv) @fixture @@ -194,8 +165,8 @@ def workers(worker_cmd): @fixture -def flame_graph_svg(pytestconfig, tmp_path, flame_graph_repo): - dir = pytestconfig.getoption(PARAM_FLAME_GRAPHS_DIR.codename) +def flame_graph_svg(params, tmp_path, flame_graph_repo): + dir = params.flame_graphs_dir if dir is None: return os.path.join(tmp_path, 'flame_graph.svg') os.makedirs(dir, exist_ok=True) @@ -203,11 +174,10 @@ def flame_graph_svg(pytestconfig, tmp_path, flame_graph_repo): @fixture -def profiler(pytestconfig, server, workers, flame_graph_svg): - script = pytestconfig.getoption(PARAM_FLAMEGRAPH.codename) +def profiler(params, server, workers, flame_graph_svg): pids = [server.pid] + [worker.pid for worker in workers] pids = map(str, pids) - cmd_line = CmdLine(script, flame_graph_svg, *pids) + cmd_line = CmdLine(params.flamegraph, flame_graph_svg, *pids) with cmd_line.run_async() as proc: yield assert proc.returncode == 0 @@ -232,21 +202,21 @@ STRESS_TEST_REPOS = [ ] -def _make_repo(repo_path, paths, cls): +def _make_repo(repo_path, params, cls): args = [repo_path] if cls is repo.TestRepoSegfault: - args += [paths.sigsegv_binary] + args += [params.sigsegv] return cls(*args) @fixture(params=TEST_REPOS, ids=[repo.codename() for repo in TEST_REPOS]) -def test_repo(repo_path, paths, request): - return _make_repo(repo_path, paths, request.param) +def test_repo(repo_path, params, request): + return _make_repo(repo_path, params, request.param) @fixture(params=STRESS_TEST_REPOS, ids=[repo.codename() for repo in STRESS_TEST_REPOS]) -def stress_test_repo(repo_path, paths, request): - return _make_repo(repo_path, paths, request.param) +def stress_test_repo(repo_path, params, request): + return _make_repo(repo_path, params, request.param) @fixture |