"""to_plain: pyVmomi objects -> JSON-safe Python values. Every parser in this package works on the output of ``to_plain`` rather than on pyVmomi objects. That is what lets the harvest tool dump a real host to JSON and the tests feed that same JSON back in. """ from __future__ import annotations import datetime import json from pyVmomi import vim from napalm_vmware._plain import to_plain def test_scalars_pass_through(): assert to_plain("a") == "a" assert to_plain(3) == 3 assert to_plain(True) is True assert to_plain(None) is None def test_enum_becomes_plain_str(): value = to_plain(vim.VirtualMachine.PowerState.poweredOn) assert value == "poweredOn" assert type(value) is str def test_managed_object_becomes_its_moref(): assert to_plain(vim.HostSystem("host-21", None)) == "host-21" def test_data_object_becomes_dict_with_type_and_without_unset_fields(): nic = vim.host.PhysicalNic( device="vmnic0", linkSpeed=vim.host.PhysicalNic.LinkSpeedDuplex(speedMb=1000, duplex=True), ) plain = to_plain(nic) assert plain["_type"] == "PhysicalNic" assert plain["device"] == "vmnic0" assert plain["linkSpeed"] == {"_type": "PhysicalNicLinkInfo", "speedMb": 1000, "duplex": True} assert "driver" not in plain # optional and unset def test_dynamic_property_noise_is_dropped(): assert "dynamicProperty" not in to_plain(vim.host.PhysicalNic(device="vmnic0")) def test_lists_are_converted_elementwise(): devices = [vim.vm.device.VirtualVmxnet3(key=4000), vim.vm.device.VirtualDisk(key=2000)] assert [d["_type"] for d in to_plain(devices)] == ["VirtualVmxnet3", "VirtualDisk"] def test_datetime_becomes_epoch_seconds(): when = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) assert to_plain(when) == 1767225600.0 def test_bytes_are_dropped_to_none(): assert to_plain(b"\x00\x01") is None def test_result_is_json_serialisable(): nic = vim.host.PhysicalNic(device="vmnic0", pci="0000:0b:00.0") json.dumps(to_plain([nic, vim.HostSystem("host-1", None), 1.5]))