Skip to content

Commit 9bf956c

Browse files
authored
Refactor harness 2 (#11220)
1 parent da71b30 commit 9bf956c

File tree

9 files changed

+64
-124
lines changed

9 files changed

+64
-124
lines changed

ydb/public/tools/lib/cmds/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def __init__(self):
4141
self.enabled_grpc_services = []
4242

4343

44+
def _get_build_path(path):
45+
try:
46+
result = yatest.common.build_path(path)
47+
except (AttributeError, yatest.common.NoRuntimeFormed):
48+
result = path
49+
50+
return result
51+
52+
4453
def ensure_path_exists(path):
4554
if not os.path.isdir(path):
4655
os.makedirs(path)
@@ -358,9 +367,8 @@ def deploy(arguments):
358367
domain_name='local',
359368
pq_client_service_types=pq_client_service_types(arguments),
360369
enable_pqcd=enable_pqcd(arguments),
361-
load_udfs=True,
362370
suppress_version_check=arguments.suppress_version_check,
363-
udfs_path=arguments.ydb_udfs_dir,
371+
udfs_path=arguments.ydb_udfs_dir or _get_build_path("yql/udfs"),
364372
additional_log_configs=additional_log_configs,
365373
port_allocator=port_allocator,
366374
use_in_memory_pdisks=use_in_memory_pdisks_flag(arguments.ydb_working_dir),

ydb/tests/functional/api/test_crud.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from hamcrest import assert_that, equal_to, raises
77

8+
import yatest
9+
810
from ydb.tests.library.harness.kikimr_runner import KiKiMR
911
from ydb.tests.library.harness.kikimr_config import KikimrConfigGenerator
1012
from ydb.tests.oss.ydb_sdk_import import ydb
@@ -73,7 +75,7 @@ def test_create_and_upsert_data_with_repetitions(self, repetitions, column_count
7375
class TestCRUDOperations(object):
7476
@classmethod
7577
def setup_class(cls):
76-
cls.cluster = KiKiMR(KikimrConfigGenerator(load_udfs=True))
78+
cls.cluster = KiKiMR(KikimrConfigGenerator(udfs_path=yatest.common.build_path("yql/udfs")))
7779
cls.cluster.start()
7880
cls.driver = ydb.Driver(
7981
ydb.DriverConfig(
@@ -134,7 +136,7 @@ def callee():
134136
class TestSelect(object):
135137
@classmethod
136138
def setup_class(cls):
137-
cls.cluster = KiKiMR(KikimrConfigGenerator(load_udfs=True))
139+
cls.cluster = KiKiMR(KikimrConfigGenerator(udfs_path=yatest.common.build_path("yql/udfs")))
138140
cls.cluster.start()
139141
cls.driver = ydb.Driver(
140142
ydb.DriverConfig(

ydb/tests/functional/canonical/test_sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def setup_class(cls):
111111
cls.database = '/local'
112112
cls.cluster = KiKiMR(
113113
KikimrConfigGenerator(
114-
load_udfs=True,
114+
udfs_path=yatest.common.build_path("yql/udfs"),
115115
domain_name='local',
116116
use_in_memory_pdisks=True,
117117
disable_iterator_reads=True,

ydb/tests/functional/suite_tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class BaseSuiteRunner(object):
245245
def setup_class(cls):
246246
cls.cluster = KiKiMR(
247247
KikimrConfigGenerator(
248-
load_udfs=True,
248+
udfs_path=yatest.common.build_path("yql/udfs"),
249249
use_in_memory_pdisks=True,
250250
disable_iterator_reads=True,
251251
disable_iterator_lookups=True,

ydb/tests/library/harness/daemon.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77
import subprocess
88

9-
import yatest
109
from yatest.common import process
1110
import six
1211

@@ -53,23 +52,15 @@ def __init__(self, exceptions):
5352
super(SeveralDaemonErrors, self).__init__("\n".join(str(x) for x in exceptions))
5453

5554

56-
def _work_path(name):
57-
# TODO: remove yatest dependency from harness
58-
try:
59-
return yatest.common.work_path(name)
60-
except (AttributeError, yatest.common.NoRuntimeFormed):
61-
return name
62-
63-
6455
class Daemon(object):
6556
"""Local process executed as process in current host"""
6657
def __init__(
6758
self,
6859
command,
6960
cwd,
7061
timeout,
71-
stdout_file=_work_path('stdout'),
72-
stderr_file=_work_path('stderr'),
62+
stdout_file,
63+
stderr_file,
7364
stderr_on_error_lines=0,
7465
core_pattern=None,
7566
):

ydb/tests/library/harness/kikimr_config.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,9 @@ def _load_default_yaml(default_tablet_node_ids, ydb_domain_name, static_erasure,
8989
return yaml_dict
9090

9191

92-
def _read_file(filename):
93-
with open(filename, "r") as f:
94-
return f.read()
95-
96-
9792
def _load_yaml_config(filename):
98-
return yaml.safe_load(_read_file(filename))
93+
with open(filename, "r") as f:
94+
return yaml.safe_load(f)
9995

10096

10197
def _use_in_memory_pdisks_var(pdisk_store_path, use_in_memory_pdisks):
@@ -108,16 +104,6 @@ def _use_in_memory_pdisks_var(pdisk_store_path, use_in_memory_pdisks):
108104
return use_in_memory_pdisks
109105

110106

111-
def _get_build_path(path):
112-
# TODO: remove yatest dependency from harness
113-
try:
114-
result = yatest.common.build_path(path)
115-
except (AttributeError, yatest.common.NoRuntimeFormed):
116-
result = path
117-
118-
return result
119-
120-
121107
class KikimrConfigGenerator(object):
122108
def __init__(
123109
self,
@@ -126,7 +112,6 @@ def __init__(
126112
nodes=None,
127113
additional_log_configs=None,
128114
port_allocator=None,
129-
load_udfs=False,
130115
udfs_path=None,
131116
output_path=None,
132117
enable_pq=True,
@@ -217,7 +202,6 @@ def __init__(
217202
self.static_erasure = erasure
218203
self.domain_name = domain_name
219204
self.__number_of_pdisks_per_node = 1 + len(dynamic_pdisks)
220-
self.__load_udfs = load_udfs
221205
self.__udfs_path = udfs_path
222206
self._dcs = [1]
223207
if erasure == Erasure.MIRROR_3_DC:
@@ -236,7 +220,11 @@ def __init__(
236220

237221
self.__dynamic_pdisks = dynamic_pdisks
238222

239-
self.__output_path = output_path or yatest.common.output_path()
223+
self.__working_dir = output_path or yatest.common.test_output_path()
224+
225+
if not os.path.isdir(self.__working_dir):
226+
os.makedirs(self.__working_dir)
227+
240228
self.node_kind = node_kind
241229
self.yq_tenant = yq_tenant
242230
self.dc_mapping = dc_mapping
@@ -296,8 +284,10 @@ def __init__(
296284
# NOTE(shmel1k@): change to 'true' after migration to YDS scheme
297285
self.yaml_config['sqs_config']['enable_sqs'] = enable_sqs
298286
self.yaml_config['pqcluster_discovery_config']['enabled'] = enable_pqcd
299-
self.yaml_config["net_classifier_config"]["net_data_file_path"] = os.path.join(self.__output_path,
300-
'netData.tsv')
287+
self.yaml_config["net_classifier_config"]["net_data_file_path"] = os.path.join(
288+
self.__working_dir,
289+
'netData.tsv',
290+
)
301291
with open(self.yaml_config["net_classifier_config"]["net_data_file_path"], "w") as net_data_file:
302292
net_data_file.write("")
303293

@@ -482,37 +472,13 @@ def names_txt(self):
482472
return self.naming_config.NameserviceConfig
483473

484474
def __set_enable_metering(self):
485-
def ensure_path_exists(path):
486-
if not os.path.isdir(path):
487-
os.makedirs(path)
488-
return path
489-
490-
def get_cwd_for_test(output_path):
491-
test_name = yatest.common.context.test_name or ""
492-
test_name = test_name.replace(':', '_')
493-
return os.path.join(output_path, test_name)
494-
495-
cwd = get_cwd_for_test(self.__output_path)
496-
ensure_path_exists(cwd)
497-
metering_file_path = os.path.join(cwd, 'metering.txt')
475+
metering_file_path = os.path.join(self.__working_dir, 'metering.txt')
498476
with open(metering_file_path, "w") as metering_file:
499477
metering_file.write('')
500478
self.yaml_config['metering_config'] = {'metering_file_path': metering_file_path}
501479

502480
def __set_enable_audit_log(self):
503-
def ensure_path_exists(path):
504-
if not os.path.isdir(path):
505-
os.makedirs(path)
506-
return path
507-
508-
def get_cwd_for_test(output_path):
509-
test_name = yatest.common.context.test_name or ""
510-
test_name = test_name.replace(':', '_')
511-
return os.path.join(output_path, test_name)
512-
513-
cwd = get_cwd_for_test(self.__output_path)
514-
ensure_path_exists(cwd)
515-
audit_file_path = os.path.join(cwd, 'audit.txt')
481+
audit_file_path = os.path.join(self.__working_dir, 'audit.txt')
516482
with open(audit_file_path, "w") as audit_file:
517483
audit_file.write('')
518484
self.yaml_config['audit_config'] = dict(
@@ -534,8 +500,8 @@ def sqs_service_enabled(self):
534500
return self.yaml_config['sqs_config']['enable_sqs']
535501

536502
@property
537-
def output_path(self):
538-
return self.__output_path
503+
def working_dir(self):
504+
return self.__working_dir
539505

540506
def get_binary_path(self, node_id):
541507
binary_paths = self.__binary_paths
@@ -573,9 +539,9 @@ def clone_grpc_as_ext_endpoint(self, port, endpoint_id=None):
573539
self.yaml_config['grpc_config']['ext_endpoints'].append(cur_grpc_config)
574540

575541
def get_yql_udfs_to_load(self):
576-
if not self.__load_udfs:
542+
if self.__udfs_path is None:
577543
return []
578-
udfs_path = self.__udfs_path or _get_build_path("yql/udfs")
544+
udfs_path = self.__udfs_path
579545
result = []
580546
for dirpath, dnames, fnames in os.walk(udfs_path):
581547
is_loaded = False

ydb/tests/library/harness/kikimr_runner.py

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
logger = logging.getLogger(__name__)
2525

2626

27-
def get_unique_path_for_current_test(output_path, sub_folder):
28-
# TODO: remove yatest dependency from harness
29-
try:
30-
test_name = yatest.common.context.test_name
31-
except (AttributeError, yatest.common.NoRuntimeFormed):
32-
test_name = ""
33-
test_name = test_name.replace(':', '_')
34-
35-
return os.path.join(output_path, test_name, sub_folder)
36-
37-
3827
def ensure_path_exists(path):
3928
# NOTE: can't switch to os.makedirs(path, exist_ok=True) as some tests
4029
# are still running under python2 (exist_ok was added in py3.2)
@@ -43,14 +32,6 @@ def ensure_path_exists(path):
4332
return path
4433

4534

46-
def join(a, b):
47-
if a is None:
48-
a = ''
49-
if b is None:
50-
b = ''
51-
return os.path.join(a, b)
52-
53-
5435
class KiKiMRNode(daemon.Daemon, kikimr_node_interface.NodeInterface):
5536
def __init__(self, node_id, config_path, port_allocator, cluster_name, configurator,
5637
udfs_dir=None, role='node', node_broker_port=None, tenant_affiliation=None, encryption_key=None,
@@ -59,7 +40,6 @@ def __init__(self, node_id, config_path, port_allocator, cluster_name, configura
5940
super(kikimr_node_interface.NodeInterface, self).__init__()
6041
self.node_id = node_id
6142
self.data_center = data_center
62-
self.__cwd = None
6343
self.__config_path = config_path
6444
self.__cluster_name = cluster_name
6545
self.__configurator = configurator
@@ -80,33 +60,35 @@ def __init__(self, node_id, config_path, port_allocator, cluster_name, configura
8060
self.__role = role
8161
self.__node_broker_port = node_broker_port
8262

63+
self.__working_dir = ensure_path_exists(
64+
os.path.join(
65+
self.__configurator.working_dir,
66+
self.__cluster_name,
67+
"{}_{}".format(
68+
self.__role,
69+
self.node_id
70+
)
71+
)
72+
)
73+
8374
if configurator.use_log_files:
84-
self.__log_file = tempfile.NamedTemporaryFile(dir=self.cwd, prefix="logfile_", suffix=".log", delete=False)
85-
kwargs = {}
75+
self.__log_file = tempfile.NamedTemporaryFile(dir=self.__working_dir, prefix="logfile_", suffix=".log", delete=False)
76+
kwargs = {
77+
"stdout_file": os.path.join(self.__working_dir, "stdout"),
78+
"stderr_file": os.path.join(self.__working_dir, "stderr")
79+
}
8680
else:
8781
self.__log_file = None
8882
kwargs = {
8983
"stdout_file": "/dev/stdout",
9084
"stderr_file": "/dev/stderr"
9185
}
9286

93-
daemon.Daemon.__init__(self, self.command, cwd=self.cwd, timeout=180, stderr_on_error_lines=240, **kwargs)
87+
daemon.Daemon.__init__(self, self.command, cwd=self.__working_dir, timeout=180, stderr_on_error_lines=240, **kwargs)
9488

9589
@property
9690
def cwd(self):
97-
if self.__cwd is None:
98-
self.__cwd = ensure_path_exists(
99-
get_unique_path_for_current_test(
100-
self.__configurator.output_path,
101-
join(
102-
self.__cluster_name, "{}_{}".format(
103-
self.__role,
104-
self.node_id
105-
)
106-
)
107-
)
108-
)
109-
return self.__cwd
91+
return self.__working_dir
11092

11193
@property
11294
def binary_path(self):
@@ -171,7 +153,7 @@ def __make_run_command(self):
171153

172154
command.extend(
173155
[
174-
"--yaml-config=%s" % join(self.__config_path, "config.yaml"),
156+
"--yaml-config=%s" % os.path.join(self.__config_path, "config.yaml"),
175157
"--grpc-port=%s" % self.grpc_port,
176158
"--mon-port=%d" % self.mon_port,
177159
"--ic-port=%d" % self.ic_port,
@@ -232,7 +214,7 @@ def start(self):
232214

233215

234216
class KiKiMR(kikimr_cluster_interface.KiKiMRClusterInterface):
235-
def __init__(self, configurator=None, cluster_name=''):
217+
def __init__(self, configurator=None, cluster_name='cluster'):
236218
super(KiKiMR, self).__init__()
237219

238220
self.__tmpdir = tempfile.mkdtemp(prefix="kikimr_" + cluster_name + "_")
@@ -243,11 +225,10 @@ def __init__(self, configurator=None, cluster_name=''):
243225
self._nodes = {}
244226
self._slots = {}
245227
self.__server = 'localhost'
246-
self.__client = None
247-
self.__kv_client = None
248-
self.__scheme_client = None
249228
self.__storage_pool_id_allocator = itertools.count(1)
250-
self.__config_path = None
229+
self.__config_path = ensure_path_exists(
230+
os.path.join(self.__configurator.working_dir, self.__cluster_name, "kikimr_configs")
231+
)
251232
self._slot_index_allocator = itertools.count(1)
252233
self._node_index_allocator = itertools.count(1)
253234
self.default_channel_bindings = None
@@ -313,9 +294,6 @@ def prepare(self):
313294
return
314295

315296
self.__initialy_prepared = True
316-
self.__client = None
317-
self.__kv_client = None
318-
self.__scheme_client = None
319297
self.__instantiate_udfs_dir()
320298
self.__write_configs()
321299
for _ in self.__configurator.all_node_ids():
@@ -463,15 +441,6 @@ def restart_nodes(self):
463441

464442
@property
465443
def config_path(self):
466-
if self.__config_path is None:
467-
self.__config_path = ensure_path_exists(
468-
get_unique_path_for_current_test(
469-
self.__configurator.output_path,
470-
join(
471-
self.__cluster_name, "kikimr_configs"
472-
)
473-
)
474-
)
475444
return self.__config_path
476445

477446
def __write_configs(self):

0 commit comments

Comments
 (0)