Files
napalm-vmware/tests/conftest.py
T
Christian Manivong ab18ca87ca feat: get_web_login_auth on the vCenter, a source per provider and SSO domain (NetOrk/netork#662)
The vCenter driver reads how people log in to the vSphere Client, in
napalm-device-types' WebLoginAuthDict: each identity provider of identity
federation (GET /api/vcenter/identity/providers) is a source that decides
itself (external), the default first; each SSO domain the vSphere API names
(UserDirectory.domainList) is a source whose second factor cannot be read
(unknown), since smart card and RSA SecurID live in SSO's authentication
policy, which neither API exposes.

Listing the providers needs VcIdentityProviders.Read and .Manage; without
them the vCenter answers 403 and the driver raises PermissionError naming
them. The REST read logs in to /api/session with the same account and logs
out after. The ESXi driver has no reader.
2026-11-29 20:09:55 +01:00

72 lines
2.2 KiB
Python

"""Shared fixtures: harvested inventories and a fake Inventory that serves them.
The JSON files under ``fixtures/`` were produced by ``tools/harvest.py``
against govmomi's vcsim simulator (``vmware/vcsim`` and ``vmware/vcsim -esx``).
They have the real vSphere shapes but simulator values: vcsim labels devices
``disk-202-0`` where a real host says ``Hard disk 1``, reports no sensors to
speak of, and sets ``managementServerIp`` to ``0.0.0.0``. Tests that need a
branch vcsim does not produce copy a fixture and edit it.
"""
from __future__ import annotations
import copy
import json
from collections.abc import Sequence
from pathlib import Path
from typing import Any
import pytest
FIXTURES = Path(__file__).parent / "fixtures"
_CACHE: dict[str, dict[str, Any]] = {}
def load(name: str) -> dict[str, Any]:
"""A private deep copy of fixture ``name``; tests may mutate it freely."""
if name not in _CACHE:
_CACHE[name] = json.loads((FIXTURES / f"{name}.json").read_text())
return copy.deepcopy(_CACHE[name])
class FakeInventory:
"""Serves a harvest dump the way ``Inventory`` serves a live endpoint."""
def __init__(self, data: dict[str, Any]) -> None:
self.data = data
self.calls: list[tuple[str, tuple[str, ...]]] = []
def about(self) -> dict[str, Any]:
return self.data["about"]
def licenses(self) -> list[dict[str, Any]]:
return self.data.get("licenses", [])
def sso_domains(self) -> list[str]:
return self.data.get("sso_domains", [])
def collect(self, type_name: str, paths: Sequence[str]) -> list[dict[str, Any]]:
self.calls.append((type_name, tuple(paths)))
rows = self.data["objects"].get(type_name, [])
wanted = set(paths) | {"_moref"}
return [{k: v for k, v in row.items() if k in wanted} for row in rows]
# Convenience for tests that tweak a fixture.
def objects(self, type_name: str) -> list[dict[str, Any]]:
return self.data["objects"][type_name]
@pytest.fixture
def esxi_data() -> dict[str, Any]:
return load("vcsim-esxi")
@pytest.fixture
def vcenter_data() -> dict[str, Any]:
return load("vcsim-vcenter")
@pytest.fixture
def esxi_host(esxi_data) -> dict[str, Any]:
return esxi_data["objects"]["HostSystem"][0]