From b5082c7891ad0edce4467627e5df684f73db4b37 Mon Sep 17 00:00:00 2001 From: Bill Fisher Date: Fri, 10 May 2024 16:53:16 -0700 Subject: [PATCH 1/2] Add pkg_info accessor to P4Schema. Add tests for pkg_info and PlatformProperties. --- finsy/p4schema.py | 13 ++++++++++++- tests/test_p4schema.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/finsy/p4schema.py b/finsy/p4schema.py index 9cb15193..53686ef1 100644 --- a/finsy/p4schema.py +++ b/finsy/p4schema.py @@ -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: @@ -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 RuntimeError("P4Info: No pipeline configured") + return self._p4info.pkg_info + @property def tables(self) -> P4EntityMap["P4Table"]: "Collection of P4 tables." diff --git a/tests/test_p4schema.py b/tests/test_p4schema.py index 7be1d7af..a5e6eccf 100644 --- a/tests/test_p4schema.py +++ b/tests/test_p4schema.py @@ -112,6 +112,38 @@ 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_tables(): p4 = P4Schema(P4INFO_TEST_DIR / "basic.p4.p4info.txt") From cf004ab812a838ba5a879d9693d8c33218148fdb Mon Sep 17 00:00:00 2001 From: Bill Fisher Date: Fri, 10 May 2024 18:17:37 -0700 Subject: [PATCH 2/2] Improve unit tests. --- finsy/p4schema.py | 2 +- tests/test_p4schema.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/finsy/p4schema.py b/finsy/p4schema.py index 53686ef1..875c10d4 100644 --- a/finsy/p4schema.py +++ b/finsy/p4schema.py @@ -594,7 +594,7 @@ def pkg_info(self) -> p4i.PkgInfo: and `platform_properties`. """ if self._p4info is None: - raise RuntimeError("P4Info: No pipeline configured") + raise ValueError("P4Info: No pipeline configured") return self._p4info.pkg_info @property diff --git a/tests/test_p4schema.py b/tests/test_p4schema.py index a5e6eccf..73f91ecf 100644 --- a/tests/test_p4schema.py +++ b/tests/test_p4schema.py @@ -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") @@ -144,6 +158,20 @@ def test_p4info_pkginfo(): 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")