2020-06-29 12:55:27 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import inspect
|
|
|
|
import threading
|
|
|
|
import tempfile
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
import testflows.settings as settings
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
from testflows.core import *
|
|
|
|
from testflows.asserts import error
|
2021-05-07 13:14:40 +00:00
|
|
|
from testflows.connect import Shell as ShellBase
|
2020-10-15 20:23:49 +00:00
|
|
|
from testflows.uexpect import ExpectTimeoutError
|
2021-05-07 13:14:40 +00:00
|
|
|
from testflows._core.testtype import TestSubType
|
|
|
|
|
|
|
|
class Shell(ShellBase):
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
# send exit and Ctrl-D repeatedly
|
|
|
|
# to terminate any open shell commands.
|
|
|
|
# This is needed for example
|
|
|
|
# to solve a problem with
|
|
|
|
# 'docker-compose exec {name} bash --noediting'
|
|
|
|
# that does not clean up open bash processes
|
|
|
|
# if not exited normally
|
|
|
|
for i in range(10):
|
|
|
|
if self.child is not None:
|
|
|
|
try:
|
|
|
|
self.send('exit\r', eol='')
|
|
|
|
self.send('\x04\r', eol='')
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return super(Shell, self).__exit__(type, value, traceback)
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
class QueryRuntimeException(Exception):
|
|
|
|
"""Exception during query execution on the server.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Node(object):
|
|
|
|
"""Generic cluster node.
|
|
|
|
"""
|
|
|
|
config_d_dir = "/etc/clickhouse-server/config.d/"
|
|
|
|
|
|
|
|
def __init__(self, cluster, name):
|
|
|
|
self.cluster = cluster
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def repr(self):
|
|
|
|
return f"Node(name='{self.name}')"
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
def close_bashes(self):
|
|
|
|
"""Close all active bashes to the node.
|
2020-06-29 12:55:27 +00:00
|
|
|
"""
|
|
|
|
with self.cluster.lock:
|
|
|
|
for key in list(self.cluster._bash.keys()):
|
|
|
|
if key.endswith(f"-{self.name}"):
|
|
|
|
shell = self.cluster._bash.pop(key)
|
|
|
|
shell.__exit__(None, None, None)
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
def restart(self, timeout=300, retries=5, safe=True):
|
|
|
|
"""Restart node.
|
|
|
|
"""
|
|
|
|
self.close_bashes()
|
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} restart {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
def start(self, timeout=300, retries=5):
|
2020-11-17 18:41:28 +00:00
|
|
|
"""Start node.
|
|
|
|
"""
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} start {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-11-17 18:41:28 +00:00
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
def stop(self, timeout=300, retries=5, safe=True):
|
2020-11-17 18:41:28 +00:00
|
|
|
"""Stop node.
|
|
|
|
"""
|
2021-05-07 13:14:40 +00:00
|
|
|
self.close_bashes()
|
2020-11-17 18:41:28 +00:00
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} stop {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-11-17 18:41:28 +00:00
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
def command(self, *args, **kwargs):
|
|
|
|
return self.cluster.command(self.name, *args, **kwargs)
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
def cmd(self, cmd, message=None, exitcode=None, steps=True, shell_command="bash --noediting", no_checks=False,
|
|
|
|
raise_on_exception=False, step=By, *args, **kwargs):
|
|
|
|
"""Execute and check command.
|
|
|
|
:param cmd: command
|
|
|
|
:param message: expected message that should be in the output, default: None
|
|
|
|
:param exitcode: expected exitcode, default: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = f"{cmd}"
|
|
|
|
with Step("executing command", description=command, format_description=False) if steps else NullStep():
|
|
|
|
try:
|
|
|
|
r = self.cluster.bash(self.name, command=shell_command)(command, *args, **kwargs)
|
|
|
|
except ExpectTimeoutError:
|
|
|
|
self.cluster.close_bash(self.name)
|
|
|
|
raise
|
|
|
|
|
|
|
|
if no_checks:
|
|
|
|
return r
|
|
|
|
|
|
|
|
if exitcode is not None:
|
|
|
|
with Then(f"exitcode should be {exitcode}") if steps else NullStep():
|
|
|
|
assert r.exitcode == exitcode, error(r.output)
|
|
|
|
|
|
|
|
if message is not None:
|
|
|
|
with Then(f"output should contain message", description=message) if steps else NullStep():
|
|
|
|
assert message in r.output, error(r.output)
|
|
|
|
|
|
|
|
if message is None or "Exception:" not in message:
|
|
|
|
with Then("check if output has exception") if steps else NullStep():
|
|
|
|
if "Exception:" in r.output:
|
|
|
|
if raise_on_exception:
|
|
|
|
raise QueryRuntimeException(r.output)
|
|
|
|
assert False, error(r.output)
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
class ClickHouseNode(Node):
|
|
|
|
"""Node with ClickHouse server.
|
|
|
|
"""
|
2020-10-19 16:48:39 +00:00
|
|
|
def wait_healthy(self, timeout=300):
|
2020-06-29 12:55:27 +00:00
|
|
|
with By(f"waiting until container {self.name} is healthy"):
|
|
|
|
start_time = time.time()
|
|
|
|
while True:
|
2021-05-09 16:34:56 +00:00
|
|
|
if self.query("select 1", no_checks=1, timeout=300, steps=False).exitcode == 0:
|
2020-06-29 12:55:27 +00:00
|
|
|
break
|
|
|
|
if time.time() - start_time < timeout:
|
|
|
|
time.sleep(2)
|
|
|
|
continue
|
|
|
|
assert False, "container is not healthy"
|
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
def stop(self, timeout=300, safe=True, retries=5):
|
2020-10-27 22:45:04 +00:00
|
|
|
"""Stop node.
|
|
|
|
"""
|
|
|
|
if safe:
|
|
|
|
self.query("SYSTEM STOP MOVES")
|
|
|
|
self.query("SYSTEM STOP MERGES")
|
|
|
|
self.query("SYSTEM FLUSH LOGS")
|
|
|
|
with By("waiting for 5 sec for moves and merges to stop"):
|
|
|
|
time.sleep(5)
|
|
|
|
with And("forcing to sync everything to disk"):
|
2021-05-09 16:34:56 +00:00
|
|
|
self.command("sync", timeout=300)
|
2020-10-27 22:45:04 +00:00
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
self.close_bashes()
|
2020-10-27 22:45:04 +00:00
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} stop {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-10-27 22:45:04 +00:00
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
def start(self, timeout=300, wait_healthy=True, retries=5):
|
2020-10-27 22:45:04 +00:00
|
|
|
"""Start node.
|
|
|
|
"""
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} start {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-10-27 22:45:04 +00:00
|
|
|
|
|
|
|
if wait_healthy:
|
|
|
|
self.wait_healthy(timeout)
|
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
def restart(self, timeout=300, safe=True, wait_healthy=True, retries=5):
|
2020-06-29 12:55:27 +00:00
|
|
|
"""Restart node.
|
|
|
|
"""
|
|
|
|
if safe:
|
|
|
|
self.query("SYSTEM STOP MOVES")
|
|
|
|
self.query("SYSTEM STOP MERGES")
|
|
|
|
self.query("SYSTEM FLUSH LOGS")
|
|
|
|
with By("waiting for 5 sec for moves and merges to stop"):
|
|
|
|
time.sleep(5)
|
|
|
|
with And("forcing to sync everything to disk"):
|
2021-05-09 16:34:56 +00:00
|
|
|
self.command("sync", timeout=300)
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
self.close_bashes()
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2021-01-29 22:55:08 +00:00
|
|
|
for retry in range(retries):
|
|
|
|
r = self.cluster.command(None, f'{self.cluster.docker_compose} restart {self.name}', timeout=timeout)
|
|
|
|
if r.exitcode == 0:
|
|
|
|
break
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2020-09-03 14:53:09 +00:00
|
|
|
if wait_healthy:
|
|
|
|
self.wait_healthy(timeout)
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
def query(self, sql, message=None, exitcode=None, steps=True, no_checks=False,
|
|
|
|
raise_on_exception=False, step=By, settings=None, *args, **kwargs):
|
|
|
|
"""Execute and check query.
|
|
|
|
:param sql: sql query
|
|
|
|
:param message: expected message that should be in the output, default: None
|
|
|
|
:param exitcode: expected exitcode, default: None
|
|
|
|
"""
|
2020-10-15 20:23:49 +00:00
|
|
|
settings = list(settings or [])
|
|
|
|
|
|
|
|
if hasattr(current().context, "default_query_settings"):
|
|
|
|
settings += current().context.default_query_settings
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
if len(sql) > 1024:
|
|
|
|
with tempfile.NamedTemporaryFile("w", encoding="utf-8") as query:
|
|
|
|
query.write(sql)
|
|
|
|
query.flush()
|
|
|
|
command = f"cat \"{query.name}\" | {self.cluster.docker_compose} exec -T {self.name} clickhouse client -n"
|
2020-10-15 20:23:49 +00:00
|
|
|
for setting in settings:
|
2020-06-29 12:55:27 +00:00
|
|
|
name, value = setting
|
|
|
|
command += f" --{name} \"{value}\""
|
|
|
|
description = f"""
|
2020-08-15 18:24:33 +00:00
|
|
|
echo -e \"{sql[:100]}...\" > {query.name}
|
2020-06-29 12:55:27 +00:00
|
|
|
{command}
|
|
|
|
"""
|
2021-03-11 20:41:10 +00:00
|
|
|
with Step("executing command", description=description, format_description=False) if steps else NullStep():
|
2020-10-15 20:23:49 +00:00
|
|
|
try:
|
|
|
|
r = self.cluster.bash(None)(command, *args, **kwargs)
|
|
|
|
except ExpectTimeoutError:
|
|
|
|
self.cluster.close_bash(None)
|
2020-06-29 12:55:27 +00:00
|
|
|
else:
|
|
|
|
command = f"echo -e \"{sql}\" | clickhouse client -n"
|
2020-10-15 20:23:49 +00:00
|
|
|
for setting in settings:
|
2020-06-29 12:55:27 +00:00
|
|
|
name, value = setting
|
|
|
|
command += f" --{name} \"{value}\""
|
2021-03-11 20:41:10 +00:00
|
|
|
with Step("executing command", description=command, format_description=False) if steps else NullStep():
|
2020-10-15 20:23:49 +00:00
|
|
|
try:
|
|
|
|
r = self.cluster.bash(self.name)(command, *args, **kwargs)
|
|
|
|
except ExpectTimeoutError:
|
|
|
|
self.cluster.close_bash(self.name)
|
|
|
|
raise
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
if no_checks:
|
|
|
|
return r
|
|
|
|
|
|
|
|
if exitcode is not None:
|
|
|
|
with Then(f"exitcode should be {exitcode}") if steps else NullStep():
|
|
|
|
assert r.exitcode == exitcode, error(r.output)
|
|
|
|
|
|
|
|
if message is not None:
|
|
|
|
with Then(f"output should contain message", description=message) if steps else NullStep():
|
|
|
|
assert message in r.output, error(r.output)
|
|
|
|
|
|
|
|
if message is None or "Exception:" not in message:
|
|
|
|
with Then("check if output has exception") if steps else NullStep():
|
|
|
|
if "Exception:" in r.output:
|
|
|
|
if raise_on_exception:
|
|
|
|
raise QueryRuntimeException(r.output)
|
|
|
|
assert False, error(r.output)
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
class Cluster(object):
|
|
|
|
"""Simple object around docker-compose cluster.
|
|
|
|
"""
|
2020-08-15 18:24:33 +00:00
|
|
|
def __init__(self, local=False,
|
|
|
|
clickhouse_binary_path=None, configs_dir=None,
|
2020-06-29 12:55:27 +00:00
|
|
|
nodes=None,
|
2020-08-15 18:24:33 +00:00
|
|
|
docker_compose="docker-compose", docker_compose_project_dir=None,
|
2020-06-29 12:55:27 +00:00
|
|
|
docker_compose_file="docker-compose.yml"):
|
2020-08-15 18:24:33 +00:00
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
self._bash = {}
|
2021-05-10 13:26:24 +00:00
|
|
|
self._control_shell = None
|
2021-05-07 13:14:40 +00:00
|
|
|
self.environ = {}
|
2020-06-29 12:55:27 +00:00
|
|
|
self.clickhouse_binary_path = clickhouse_binary_path
|
|
|
|
self.configs_dir = configs_dir
|
|
|
|
self.local = local
|
|
|
|
self.nodes = nodes or {}
|
|
|
|
self.docker_compose = docker_compose
|
|
|
|
|
|
|
|
frame = inspect.currentframe().f_back
|
|
|
|
caller_dir = os.path.dirname(os.path.abspath(frame.f_globals["__file__"]))
|
|
|
|
|
|
|
|
# auto set configs directory
|
|
|
|
if self.configs_dir is None:
|
|
|
|
caller_configs_dir = caller_dir
|
|
|
|
if os.path.exists(caller_configs_dir):
|
|
|
|
self.configs_dir = caller_configs_dir
|
|
|
|
|
|
|
|
if not os.path.exists(self.configs_dir):
|
2021-03-11 20:41:10 +00:00
|
|
|
raise TypeError(f"configs directory '{self.configs_dir}' does not exist")
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
# auto set docker-compose project directory
|
|
|
|
if docker_compose_project_dir is None:
|
|
|
|
caller_project_dir = os.path.join(caller_dir, "docker-compose")
|
|
|
|
if os.path.exists(caller_project_dir):
|
|
|
|
docker_compose_project_dir = caller_project_dir
|
|
|
|
|
2020-08-15 18:24:33 +00:00
|
|
|
docker_compose_file_path = os.path.join(docker_compose_project_dir or "", docker_compose_file)
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
if not os.path.exists(docker_compose_file_path):
|
|
|
|
raise TypeError("docker compose file '{docker_compose_file_path}' does not exist")
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
self.docker_compose += f" --ansi never --project-directory \"{docker_compose_project_dir}\" --file \"{docker_compose_file_path}\""
|
2020-06-29 12:55:27 +00:00
|
|
|
self.lock = threading.Lock()
|
2021-05-10 13:26:24 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def control_shell(self):
|
|
|
|
"""Must be called with self.lock.acquired.
|
|
|
|
"""
|
|
|
|
if self._control_shell is None:
|
|
|
|
self._control_shell = Shell()
|
|
|
|
return self._control_shell
|
|
|
|
|
|
|
|
def node_container_id(self, node, timeout=300):
|
|
|
|
"""Must be called with self.lock acquired.
|
|
|
|
"""
|
|
|
|
container_id = None
|
|
|
|
time_start = time.time()
|
|
|
|
while True:
|
|
|
|
c = self.control_shell(f"{self.docker_compose} ps -q {node}")
|
|
|
|
container_id = c.output.strip()
|
|
|
|
if c.exitcode == 0 and len(container_id) > 1:
|
|
|
|
break
|
|
|
|
if time.time() - time_start > timeout:
|
|
|
|
raise RuntimeError(f"failed to get docker container id for the {node} service")
|
|
|
|
return container_id
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2020-10-19 16:48:39 +00:00
|
|
|
def shell(self, node, timeout=300):
|
2020-07-13 20:36:56 +00:00
|
|
|
"""Returns unique shell terminal to be used.
|
|
|
|
"""
|
|
|
|
if node is None:
|
|
|
|
return Shell()
|
2021-05-10 13:26:24 +00:00
|
|
|
|
|
|
|
with self.lock:
|
|
|
|
container_id = self.node_container_id(node=node, timeout=timeout)
|
2021-05-11 12:56:14 +00:00
|
|
|
|
|
|
|
time_start = time.time()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
shell = Shell(command=[
|
|
|
|
"/bin/bash", "--noediting", "-c", f"docker exec -it {container_id} bash --noediting"
|
|
|
|
], name=node)
|
|
|
|
shell.timeout = 30
|
|
|
|
shell("echo 1")
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
shell.__exit__(None, None, None)
|
|
|
|
if time.time() - time_start > timeout:
|
|
|
|
raise RuntimeError(f"failed to open bash to node {node}")
|
2020-07-13 20:36:56 +00:00
|
|
|
|
2020-07-24 14:05:13 +00:00
|
|
|
shell.timeout = timeout
|
|
|
|
return shell
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
def bash(self, node, timeout=300, command="bash --noediting"):
|
2020-06-29 12:55:27 +00:00
|
|
|
"""Returns thread-local bash terminal
|
|
|
|
to a specific node.
|
|
|
|
:param node: name of the service
|
|
|
|
"""
|
2020-10-15 20:23:49 +00:00
|
|
|
test = current()
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
if top().terminating:
|
|
|
|
if test and (test.cflags & MANDATORY and test.subtype is not TestSubType.Given):
|
2020-10-15 20:23:49 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise InterruptedError("terminating")
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
current_thread = threading.current_thread()
|
2020-10-15 20:23:49 +00:00
|
|
|
id = f"{current_thread.name}-{node}"
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
with self.lock:
|
|
|
|
if self._bash.get(id) is None:
|
|
|
|
if node is None:
|
|
|
|
self._bash[id] = Shell().__enter__()
|
2021-05-07 13:14:40 +00:00
|
|
|
for name,value in self.environ.items():
|
|
|
|
self._bash[id](f"export {name}={value}")
|
2020-06-29 12:55:27 +00:00
|
|
|
else:
|
2021-05-10 13:26:24 +00:00
|
|
|
container_id = self.node_container_id(node=node, timeout=timeout)
|
2021-05-11 12:56:14 +00:00
|
|
|
time_start = time.time()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self._bash[id] = Shell(command=[
|
|
|
|
"/bin/bash", "--noediting", "-c", f"docker exec -it {container_id} {command}"
|
|
|
|
], name=node).__enter__()
|
|
|
|
self._bash[id].timeout = 30
|
|
|
|
self._bash[id]("echo 1")
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
self._bash[id].__exit__(None, None, None)
|
|
|
|
if time.time() - time_start > timeout:
|
|
|
|
raise RuntimeError(f"failed to open bash to node {node}")
|
2020-10-15 20:23:49 +00:00
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
self._bash[id].timeout = timeout
|
2020-10-15 20:23:49 +00:00
|
|
|
|
|
|
|
# clean up any stale open shells for threads that have exited
|
|
|
|
active_thread_names = {thread.name for thread in threading.enumerate()}
|
2020-10-16 17:24:56 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
for bash_id in list(self._bash.keys()):
|
|
|
|
thread_name, node_name = bash_id.rsplit("-", 1)
|
|
|
|
if thread_name not in active_thread_names:
|
|
|
|
self._bash[bash_id].__exit__(None, None, None)
|
|
|
|
del self._bash[bash_id]
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
return self._bash[id]
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
def close_bash(self, node):
|
|
|
|
current_thread = threading.current_thread()
|
|
|
|
id = f"{current_thread.name}-{node}"
|
|
|
|
|
|
|
|
with self.lock:
|
|
|
|
if self._bash.get(id) is None:
|
|
|
|
return
|
|
|
|
self._bash[id].__exit__(None, None, None)
|
|
|
|
del self._bash[id]
|
|
|
|
|
2020-06-29 12:55:27 +00:00
|
|
|
def __enter__(self):
|
|
|
|
with Given("docker-compose cluster"):
|
|
|
|
self.up()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
try:
|
|
|
|
with Finally("I clean up"):
|
|
|
|
self.down()
|
|
|
|
finally:
|
|
|
|
with self.lock:
|
2020-10-15 20:23:49 +00:00
|
|
|
for shell in self._bash.values():
|
2020-06-29 12:55:27 +00:00
|
|
|
shell.__exit__(type, value, traceback)
|
|
|
|
|
|
|
|
def node(self, name):
|
|
|
|
"""Get object with node bound methods.
|
|
|
|
:param name: name of service name
|
|
|
|
"""
|
|
|
|
if name.startswith("clickhouse"):
|
|
|
|
return ClickHouseNode(self, name)
|
|
|
|
return Node(self, name)
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
def down(self, timeout=300):
|
2020-06-29 12:55:27 +00:00
|
|
|
"""Bring cluster down by executing docker-compose down."""
|
2020-10-15 20:23:49 +00:00
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
# add message to each clickhouse-server.log
|
|
|
|
if settings.debug:
|
|
|
|
for node in self.nodes["clickhouse"]:
|
|
|
|
self.command(node=node, command=f"echo -e \"\n-- sending stop to: {node} --\n\" >> /var/log/clickhouse-server/clickhouse-server.log")
|
2020-06-29 12:55:27 +00:00
|
|
|
try:
|
|
|
|
bash = self.bash(None)
|
|
|
|
with self.lock:
|
|
|
|
# remove and close all not None node terminals
|
|
|
|
for id in list(self._bash.keys()):
|
|
|
|
shell = self._bash.pop(id)
|
|
|
|
if shell is not bash:
|
|
|
|
shell.__exit__(None, None, None)
|
|
|
|
else:
|
|
|
|
self._bash[id] = shell
|
|
|
|
finally:
|
2021-05-10 13:26:24 +00:00
|
|
|
cmd = self.command(None, f"{self.docker_compose} down --timeout 60", bash=bash, timeout=timeout)
|
|
|
|
with self.lock:
|
|
|
|
if self._control_shell:
|
|
|
|
self._control_shell.__exit__(None, None, None)
|
|
|
|
self._control_shell = None
|
|
|
|
return cmd
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2020-08-15 18:24:33 +00:00
|
|
|
def up(self, timeout=30*60):
|
2020-06-29 12:55:27 +00:00
|
|
|
if self.local:
|
|
|
|
with Given("I am running in local mode"):
|
|
|
|
with Then("check --clickhouse-binary-path is specified"):
|
|
|
|
assert self.clickhouse_binary_path, "when running in local mode then --clickhouse-binary-path must be specified"
|
|
|
|
with And("path should exist"):
|
|
|
|
assert os.path.exists(self.clickhouse_binary_path)
|
|
|
|
|
2020-08-15 18:24:33 +00:00
|
|
|
with And("I set all the necessary environment variables"):
|
2021-05-07 13:14:40 +00:00
|
|
|
self.environ["COMPOSE_HTTP_TIMEOUT"] = "300"
|
|
|
|
self.environ["CLICKHOUSE_TESTS_SERVER_BIN_PATH"] = self.clickhouse_binary_path
|
|
|
|
self.environ["CLICKHOUSE_TESTS_ODBC_BRIDGE_BIN_PATH"] = os.path.join(
|
2020-08-15 18:24:33 +00:00
|
|
|
os.path.dirname(self.clickhouse_binary_path), "clickhouse-odbc-bridge")
|
2021-05-07 13:14:40 +00:00
|
|
|
self.environ["CLICKHOUSE_TESTS_DIR"] = self.configs_dir
|
2020-06-29 12:55:27 +00:00
|
|
|
|
2020-08-15 18:24:33 +00:00
|
|
|
with And("I list environment variables to show their values"):
|
2020-06-29 12:55:27 +00:00
|
|
|
self.command(None, "env | grep CLICKHOUSE")
|
2020-08-15 18:24:33 +00:00
|
|
|
|
|
|
|
with Given("docker-compose"):
|
2020-08-26 14:00:12 +00:00
|
|
|
max_attempts = 5
|
2021-05-07 13:14:40 +00:00
|
|
|
max_up_attempts = 1
|
|
|
|
|
2020-08-26 14:00:12 +00:00
|
|
|
for attempt in range(max_attempts):
|
2020-08-26 23:13:16 +00:00
|
|
|
with When(f"attempt {attempt}/{max_attempts}"):
|
2020-08-26 14:00:12 +00:00
|
|
|
with By("pulling images for all the services"):
|
2020-08-26 23:13:16 +00:00
|
|
|
cmd = self.command(None, f"{self.docker_compose} pull 2>&1 | tee", exitcode=None, timeout=timeout)
|
2020-08-26 14:00:12 +00:00
|
|
|
if cmd.exitcode != 0:
|
|
|
|
continue
|
2021-05-07 13:14:40 +00:00
|
|
|
|
|
|
|
with And("checking if any containers are already running"):
|
|
|
|
self.command(None, f"{self.docker_compose} ps | tee")
|
|
|
|
|
2020-08-26 14:00:12 +00:00
|
|
|
with And("executing docker-compose down just in case it is up"):
|
2021-05-07 13:14:40 +00:00
|
|
|
cmd = self.command(None, f"{self.docker_compose} down 2>&1 | tee", exitcode=None, timeout=timeout)
|
2020-08-26 14:00:12 +00:00
|
|
|
if cmd.exitcode != 0:
|
|
|
|
continue
|
2021-05-07 13:14:40 +00:00
|
|
|
|
|
|
|
with And("checking if any containers are still left running"):
|
|
|
|
self.command(None, f"{self.docker_compose} ps | tee")
|
|
|
|
|
2020-08-26 14:00:12 +00:00
|
|
|
with And("executing docker-compose up"):
|
2021-05-07 13:14:40 +00:00
|
|
|
for up_attempt in range(max_up_attempts):
|
|
|
|
with By(f"attempt {up_attempt}/{max_up_attempts}"):
|
|
|
|
cmd = self.command(None, f"{self.docker_compose} up --renew-anon-volumes --force-recreate --timeout 300 -d 2>&1 | tee", timeout=timeout)
|
|
|
|
if "is unhealthy" not in cmd.output:
|
|
|
|
break
|
2020-08-26 14:00:12 +00:00
|
|
|
|
|
|
|
with Then("check there are no unhealthy containers"):
|
2021-05-07 13:14:40 +00:00
|
|
|
ps_cmd = self.command(None, f"{self.docker_compose} ps | tee | grep -v \"Exit 0\"")
|
|
|
|
if "is unhealthy" in cmd.output or "Exit" in ps_cmd.output:
|
2020-08-26 23:13:16 +00:00
|
|
|
self.command(None, f"{self.docker_compose} logs | tee")
|
2021-05-07 13:14:40 +00:00
|
|
|
continue
|
2020-08-26 14:00:12 +00:00
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
if cmd.exitcode == 0 and "is unhealthy" not in cmd.output and "Exit" not in ps_cmd.output:
|
2020-08-26 14:00:12 +00:00
|
|
|
break
|
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
if cmd.exitcode != 0 or "is unhealthy" in cmd.output or "Exit" in ps_cmd.output:
|
2020-08-26 14:00:12 +00:00
|
|
|
fail("could not bring up docker-compose cluster")
|
2020-06-29 12:55:27 +00:00
|
|
|
|
|
|
|
with Then("wait all nodes report healhy"):
|
|
|
|
for name in self.nodes["clickhouse"]:
|
|
|
|
self.node(name).wait_healthy()
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
def command(self, node, command, message=None, exitcode=None, steps=True, bash=None, *args, **kwargs):
|
2020-06-29 12:55:27 +00:00
|
|
|
"""Execute and check command.
|
|
|
|
:param node: name of the service
|
|
|
|
:param command: command
|
|
|
|
:param message: expected message that should be in the output, default: None
|
|
|
|
:param exitcode: expected exitcode, default: None
|
|
|
|
:param steps: don't break command into steps, default: True
|
|
|
|
"""
|
2020-10-15 20:23:49 +00:00
|
|
|
with By("executing command", description=command, format_description=False) if steps else NullStep():
|
|
|
|
if bash is None:
|
|
|
|
bash = self.bash(node)
|
|
|
|
try:
|
|
|
|
r = bash(command, *args, **kwargs)
|
|
|
|
except ExpectTimeoutError:
|
|
|
|
self.close_bash(node)
|
|
|
|
raise
|
2020-06-29 12:55:27 +00:00
|
|
|
if exitcode is not None:
|
2020-10-15 20:23:49 +00:00
|
|
|
with Then(f"exitcode should be {exitcode}", format_name=False) if steps else NullStep():
|
2020-06-29 12:55:27 +00:00
|
|
|
assert r.exitcode == exitcode, error(r.output)
|
|
|
|
if message is not None:
|
2020-10-15 20:23:49 +00:00
|
|
|
with Then(f"output should contain message", description=message, format_description=False) if steps else NullStep():
|
2020-06-29 12:55:27 +00:00
|
|
|
assert message in r.output, error(r.output)
|
|
|
|
return r
|