"""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 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]