| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | import os |
| 20 | import unittest |
| 21 | from unittest import mock |
| 22 | |
| 23 | from src.service import http_app |
| 24 | |
| 25 | |
| 26 | class CudaStatusTests(unittest.TestCase): |
| 27 | |
| 28 | |
| 29 | def test_reports_the_required_fields(self): |
| 30 | d = http_app.cuda_status() |
| 31 | for alan in ("available", "device_count", "required"): |
| 32 | self.assertIn(alan, d) |
| 33 | self.assertIsInstance(d["available"], bool) |
| 34 | |
| 35 | def test_missing_torch_does_not_crash(self): |
| 36 | with mock.patch.dict("sys.modules", {"torch": None}): |
| 37 | d = http_app.cuda_status() |
| 38 | self.assertFalse(d["available"]) |
| 39 | self.assertIn("reason", d) |
| 40 | |
| 41 | def test_broken_torch_does_not_crash(self): |
| 42 | sahte = mock.MagicMock() |
| 43 | sahte.cuda.is_available.side_effect = RuntimeError("driver mismatch") |
| 44 | with mock.patch.dict("sys.modules", {"torch": sahte}): |
| 45 | d = http_app.cuda_status() |
| 46 | self.assertFalse(d["available"]) |
| 47 | self.assertIn("driver mismatch", d["reason"]) |
| 48 | |
| 49 | def test_device_name_only_when_available(self): |
| 50 | sahte = mock.MagicMock() |
| 51 | sahte.cuda.is_available.return_value = False |
| 52 | with mock.patch.dict("sys.modules", {"torch": sahte}): |
| 53 | d = http_app.cuda_status() |
| 54 | self.assertEqual(d["device_count"], 0) |
| 55 | self.assertEqual(d.get("device_name", ""), "") |
| 56 | |
| 57 | |
| 58 | class RequireCudaTests(unittest.TestCase): |
| 59 | def test_disabled_by_default_is_a_no_op(self): |
| 60 | |
| 61 | |
| 62 | with mock.patch.object(http_app, "REQUIRE_CUDA", False), \ |
| 63 | mock.patch.object(http_app, "cuda_status", |
| 64 | return_value={"available": False}): |
| 65 | http_app.require_cuda_or_fail() |
| 66 | |
| 67 | def test_raises_when_required_but_missing(self): |
| 68 | with mock.patch.object(http_app, "REQUIRE_CUDA", True), \ |
| 69 | mock.patch.object(http_app, "cuda_status", |
| 70 | return_value={"available": False, |
| 71 | "reason": "torch yok"}): |
| 72 | with self.assertRaises(RuntimeError) as ctx: |
| 73 | http_app.require_cuda_or_fail() |
| 74 | self.assertIn("RAG_REQUIRE_CUDA", str(ctx.exception)) |
| 75 | |
| 76 | def test_the_error_names_all_three_causes(self): |
| 77 | |
| 78 | |
| 79 | with mock.patch.object(http_app, "REQUIRE_CUDA", True), \ |
| 80 | mock.patch.object(http_app, "cuda_status", |
| 81 | return_value={"available": False, |
| 82 | "torch_cuda_build": None}): |
| 83 | try: |
| 84 | http_app.require_cuda_or_fail() |
| 85 | except RuntimeError as e: |
| 86 | metin = str(e) |
| 87 | for ipucu in ("TORCH_INDEX", "nvidia-container-toolkit", |
| 88 | "nvidia.com/gpu=all"): |
| 89 | self.assertIn(ipucu, metin, ipucu) |
| 90 | self.assertIn("96 s", metin, "bedeli sayiyla soylenmeli") |
| 91 | |
| 92 | def test_passes_when_cuda_is_present(self): |
| 93 | with mock.patch.object(http_app, "REQUIRE_CUDA", True), \ |
| 94 | mock.patch.object(http_app, "cuda_status", |
| 95 | return_value={"available": True, |
| 96 | "device_count": 1}): |
| 97 | http_app.require_cuda_or_fail() |
| 98 | |
| 99 | def test_env_parsing_accepts_common_spellings(self): |
| 100 | import importlib |
| 101 | for deger, beklenen in (("1", True), ("true", True), ("TRUE", True), |
| 102 | ("yes", True), ("on", True), ("0", False), |
| 103 | ("", False), ("hayir", False)): |
| 104 | with self.subTest(deger=deger): |
| 105 | with mock.patch.dict(os.environ, {"RAG_REQUIRE_CUDA": deger}): |
| 106 | mod = importlib.reload(http_app) |
| 107 | self.assertEqual(mod.REQUIRE_CUDA, beklenen) |
| 108 | |
| 109 | importlib.reload(http_app) |
| 110 | |
| 111 | |
| 112 | class BuildServiceGuardTests(unittest.TestCase): |
| 113 | |
| 114 | |
| 115 | |
| 116 | def test_build_service_checks_before_loading_models(self): |
| 117 | import inspect |
| 118 | kaynak = inspect.getsource(http_app.build_service) |
| 119 | kontrol = kaynak.index("require_cuda_or_fail()") |
| 120 | |
| 121 | |
| 122 | for agir in ("build_canonical(", "build_embedder(", "build_reranker("): |
| 123 | self.assertLess(kontrol, kaynak.index(agir), |
| 124 | f"CUDA kontrolu {agir} sonrasinda") |
| 125 | |
| 126 | |
| 127 | class ConfigWarningTests(unittest.TestCase): |
| 128 | def test_cpu_without_require_flag_warns_with_numbers(self): |
| 129 | |
| 130 | |
| 131 | with mock.patch.object(http_app, "REQUIRE_CUDA", False), \ |
| 132 | mock.patch.object(http_app, "cuda_status", |
| 133 | return_value={"available": False}): |
| 134 | uyarilar = " ".join(http_app._yapilandirma_uyarilari()) |
| 135 | self.assertIn("CUDA yok", uyarilar) |
| 136 | self.assertIn("96 s", uyarilar) |
| 137 | |
| 138 | def test_no_cuda_warning_when_gpu_is_present(self): |
| 139 | with mock.patch.object(http_app, "REQUIRE_CUDA", False), \ |
| 140 | mock.patch.object(http_app, "cuda_status", |
| 141 | return_value={"available": True}): |
| 142 | uyarilar = " ".join(http_app._yapilandirma_uyarilari()) |
| 143 | self.assertNotIn("CUDA yok", uyarilar) |
| 144 | |
| 145 | |
| 146 | class ReadyEndpointTests(unittest.TestCase): |
| 147 | def test_ready_reports_cuda(self): |
| 148 | from fastapi.testclient import TestClient |
| 149 | |
| 150 | class _Svc: |
| 151 | generator = object() |
| 152 | doc = None |
| 153 | question_gen = None |
| 154 | |
| 155 | c = TestClient(http_app.create_app(_Svc())) |
| 156 | d = c.get("/ready").json() |
| 157 | self.assertIn("cuda", d) |
| 158 | self.assertIn("available", d["cuda"]) |
| 159 | |
| 160 | |
| 161 | if __name__ == "__main__": |
| 162 | unittest.main() |