Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mfd_kvm/hypervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,21 +764,28 @@ def prepare_vf_xml(
template_path: Union[Path, str] = Path(__file__).parent / "vf_template.xml",
file_to_save: str,
pci_address: "PCIAddress",
mac_address: Optional[MACAddress] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use new typehints, "MACAddress" | None

) -> Path:
"""
Create configuration xml of VF for QEMU.

:param template_path: Path to template config
:param file_to_save: Path to destination directory
:param pci_address: PCI Address of VF
:param mac_address: MAC address of VF, if needed to set in config
Comment on lines 772 to +775
:return: Path to saved configuration
"""
logger.log(log_levels.MODULE_DEBUG, msg="Creating configuration file for VF.")
data_for_template = {}
for key, value in asdict(pci_address).items():
# convert int to hex required for xml
data_for_template[key] = hex(value)
file_to_save = self._render_file(file_to_save, data_for_template, template_path)
if mac_address is not None:
data_for_template["mac_address"] = str(mac_address)
template_path_with_mac = Path(__file__).parent / "vf_template_with_mac.xml"
file_to_save = self._render_file(file_to_save, data_for_template, template_path_with_mac)
else:
file_to_save = self._render_file(file_to_save, data_for_template, template_path)
Comment on lines +783 to +788
return file_to_save

def _render_file(
Expand Down
6 changes: 6 additions & 0 deletions mfd_kvm/vf_template_with_mac.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<hostdev mode='subsystem' type='pci' managed='yes'>
<mac address='{{mac_address}}'/>
<source>
<address domain='{{domain}}' bus='{{bus}}' slot='{{slot}}' function='{{func}}'/>
</source>
</hostdev>
15 changes: 15 additions & 0 deletions tests/unit/test_mfd_kvm/test_hypervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,21 @@ def test_prepare_vf_xml(self, hv, mocker):
hv.prepare_vf_xml(template_path="", file_to_save="", pci_address=PCIAddress(0, 0, 15, 1))
hv._conn.path().write_text.assert_called_once()

def test_prepare_vf_xml_with_mac_address(self, hv, mocker):
template_data = b"<mac address='{{mac_address}}'/><address domain='{{domain}}' bus='{{bus}}' slot='{{slot}}' function='{{func}}'/>"
mocker.patch("mfd_kvm.hypervisor.open", mocker.mock_open(read_data=template_data))
hv._conn.path = mocker.Mock()
pci_address = PCIAddress(0, 0, 15, 1)
mac = MACAddress("AA:BB:CC:DD:EE:FF")

hv.prepare_vf_xml(template_path="", file_to_save="", pci_address=pci_address, mac_address=mac)

expected_rendered = (
"<mac address='aa:bb:cc:dd:ee:ff'/>"
"<address domain='0x0' bus='0x0' slot='0xf' function='0x1'/>"
)
hv._conn.path().write_text.assert_called_once_with(expected_rendered)

def test_prepare_pci_controller_xml(self, hv, mocker):
template_data = (
b"index='{{index}}' chassis='{{chassis}}' port='{{port}}' bus='{{bus}}' slot='{{slot}}' "
Expand Down
Loading