1717import pytest
1818
1919
20+ @pytest .fixture (scope = "module" )
21+ def cuda_driver (tmp_path_factory : pytest .TempPathFactory ) -> Path :
22+ """Exercise the real dynamic loader without depending on the test host GPU."""
23+ compiler = shutil .which ("cc" )
24+ if platform .system () != "Linux" or compiler is None :
25+ pytest .skip ("CUDA driver probe tests require Linux and a C compiler" )
26+ assert compiler is not None
27+ directory = tmp_path_factory .mktemp ("cuda compat" )
28+ subprocess .run (
29+ [compiler , "-shared" , "-fPIC" , "-x" , "c" , "-" , "-o" , str (directory / "libcuda.so.1" )],
30+ input = """
31+ #include <stdlib.h>
32+ static int value(const char *name, int fallback) {
33+ const char *text = getenv(name);
34+ return text ? atoi(text) : fallback;
35+ }
36+ int cuDriverGetVersion(int *version) {
37+ *version = value("TEST_DRIVER_API", 13020);
38+ return value("TEST_DRIVER_STATUS", 0);
39+ }
40+ int cuInit(unsigned int flags) {
41+ return value("TEST_INIT_STATUS", 0);
42+ }
43+ int cuDeviceGetCount(int *count) {
44+ *count = value("TEST_DEVICE_COUNT", 1);
45+ return value("TEST_COUNT_STATUS", 0);
46+ }
47+ """ ,
48+ text = True ,
49+ check = True ,
50+ capture_output = True ,
51+ )
52+ return directory
53+
54+
2055@pytest .fixture
2156def installer_environment (tmp_path : Path ) -> dict [str , str ]:
2257 commands = {
@@ -47,6 +82,8 @@ def installer_environment(tmp_path: Path) -> dict[str, str]:
4782 "TEST_MACHINE" : "aarch64" ,
4883 "TEST_COMPUTE_CAPS" : "11.0" ,
4984 "TEST_CUDA_VERSION" : "13.2" ,
85+ "SONAR_ENABLE_CUDA_COMPATIBILITY" : "0" ,
86+ "SONAR_CUDA_COMPATIBILITY_PATH" : "" ,
5087 }
5188
5289
@@ -127,6 +164,7 @@ def test_installer_validates_cuda_platform(
127164 tmp_path : Path ,
128165 installer_environment : dict [str , str ],
129166 machine : str ,
167+ cuda_driver : Path ,
130168 os_name : str ,
131169 cuda_version : str ,
132170 compute_caps : str ,
@@ -138,6 +176,7 @@ def test_installer_validates_cuda_platform(
138176 TEST_CUDA_VERSION = cuda_version ,
139177 TEST_COMPUTE_CAPS = compute_caps ,
140178 )
179+ installer_environment .update (LD_LIBRARY_PATH = str (cuda_driver ), TEST_DRIVER_API = "13000" )
141180 result = _installer_dry_run (tmp_path , installer_environment , "cuda" )
142181
143182 if error is None :
@@ -150,6 +189,66 @@ def test_installer_validates_cuda_platform(
150189 assert not (tmp_path / "venv" ).exists ()
151190
152191
192+ @pytest .mark .parametrize ("explicit_path" , [False , True ])
193+ @pytest .mark .parametrize (
194+ "overrides,error" ,
195+ [
196+ ({}, None ),
197+ ({"TEST_DRIVER_API" : "13000" }, "requires CUDA 13.2" ),
198+ ({"TEST_DRIVER_STATUS" : "999" }, "cuDriverGetVersion failed" ),
199+ ({"TEST_INIT_STATUS" : "803" }, "cuInit failed" ),
200+ ({"TEST_DEVICE_COUNT" : "0" }, "exposes no devices" ),
201+ ({"TEST_COUNT_STATUS" : "100" }, "cuDeviceGetCount failed" ),
202+ ({"TEST_COMPUTE_CAPS" : "8.7" }, "compute capability: 8.7" ),
203+ ],
204+ )
205+ def test_installer_probes_compatibility_driver (
206+ tmp_path : Path , installer_environment : dict [str , str ], cuda_driver : Path , explicit_path : bool , overrides , error
207+ ) -> None :
208+ installer_environment .update (
209+ TEST_CUDA_VERSION = "13.0" ,
210+ LD_LIBRARY_PATH = str (tmp_path / "unused" ),
211+ SONAR_ENABLE_CUDA_COMPATIBILITY = "true" if explicit_path else "0" ,
212+ SONAR_CUDA_COMPATIBILITY_PATH = str (cuda_driver ),
213+ )
214+ if not explicit_path :
215+ installer_environment ["LD_LIBRARY_PATH" ] = str (cuda_driver )
216+ installer_environment .update (overrides )
217+ result = _installer_dry_run (tmp_path , installer_environment , "cuda" )
218+ if error is None :
219+ assert result .returncode == 0 , result .stdout + result .stderr
220+ assert "https://download.pytorch.org/whl/cu132" in result .stdout
221+ export = next (line .strip () for line in result .stdout .splitlines () if "export LD_LIBRARY_PATH=" in line )
222+ subprocess .run (
223+ [
224+ "sh" ,
225+ "-c" ,
226+ f"{ export } ; exec python3 -c 'import ctypes; assert ctypes.CDLL(\" libcuda.so.1\" ).cuInit(0) == 0'" ,
227+ ],
228+ env = {** installer_environment , "LD_LIBRARY_PATH" : "" },
229+ capture_output = True ,
230+ text = True ,
231+ check = True ,
232+ )
233+ else :
234+ assert result .returncode != 0 , result .stdout + result .stderr
235+ assert error in result .stderr
236+ assert "https://sonar.invalid/whl/" not in result .stdout
237+ assert not (tmp_path / "venv" ).exists ()
238+
239+
240+ def test_installer_rejects_missing_explicit_compatibility_library (tmp_path : Path , installer_environment ) -> None :
241+ installer_environment .update (
242+ TEST_CUDA_VERSION = "13.0" ,
243+ SONAR_ENABLE_CUDA_COMPATIBILITY = "1" ,
244+ SONAR_CUDA_COMPATIBILITY_PATH = str (tmp_path / "missing" ),
245+ )
246+ result = _installer_dry_run (tmp_path , installer_environment , "cuda" )
247+ assert result .returncode != 0
248+ assert "CUDA compatibility library not found" in result .stderr
249+ assert not (tmp_path / "venv" ).exists ()
250+
251+
153252def _wheel (name : str , version : str , * , dependency : bool = False ) -> tuple [str , bytes ]:
154253 module = name .replace ("-" , "_" )
155254 dist_info = f"{ module } -{ version } .dist-info"
0 commit comments