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): Verify cvx status #930

Merged
merged 43 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
efd94ae
ruff lint changes
sarunac Nov 8, 2024
d2dd63c
check for cvx server role and enabled
sarunac Nov 8, 2024
d3e2d35
start testing for enabled and cluster_mode
sarunac Nov 14, 2024
94cc001
Merge remote-tracking branch 'origin/main' into verifyCVXStatus
sarunac Nov 14, 2024
8396236
Merge remote-tracking branch 'origin/main' into verifyCVXStatus
sarunac Nov 14, 2024
0dc9b49
add cvx peer validation tests
sarunac Nov 18, 2024
4aa65a6
add test case for registration state and add example
sarunac Nov 18, 2024
1d381a1
Merge branch 'main' into verifyCVXStatus
sarunac Nov 18, 2024
aae913b
update code to reduce its Cognitive Complexity. add additional testfo…
sarunac Nov 19, 2024
e1d671b
Merge branch 'verifyCVXStatus' of https://github.com/sarunac/anta int…
sarunac Nov 19, 2024
cceba67
add test cases for coverage
sarunac Nov 19, 2024
20996bf
Merge branch 'main' into verifyCVXStatus
gmuloc Nov 28, 2024
8bd6eea
Merge branch 'main' into verifyCVXStatus
sarunac Nov 29, 2024
8b57c0c
address review board and update tests
sarunac Nov 29, 2024
fa29f29
Merge branch 'verifyCVXStatus' of https://github.com/sarunac/anta int…
sarunac Nov 29, 2024
060ec48
Update anta/tests/cvx.py
gmuloc Dec 2, 2024
a4adf76
Update anta/tests/cvx.py
gmuloc Dec 2, 2024
8a2f4c5
Merge branch 'main' into verifyCVXStatus
gmuloc Dec 2, 2024
936bd2e
Merge branch 'main' into verifyCVXStatus
sarunac Dec 2, 2024
6e9c0ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 2, 2024
0e8d585
Merge branch 'main' into verifyCVXStatus
sarunac Dec 3, 2024
9b591b7
update docstring to cover different conditions needed
sarunac Dec 3, 2024
d428d51
address review comments
sarunac Dec 3, 2024
73eee54
Merge branch 'main' into verifyCVXStatus
sarunac Dec 4, 2024
8f4a0d2
Merge branch 'main' into verifyCVXStatus
gmuloc Dec 5, 2024
9060253
refactor: Simplify the logic by removing some inner functions
gmuloc Dec 5, 2024
9ead152
Update anta/tests/cvx.py
gmuloc Dec 5, 2024
f5bb5cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 5, 2024
e80f0e9
Update anta/tests/cvx.py
sarunac Dec 6, 2024
8385616
Update anta/tests/cvx.py
sarunac Dec 6, 2024
11737bc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2024
e76cbe4
address review comments
sarunac Dec 9, 2024
5c7a3e2
Merge branch 'verifyCVXStatus' of https://github.com/sarunac/anta int…
sarunac Dec 9, 2024
8bae858
Merge branch 'main' into verifyCVXStatus
sarunac Dec 9, 2024
1757d16
Merge branch 'main' into verifyCVXStatus
gmuloc Dec 12, 2024
cbaacae
Update anta/tests/cvx.py
sarunac Dec 12, 2024
ed903b5
Update tests/units/anta_tests/test_cvx.py
sarunac Dec 12, 2024
a86ea59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 12, 2024
d489e4c
Update tests/units/anta_tests/test_cvx.py
sarunac Dec 12, 2024
1078844
Update anta/tests/cvx.py
gmuloc Dec 12, 2024
7f2190e
Update anta/tests/cvx.py
gmuloc Dec 12, 2024
cad101e
Merge branch 'main' into verifyCVXStatus
gmuloc Dec 12, 2024
93de47a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 12, 2024
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
19 changes: 19 additions & 0 deletions anta/input_models/cvx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Module containing input models for CVX tests."""

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel

from anta.custom_types import Hostname


class CVXPeers(BaseModel):
"""Model for a CVX Cluster Peer."""

peer_name: Hostname
registration_state: Literal["Connecting", "Connected", "Registration error", "Registration complete", "Unexpected peer state"] = "Registration complete"
78 changes: 77 additions & 1 deletion anta/tests/cvx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
# mypy: disable-error-code=attr-defined
from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, Literal
gmuloc marked this conversation as resolved.
Show resolved Hide resolved

from anta.models import AntaCommand, AntaTest

if TYPE_CHECKING:
from anta.models import AntaTemplate
from anta.input_models.cvx import CVXPeers
from anta.tools import get_value


class VerifyMcsClientMounts(AntaTest):
Expand Down Expand Up @@ -87,3 +89,77 @@ def test(self) -> None:
cluster_status = command_output["clusterStatus"]
if (cluster_state := cluster_status.get("enabled")) != self.inputs.enabled:
self.result.is_failure(f"Management CVX status is not valid: {cluster_state}")


class VerifyCVXClusterStatus(AntaTest):
"""Verifies the CVX Server Cluster status.

Expected Results
----------------
* Success: The test will pass if all of the following conditions is met:
- CVX Enabled state is true
- Cluster Mode is true
- Role is either Master or Standby.
- peer_status matches defined state
* Failure: The test will fail if any of the success conditions is not met.

Examples
--------
```yaml
anta.tests.cvx:
- VerifyCVXClusterStatus:
role: Master
peer_status:
- peer_name : cvx-red-2
registration_state: Registration complete
sarunac marked this conversation as resolved.
Show resolved Hide resolved
- peer_name: cvx-red-3
registration_state: Registration error
```
"""

categories: ClassVar[list[str]] = ["cvx"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show cvx", revision=1)]

class Input(AntaTest.Input):
"""Input model for the VerifyCVXClusterStatus test."""

role: Literal["Master", "Standby", "Disconnected"] = "Master"
peer_status: list[CVXPeers]

@AntaTest.anta_test
def test(self) -> None:
"""Run the main test for VerifyCVXClusterStatus."""
command_output = self.instance_commands[0].json_output
self.result.is_success()

# Validate Server enabled status
if not command_output.get("enabled"):
self.result.is_failure("CVX Server status is not enabled")

# Validate cluster status and mode
if not (cluster_status := command_output.get("clusterStatus")) or not command_output.get("clusterMode"):
self.result.is_failure("CVX Server is not a cluster")
return

# Check cluster role
if (cluster_role := cluster_status.get("role")) != self.inputs.role:
self.result.is_failure(f"CVX Role is not valid: {cluster_role}")
return

# Validate peer status
peer_cluster = cluster_status.get("peerStatus", {})

# Check peer count
if (num_of_peers := len(peer_cluster)) != (expected_num_of_peers := len(self.inputs.peer_status)):
self.result.is_failure(f"Unexpected number of peers {num_of_peers} vs {expected_num_of_peers}")

# Check each peer
for peer in self.inputs.peer_status:
# Retrieve the peer status from the peer cluster
if (eos_peer_status := get_value(peer_cluster, peer.peer_name, separator="..")) is None:
self.result.is_failure(f"{peer.peer_name} is not present")
continue

# Validate the registration state of the peer
if (peer_reg_state := eos_peer_status.get("registrationState")) != peer.registration_state:
self.result.is_failure(f"{peer.peer_name} registration state is not complete: {peer_reg_state}")
8 changes: 8 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ anta.tests.connectivity:
df_bit: True
size: 100
anta.tests.cvx:
- VerifyCVXClusterStatus:
# Verifies the CVX Server Cluster status.
role: Master
peer_status:
- peer_name : cvx-red-2
registration_state: Registration complete
- peer_name: cvx-red-3
registration_state: Registration error
- VerifyManagementCVX:
# Verifies the management CVX global status.
enabled: true
Expand Down
158 changes: 157 additions & 1 deletion tests/units/anta_tests/test_cvx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from typing import Any

from anta.tests.cvx import VerifyManagementCVX, VerifyMcsClientMounts
from anta.tests.cvx import VerifyCVXClusterStatus, VerifyManagementCVX, VerifyMcsClientMounts
from tests.units.anta_tests import test

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -146,4 +146,160 @@
"inputs": {"enabled": False},
"expected": {"result": "failure", "messages": ["Management CVX status is not valid: None"]},
},
{
"name": "success-all",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": True,
"clusterStatus": {
"role": "Master",
"peerStatus": {
"cvx-red-2": {"peerName": "cvx-red-2", "registrationState": "Registration complete"},
"cvx-red-3": {"peerName": "cvx-red-3", "registrationState": "Registration complete"},
},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [
{"peer_name": "cvx-red-2", "registrationState": "Registration complete"},
{"peer_name": "cvx-red-3", "registrationState": "Registration complete"},
],
},
"expected": {"result": "success"},
},
{
"name": "failure-invalid-role",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": True,
"clusterStatus": {
"role": "Standby",
"peerStatus": {
"cvx-red-2": {"peerName": "cvx-red-2", "registrationState": "Registration complete"},
"cvx-red-3": {"peerName": "cvx-red-3", "registrationState": "Registration complete"},
},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [
{"peer_name": "cvx-red-2", "registrationState": "Registration complete"},
{"peer_name": "cvx-red-3", "registrationState": "Registration complete"},
],
},
"expected": {"result": "failure", "messages": ["CVX Role is not valid: Standby"]},
},
{
"name": "failure-cvx-enabled",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": False,
"clusterMode": True,
"clusterStatus": {
"role": "Master",
"peerStatus": {},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [],
},
"expected": {"result": "failure", "messages": ["CVX Server status is not enabled"]},
},
{
"name": "failure-cluster-enabled",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": False,
"clusterStatus": {},
}
],
"inputs": {
"role": "Master",
"peer_status": [],
},
"expected": {"result": "failure", "messages": ["CVX Server is not a cluster"]},
},
{
"name": "failure-missing-peers",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": True,
"clusterStatus": {
"role": "Master",
"peerStatus": {
"cvx-red-2": {"peerName": "cvx-red-2", "registrationState": "Registration complete"},
},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [
{"peer_name": "cvx-red-2", "registrationState": "Registration complete"},
{"peer_name": "cvx-red-3", "registrationState": "Registration complete"},
],
},
"expected": {"result": "failure", "messages": ["Unexpected number of peers 1 vs 2", "cvx-red-3 is not present"]},
},
{
"name": "failure-invalid-peers",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": True,
"clusterStatus": {
"role": "Master",
"peerStatus": {},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [
{"peer_name": "cvx-red-2", "registrationState": "Registration complete"},
{"peer_name": "cvx-red-3", "registrationState": "Registration complete"},
],
},
"expected": {"result": "failure", "messages": ["Unexpected number of peers 0 vs 2", "cvx-red-2 is not present", "cvx-red-3 is not present"]},
},
{
"name": "failure-registration-error",
"test": VerifyCVXClusterStatus,
"eos_data": [
{
"enabled": True,
"clusterMode": True,
"clusterStatus": {
"role": "Master",
"peerStatus": {
"cvx-red-2": {"peerName": "cvx-red-2", "registrationState": "Registration error"},
"cvx-red-3": {"peerName": "cvx-red-3", "registrationState": "Registration complete"},
},
},
}
],
"inputs": {
"role": "Master",
"peer_status": [
{"peer_name": "cvx-red-2", "registrationState": "Registration complete"},
{"peer_name": "cvx-red-3", "registrationState": "Registration complete"},
],
},
"expected": {"result": "failure", "messages": ["cvx-red-2 registration state is not complete: Registration error"]},
},
]
Loading