Skip to content

Commit

Permalink
issue-493: removed ipv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB committed Jan 9, 2024
1 parent 4592b38 commit d368089
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 73 deletions.
28 changes: 12 additions & 16 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from anta.custom_types import Afi, Safi
from anta.models import AntaCommand, AntaTemplate, AntaTest
from anta.tools.get_item import get_item
from anta.tools.get_value import get_value
from anta.tools.utils import create_index

# Need to keep List for pydantic in python 3.8

Expand Down Expand Up @@ -461,7 +461,7 @@ class VerifyBGPPeerMD5Auth(AntaTest):
name = "VerifyBGPPeerMD5Auth"
description = "Verifies the MD5 authentication and state of BGP peers in a specified VRF"
categories = ["routing", "bgp"]
commands = [AntaCommand(command="show bgp neighbors")]
commands = [AntaCommand(command="show bgp neighbors vrf all")]

class Input(AntaTest.Input):
"""
Expand All @@ -476,37 +476,33 @@ class BgpPeers(BaseModel):
This class defines the details of a BGP peer.
"""

peer: Union[IPv4Address, IPv6Address]
"""IPv4/IPv6 BGP peer"""
peer_address: IPv4Address
"""IPv4 address of a BGP peer."""
vrf: str = "default"
"""VRF context"""
"""Optional VRF for BGP peer. If not provided, it defaults to `default`."""

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

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

# Check if BGP output exists
if not (bgp_output := get_value(self.instance_commands[0].json_output, f"vrfs.{vrf}.peerList")):
failures[str(peer)] = {vrf: "Not Configured"}
continue

bgp_index = create_index(bgp_output, "peerAddress")
bgp_output = bgp_index.get(peer)
if not bgp_output:
failures[str(peer)] = {vrf: "Not Configured"}
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)) is None
):
failures.setdefault("bgp_peers", {})[peer] = {vrf: "Not configured"}
continue

# Check if BGP peer state and authentication
bgp_output = bgp_output[0]
state = bgp_output.get("state")
md5_auth_enabled = bgp_output.get("md5AuthEnabled")
if state != "Established" or not md5_auth_enabled:
failures[str(peer)] = {vrf: {"state": state, "md5_auth_enabled": md5_auth_enabled}}
failures.setdefault("bgp_peers", {})[peer] = {vrf: {"state": state, "md5_auth_enabled": md5_auth_enabled}}

# Check if there are any failures
if not failures:
Expand Down
36 changes: 0 additions & 36 deletions anta/tools/utils.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ anta.tests.routing:
- 10.1.255.4
- VerifyBGPPeerMD5Auth:
bgp_peers:
- peer: 172.30.11.1
- peer_address: 172.30.11.1
vrf: default
- peer: 172.30.11.5
- peer_address: 172.30.11.5
vrf: default
ospf:
- VerifyOSPFNeighborState:
Expand Down
101 changes: 82 additions & 19 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,36 +1253,64 @@
"md5AuthEnabled": True,
}
]
}
},
"CS": {
"peerList": [
{
"peerAddress": "172.30.11.10",
"state": "Established",
"md5AuthEnabled": True,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer": "172.30.11.1",
"peer_address": "172.30.11.1",
"vrf": "default",
}
},
{
"peer_address": "172.30.11.10",
"vrf": "CS",
},
]
},
"expected": {"result": "success"},
},
{
"name": "failure-no-vrf",
"test": VerifyBGPPeerMD5Auth,
"eos_data": [{"vrfs": {}}],
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "172.30.11.10",
"state": "Established",
"md5AuthEnabled": True,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer": "172.30.11.1",
"peer_address": "172.30.11.1",
"vrf": "MGMT",
}
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n{'172.30.11.1': {'MGMT': 'Not Configured'}}"
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n"
"{'bgp_peers': {'172.30.11.1': {'MGMT': 'Not configured'}}}"
],
},
},
Expand All @@ -1300,22 +1328,36 @@
"md5AuthEnabled": True,
}
]
}
},
"CS": {
"peerList": [
{
"peerAddress": "172.30.11.11",
"state": "Established",
"md5AuthEnabled": True,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer": "172.30.11.10",
"peer_address": "172.30.11.10",
"vrf": "default",
}
},
{
"peer_address": "172.30.11.11",
"vrf": "default",
},
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n{'172.30.11.10': {'default': 'Not Configured'}}"
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n"
"{'bgp_peers': {'172.30.11.10': {'default': 'Not configured'}, '172.30.11.11': {'default': 'Not configured'}}}"
],
},
},
Expand All @@ -1333,23 +1375,37 @@
"md5AuthEnabled": True,
}
]
}
},
"MGMT": {
"peerList": [
{
"peerAddress": "172.30.11.10",
"state": "Idle",
"md5AuthEnabled": False,
}
]
},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer": "172.30.11.1",
"peer_address": "172.30.11.1",
"vrf": "default",
}
},
{
"peer_address": "172.30.11.10",
"vrf": "MGMT",
},
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n"
"{'172.30.11.1': {'default': {'state': 'Idle', 'md5_auth_enabled': True}}}"
"{'bgp_peers': {'172.30.11.1': {'default': {'state': 'Idle', 'md5_auth_enabled': True}}, "
"'172.30.11.10': {'MGMT': {'state': 'Idle', 'md5_auth_enabled': False}}}}"
],
},
},
Expand All @@ -1364,25 +1420,32 @@
{
"peerAddress": "172.30.11.1",
"state": "Established",
}
},
{"peerAddress": "172.30.11.10", "state": "Established", "md5AuthEnabled": False},
]
}
},
"MGMT": {"peerList": [{"peerAddress": "172.30.11.11", "state": "Established", "md5AuthEnabled": False}]},
}
}
],
"inputs": {
"bgp_peers": [
{
"peer": "172.30.11.1",
"peer_address": "172.30.11.1",
"vrf": "default",
}
},
{
"peer_address": "172.30.11.11",
"vrf": "MGMT",
},
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n"
"{'172.30.11.1': {'default': {'state': 'Established', 'md5_auth_enabled': None}}}"
"{'bgp_peers': {'172.30.11.1': {'default': {'state': 'Established', 'md5_auth_enabled': None}}, "
"'172.30.11.11': {'MGMT': {'state': 'Established', 'md5_auth_enabled': False}}}}"
],
},
},
Expand Down

0 comments on commit d368089

Please sign in to comment.