Skip to content

Commit

Permalink
issue-493: updated the setdefault method
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB committed Jan 12, 2024
1 parent 5a38c41 commit 0c9b6c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions 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, IPv6Address
from typing import Any, List, Optional, Union, cast

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

from anta.custom_types import Afi, Safi
from anta.models import AntaCommand, AntaTemplate, AntaTest
Expand Down Expand Up @@ -483,29 +483,32 @@ class BgpPeers(BaseModel):

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

# Iterate over each command
for bgp_peer in self.inputs.bgp_peers:
peer = str(bgp_peer.peer_address)
vrf = bgp_peer.vrf
failure: dict[str, dict[str, dict[str, Any]]] = {"bgp_peers": {peer: {vrf: {}}}}

# Check if BGP output exists
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"}
failure["bgp_peers"][peer][vrf] = {"status": "Not configured"}
failures = utils.deep_update(failures, failure)
continue

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

# Check if there are any failures
if not failures:
if not failures["bgp_peers"]:
self.result.is_success()
else:
self.result.is_failure(f"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n{failures}")
4 changes: 2 additions & 2 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@
"result": "failure",
"messages": [
"Following BGP peers are not configured, not established or MD5 authentication is not enabled:\n"
"{'bgp_peers': {'172.30.11.1': {'MGMT': 'Not configured'}}}"
"{'bgp_peers': {'172.30.11.1': {'MGMT': {'status': 'Not configured'}}}}"
],
},
},
Expand Down Expand Up @@ -1357,7 +1357,7 @@
"result": "failure",
"messages": [
"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'}}}"
"{'bgp_peers': {'172.30.11.10': {'default': {'status': 'Not configured'}}, '172.30.11.11': {'default': {'status': 'Not configured'}}}}"
],
},
},
Expand Down

0 comments on commit 0c9b6c6

Please sign in to comment.