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

feat(anta.tests): Adding the test case to verify BGP timers #510

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 65 additions & 1 deletion anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ipaddress import IPv4Address, IPv4Network, IPv6Address
from typing import Any, List, Optional, Union, cast

from pydantic import BaseModel, PositiveInt, model_validator, utils
from pydantic import BaseModel, Field, PositiveInt, model_validator, utils

from anta.custom_types import Afi, MultiProtocolCaps, Safi
from anta.models import AntaCommand, AntaTemplate, AntaTest
Expand Down Expand Up @@ -789,3 +789,67 @@ def test(self) -> None:
self.result.is_success()
else:
self.result.is_failure(f"Following BGP peer route refresh capabilities are not found or not ok:\n{failures}")


class VerifyBGPTimers(AntaTest):
"""
Verifies if the BGP peers are configured with the correct hold and keep-alive timers in the specified VRF.
Expected results:
* success: The test will pass if the hold and keep-alive timers are correct for BGP peers in the specified VRF.
* failure: The test will fail if BGP peers are not found or hold and keep-alive timers are not correct in the specified VRF.
"""

name = "VerifyBGPTimers"
description = "Verifies if the BGP peers are configured with the correct hold and keep alive timers in the specified VRF."
categories = ["routing", "bgp"]
commands = [AntaCommand(command="show bgp neighbors vrf all")]

class Input(AntaTest.Input):
"""
Input parameters for the test.
"""

bgp_peers: List[BgpPeers]
"""List of BGP peers"""

class BgpPeers(BaseModel):
"""
This class defines the details of a BGP peer.
"""

peer_address: IPv4Address
"""IPv4 address of a BGP peer"""
vrf: str = "default"
"""Optional VRF for BGP peer. If not provided, it defaults to `default`."""
hold_time: int = Field(ge=3, le=7200)
"""BGP hold time in seconds"""
keep_alive_time: int = Field(ge=0, le=3600)
"""BGP keep-alive time in seconds"""

@AntaTest.anta_test
def test(self) -> None:
failures: dict[str, Any] = {}

# Iterate over each bgp peer
for bgp_peer in self.inputs.bgp_peers:
peer_address = str(bgp_peer.peer_address)
vrf = bgp_peer.vrf
hold_time = bgp_peer.hold_time
keep_alive_time = bgp_peer.keep_alive_time

# Verify BGP peer
if (
not (bgp_output := get_value(self.instance_commands[0].json_output, f"vrfs.{vrf}.peerList"))
or (bgp_output := get_item(bgp_output, "peerAddress", peer_address)) is None
):
failures[peer_address] = {vrf: "Not configured"}
continue

# Verify BGP peer's hold and keep alive timers
if bgp_output.get("holdTime") != hold_time or bgp_output.get("keepaliveTime") != keep_alive_time:
failures[peer_address] = {vrf: {"hold_time": bgp_output.get("holdTime"), "keep_alive_time": bgp_output.get("keepaliveTime")}}

if not failures:
self.result.is_success()
else:
self.result.is_failure(f"Following BGP peers are not configured or hold and keep-alive timers are not correct:\n{failures}")
10 changes: 10 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ anta.tests.routing:
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
- VerifyBGPTimers:
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
hold_time: 180
keep_alive_time: 60
- peer_address: 172.30.11.5
vrf: default
hold_time: 180
keep_alive_time: 60
ospf:
- VerifyOSPFNeighborState:
- VerifyOSPFNeighborCount:
Expand Down
141 changes: 141 additions & 0 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
VerifyBGPPeerRouteRefreshCap,
VerifyBGPPeersHealth,
VerifyBGPSpecificPeers,
VerifyBGPTimers,
)
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611

Expand Down Expand Up @@ -2406,4 +2407,144 @@
],
},
},
{
"name": "success",
"test": VerifyBGPTimers,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "172.30.11.1",
"holdTime": 180,
"keepaliveTime": 60,
}
]
},
"MGMT": {
"peerList": [
{
"peerAddress": "172.30.11.11",
"holdTime": 180,
"keepaliveTime": 60,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer_address": "172.30.11.1",
"vrf": "default",
"hold_time": 180,
"keep_alive_time": 60,
},
{
"peer_address": "172.30.11.11",
"vrf": "MGMT",
"hold_time": 180,
"keep_alive_time": 60,
},
]
},
"expected": {"result": "success"},
},
{
"name": "failure-no-peer",
"test": VerifyBGPTimers,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "172.30.11.1",
"holdTime": 180,
"keepaliveTime": 60,
}
]
},
"MGMT": {"peerList": []},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer_address": "172.30.11.1",
"vrf": "MGMT",
"hold_time": 180,
"keep_alive_time": 60,
},
{
"peer_address": "172.30.11.11",
"vrf": "MGMT",
"hold_time": 180,
"keep_alive_time": 60,
},
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured or hold and keep-alive timers are not correct:\n"
"{'172.30.11.1': {'MGMT': 'Not configured'}, '172.30.11.11': {'MGMT': 'Not configured'}}"
],
},
},
{
"name": "failure-not-correct-timers",
"test": VerifyBGPTimers,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "172.30.11.1",
"holdTime": 160,
"keepaliveTime": 60,
}
]
},
"MGMT": {
"peerList": [
{
"peerAddress": "172.30.11.11",
"holdTime": 120,
"keepaliveTime": 40,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer_address": "172.30.11.1",
"vrf": "default",
"hold_time": 180,
"keep_alive_time": 60,
},
{
"peer_address": "172.30.11.11",
"vrf": "MGMT",
"hold_time": 180,
"keep_alive_time": 60,
},
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured or hold and keep-alive timers are not correct:\n"
"{'172.30.11.1': {'default': {'hold_time': 160, 'keep_alive_time': 60}}, "
"'172.30.11.11': {'MGMT': {'hold_time': 120, 'keep_alive_time': 40}}}"
],
},
},
]
Loading