ClickHouse/tests/integration/conftest.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3.0 KiB
Python
Raw Normal View History

2023-05-30 08:16:38 +00:00
#!/usr/bin/env python3
2021-06-04 14:33:22 +00:00
import logging
2021-06-09 13:53:16 +00:00
import os
2024-02-27 15:02:30 +00:00
import pytest # pylint:disable=import-error; for style check
2023-05-30 08:16:38 +00:00
from helpers.cluster import run_and_check
2021-06-04 14:33:22 +00:00
from helpers.network import _NetworkManager
# This is a workaround for a problem with logging in pytest [1].
#
# [1]: https://github.com/pytest-dev/pytest/issues/5502
logging.raiseExceptions = False
@pytest.fixture(autouse=True, scope="session")
def tune_local_port_range():
# Lots of services uses non privileged ports:
# - hdfs -- 50020/50070/...
# - minio
#
# NOTE: 5K is not enough, and sometimes leads to EADDRNOTAVAIL error.
# NOTE: it is not inherited, so you may need to specify this in docker_compose_$SERVICE.yml
try:
run_and_check(["sysctl net.ipv4.ip_local_port_range='55000 65535'"], shell=True)
except Exception as ex:
logging.warning(
"Failed to run sysctl, tests may fail with EADDRINUSE %s", str(ex)
)
2021-06-04 14:33:22 +00:00
@pytest.fixture(autouse=True, scope="session")
def cleanup_environment():
try:
if int(os.environ.get("PYTEST_CLEANUP_CONTAINERS", 0)) == 1:
2024-02-27 15:02:30 +00:00
logging.debug("Cleaning all iptables rules")
2021-11-19 09:48:08 +00:00
_NetworkManager.clean_all_user_iptables_rules()
result = run_and_check(["docker ps | wc -l"], shell=True)
2021-06-09 13:53:16 +00:00
if int(result) > 1:
if int(os.environ.get("PYTEST_CLEANUP_CONTAINERS", 0)) != 1:
logging.warning(
2024-02-27 15:02:30 +00:00
"Docker containters(%s) are running before tests run. "
"They can be left from previous pytest run and cause test failures.\n"
"You can set env PYTEST_CLEANUP_CONTAINERS=1 or use runner with "
"--cleanup-containers argument to enable automatic containers cleanup.",
int(result),
)
2021-06-04 15:00:59 +00:00
else:
2021-06-09 13:53:16 +00:00
logging.debug("Trying to kill unstopped containers...")
run_and_check(
2024-02-27 15:02:30 +00:00
["docker kill $(docker container list --all --quiet)"],
shell=True,
nothrow=True,
)
run_and_check(
2024-02-27 15:02:30 +00:00
["docker rm $docker container list --all --quiet)"],
shell=True,
nothrow=True,
)
2021-06-09 13:53:16 +00:00
logging.debug("Unstopped containers killed")
r = run_and_check(["docker-compose", "ps", "--services", "--all"])
2024-02-27 15:02:30 +00:00
logging.debug("Docker ps before start:%s", r.stdout)
2021-06-09 13:53:16 +00:00
else:
2024-02-27 15:02:30 +00:00
logging.debug("No running containers")
2023-07-08 04:26:44 +00:00
logging.debug("Pruning Docker networks")
run_and_check(
["docker network prune --force"],
2023-07-08 04:26:44 +00:00
shell=True,
nothrow=True,
)
2021-06-04 15:00:59 +00:00
except Exception as e:
2024-02-27 15:02:30 +00:00
logging.exception("cleanup_environment:%s", e)
yield
def pytest_addoption(parser):
parser.addoption(
"--run-id",
default="",
help="run-id is used as postfix in _instances_{} directory",
)
def pytest_configure(config):
os.environ["INTEGRATION_TESTS_RUN_ID"] = config.option.run_id