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 neighbor routes #484

Merged
merged 11 commits into from
Jan 24, 2024

Conversation

MaheshGSLAB
Copy link
Collaborator

@MaheshGSLAB MaheshGSLAB commented Dec 5, 2023

Description

Add the test case to verify the BGP neighbor routes

Run the testcase by adding catalog as below:

anta.tests.routing:
  bgp:
    - VerifyBGPExchangedRoutes:
        bgp_peers:
          - peer_address: 172.30.255.5
            vrf: default
            advertised_routes:
              - 192.0.254.5/32
            received_routes:
              - 192.0.255.4/32
          - peer_address: 172.30.255.1
            vrf: default
            advertised_routes:
              - 192.0.255.1/32
              - 192.0.254.5/32
            received_routes:
              - 192.0.254.3/32
  1. Testcase pass when routes are correct:
                                                                                          All tests results                                                                                           
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Device    ┃ Test Name                ┃ Test Status ┃ Message(s) ┃ Test description                                                                                                 ┃ Test category ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ s1-spine1 │ VerifyBGPExchangedRoutes │ success     │            │ Verifies if BGP peers have correctly advertised/received routes with type as valid and active for a specified    │ routing, bgp  │
│           │                          │             │            │ VRF.                                                                                                             │               │
└───────────┴──────────────────────────┴─────────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────────┘
  1. Testcase fail when routes are missing:
                                                                                          All tests results                                                                                           
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Device    ┃ Test Name                ┃ Test Status ┃ Message(s)                                                    ┃ Test description                                              ┃ Test category ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ s1-spine1 │ VerifyBGPExchangedRoutes │ failure     │ Following BGP peers are not found or routes are not exchanged │ Verifies if BGP peers have correctly advertised/received      │ routing, bgp  │
│           │                          │             │ properly:                                                     │ routes with type as valid and active for a specified VRF.     │               │
│           │                          │             │ {'bgp_peers': {'172.30.11.1': {'MGMT': 'Not configured'}}}    │                                                               │               │
└───────────┴──────────────────────────┴─────────────┴───────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────┴───────────────┘
  1. Testcase fail when routes are invalid or inactive
 
                                                                                          All tests results                                                                                           
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Device    ┃ Test Name                ┃ Test Status ┃ Message(s)                                                    ┃ Test description                                              ┃ Test category ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ s1-spine1 │ VerifyBGPExchangedRoutes │ failure     │ Following BGP peers are not found or routes are not exchanged │ Verifies if BGP peers have correctly advertised/received      │ routing, bgp  │
│           │                          │             │ properly:                                                     │ routes with type as valid and active for a specified VRF.     │               │
│           │                          │             │ {'bgp_peers': {'172.30.11.1': {'default':                     │                                                               │               │
│           │                          │             │ {'advertised_routes': {'192.0.254.31/32': 'Not found'},       │                                                               │               │
│           │                          │             │ 'received_routes': {'192.0.255.4/32': {'valid': False,        │                                                               │               │
│           │                          │             │ 'active': True}}}}, '172.30.11.5': {'default':                │                                                               │               │
│           │                          │             │ {'advertised_routes': {'192.2.254.31/32': 'Not found'},       │                                                               │               │
│           │                          │             │ 'received_routes': {'192.0.255.41/32': 'Not found'}}}}}       │                                                               │               │
└───────────┴──────────────────────────┴─────────────┴───────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────┴───────────────┘

Fixes #482

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have run pre-commit for code linting and typing (pre-commit run)
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (tox -e testenv)

@MaheshGSLAB MaheshGSLAB self-assigned this Dec 5, 2023
@MaheshGSLAB MaheshGSLAB marked this pull request as draft December 5, 2023 11:56
@MaheshGSLAB MaheshGSLAB requested review from carl-baillargeon and removed request for carl-baillargeon December 5, 2023 11:56
@MaheshGSLAB MaheshGSLAB marked this pull request as ready for review December 7, 2023 10:07
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
tests/units/anta_tests/routing/test_bgp.py Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
anta/tests/routing/bgp.py Outdated Show resolved Hide resolved
@@ -132,6 +132,61 @@ def _check_peer_issues(peer_data: Optional[dict[str, Any]]) -> dict[str, Any]:
return {}


def _add_bgp_routes_failure(
bgp_routes: List[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
bgp_routes: List[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes"
bgp_routes: list[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes"

It identifies any missing routes as well as any routes that are invalid or inactive. The results are returned in a dictionary.

Parameters:
bgp_routes (List[str]): The list of expected routes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
bgp_routes (List[str]): The list of expected routes.
bgp_routes (list[str]): The list of expected routes.

Comment on lines 175 to 181
missing_routes = [route for route in bgp_routes if route not in bgp_output]
invalid_or_inactive = [
route
for route in bgp_routes
if route in bgp_output
and (not bgp_output[route]["bgpRoutePaths"][0]["routeType"]["valid"] or not bgp_output[route]["bgpRoutePaths"][0]["routeType"]["active"])
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here you are looping twice over the bgp_routes so it is probably more efficient to use a for loop and loop only once:

for route in bgp_routes:
  if not route in bgp_output:
    missing_routes.append(route)
    continue
  if (big condition):
    invalid_or_inactive.append(route)

received_routes = command.params.get("received_routes", [])

# Verify if BGP neighbor is configured with provided vrf
if (bgp_routes := get_value(command.json_output, f"vrfs..{vrf}", separator="..")) is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need .. as separator here?

# Verify if BGP neighbor is configured with provided vrf
if (bgp_routes := get_value(command.json_output, f"vrfs..{vrf}", separator="..")) is None:
self.result.is_failure(f"BGP neighbor {str(neighbor)} is not configured for `{vrf}` VRF.")
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you return here then you won't check all the commands right? So maybe you will miss some failures in other commands that you could report

Would it not be better with

Suggested change
return
continue

?

(same below)

tests/units/anta_tests/routing/test_bgp.py Show resolved Hide resolved
Comment on lines 1379 to 1624
{
"neighbor": "172.30.11.1",
"vrf": "default",
"advertised_routes": ["192.0.254.31/32"],
"received_routes": ["192.0.255.31/32"],
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

test with more than one neighbor in your inputs, in particular with one success and one failure and check that the test is failing

@MaheshGSLAB MaheshGSLAB requested a review from gmuloc January 4, 2024 10:34
@MaheshGSLAB MaheshGSLAB changed the title feat: Adding the test case to verify BGP neighbor routes feat(anta.tests)!: Adding the test case to verify BGP neighbor routes Jan 4, 2024
# Check if the route is missing in the BGP output
if route not in bgp_output:
# If missing, add it to the failure routes dictionary
failure_routes.setdefault("bgp_peers", {}).setdefault(peer, {}).setdefault(vrf, {}).setdefault(route_type, {})[route] = "Not found"
Copy link
Collaborator

Choose a reason for hiding this comment

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

initialize this at the begining of the function with the proper structure and use a flag to track if a failure happened

this chain of setdefault is dificult to understand

Comment on lines 552 to 555
peer = str(command.params.get("peer", ""))
vrf = command.params.get("vrf", "")
advertised_routes = command.params.get("advertised_routes", [])
received_routes = command.params.get("received_routes", [])
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed, in recent ANTA refactor, it looks like command.params is properly typed as Dict[str, Any] so we can simply do a dict lookup without mypy complaining. No need to bother with .get() or cast anymore.

@gmuloc gmuloc merged commit d6bb7cb into aristanetworks:main Jan 24, 2024
19 checks passed
@gmuloc gmuloc changed the title feat(anta.tests)!: Adding the test case to verify BGP neighbor routes feat(anta.tests): Adding the test case to verify BGP neighbor routes Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the test case to verify the BGP redistribution
4 participants