aboutsummaryrefslogtreecommitdiffstats
path: root/library/recipes/common
diff options
context:
space:
mode:
authorgalaxycrab <UgnineSirdis@ydb.tech>2023-03-03 16:10:03 +0300
committergalaxycrab <UgnineSirdis@ydb.tech>2023-03-03 16:10:03 +0300
commita5431f1bb7d80f54a964b4e8454a875c3d9c4f6f (patch)
tree8b31c9231ead7e76c0fbab37d342cb31db0002e3 /library/recipes/common
parentb53e1ad22f86b89727d455711c68036a4496d06d (diff)
downloadydb-a5431f1bb7d80f54a964b4e8454a875c3d9c4f6f.tar.gz
Separate test for federated query with S3 recipe
Diffstat (limited to 'library/recipes/common')
-rw-r--r--library/recipes/common/__init__.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/library/recipes/common/__init__.py b/library/recipes/common/__init__.py
new file mode 100644
index 0000000000..84308b1327
--- /dev/null
+++ b/library/recipes/common/__init__.py
@@ -0,0 +1,74 @@
+import os.path
+import psutil
+import socket
+import subprocess
+import time
+import yatest.common
+
+
+def find_free_ports(count):
+ sockets = []
+ ports = []
+
+ for _ in range(count):
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.bind(('', 0))
+ ports.append(sock.getsockname()[1])
+ sockets.append(sock)
+
+ for sock in sockets:
+ sock.close()
+ return ports
+
+
+def start_daemon(command, environment, is_alive_check, pid_file_name, timeout=60, daemon_name=None):
+ daemon_name = daemon_name or os.path.basename(command[0])
+ stdout_path = yatest.common.output_path('{}.out.log').format(daemon_name)
+ stderr_path = yatest.common.output_path('{}.err.log').format(daemon_name)
+
+ process = subprocess.Popen(
+ command,
+ stdout=open(stdout_path, 'w'),
+ stderr=open(stderr_path, 'w'),
+ env=environment)
+ with open(pid_file_name, 'w') as fout:
+ fout.write(str(process.pid))
+
+ for attempts in range(timeout):
+ result = process.poll()
+ if result is not None:
+ raise RuntimeError(
+ 'Could not launch "{}" with exit code {}\nStdout: {}\nStderr: {}'
+ .format(daemon_name, result, stdout_path, stderr_path)
+ )
+
+ if is_alive_check():
+ return
+
+ time.sleep(1)
+
+ raise RuntimeError(
+ 'Could not launch "{}" for {} seconds\nStdout: {}\nStderr: {}'
+ .format(daemon_name, timeout, stdout_path, stderr_path)
+ )
+
+
+def pid_exists(pid):
+ try:
+ if psutil.Process(pid).status() == psutil.STATUS_ZOMBIE:
+ return False
+ except psutil.NoSuchProcess:
+ return False
+ return True
+
+
+def stop_daemon(pid, signal=15):
+ pid = int(pid)
+ if not pid_exists(pid):
+ return False
+
+ os.kill(pid, signal)
+ while pid_exists(pid):
+ time.sleep(1)
+
+ return True