Skip to content

Commit 11eed52

Browse files
committed
use rich to handle logging
1 parent 974c0d1 commit 11eed52

4 files changed

Lines changed: 241 additions & 198 deletions

File tree

e2e-tests/conftest.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@
1111
import pytest
1212
import yaml
1313
from lib import k8s_collector, tools
14-
14+
from rich.console import Console
15+
from rich.highlighter import NullHighlighter
16+
from rich.logging import RichHandler
17+
18+
logging.basicConfig(
19+
level=logging.INFO,
20+
format="%(message)s",
21+
handlers=[
22+
RichHandler(
23+
console=Console(highlight=False),
24+
show_time=True,
25+
show_path=False,
26+
markup=True,
27+
highlighter=NullHighlighter(),
28+
keywords=[],
29+
log_time_format="[%X.%f]",
30+
)
31+
],
32+
)
1533
logging.getLogger("pytest_dependency").setLevel(logging.WARNING)
1634
logger = logging.getLogger(__name__)
1735

e2e-tests/lib/tools.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515

1616
logger = logging.getLogger(__name__)
1717

18-
RED = "\033[31m"
19-
GREEN = "\033[32m"
20-
YELLOW = "\033[33m"
21-
BLUE = "\033[34m"
22-
MAGENTA = "\033[35m"
23-
CYAN = "\033[36m"
24-
RESET = "\033[0m"
25-
2618

2719
def kubectl_bin(*args: str, check: bool = True, input_data: str = "") -> str:
2820
"""Execute kubectl command"""
@@ -289,7 +281,7 @@ def clean_all_namespaces() -> None:
289281
def wait_pod(pod_name: str, timeout: int = 360) -> None:
290282
"""Wait for pod to be ready."""
291283
start_time = time.time()
292-
logger.info(f"Waiting for {CYAN}pod/{pod_name}{RESET} to be ready...")
284+
logger.info(f"Waiting for [cyan]pod/{pod_name}[/cyan] to be ready...")
293285
while time.time() - start_time < timeout:
294286
try:
295287
result = kubectl_bin(
@@ -300,7 +292,7 @@ def wait_pod(pod_name: str, timeout: int = 360) -> None:
300292
"jsonpath={.status.conditions[?(@.type=='Ready')].status}",
301293
).strip("'")
302294
if result == "True":
303-
logger.info(f"Pod {CYAN}{pod_name}{RESET} is ready")
295+
logger.info(f"Pod [cyan]{pod_name}[/cyan] is ready")
304296
return
305297
except subprocess.CalledProcessError:
306298
pass
@@ -351,14 +343,14 @@ def wait_for_running(
351343
cluster_name = cluster_name.replace(f"-{rs_name}", "")
352344
if check_cluster_readyness:
353345
start_time = time.time()
354-
logger.info(f"Waiting for cluster {CYAN}{cluster_name}{RESET} readiness")
346+
logger.info(f"Waiting for cluster [cyan]{cluster_name}[/cyan] readiness")
355347
while time.time() - start_time < timeout:
356348
try:
357349
state = kubectl_bin(
358350
"get", "psmdb", cluster_name, "-o", "jsonpath={.status.state}"
359351
).strip("'")
360352
if state == "ready":
361-
logger.info(f"Cluster {CYAN}{cluster_name}{RESET} is ready")
353+
logger.info(f"Cluster [cyan]{cluster_name}[/cyan] is ready")
362354
return
363355
except subprocess.CalledProcessError:
364356
pass
@@ -381,7 +373,7 @@ def wait_for_running(
381373

382374
def wait_for_delete(resource: str, timeout: int = 180) -> None:
383375
"""Wait for a specific resource to be deleted"""
384-
logger.info(f"Waiting for {CYAN}{resource}{RESET} to be deleted")
376+
logger.info(f"Waiting for [cyan]{resource}[/cyan] to be deleted")
385377
time.sleep(1)
386378
try:
387379
kubectl_bin("wait", "--for=delete", resource, f"--timeout={timeout}s")
@@ -788,12 +780,12 @@ def compare_mongo_cmd(
788780
if sort:
789781
full_cmd = f"{collection}.{command}.{sort}"
790782

791-
logger.info(f"Running: {CYAN}{full_cmd}{RESET} on db {CYAN}{database}{RESET}")
783+
logger.info(f"Running: [cyan]{full_cmd}[/cyan] on db [cyan]{database}[/cyan]")
792784

793785
mongo_expr = f"EJSON.stringify(db.getSiblingDB('{database}').{full_cmd})"
794786
result = json.loads(self.run_mongosh(mongo_expr, uri, "mongodb"))
795787

796-
logger.info(f"MongoDB output: {CYAN}{result}{RESET}")
788+
logger.info(f"MongoDB output: [cyan]{result}[/cyan]")
797789

798790
with open(test_file) as file:
799791
expected = json.load(file)

pyproject.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ dependencies = [
1111
"pytest-html>=4.1.1",
1212
"pytest-json-report>=1.5.0",
1313
"pyyaml>=6.0.2",
14+
"rich>=14.0.0",
1415
"ruff>=0.11.12",
1516
"types-pyyaml>=6.0.12.20250915",
1617
]
1718

1819
[tool.pytest.ini_options]
19-
log_cli = true
20-
log_level = "INFO"
21-
log_format = "%(levelname)s %(asctime)s %(message)s"
22-
log_date_format = "%Y-%m-%dT%H:%M:%SZ"
23-
addopts = "--html=e2e-tests/reports/report.html --self-contained-html --json-report --json-report-file=e2e-tests/reports/report.json --junitxml=e2e-tests/reports/report.xml"
20+
addopts = "-s --html=e2e-tests/reports/report.html --self-contained-html --json-report --json-report-file=e2e-tests/reports/report.json --junitxml=e2e-tests/reports/report.xml"
2421
render_collapsed = "all"
2522

2623
[[tool.mypy.overrides]]

0 commit comments

Comments
 (0)