2023-05-30 08:16:38 +00:00
|
|
|
#!/usr/bin/env python3
|
2024-08-02 14:09:48 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# pylint: disable=broad-exception-raised
|
2023-05-30 08:16:38 +00:00
|
|
|
|
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
|
2024-08-02 12:03:27 +00:00
|
|
|
from helpers.cluster import run_and_check, is_port_free
|
2021-06-04 14:33:22 +00:00
|
|
|
from helpers.network import _NetworkManager
|
2017-05-19 18:54:05 +00:00
|
|
|
|
2022-12-26 21:46:17 +00:00
|
|
|
# This is a workaround for a problem with logging in pytest [1].
|
|
|
|
#
|
|
|
|
# [1]: https://github.com/pytest-dev/pytest/issues/5502
|
|
|
|
logging.raiseExceptions = False
|
2024-08-02 11:32:59 +00:00
|
|
|
PORTS_PER_WORKER = 50
|
2022-12-26 21:46:17 +00:00
|
|
|
|
|
|
|
|
2024-03-25 17:39:27 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def pdb_history(request):
|
|
|
|
"""
|
|
|
|
Fixture loads and saves pdb history to file, so it can be preserved between runs
|
|
|
|
"""
|
|
|
|
if request.config.getoption("--pdb"):
|
|
|
|
import readline # pylint:disable=import-outside-toplevel
|
|
|
|
import pdb # pylint:disable=import-outside-toplevel
|
|
|
|
|
|
|
|
def save_history():
|
|
|
|
readline.write_history_file(".pdb_history")
|
|
|
|
|
|
|
|
def load_history():
|
|
|
|
try:
|
|
|
|
readline.read_history_file(".pdb_history")
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
load_history()
|
|
|
|
pdb.Pdb.use_rawinput = True
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
save_history()
|
|
|
|
else:
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2023-07-16 06:07:50 +00:00
|
|
|
@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.
|
2023-08-02 09:08:47 +00:00
|
|
|
# NOTE: it is not inherited, so you may need to specify this in docker_compose_$SERVICE.yml
|
2023-08-08 19:23:18 +00:00
|
|
|
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)
|
|
|
|
)
|
2023-07-16 06:07:50 +00:00
|
|
|
|
|
|
|
|
2021-06-04 14:33:22 +00:00
|
|
|
@pytest.fixture(autouse=True, scope="session")
|
|
|
|
def cleanup_environment():
|
|
|
|
try:
|
2021-12-18 10:19:53 +00:00
|
|
|
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()
|
2022-03-22 16:39:58 +00:00
|
|
|
result = run_and_check(["docker ps | wc -l"], shell=True)
|
2021-06-09 13:53:16 +00:00
|
|
|
if int(result) > 1:
|
2021-12-18 10:19:53 +00:00
|
|
|
if int(os.environ.get("PYTEST_CLEANUP_CONTAINERS", 0)) != 1:
|
2022-03-22 16:39:58 +00:00
|
|
|
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),
|
2022-03-22 16:39:58 +00:00
|
|
|
)
|
2021-06-04 15:00:59 +00:00
|
|
|
else:
|
2021-06-09 13:53:16 +00:00
|
|
|
logging.debug("Trying to kill unstopped containers...")
|
2022-03-22 16:39:58 +00:00
|
|
|
run_and_check(
|
2024-02-27 15:02:30 +00:00
|
|
|
["docker kill $(docker container list --all --quiet)"],
|
2022-03-22 16:39:58 +00:00
|
|
|
shell=True,
|
|
|
|
nothrow=True,
|
|
|
|
)
|
|
|
|
run_and_check(
|
2024-02-27 15:02:30 +00:00
|
|
|
["docker rm $docker container list --all --quiet)"],
|
2022-03-22 16:39:58 +00:00
|
|
|
shell=True,
|
|
|
|
nothrow=True,
|
|
|
|
)
|
2021-06-09 13:53:16 +00:00
|
|
|
logging.debug("Unstopped containers killed")
|
2022-03-22 16:39:58 +00:00
|
|
|
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(
|
2023-07-13 18:38:17 +00:00
|
|
|
["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)
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2021-06-21 08:14:26 +00:00
|
|
|
yield
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2021-06-21 08:14:26 +00:00
|
|
|
def pytest_addoption(parser):
|
2022-03-22 16:39:58 +00:00
|
|
|
parser.addoption(
|
|
|
|
"--run-id",
|
|
|
|
default="",
|
|
|
|
help="run-id is used as postfix in _instances_{} directory",
|
|
|
|
)
|
|
|
|
|
2021-06-21 08:14:26 +00:00
|
|
|
|
2024-08-02 12:03:27 +00:00
|
|
|
def get_unique_free_ports(total):
|
2024-08-02 11:32:59 +00:00
|
|
|
ports = []
|
2024-08-02 12:03:27 +00:00
|
|
|
for port in range(30000, 55000):
|
|
|
|
if is_port_free(port) and port not in ports:
|
|
|
|
ports.append(port)
|
2024-08-02 11:32:59 +00:00
|
|
|
|
2024-08-02 12:03:27 +00:00
|
|
|
if len(ports) == total:
|
|
|
|
return ports
|
2024-08-02 11:32:59 +00:00
|
|
|
|
2024-08-02 12:03:27 +00:00
|
|
|
raise Exception(f"Can't collect {total} ports. Collected: {len(ports)}")
|
2024-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
|
2021-06-21 08:14:26 +00:00
|
|
|
def pytest_configure(config):
|
2022-03-22 16:39:58 +00:00
|
|
|
os.environ["INTEGRATION_TESTS_RUN_ID"] = config.option.run_id
|
2024-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
# When running tests without pytest-xdist,
|
|
|
|
# the `pytest_xdist_setupnodes` hook is not executed
|
|
|
|
worker_ports = os.getenv("WORKER_FREE_PORTS", None)
|
|
|
|
if worker_ports is None:
|
2024-08-02 12:03:27 +00:00
|
|
|
master_ports = get_unique_free_ports(PORTS_PER_WORKER)
|
2024-08-02 12:36:33 +00:00
|
|
|
os.environ["WORKER_FREE_PORTS"] = " ".join([str(p) for p in master_ports])
|
2024-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_xdist_setupnodes(config, specs):
|
|
|
|
# Find {PORTS_PER_WORKER} * {number of xdist workers} ports and
|
|
|
|
# allocate pool of {PORTS_PER_WORKER} ports to each worker
|
|
|
|
|
|
|
|
# Get number of xdist workers
|
2024-08-02 12:03:27 +00:00
|
|
|
num_workers = len(specs)
|
2024-08-02 11:32:59 +00:00
|
|
|
# Get free ports which will be distributed across workers
|
2024-08-02 12:03:27 +00:00
|
|
|
ports = get_unique_free_ports(num_workers * PORTS_PER_WORKER)
|
2024-08-02 11:32:59 +00:00
|
|
|
|
|
|
|
# Iterate over specs of workers and add allocated ports to env variable
|
|
|
|
for i, spec in enumerate(specs):
|
|
|
|
start_range = i * PORTS_PER_WORKER
|
2024-08-02 12:03:27 +00:00
|
|
|
per_workrer_ports = ports[start_range : start_range + PORTS_PER_WORKER]
|
2024-08-02 12:36:33 +00:00
|
|
|
spec.env["WORKER_FREE_PORTS"] = " ".join([str(p) for p in per_workrer_ports])
|