Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(anta.tests): Updated testcase to support more address family #566

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions anta/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def bgp_multiprotocol_capabilities_abbreviations(value: str) -> str:
BeforeValidator(interface_autocomplete),
BeforeValidator(interface_case_sensitivity),
]
Afi = Literal["ipv4", "ipv6", "vpn-ipv4", "vpn-ipv6", "evpn", "rt-membership"]
Safi = Literal["unicast", "multicast", "labeled-unicast"]
Afi = Literal["ipv4", "ipv6", "vpn-ipv4", "vpn-ipv6", "evpn", "rt-membership", "path-selection", "link-state"]
Safi = Literal["unicast", "multicast", "labeled-unicast", "sr-te"]
EncryptionAlgorithm = Literal["RSA", "ECDSA"]
RsaKeySize = Literal[2048, 3072, 4096]
EcdsaKeySize = Literal[256, 384, 521]
Expand Down
49 changes: 40 additions & 9 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def _add_bgp_routes_failure(
class VerifyBGPPeerCount(AntaTest):
"""Verifies the count of BGP peers for a given address family.

It supports multiple types of address families (AFI) and subsequent service families (SAFI).
It supports multiple types of Address Families Identifiers (AFI) and Subsequent Address Family Identifiers (SAFI).

For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6 (AFI) which is handled automatically in this test.

Please refer to the Input class attributes below for details.

Expand All @@ -176,7 +178,7 @@ class Input(AntaTest.Input):
"""List of BGP address families (BgpAfi)."""

class BgpAfi(BaseModel):
"""Model for a BGP address family (AFI) and subsequent service family (SAFI)."""
"""Model for a BGP address family (AFI) and subsequent address family (SAFI)."""

afi: Afi
"""BGP address family (AFI)."""
Expand Down Expand Up @@ -218,8 +220,12 @@ def render(self, template: AntaTemplate) -> list[AntaCommand]:
"""Render the template for each BGP address family in the input list."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is a docstring that deserves more info given the "dark magic" in the rendering function

commands = []
for afi in self.inputs.address_families:
if template == VerifyBGPPeerCount.commands[0] and afi.afi in ["ipv4", "ipv6"]:
if template == VerifyBGPPeerCount.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi != "sr-te":
commands.append(template.render(afi=afi.afi, safi=afi.safi, vrf=afi.vrf, num_peers=afi.num_peers))

# For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6
elif template == VerifyBGPPeerCount.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi == "sr-te":
commands.append(template.render(afi=afi.safi, safi=afi.afi, vrf=afi.vrf, num_peers=afi.num_peers))
elif template == VerifyBGPPeerCount.commands[1] and afi.afi not in ["ipv4", "ipv6"]:
commands.append(template.render(afi=afi.afi, vrf=afi.vrf, num_peers=afi.num_peers))
return commands
Expand All @@ -240,6 +246,10 @@ def test(self) -> None:
afi_vrf = cast(str, command.params.get("vrf"))
num_peers = cast(PositiveInt, command.params.get("num_peers"))

# Swaping AFI and SAFI in case of SR-TE
if afi == "sr-te":
afi, safi = safi, afi

if not (vrfs := command_output.get("vrfs")):
_add_bgp_failures(failures=failures, afi=afi, safi=safi, vrf=afi_vrf, issue="Not Configured")
continue
Expand All @@ -262,7 +272,9 @@ class VerifyBGPPeersHealth(AntaTest):

It will validate that all BGP sessions are established and all message queues for these BGP sessions are empty for a given address family.

It supports multiple types of address families (AFI) and subsequent service families (SAFI).
It supports multiple types of Address Families Identifiers (AFI) and Subsequent Address Family Identifiers (SAFI).

For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6 (AFI) which is handled automatically in this test.

Please refer to the Input class attributes below for details.

Expand All @@ -286,7 +298,7 @@ class Input(AntaTest.Input):
"""List of BGP address families (BgpAfi)."""

class BgpAfi(BaseModel):
"""Model for a BGP address family (AFI) and subsequent service family (SAFI)."""
"""Model for a BGP address family (AFI) and subsequent address family (SAFI)."""

afi: Afi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we work to avoid the duplication of BgpAfi in every test?

"""BGP address family (AFI)."""
Expand Down Expand Up @@ -326,8 +338,12 @@ def render(self, template: AntaTemplate) -> list[AntaCommand]:
"""Render the template for each BGP address family in the input list."""
commands = []
for afi in self.inputs.address_families:
if template == VerifyBGPPeersHealth.commands[0] and afi.afi in ["ipv4", "ipv6"]:
if template == VerifyBGPPeersHealth.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi != "sr-te":
commands.append(template.render(afi=afi.afi, safi=afi.safi, vrf=afi.vrf))

# For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6
elif template == VerifyBGPPeersHealth.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi == "sr-te":
commands.append(template.render(afi=afi.safi, safi=afi.afi, vrf=afi.vrf))
elif template == VerifyBGPPeersHealth.commands[1] and afi.afi not in ["ipv4", "ipv6"]:
commands.append(template.render(afi=afi.afi, vrf=afi.vrf))
return commands
Expand All @@ -344,6 +360,10 @@ def test(self) -> None:

afi = cast(Afi, command.params.get("afi"))
safi = cast(Optional[Safi], command.params.get("safi"))

# Swaping AFI and SAFI in case of SR-TE
if afi == "sr-te":
afi, safi = safi, afi
afi_vrf = cast(str, command.params.get("vrf"))

if not (vrfs := command_output.get("vrfs")):
Expand Down Expand Up @@ -374,7 +394,9 @@ class VerifyBGPSpecificPeers(AntaTest):

It will validate that the BGP session is established and all message queues for this BGP session are empty for the given peer(s).

It supports multiple types of address families (AFI) and subsequent service families (SAFI).
It supports multiple types of Address Families Identifiers (AFI) and Subsequent Address Family Identifiers (SAFI).

For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6 (AFI) which is handled automatically in this test.

Please refer to the Input class attributes below for details.

Expand All @@ -398,7 +420,7 @@ class Input(AntaTest.Input):
"""List of BGP address families (BgpAfi)."""

class BgpAfi(BaseModel):
"""Model for a BGP address family (AFI) and subsequent service family (SAFI)."""
"""Model for a BGP address family (AFI) and subsequent address family (SAFI)."""

afi: Afi
"""BGP address family (AFI)."""
Expand Down Expand Up @@ -444,9 +466,14 @@ def validate_inputs(self: BaseModel) -> BaseModel:
def render(self, template: AntaTemplate) -> list[AntaCommand]:
"""Render the template for each BGP address family in the input list."""
commands = []

for afi in self.inputs.address_families:
if template == VerifyBGPSpecificPeers.commands[0] and afi.afi in ["ipv4", "ipv6"]:
if template == VerifyBGPSpecificPeers.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi != "sr-te":
commands.append(template.render(afi=afi.afi, safi=afi.safi, vrf=afi.vrf, peers=afi.peers))

# For SR-TE SAFI, the EOS command supports sr-te first then ipv4/ipv6
elif template == VerifyBGPSpecificPeers.commands[0] and afi.afi in ["ipv4", "ipv6"] and afi.safi == "sr-te":
commands.append(template.render(afi=afi.safi, safi=afi.afi, vrf=afi.vrf, peers=afi.peers))
elif template == VerifyBGPSpecificPeers.commands[1] and afi.afi not in ["ipv4", "ipv6"]:
commands.append(template.render(afi=afi.afi, vrf=afi.vrf, peers=afi.peers))
return commands
Expand All @@ -463,6 +490,10 @@ def test(self) -> None:

afi = cast(Afi, command.params.get("afi"))
safi = cast(Optional[Safi], command.params.get("safi"))

# Swaping AFI and SAFI in case of SR-TE
if afi == "sr-te":
afi, safi = safi, afi
afi_vrf = cast(str, command.params.get("vrf"))
afi_peers = cast(List[Union[IPv4Address, IPv6Address]], command.params.get("peers", []))

Expand Down
Loading
Loading