Skip to content

Commit

Permalink
issue_766 Added registered protocol optional check to VerifyBFDSpecif…
Browse files Browse the repository at this point in the history
…icPeers
  • Loading branch information
VitthalMagadum committed Aug 1, 2024
1 parent 9735efb commit cf88c64
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 12 deletions.
39 changes: 30 additions & 9 deletions anta/tests/bfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from datetime import datetime, timezone
from ipaddress import IPv4Address
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, Literal

from pydantic import BaseModel, Field

Expand All @@ -24,10 +24,14 @@
class VerifyBFDSpecificPeers(AntaTest):
"""Verifies if the IPv4 BFD peer's sessions are UP and remote disc is non-zero in the specified VRF.
Optionally, it can also verify registered protocols for BFD peers.
Expected Results
----------------
* Success: The test will pass if IPv4 BFD peers are up and remote disc is non-zero in the specified VRF.
Also registered protocol is as defined if specified.
* Failure: The test will fail if IPv4 BFD peers are not found, the status is not UP or remote disc is zero in the specified VRF.
Also registered protocol is not as defined.
Examples
--------
Expand All @@ -39,13 +43,15 @@ class VerifyBFDSpecificPeers(AntaTest):
vrf: default
- peer_address: 192.0.255.7
vrf: default
protocols:
- bgp
```
"""

name = "VerifyBFDSpecificPeers"
description = "Verifies the IPv4 BFD peer's sessions and remote disc in the specified VRF."
categories: ClassVar[list[str]] = ["bfd"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bfd peers", revision=4)]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bfd peers detail", revision=1)]

class Input(AntaTest.Input):
"""Input model for the VerifyBFDSpecificPeers test."""
Expand All @@ -60,16 +66,20 @@ class BFDPeer(BaseModel):
"""IPv4 address of a BFD peer."""
vrf: str = "default"
"""Optional VRF for BFD peer. If not provided, it defaults to `default`."""
protocols: list[Literal["bgp", "isis", "ospf", "eigrp"]] | None = None
"""Optional protocol for BFD peer to check."""

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyBFDSpecificPeers."""
# Initialize failure messages
failures: dict[Any, Any] = {}

# Iterating over BFD peers
# Iterating over BFD peers, extract the parameters and command output
for bfd_peer in self.inputs.bfd_peers:
peer = str(bfd_peer.peer_address)
vrf = bfd_peer.vrf
protocols = bfd_peer.protocols
bfd_output = get_value(
self.instance_commands[0].json_output,
f"vrfs..{vrf}..ipv4Neighbors..{peer}..peerStats..",
Expand All @@ -81,19 +91,30 @@ def test(self) -> None:
failures[peer] = {vrf: "Not Configured"}
continue

# Initializing failure message for each BFD peer
failure = {}

# Check BFD peer status and remote disc
if not (bfd_output.get("status") == "up" and bfd_output.get("remoteDisc") != 0):
failures[peer] = {
vrf: {
"status": bfd_output.get("status"),
"remote_disc": bfd_output.get("remoteDisc"),
}
failure = {
"status": bfd_output.get("status"),
"remote_disc": bfd_output.get("remoteDisc"),
}

# Check registered protocols if specified.
if protocols:
actual_protocols = get_value(bfd_output, "peerStatsDetail.apps")
if not all(protocol in actual_protocols for protocol in protocols):
failure["protocols"] = actual_protocols

# Updated failure messages if any
if failure:
failures[peer] = {vrf: failure}

if not failures:
self.result.is_success()
else:
self.result.is_failure(f"Following BFD peers are not configured, status is not up or remote disc is zero:\n{failures}")
self.result.is_failure(f"For following BFD peers, session parameters are not Ok:\n{failures}")


class VerifyBFDPeersIntervals(AntaTest):
Expand Down
2 changes: 2 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ anta.tests.bfd:
vrf: default
- peer_address: 192.0.255.7
vrf: default
protocols:
- isis
- VerifyBFDPeersIntervals:
bfd_peers:
- peer_address: 192.0.255.8
Expand Down
166 changes: 163 additions & 3 deletions tests/units/anta_tests/test_bfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,55 @@
"inputs": {"bfd_peers": [{"peer_address": "192.0.255.7", "vrf": "default"}, {"peer_address": "192.0.255.70", "vrf": "MGMT"}]},
"expected": {"result": "success"},
},
{
"name": "success-protocols",
"test": VerifyBFDSpecificPeers,
"eos_data": [
{
"vrfs": {
"default": {
"ipv4Neighbors": {
"192.0.255.7": {
"peerStats": {
"": {
"status": "up",
"remoteDisc": 108328132,
"peerStatsDetail": {
"role": "active",
"apps": ["bgp"],
},
}
}
}
}
},
"MGMT": {
"ipv4Neighbors": {
"192.0.255.70": {
"peerStats": {
"": {
"status": "up",
"remoteDisc": 108328132,
"peerStatsDetail": {
"role": "active",
"apps": ["ospf"],
},
}
}
}
}
},
}
}
],
"inputs": {
"bfd_peers": [
{"peer_address": "192.0.255.7", "vrf": "default", "protocols": ["bgp"]},
{"peer_address": "192.0.255.70", "vrf": "MGMT", "protocols": ["ospf"]},
]
},
"expected": {"result": "success"},
},
{
"name": "failure-no-peer",
"test": VerifyBFDSpecificPeers,
Expand Down Expand Up @@ -241,8 +290,7 @@
"expected": {
"result": "failure",
"messages": [
"Following BFD peers are not configured, status is not up or remote disc is zero:\n"
"{'192.0.255.7': {'CS': 'Not Configured'}, '192.0.255.70': {'MGMT': 'Not Configured'}}"
"For following BFD peers, session parameters are not Ok:\n{'192.0.255.7': {'CS': 'Not Configured'}, '192.0.255.70': {'MGMT': 'Not Configured'}}"
],
},
},
Expand Down Expand Up @@ -283,12 +331,124 @@
"expected": {
"result": "failure",
"messages": [
"Following BFD peers are not configured, status is not up or remote disc is zero:\n"
"For following BFD peers, session parameters are not Ok:\n"
"{'192.0.255.7': {'default': {'status': 'Down', 'remote_disc': 108328132}}, "
"'192.0.255.70': {'MGMT': {'status': 'Down', 'remote_disc': 0}}}"
],
},
},
{
"name": "failure-protocols",
"test": VerifyBFDSpecificPeers,
"eos_data": [
{
"vrfs": {
"default": {
"ipv4Neighbors": {
"192.0.255.7": {
"peerStats": {
"": {
"status": "up",
"remoteDisc": 108328132,
"peerStatsDetail": {
"role": "active",
"apps": ["ospf"],
},
}
}
}
}
},
"MGMT": {
"ipv4Neighbors": {
"192.0.255.70": {
"peerStats": {
"": {
"status": "up",
"remoteDisc": 108328132,
"peerStatsDetail": {
"role": "active",
"apps": ["bgp"],
},
}
}
}
}
},
}
}
],
"inputs": {
"bfd_peers": [
{"peer_address": "192.0.255.7", "vrf": "default", "protocols": ["isis"]},
{"peer_address": "192.0.255.70", "vrf": "MGMT", "protocols": ["isis"]},
]
},
"expected": {
"result": "failure",
"messages": [
"For following BFD peers, session parameters are not Ok:\n"
"{'192.0.255.7': {'default': {'protocols': ['ospf']}}, "
"'192.0.255.70': {'MGMT': {'protocols': ['bgp']}}}"
],
},
},
{
"name": "failure-misc",
"test": VerifyBFDSpecificPeers,
"eos_data": [
{
"vrfs": {
"default": {
"ipv4Neighbors": {
"192.0.255.7": {
"peerStats": {
"": {
"status": "down",
"remoteDisc": 0,
"peerStatsDetail": {
"role": "active",
"apps": ["ospf"],
},
}
}
}
}
},
"MGMT": {
"ipv4Neighbors": {
"192.0.255.70": {
"peerStats": {
"": {
"status": "down",
"remoteDisc": 0,
"peerStatsDetail": {
"role": "active",
"apps": ["bgp"],
},
}
}
}
}
},
}
}
],
"inputs": {
"bfd_peers": [
{"peer_address": "192.0.255.7", "vrf": "default", "protocols": ["isis"]},
{"peer_address": "192.0.255.70", "vrf": "MGMT", "protocols": ["isis"]},
]
},
"expected": {
"result": "failure",
"messages": [
"For following BFD peers, session parameters are not Ok:\n"
"{'192.0.255.7': {'default': {'status': 'down', 'remote_disc': 0, 'protocols': ['ospf']}}, "
"'192.0.255.70': {'MGMT': {'status': 'down', 'remote_disc': 0, 'protocols': ['bgp']}}}"
],
},
},
{
"name": "success",
"test": VerifyBFDPeersHealth,
Expand Down

0 comments on commit cf88c64

Please sign in to comment.