Skip to content

Commit dcafe04

Browse files
committed
cuda.bindings.nvml: Correctly handle a test requiring root
1 parent 3257477 commit dcafe04

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cuda_bindings/tests/nvml/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

4+
import os
45
from collections import namedtuple
6+
from contextlib import contextmanager
57

68
import pytest
79
from cuda.bindings import _nvml as nvml
@@ -128,3 +130,12 @@ def pci_info(ngpus, handles):
128130
pci_info = [nvml.device_get_pci_info_v3(handles[i]) for i in range(ngpus)]
129131
assert len(pci_info) == ngpus
130132
return pci_info
133+
134+
135+
@contextmanager
136+
def requires_root():
137+
if os.geteuid() == 0:
138+
yield
139+
else:
140+
with pytest.raises(nvml.NoPermissionError):
141+
yield

cuda_bindings/tests/nvml/test_compute_mode.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import pytest
88
from cuda.bindings import _nvml as nvml
99

10+
from . import conftest
11+
1012
COMPUTE_MODES = [
1113
nvml.ComputeMode.COMPUTEMODE_DEFAULT,
1214
nvml.ComputeMode.COMPUTEMODE_PROHIBITED,
@@ -25,7 +27,7 @@ def test_compute_mode_supported_nonroot(all_devices):
2527
continue
2628

2729
for cm in COMPUTE_MODES:
28-
with pytest.raises(nvml.NoPermissionError):
30+
with conftest.requires_root():
2931
nvml.device_set_compute_mode(device, cm)
3032
assert original_compute_mode == nvml.device_get_compute_mode(device), "Compute mode shouldn't have changed"
3133

0 commit comments

Comments
 (0)