feat: a new VM's CPU model can be chosen, from a list the hypervisor offers

create_vm_from_cloud_init takes cpu_type. Proxmox gives a VM created without
one the kvm64 model, which has no AVX, so MongoDB 5.0 and later do not start
there, and netOrk's graylog role failed on every VM it provisioned
(netork#494). Which model is right depends on the cluster: host cannot
live-migrate between different CPUs, x86-64-v3 does not start on a CPU older
than Haswell. So the caller chooses.

get_vm_cpu_types() is the new, optional listing behind that choice. Each
VMCpuTypeDict names the model, says what it is for, lists the /proc/cpuinfo
flags the guest gets (so a caller can ask "does this give AVX?" without
knowing model names), whether the node at hand can run it, and which one is
the default. A hypervisor whose VMs have no per-VM CPU model (VMware sets CPU
compatibility per cluster) does not implement it and must reject any
cpu_type other than None.

Both declarations sit under TYPE_CHECKING like the rest of the contract, so
hasattr stays a truthful capability probe; the tests read the signature from
the source.
This commit is contained in:
2026-10-04 12:02:38 +02:00
parent 36b7852bce
commit b5c40019af
4 changed files with 111 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
"""A new VM's virtual CPU model can be chosen, from a list the hypervisor offers.
Proxmox gives a VM created without a ``cpu`` argument the ``kvm64`` model,
which has no AVX -- and MongoDB 5.0 and later will not start without it. Which
model is right depends on the cluster (``host`` cannot live-migrate between
different CPUs, ``x86-64-v3`` does not start on a CPU older than Haswell), so
the caller chooses, from entries that say what each model provides and whether
the node at hand can run it.
The declarations sit under ``TYPE_CHECKING`` (see test_role_contracts), so the
signature is read from the source rather than from the class.
"""
from __future__ import annotations
import ast
import inspect
from typing import List, get_type_hints
import napalm_device_types.hypervisor as hypervisor_module
from napalm_device_types import HypervisorDriver
from napalm_device_types.models import VMCpuTypeDict
def _declared(name: str) -> ast.FunctionDef:
tree = ast.parse(inspect.getsource(hypervisor_module))
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name == name:
return node
raise AssertionError(f"HypervisorDriver does not declare {name}()")
class TestCreateVmTakesACpuType:
def test_cpu_type_is_an_optional_keyword(self):
fn = _declared("create_vm_from_cloud_init")
kwonly = {arg.arg: default for arg, default in zip(fn.args.kwonlyargs, fn.args.kw_defaults)}
assert "cpu_type" in kwonly
default = kwonly["cpu_type"]
assert isinstance(default, ast.Constant) and default.value is None
class TestCpuTypeListing:
def test_is_declared(self):
assert _declared("get_vm_cpu_types").returns is not None
def test_absent_until_a_driver_implements_it(self):
"""netOrk probes capabilities with hasattr; a hypervisor without a
choice of CPU model must not seem to offer one."""
assert not hasattr(HypervisorDriver, "get_vm_cpu_types")
def test_entry_shape(self):
assert get_type_hints(VMCpuTypeDict) == {
"name": str,
"description": str,
"features": List[str],
"available": bool,
"default": bool,
}