Skip to content

Commit

Permalink
Merge pull request #534 from byllyfish/platformprops
Browse files Browse the repository at this point in the history
Add pkg_info accessor.
  • Loading branch information
byllyfish authored May 11, 2024
2 parents 4bc0225 + cf004ab commit 5dba37b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
13 changes: 12 additions & 1 deletion finsy/p4schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _validate_enum(enum_class: Any, pbuf_class: Any):
# ~~~~~~~~~~~~

# Don't include properties that are redundant or verbose.
_EXEMPT_PROPERTIES = {"pbuf", "p4info", "p4blob", "data_type_spec"}
_EXEMPT_PROPERTIES = {"pbuf", "p4info", "p4blob", "data_type_spec", "pkg_info"}


class _ReprMixin:
Expand Down Expand Up @@ -586,6 +586,17 @@ def arch(self) -> str:
return ""
return self._p4info.pkg_info.arch

@property
def pkg_info(self) -> p4i.PkgInfo:
"""Protobuf message containing original `PkgInfo` header.
Use this to access less frequently used fields like `contact`, `url`,
and `platform_properties`.
"""
if self._p4info is None:
raise ValueError("P4Info: No pipeline configured")
return self._p4info.pkg_info

@property
def tables(self) -> P4EntityMap["P4Table"]:
"Collection of P4 tables."
Expand Down
60 changes: 60 additions & 0 deletions tests/test_p4schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def test_entity_map():
assert repr(entities) == "[_Example(id=1, name='example.one', alias='one')]"


def test_entity_map_empty():
"Test P4EntityMap helper class when it is empty."
entities = P4EntityMap[_Example]("entry_type")
assert len(entities) == 0

with pytest.raises(ValueError, match="no entry_type with id=1"):
entities[1]

with pytest.raises(
ValueError, match="no entry_types present; you asked for 'one'?"
):
entities["one"]


def test_entity_map_split():
"Test P4EntityMap helper class with split_suffix."
example = _Example(1, "example.one", "example.one")
Expand Down Expand Up @@ -112,6 +126,52 @@ def test_p4schema():
assert str(schema)


def test_p4info_pkginfo():
"Test P4Schema's PkgInfo accessors."
p4 = P4Schema(P4INFO_TEST_DIR / "basic.p4.p4info.txt")

assert p4.exists
assert p4.name == ""
assert p4.arch == "v1model"
assert p4.version == ""

assert pbutil.to_dict(p4.pkg_info) == {"arch": "v1model"}
assert p4.pkg_info.name == ""
assert p4.pkg_info.arch == "v1model"
assert p4.pkg_info.version == ""
assert p4.pkg_info.contact == ""
assert p4.pkg_info.organization == ""

assert pbutil.to_dict(p4.pkg_info.doc) == {}
assert p4.pkg_info.doc.brief == ""
assert p4.pkg_info.doc.description == ""

assert pbutil.to_dict(p4.pkg_info.platform_properties) == {}
assert p4.pkg_info.platform_properties.multicast_group_table_size == 0
assert p4.pkg_info.platform_properties.multicast_group_table_total_replicas == 0
assert (
p4.pkg_info.platform_properties.multicast_group_table_max_replicas_per_entry
== 0
)

assert not p4.pkg_info.HasField("doc")
assert not p4.pkg_info.HasField("platform_properties")


def test_p4info_pkginfo_empty():
"Test P4Schema's PkgInfo accessors when P4Info doesn't exist."
p4 = P4Schema()

assert not p4.exists
assert p4.name == ""
assert p4.arch == ""
assert p4.version == ""

# Trying to access `pkg_info` when it doesn't exist is an error.
with pytest.raises(ValueError, match="No pipeline configured"):
p4.pkg_info


def test_p4info_tables():
p4 = P4Schema(P4INFO_TEST_DIR / "basic.p4.p4info.txt")

Expand Down

0 comments on commit 5dba37b

Please sign in to comment.