Skip to content

Commit 6972044

Browse files
tests/test_sbdfi_to_nic.py : Add test for xcp.pci.pci_sbdfi_to_nic()
xcp.pci.pci_sbdfi_to_nic() was not covered by a test and the regex it uses triggers a warning because of using [[] and []]. To ensure that the function keeps working as expected, add a testcase for it. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
1 parent 007e6d5 commit 6972044

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

stubs/pytest.pyi

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# pylint: disable=reimported,no-name-in-module,unused-import,function-redefined.redefined-builtin
2+
from _pytest.python_api import raises
3+
from _typeshed import Incomplete as fixture
4+
from _typeshed import Incomplete as mark
5+
6+
def skip(msg: str = "", *, allow_module_level: bool = False): ...
7+
8+
__all__ = ["mark", "fixture", "skip", "raises"]

tests/test_sbdfi_to_nic.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from xcp.pci import pci_sbdfi_to_nic
4+
5+
nics = [
6+
# One PCI NIC may have multiple MACs, see the comment in xcp/pci.py:
7+
type("Nic", (object,), {"pci": "0000:01:00.0", "mac": "00:11:22:33:44:55"}),
8+
type("Nic", (object,), {"pci": "0000:01:00.0", "mac": "00:11:22:33:44:56"}),
9+
type("Nic", (object,), {"pci": "0000:02:00.0", "mac": "00:11:22:33:44:57"}),
10+
]
11+
12+
13+
def test_sbdf_index():
14+
assert pci_sbdfi_to_nic("0000:01:00.0", nics) == nics[0]
15+
assert pci_sbdfi_to_nic("0000:01:00.0[0]", nics) == nics[0]
16+
assert pci_sbdfi_to_nic("0000:01:00.0[1]", nics) == nics[1]
17+
assert pci_sbdfi_to_nic("0000:02:00.0", nics) == nics[2]
18+
with pytest.raises(Exception) as e:
19+
pci_sbdfi_to_nic("0000:01:00.0[3]", nics)
20+
exp = "Insufficient NICs with PCI SBDF 0000:01:00.0 (Found 2, wanted at least 3)"
21+
assert str(e) == exp
22+
pytest.raises(Exception, pci_sbdfi_to_nic, "0000:03:00.0", nics) # no matching SBDF
23+
pytest.raises(Exception, pci_sbdfi_to_nic, "0000:01:00.1[2]", nics) # Not enough MACs
24+
pytest.raises(Exception, pci_sbdfi_to_nic, "0000:01:00.1[-1]", nics) # Negative index
25+
pytest.raises(Exception, pci_sbdfi_to_nic, "0000:01:00.0", [])
26+
pytest.raises(Exception, pci_sbdfi_to_nic, "", nics)
27+
pytest.raises(Exception, pci_sbdfi_to_nic, "", [])

0 commit comments

Comments
 (0)