| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from vllm.v1.executor.vllm_net_devices import normalize_pci |
| 7 | |
| 8 | |
| 9 | @pytest.mark.parametrize( |
| 10 | "addr, expected", |
| 11 | [ |
| 12 | ("0000:3f:00.0", (0, 0x3F, 0, 0)), |
| 13 | ("0001:00:00.0", (1, 0, 0, 0)), |
| 14 | ("00000001:00:00.0", (1, 0, 0, 0)), |
| 15 | ("0009:00:00.0", (9, 0, 0, 0)), |
| 16 | ("0105:00:00.0", (0x105, 0, 0, 0)), |
| 17 | ("0000:0a:1f.7", (0, 0x0A, 0x1F, 7)), |
| 18 | ], |
| 19 | ) |
| 20 | def test_normalize_pci_full_domain(addr, expected): |
| 21 | assert normalize_pci(addr) == expected |
| 22 | |
| 23 | |
| 24 | @pytest.mark.parametrize( |
| 25 | "addr, expected", |
| 26 | [ |
| 27 | ("01:00.0", (0, 1, 0, 0)), |
| 28 | ("3f:00.0", (0, 0x3F, 0, 0)), |
| 29 | ("40:00.0", (0, 0x40, 0, 0)), |
| 30 | ("ff:1f.7", (0, 0xFF, 0x1F, 7)), |
| 31 | ], |
| 32 | ) |
| 33 | def test_normalize_pci_short_form(addr, expected): |
| 34 | assert normalize_pci(addr) == expected |
| 35 | |
| 36 | |
| 37 | def test_normalize_pci_case_insensitive(): |
| 38 | assert normalize_pci("0A:1F.7") == normalize_pci("0a:1f.7") |
| 39 | |
| 40 | |
| 41 | def test_normalize_pci_strips_whitespace(): |
| 42 | assert normalize_pci(" 0001:00:00.0 ") == (1, 0, 0, 0) |
| 43 | |
| 44 | |
| 45 | def test_normalize_pci_strips_0x_prefix(): |
| 46 | assert normalize_pci("0x0001:00:00.0") == (1, 0, 0, 0) |
| 47 | |
| 48 | |
| 49 | def test_normalize_pci_missing_function_raises(): |
| 50 | with pytest.raises(ValueError, match="missing function suffix"): |
| 51 | normalize_pci("0001:00:00") |
| 52 | |
| 53 | |
| 54 | def test_normalize_pci_invalid_function_char_raises(): |
| 55 | with pytest.raises(ValueError, match="invalid PCI function"): |
| 56 | normalize_pci("0001:00:00.z") |
| 57 | |
| 58 | |
| 59 | def test_normalize_pci_too_many_segments_raises(): |
| 60 | with pytest.raises(ValueError, match="invalid PCI BDF"): |
| 61 | normalize_pci("a:b:c:d.0") |
| 62 | |
| 63 | |
| 64 | def test_normalize_pci_bus_out_of_range_raises(): |
| 65 | with pytest.raises(ValueError, match="out of range"): |
| 66 | normalize_pci("0000:1ff:00.0") |
| 67 | |
| 68 | |
| 69 | def test_normalize_pci_device_out_of_range_raises(): |
| 70 | with pytest.raises(ValueError, match="out of range"): |
| 71 | normalize_pci("0000:00:20.0") |
| 72 | |
| 73 | |
| 74 | def test_normalize_pci_empty_string_raises(): |
| 75 | with pytest.raises(ValueError): |
| 76 | normalize_pci("") |