|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from collections.abc import Iterator |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +import ai.backend.common.metrics.multiprocess as mp_mod |
| 11 | +from ai.backend.common.metrics.multiprocess import ( |
| 12 | + cleanup_prometheus_multiprocess_dir, |
| 13 | + generate_latest_multiprocess, |
| 14 | + setup_prometheus_multiprocess_dir, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(autouse=True) |
| 19 | +def _reset_multiprocess_state() -> Iterator[None]: |
| 20 | + """Reset the module-level global state before each test.""" |
| 21 | + original_dir = mp_mod._multiprocess_dir |
| 22 | + original_env = os.environ.get("PROMETHEUS_MULTIPROC_DIR") |
| 23 | + yield |
| 24 | + mp_mod._multiprocess_dir = original_dir |
| 25 | + if original_env is not None: |
| 26 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = original_env |
| 27 | + elif "PROMETHEUS_MULTIPROC_DIR" in os.environ: |
| 28 | + del os.environ["PROMETHEUS_MULTIPROC_DIR"] |
| 29 | + |
| 30 | + |
| 31 | +class TestSetupPrometheusMultiprocDir: |
| 32 | + def test_creates_directory_with_default_base(self, tmp_path: Path) -> None: |
| 33 | + with patch.object(mp_mod, "_DEFAULT_BASE_DIR", tmp_path): |
| 34 | + result = setup_prometheus_multiprocess_dir("manager") |
| 35 | + |
| 36 | + assert result == tmp_path / "manager" |
| 37 | + assert result.is_dir() |
| 38 | + assert os.environ["PROMETHEUS_MULTIPROC_DIR"] == str(result) |
| 39 | + |
| 40 | + def test_cleans_stale_db_files(self, tmp_path: Path) -> None: |
| 41 | + prom_dir = tmp_path / "manager" |
| 42 | + prom_dir.mkdir(parents=True) |
| 43 | + (prom_dir / "gauge_liveall_123.db").touch() |
| 44 | + (prom_dir / "counter_456.db").touch() |
| 45 | + (prom_dir / "keep_this.txt").touch() |
| 46 | + |
| 47 | + with patch.object(mp_mod, "_DEFAULT_BASE_DIR", tmp_path): |
| 48 | + setup_prometheus_multiprocess_dir("manager") |
| 49 | + |
| 50 | + assert not (prom_dir / "gauge_liveall_123.db").exists() |
| 51 | + assert not (prom_dir / "counter_456.db").exists() |
| 52 | + assert (prom_dir / "keep_this.txt").exists() |
| 53 | + |
| 54 | + def test_idempotent_returns_same_path(self, tmp_path: Path) -> None: |
| 55 | + with patch.object(mp_mod, "_DEFAULT_BASE_DIR", tmp_path): |
| 56 | + first = setup_prometheus_multiprocess_dir("manager") |
| 57 | + second = setup_prometheus_multiprocess_dir("manager") |
| 58 | + |
| 59 | + assert first == second |
| 60 | + |
| 61 | + def test_idempotent_ignores_different_component(self, tmp_path: Path) -> None: |
| 62 | + """Once setup, calling again with different component still returns the first path.""" |
| 63 | + with patch.object(mp_mod, "_DEFAULT_BASE_DIR", tmp_path): |
| 64 | + first = setup_prometheus_multiprocess_dir("manager") |
| 65 | + second = setup_prometheus_multiprocess_dir("agent") |
| 66 | + |
| 67 | + assert first == second # idempotent, returns first result |
| 68 | + |
| 69 | + |
| 70 | +class TestGenerateLatestMultiprocess: |
| 71 | + def test_returns_bytes_normally(self, tmp_path: Path) -> None: |
| 72 | + prom_dir = tmp_path / "test-component" |
| 73 | + prom_dir.mkdir(parents=True) |
| 74 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(prom_dir) |
| 75 | + mp_mod._multiprocess_dir = prom_dir |
| 76 | + |
| 77 | + result = generate_latest_multiprocess() |
| 78 | + assert isinstance(result, bytes) |
| 79 | + |
| 80 | + def test_recovers_from_missing_directory(self, tmp_path: Path) -> None: |
| 81 | + prom_dir = tmp_path / "test-component" |
| 82 | + prom_dir.mkdir(parents=True) |
| 83 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(prom_dir) |
| 84 | + mp_mod._multiprocess_dir = prom_dir |
| 85 | + |
| 86 | + # Simulate systemd-tmpfiles-clean deleting the directory |
| 87 | + prom_dir.rmdir() |
| 88 | + assert not prom_dir.exists() |
| 89 | + |
| 90 | + result = generate_latest_multiprocess() |
| 91 | + # Should recover by recreating the directory |
| 92 | + assert isinstance(result, bytes) |
| 93 | + assert prom_dir.exists() |
| 94 | + |
| 95 | + def test_returns_empty_bytes_on_unrecoverable_failure(self) -> None: |
| 96 | + # Set an invalid path that can't be recreated |
| 97 | + mp_mod._multiprocess_dir = Path("/nonexistent/impossible/path") |
| 98 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = "/nonexistent/impossible/path" |
| 99 | + |
| 100 | + result = generate_latest_multiprocess() |
| 101 | + assert result == b"" |
| 102 | + |
| 103 | + |
| 104 | +class TestCleanupPrometheusMultiprocDir: |
| 105 | + def test_removes_directory_and_env(self, tmp_path: Path) -> None: |
| 106 | + prom_dir = tmp_path / "manager" |
| 107 | + prom_dir.mkdir(parents=True) |
| 108 | + (prom_dir / "test.db").touch() |
| 109 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(prom_dir) |
| 110 | + mp_mod._multiprocess_dir = prom_dir |
| 111 | + |
| 112 | + cleanup_prometheus_multiprocess_dir() |
| 113 | + |
| 114 | + assert not prom_dir.exists() |
| 115 | + assert "PROMETHEUS_MULTIPROC_DIR" not in os.environ |
| 116 | + assert mp_mod._multiprocess_dir is None |
| 117 | + |
| 118 | + def test_noop_when_not_initialized(self) -> None: |
| 119 | + mp_mod._multiprocess_dir = None |
| 120 | + if "PROMETHEUS_MULTIPROC_DIR" in os.environ: |
| 121 | + del os.environ["PROMETHEUS_MULTIPROC_DIR"] |
| 122 | + |
| 123 | + # Should not raise |
| 124 | + cleanup_prometheus_multiprocess_dir() |
0 commit comments