Files
napalm-vmware/tests/test_plain.py
T
Christian Manivong 3b3f5d9d02
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 14s
driver / test (3.13) (pull_request) Successful in 55s
driver / types (pull_request) Successful in 1m7s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / lowest (pull_request) Failing after 5s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Failing after 2m51s
driver / publish (pull_request) Skipped
chore: Python 3.12 is the floor (NAPALM/driver-standard#6)
netOrk runs every driver in its own containers on Python 3.12, so 3.10
and 3.11 are no longer supported or tested. requires-python, mypy and
ruff move to 3.12, along with what ruff's pyupgrade rules then rewrite.
2026-10-27 10:19:01 +01:00

68 lines
2.0 KiB
Python

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