Merge remote-tracking branch 'origin/main' into feat/health-profile-unknown

# Conflicts:
#	CHANGELOG.md
This commit is contained in:
christianmanivong committed 2026-11-01 23:01:38 +01:00
commit 83ea13f88e
7 files changed
+247 -2

No files matched your search

+56 -2
View File
@@ -23,10 +23,13 @@ from typing import NotRequired, get_type_hints
import napalm_device_types.hypervisor as hypervisor_module
from napalm_device_types import (
BackupDict,
BackupJobDict,
ClusterDict,
ClusterNetworkDict,
ClusterNodeDict,
ClusterStorageDict,
HAResourceDict,
HypervisorDriver,
)
@@ -52,17 +55,26 @@ class TestClusterListing:
class TestClusterShape:
def test_cluster(self):
assert get_type_hints(ClusterDict) == {
assert get_type_hints(ClusterDict, include_extras=True) == {
"id": str | None,
"name": str | None,
"quorate": bool | None,
"nodes": list[ClusterNodeDict],
"storages": list[ClusterStorageDict],
"networks": list[ClusterNetworkDict],
"ha_resources": NotRequired[list[HAResourceDict] | None],
"backup_jobs": NotRequired[list[BackupJobDict] | None],
"backups": NotRequired[list[BackupDict] | None],
}
def test_cluster_keys_added_later_stay_optional(self):
"""A driver written before HA and backups were part of the report stays valid."""
assert ClusterDict.__required_keys__ == frozenset(
{"id", "name", "quorate", "nodes", "storages", "networks"}
)
def test_node(self):
hints = get_type_hints(ClusterNodeDict)
hints = get_type_hints(ClusterNodeDict, include_extras=True)
assert hints == {
"name": str,
"status": str,
@@ -75,7 +87,9 @@ class TestClusterShape:
"cpu_usage": float | None,
"memory_total": int | None,
"memory_used": int | None,
"votes": NotRequired[int | None],
}
assert "votes" not in ClusterNodeDict.__required_keys__
def test_storage(self):
assert get_type_hints(ClusterStorageDict) == {
@@ -102,6 +116,46 @@ class TestClusterShape:
)
class TestHAAndBackupShapes:
def test_ha_resource(self):
hints = get_type_hints(HAResourceDict, include_extras=True)
assert hints == {
"vmid": int,
"kind": str,
"requested": str,
"status": str | None,
"node": str | None,
"group": NotRequired[str | None],
"rules": list[str],
}
assert HAResourceDict.__required_keys__ == frozenset(
{"vmid", "kind", "requested", "status", "node", "rules"}
)
def test_backup_job(self):
assert get_type_hints(BackupJobDict) == {
"id": str,
"enabled": bool,
"schedule": str | None,
"storage": str | None,
"mode": str | None,
"all": bool,
"vmids": list[int],
"exclude": list[int],
"pool": str | None,
"node": str | None,
}
def test_backup(self):
assert get_type_hints(BackupDict) == {
"vmid": int,
"storage": str,
"time": int,
"size": int | None,
"count": int,
}
class TestCreateVmTakesANode:
def test_node_is_an_optional_keyword(self):
fn = _declared("create_vm_from_cloud_init")
+65
View File
@@ -0,0 +1,65 @@
"""Moving a guest to another node of its cluster, and a node's HA maintenance mode.
``get_vm_migration_preflight`` says where a guest could go and what holds it
back, ``migrate_vm`` moves it and waits, ``set_node_maintenance`` switches a
node's HA maintenance mode. Declared only, under ``TYPE_CHECKING``: netOrk
probes each with ``hasattr`` and hides what a driver lacks (VMware has none of
them yet).
"""
from __future__ import annotations
import ast
import inspect
from typing import get_type_hints
import pytest
import napalm_device_types.hypervisor as hypervisor_module
from napalm_device_types import HypervisorDriver, MigrationPreflightDict
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}()")
def _signature(name: str) -> str:
fn = _declared(name)
return f"({ast.unparse(fn.args)}) -> {ast.unparse(fn.returns) if fn.returns else None}"
class TestDeclarations:
@pytest.mark.parametrize(
"method", ["get_vm_migration_preflight", "migrate_vm", "set_node_maintenance"]
)
def test_absent_until_a_driver_implements_it(self, method):
assert not hasattr(HypervisorDriver, method)
def test_preflight(self):
assert _signature("get_vm_migration_preflight") == (
"(self, name: str) -> MigrationPreflightDict"
)
def test_migrate(self):
assert _signature("migrate_vm") == (
"(self, name: str, target_node: str, online: bool=True, "
"target_storage: str | None=None, timeout: int=3600) -> None"
)
def test_maintenance(self):
assert _signature("set_node_maintenance") == "(self, node: str, enabled: bool) -> None"
class TestPreflightShape:
def test_keys(self):
assert get_type_hints(MigrationPreflightDict) == {
"running": bool,
"allowed_nodes": list[str],
"refused_nodes": dict[str, str],
"local_disks": list[str],
"local_resources": list[str],
}