Files
Alexey MilovidovandClaude Opus 4.8 295ced78d1 Enable more Ruff checks (F401/F403/F405/F541/F841) and clean up tests
Follow-up to #105171, which added `ruff` with a deliberately narrow,
bug-class-only rule set and left the unused-name and star-import rules
off pending a dedicated cleanup pass. This enables that second batch in
`pyproject.toml` and fixes every violation across the tree (the bulk in
integration tests):

- `F401` unused-import and `F841` unused-variable: removed, preserving
  any side-effecting right-hand side (e.g. `result = node.query(...)`
  becomes `node.query(...)`). Two `try: import ...` feature probes are
  kept with `# noqa: F401`.
- `F541` f-string-missing-placeholders: dropped the redundant `f` prefix.
- `F403`/`F405` star imports: every `from module import *` is expanded
  into explicit imports of the names actually used, taken from each
  name's real origin, so stdlib/third-party names import from their real
  source rather than through a test "convenience bundle". The now-empty
  `tests/integration/helpers/kafka/common_direct.py` bundle is removed and
  its three remaining consumers import `_VarintBytes`/`KafkaAdminClient`
  directly.

Eliminating the star imports surfaced a real bug the wildcard had masked
from `F821`: `generate_presets.py` in `test_prometheus_protocols` called
an undefined `execute_instant_query_with_http_api`; fixed to the existing
`execute_query_via_http_api`.

No `black`/`isort` reformatting was applied (neither is enforced in the
style check), so the diff is limited to the lint fixes themselves.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-06-18 23:22:25 +00:00

126 lines
3.7 KiB
Python

#!/usr/bin/env python3
# pylint: disable=unused-argument
# pylint: disable=broad-exception-raised
import logging
import os
from pathlib import Path
import pytest # pylint:disable=import-error; for style check
from helpers.cluster import run_and_check
# This is a workaround for a problem with logging in pytest [1].
#
# [1]: https://github.com/pytest-dev/pytest/issues/5502
logging.raiseExceptions = False
_ENV_FILE = Path(__file__).resolve().parent / ".env"
if _ENV_FILE.is_file():
from dotenv import load_dotenv
load_dotenv(dotenv_path=_ENV_FILE, override=False)
PORTS_PER_WORKER = 50
@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 pdb # pylint:disable=import-outside-toplevel
import readline # 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
@pytest.fixture(autouse=True, scope="session")
def tune_local_port_range():
# Lots of services uses non privileged ports:
# - 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)
)
@pytest.fixture(autouse=True, scope="session")
def cleanup_environment():
try:
result = run_and_check(["docker ps | wc -l"], shell=True)
if int(result) > 1:
if int(os.environ.get("PYTEST_CLEANUP_CONTAINERS", 0)) != 1:
logging.warning(
"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),
)
else:
logging.debug("Trying to kill unstopped containers...")
run_and_check(
["docker kill $(docker container list --all --quiet)"],
shell=True,
nothrow=True,
)
run_and_check(
["docker rm $docker container list --all --quiet)"],
shell=True,
nothrow=True,
)
logging.debug("Unstopped containers killed")
else:
logging.debug("No running containers")
logging.debug("Pruning Docker networks")
run_and_check(
["docker network prune --force"],
shell=True,
nothrow=True,
)
except Exception as e:
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
if hasattr(pytest, "xdist_plugin"):
def pytest_xdist_setupnodes(config, specs):
pass