feat: VM provisioning, and reboot_host on ESXi
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.
This commit is contained in:
@@ -103,3 +103,49 @@ def test_wrong_driver_is_refused():
|
||||
|
||||
def _status(driver, vm):
|
||||
return next(v["status"] for v in driver.get_vms() if v["vmid"] == vm["vmid"])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def image_url(tmp_path):
|
||||
"""A tiny qcow2 served over HTTP, like a catalog image."""
|
||||
import functools
|
||||
import http.server
|
||||
import shutil
|
||||
import subprocess
|
||||
import threading
|
||||
|
||||
if not shutil.which("qemu-img"):
|
||||
pytest.skip("qemu-img not installed")
|
||||
subprocess.run(
|
||||
["qemu-img", "create", "-q", "-f", "qcow2", str(tmp_path / "tiny.qcow2"), "64M"], check=True
|
||||
)
|
||||
handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(tmp_path))
|
||||
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
yield f"http://127.0.0.1:{server.server_address[1]}/tiny.qcow2"
|
||||
server.shutdown()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("which", ["esxi", "vcenter"])
|
||||
def test_provision_and_destroy(which, image_url, tmp_path, request):
|
||||
driver = request.getfixturevalue(which)
|
||||
driver._image_cache_dir = tmp_path / "cache"
|
||||
result = driver.create_vm_from_cloud_init(
|
||||
"netork-e2e",
|
||||
image_url=image_url,
|
||||
cpu=2,
|
||||
memory=1024,
|
||||
nics=[{"bridge": "VM Network", "dhcp": True, "mac": "00:50:56:01:02:03"}],
|
||||
cloud_init_config={"hostname": "netork-e2e"},
|
||||
ssh_public_keys=["ssh-ed25519 AAAA test"],
|
||||
disk_resize_gb=1,
|
||||
)
|
||||
try:
|
||||
assert driver.get_vm_status(result["vmid"])["status"] == "running"
|
||||
config = driver.get_vm_config(result["vmid"])
|
||||
assert (config["vcpus"], config["memory"]) == (2, 1024)
|
||||
assert config["disks"][0]["size"] == 1
|
||||
assert config["nics"][0]["mac"] == "00:50:56:01:02:03"
|
||||
finally:
|
||||
driver.destroy_vm(result["vmid"])
|
||||
assert not any(vm["vmid"] == result["vmid"] for vm in driver.get_vms())
|
||||
|
||||
Reference in New Issue
Block a user