Files
napalm-vmware/tests/test_parse_clusters.py
T
Christian Manivong 52c802ed41
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Failing after 1m2s
driver / test (3.12) (pull_request) Failing after 57s
driver / test (3.13) (pull_request) Failing after 52s
driver / lowest (pull_request) Failing after 25s
driver / build (pull_request) Successful in 24s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped
feat: get_clusters() and create_vm_from_cloud_init(node=)
vCenter reports each cluster compute resource, and each standalone host
it runs as a cluster of one, identified by "<instanceUuid>/<moref>"; an
ESXi host reports itself. Nodes carry state, maintenance, CPU and memory;
datastores and port groups carry the hosts that see them.

create_vm_from_cloud_init(node=) places the VM on that host and refuses
one that does not exist or cannot take it, before anything is created.
Placement now runs before the image download, so a refused host fails at
once instead of after the download.

Refs NetOrk/netork#484, NetOrk/netork#1822
2026-11-01 16:38:38 +01:00

281 lines
10 KiB
Python

"""``get_clusters``: clusters, their hosts, datastores and port groups, from plain rows."""
from __future__ import annotations
import pytest
from napalm_vmware.parse.clusters import ClusterSources, host_cluster, manager_clusters
MB = 1024 * 1024
UUID = "35bb7c82-8526-5aa7-a886-cd7f8e6be786"
def _host(moref, name, *, state="connected", maint=False, datastores=(), networks=(), pgs=()):
return {
"_moref": moref,
"name": name,
"runtime.connectionState": state,
"runtime.inMaintenanceMode": maint,
"summary.hardware": {
"cpuMhz": 2000,
"cpuModel": "Xeon",
"memorySize": 64 * 1024 * MB,
"numCpuCores": 8,
"numCpuThreads": 16,
},
"summary.quickStats": {"overallCpuUsage": 4000, "overallMemoryUsage": 1024},
"hardware.systemInfo": {"uuid": f"uuid-{moref}"},
"datastore": list(datastores),
"network": list(networks),
"config.network.portgroup": [
{"spec": {"name": pg, "vlanId": vlan, "vswitchName": "vSwitch0"}} for pg, vlan in pgs
],
}
def _ds(moref, name, *, shared=False, accessible=True, capacity=1000, free=400, kind="VMFS"):
return {
"_moref": moref,
"name": name,
"summary": {
"accessible": accessible,
"capacity": capacity,
"freeSpace": free,
"multipleHostAccess": shared,
"type": kind,
},
}
@pytest.fixture
def src():
return ClusterSources(
hosts=[
_host(
"host-1",
"esx01",
datastores=["ds-san", "ds-local1", "ds-gone"],
networks=["network-1", "network-10", "dvportgroup-12", "dvportgroup-10"],
pgs=[("VM Network", 0), ("Storage", 30)],
),
_host(
"host-2",
"esx02",
state="disconnected",
datastores=["ds-san"],
networks=["network-1", "dvportgroup-12"],
pgs=[("VM Network", 0)],
),
_host(
"host-3",
"esx03",
maint=True,
datastores=["ds-san"],
networks=["network-1", "dvportgroup-12"],
pgs=[("VM Network", 0)],
),
_host("host-9", "lonely", datastores=["ds-local9"], networks=["network-1"]),
],
datastores=[
_ds("ds-san", "san", shared=True, capacity=10_000, free=2_500),
_ds("ds-local1", "local1", kind="vmfs"),
_ds("ds-gone", "gone", accessible=False),
_ds("ds-local9", "local9", kind="NFS"),
],
networks=[
{"_moref": "network-1", "name": "VM Network"},
{"_moref": "network-10", "name": "Storage"},
{"_moref": "dvportgroup-10", "name": "DVS0-DVUplinks-8"},
{"_moref": "dvportgroup-12", "name": "DC0_DVPG0"},
],
dv_portgroups=[
{
"_moref": "dvportgroup-10",
"name": "DVS0-DVUplinks-8",
"config.defaultPortConfig": {"vlan": {"_type": "TrunkVlanSpec"}},
},
{
"_moref": "dvportgroup-12",
"name": "DC0_DVPG0",
"config.defaultPortConfig": {
"vlan": {"_type": "VmwareDistributedVirtualSwitchVlanIdSpec", "vlanId": 42}
},
},
],
)
COMPUTE = [
{"_moref": "domain-c7", "name": "Prod", "host": ["host-1", "host-2", "host-3"]},
{"_moref": "domain-s9", "name": "lonely", "host": ["host-9"]},
]
@pytest.fixture
def clusters(src):
return manager_clusters(COMPUTE, {"domain-c7"}, src, instance_uuid=UUID)
def _standalone(src, index):
"""``src`` as an ESXi host sees it: one host, ``src.hosts[index]``."""
src.hosts = [src.hosts[index]]
return host_cluster(src)
def _by_name(items):
return {i["name"]: i for i in items}
class TestManager:
def test_one_entry_per_cluster_and_per_standalone_host(self, clusters):
assert [(c["name"], c["id"]) for c in clusters] == [
("Prod", f"{UUID}/domain-c7"),
(None, f"{UUID}/domain-s9"),
]
assert all(c["quorate"] is None for c in clusters)
def test_the_moref_alone_without_an_instance_uuid(self, src):
(prod, _) = manager_clusters(COMPUTE, {"domain-c7"}, src, instance_uuid=None)
assert prod["id"] == "domain-c7"
def test_a_cluster_lists_only_its_own_hosts(self, clusters):
prod, lonely = clusters
assert [n["name"] for n in prod["nodes"]] == ["esx01", "esx02", "esx03"]
assert [n["name"] for n in lonely["nodes"]] == ["lonely"]
assert all(n["local"] is False for c in clusters for n in c["nodes"])
def test_an_empty_cluster_is_still_a_cluster(self, src):
(empty,) = manager_clusters(
[{"_moref": "domain-c8", "name": "Empty"}], {"domain-c8"}, src, instance_uuid=UUID
)
assert (empty["name"], empty["nodes"], empty["storages"], empty["networks"]) == (
"Empty",
[],
[],
[],
)
class TestNodes:
def test_a_connected_host(self, clusters):
node = _by_name(clusters[0]["nodes"])["esx01"]
assert node == {
"name": "esx01",
"status": "online",
"local": False,
"maintenance": False,
"cpu_threads": 16,
"cpu_cores": 8,
"cpu_model": "Xeon",
"cpu_flags": None,
"cpu_usage": 0.25,
"memory_total": 64 * 1024 * MB,
"memory_used": 1024 * MB,
}
def test_a_disconnected_host_keeps_its_name_and_state_but_no_capacity(self, clusters):
node = _by_name(clusters[0]["nodes"])["esx02"]
assert (node["status"], node["maintenance"]) == ("offline", False)
capacity = ("cpu_threads", "cpu_cores", "cpu_model", "cpu_usage")
assert all(node[k] is None for k in (*capacity, "memory_total", "memory_used"))
def test_a_host_in_maintenance(self, clusters):
node = _by_name(clusters[0]["nodes"])["esx03"]
assert (node["status"], node["maintenance"], node["cpu_cores"]) == ("online", True, 8)
@pytest.mark.parametrize(
("state", "status"),
[("notResponding", "offline"), ("disconnected", "offline"), (None, "unknown")],
)
def test_status_from_the_connection_state(self, src, state, status):
src.hosts[3]["runtime.connectionState"] = state
node = _standalone(src, 3)["nodes"][0]
assert node["status"] == status
@pytest.mark.parametrize(
"change",
[
lambda h: h["summary.hardware"].update(cpuMhz=0),
lambda h: h["summary.hardware"].pop("numCpuCores"),
lambda h: h.pop("summary.quickStats"),
],
)
def test_cpu_usage_is_none_when_it_cannot_be_computed(self, src, change):
change(src.hosts[0])
node = _standalone(src, 0)["nodes"][0]
assert node["cpu_usage"] is None
def test_cpu_usage_never_exceeds_one(self, src):
src.hosts[0]["summary.quickStats"]["overallCpuUsage"] = 99_999
assert _standalone(src, 0)["nodes"][0]["cpu_usage"] == 1.0
class TestStorages:
def test_shared_and_local_datastores_with_the_hosts_that_mount_them(self, clusters):
storages = _by_name(clusters[0]["storages"])
assert storages["san"] == {
"name": "san",
"type": "vmfs",
"shared": True,
"content": ["images", "iso"],
"nodes": ["esx01", "esx02", "esx03"],
"total": 10_000,
"used": 7_500,
}
assert (storages["local1"]["shared"], storages["local1"]["nodes"]) == (False, ["esx01"])
def test_an_inaccessible_datastore_is_left_out(self, clusters):
assert "gone" not in _by_name(clusters[0]["storages"])
def test_only_the_datastores_of_the_clusters_hosts(self, clusters):
assert sorted(_by_name(clusters[0]["storages"])) == ["local1", "san"]
assert [s["name"] for s in clusters[1]["storages"]] == ["local9"]
assert clusters[1]["storages"][0]["type"] == "nfs"
class TestNetworks:
def test_standard_and_distributed_port_groups(self, clusters):
networks = _by_name(clusters[0]["networks"])
assert networks["VM Network"] == {
"name": "VM Network",
"kind": "portgroup",
"vlan_aware": False,
"fixed_vlan_tag": None,
"nodes": ["esx01", "esx02", "esx03"],
}
assert networks["DC0_DVPG0"]["fixed_vlan_tag"] == 42
def test_a_port_group_on_one_host_only(self, clusters):
storage = _by_name(clusters[0]["networks"])["Storage"]
assert (storage["nodes"], storage["fixed_vlan_tag"]) == (["esx01"], 30)
def test_the_uplink_port_group_is_no_network_for_a_vm(self, clusters):
assert "DVS0-DVUplinks-8" not in _by_name(clusters[0]["networks"])
def test_a_vlan_that_differs_between_hosts_is_not_reported(self, src, clusters):
src.hosts[1]["config.network.portgroup"][0]["spec"]["vlanId"] = 7
prod = manager_clusters(COMPUTE, {"domain-c7"}, src, instance_uuid=UUID)[0]
assert _by_name(prod["networks"])["VM Network"]["fixed_vlan_tag"] is None
def test_a_trunk_port_group_has_no_fixed_tag(self, src):
src.hosts[0]["config.network.portgroup"][1]["spec"]["vlanId"] = 4095
networks = _by_name(_standalone(src, 0)["networks"])
assert networks["Storage"]["fixed_vlan_tag"] is None
class TestStandaloneHost:
def test_the_host_is_a_cluster_of_one(self, src):
cluster = _standalone(src, 3)
assert (cluster["id"], cluster["name"], cluster["quorate"]) == ("uuid-host-9", None, None)
assert [(n["name"], n["local"]) for n in cluster["nodes"]] == [("lonely", True)]
assert [s["name"] for s in cluster["storages"]] == ["local9"]
assert [n["name"] for n in cluster["networks"]] == ["VM Network"]
def test_no_id_without_a_system_uuid(self, src):
del src.hosts[3]["hardware.systemInfo"]
assert _standalone(src, 3)["id"] is None
def test_no_host_is_a_failed_read(self, src):
with pytest.raises(RuntimeError, match="no host"):
host_cluster(ClusterSources([], [], [], []))