create_vm_from_cloud_init takes the same qcow2/raw cloud images netOrk offers for Proxmox. ESXi can neither boot nor download them, so the driver does both: download with checksum check and one retry, convert with qemu-img to a streamOptimized VMDK (cached by URL), import through a minimal OVF descriptor over NFC, pin requested MACs, grow the disk, and attach a NoCloud seed ISO placed next to the VM's files. NoCloud rather than guestinfo because it needs nothing in the guest; the user-data installs open-vm-tools, which the driver declares as its guest agent. A failure after the import removes the VM again. Placement is a pure decision over inventory rows: a connected host outside maintenance mode that sees the datastore and every port group, with the resource pool and VM folder found by walking up to the datacenter -- one path for a standalone host and for a vCenter. Two faults vcsim surfaced and the tests now pin: a chunked upload body next to a Content-Length is refused with 500, so the disk goes up as a sized file object that also reports lease progress; and a device edit replaces the device as sent, so disk and NIC edits start from the live objects, backing included (vcsim panicked on a disk without one). destroy_vm, get_vm_status, get_network_targets (port groups with their fixed VLAN) and get_image_storages complete the contract. reboot_host on ESXi uses RebootHost_Task and refuses outside maintenance mode: force=True would cut power to running VMs. Tested against vcsim in ESXi and vCenter mode, end to end.
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
"""NFC upload body with lease progress, lease URLs and datastore URLs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from napalm_vmware.provision.transfer import ProgressFile, datastore_url, lease_url
|
|
|
|
|
|
class _Clock:
|
|
def __init__(self):
|
|
self.now = 0.0
|
|
|
|
def __call__(self):
|
|
self.now += 15.0 # each read "takes" 15 s
|
|
return self.now
|
|
|
|
|
|
class TestProgressFile:
|
|
def test_has_a_length_so_requests_does_not_chunk(self, tmp_path):
|
|
"""A generator body is sent chunked; with a Content-Length header too,
|
|
vSphere's NFC endpoint answers 500. A sized body avoids both."""
|
|
path = tmp_path / "disk.vmdk"
|
|
path.write_bytes(b"x" * 1000)
|
|
body = ProgressFile(path, lambda pct: None)
|
|
assert len(body) == 1000
|
|
body.close()
|
|
|
|
def test_reads_everything_and_reports_rising_progress(self, tmp_path):
|
|
path = tmp_path / "disk.vmdk"
|
|
path.write_bytes(b"x" * (3 * 1024 * 1024 + 5))
|
|
reports = []
|
|
body = ProgressFile(path, reports.append, clock=_Clock())
|
|
data = b"".join(iter(lambda: body.read(1024 * 1024), b""))
|
|
body.close()
|
|
assert len(data) == path.stat().st_size
|
|
assert reports and reports == sorted(reports) and all(0 <= r < 100 for r in reports)
|
|
|
|
|
|
class TestLeaseUrl:
|
|
def test_star_becomes_the_host_on_the_default_port(self):
|
|
assert (
|
|
lease_url("https://*/nfc/x/disk-0.vmdk", "esx01", 443)
|
|
== "https://esx01/nfc/x/disk-0.vmdk"
|
|
)
|
|
|
|
def test_star_carries_a_non_default_port(self):
|
|
assert (
|
|
lease_url("https://*/nfc/x/d.vmdk", "10.0.0.5", 8443)
|
|
== "https://10.0.0.5:8443/nfc/x/d.vmdk"
|
|
)
|
|
|
|
def test_a_named_host_is_left_alone(self):
|
|
"""Through vCenter the URL names the ESXi host that takes the disk."""
|
|
assert (
|
|
lease_url("https://esx02.lan/nfc/x/d.vmdk", "vc01", 443)
|
|
== "https://esx02.lan/nfc/x/d.vmdk"
|
|
)
|
|
|
|
|
|
def test_datastore_url_quotes_the_path():
|
|
assert (
|
|
datastore_url("https://esx01:443", "web 01/cidata.iso")
|
|
== "https://esx01:443/folder/web%2001/cidata.iso"
|
|
)
|