"""The OVF descriptor an import is built from.""" from __future__ import annotations import xml.etree.ElementTree as ET import pytest from napalm_vmware.provision.ovf import ovf_descriptor NS = { "ovf": "http://schemas.dmtf.org/ovf/envelope/1", "rasd": "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData", "vmw": "http://www.vmware.com/schema/ovf", } OVF = "{http://schemas.dmtf.org/ovf/envelope/1}" RASD = "{" + NS["rasd"] + "}" @pytest.fixture def root(): return ET.fromstring( ovf_descriptor("web01", cpu=2, memory_mb=4096, capacity_bytes=10 * 1024**3, nic_count=2) ) def _items(root): return root.findall(".//ovf:VirtualHardwareSection/ovf:Item", NS) def _by_type(root, resource_type): return [i for i in _items(root) if i.find("rasd:ResourceType", NS).text == str(resource_type)] def test_cpu_and_memory(root): (cpu,) = _by_type(root, 3) (mem,) = _by_type(root, 4) assert cpu.find("rasd:VirtualQuantity", NS).text == "2" assert mem.find("rasd:VirtualQuantity", NS).text == "4096" def test_disk_references_the_stream_optimized_file(root): disk = root.find(".//ovf:DiskSection/ovf:Disk", NS) assert disk.get(f"{OVF}capacity") == str(10 * 1024**3) assert disk.get(f"{OVF}format").endswith("#streamOptimized") (file_ref,) = root.findall(".//ovf:References/ovf:File", NS) assert disk.get(f"{OVF}fileRef") == file_ref.get(f"{OVF}id") (item,) = _by_type(root, 17) assert item.find("rasd:HostResource", NS).text == f"ovf:/disk/{disk.get(f'{OVF}diskId')}" def test_one_vmxnet3_nic_per_network(root): nics = _by_type(root, 10) assert [n.find("rasd:Connection", NS).text for n in nics] == ["net0", "net1"] assert {n.find("rasd:ResourceSubType", NS).text for n in nics} == {"VmxNet3"} networks = root.findall(".//ovf:NetworkSection/ovf:Network", NS) assert [n.get(f"{OVF}name") for n in networks] == ["net0", "net1"] def test_rasd_elements_are_in_schema_order(root): """vSphere rejects an Item whose CIM elements are out of order.""" for item in _items(root): names = [child.tag.removeprefix(RASD) for child in item] assert names == sorted(names), names def test_name_is_escaped(): ET.fromstring(ovf_descriptor("a