From fe94dfb60fcdbcf7c2554e7e4521245036924997 Mon Sep 17 00:00:00 2001 From: Sakti Date: Mon, 28 Oct 2024 13:45:08 -0700 Subject: [PATCH] feat(anta): Management cvx enabled (#896) --- anta/tests/configuration.py | 38 ++++++++++++++++++++ examples/tests.yaml | 2 ++ tests/units/anta_tests/test_configuration.py | 35 +++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/anta/tests/configuration.py b/anta/tests/configuration.py index 4a1bd31d1..186deaf2d 100644 --- a/anta/tests/configuration.py +++ b/anta/tests/configuration.py @@ -131,3 +131,41 @@ def test(self) -> None: self.result.is_success() else: self.result.is_failure("Following patterns were not found: " + ",".join(failure_msgs)) + + +class VerifyManagementCVX(AntaTest): + """Verifies the management CVX global status. + + Expected Results + ---------------- + * Success: The test will pass if the management CVX global status matches the expected status. + * Failure: The test will fail if the management CVX global status does not match the expected status. + + Examples + -------- + ```yaml + anta.tests.configuration: + - VerifyManagementCVX: + enabled: true + ``` + """ + + name = "VerifyManagementCVX" + description = "Verifies the management CVX global status." + categories: ClassVar[list[str]] = ["configuration"] + commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show management cvx", revision=1)] + + class Input(AntaTest.Input): + """Input model for the VerifyManagementCVX test.""" + + enabled: bool + """Whether management CVX must be enabled (True) or disabled (False).""" + + @AntaTest.anta_test + def test(self) -> None: + """Main test function for VerifyManagementCVX.""" + command_output = self.instance_commands[0].json_output + self.result.is_success() + 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}") diff --git a/examples/tests.yaml b/examples/tests.yaml index d8f3332ae..3dacccb55 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -98,6 +98,8 @@ anta.tests.configuration: regex_patterns: - "^enable password.*$" - "bla bla" + - VerifyManagementCVX: + enabled: true anta.tests.connectivity: - VerifyReachability: diff --git a/tests/units/anta_tests/test_configuration.py b/tests/units/anta_tests/test_configuration.py index d8f86beaa..625619d4c 100644 --- a/tests/units/anta_tests/test_configuration.py +++ b/tests/units/anta_tests/test_configuration.py @@ -7,7 +7,7 @@ from typing import Any -from anta.tests.configuration import VerifyRunningConfigDiffs, VerifyRunningConfigLines, VerifyZeroTouch +from anta.tests.configuration import VerifyManagementCVX, VerifyRunningConfigDiffs, VerifyRunningConfigLines, VerifyZeroTouch from tests.units.anta_tests import test DATA: list[dict[str, Any]] = [ @@ -60,4 +60,37 @@ "inputs": {"regex_patterns": ["bla", "bleh"]}, "expected": {"result": "failure", "messages": ["Following patterns were not found: 'bla','bleh'"]}, }, + { + "name": "success-enabled", + "test": VerifyManagementCVX, + "eos_data": [ + { + "clusterStatus": { + "enabled": True, + } + } + ], + "inputs": {"enabled": True}, + "expected": {"result": "success"}, + }, + { + "name": "success-disabled", + "test": VerifyManagementCVX, + "eos_data": [ + { + "clusterStatus": { + "enabled": False, + } + } + ], + "inputs": {"enabled": False}, + "expected": {"result": "success"}, + }, + { + "name": "failure", + "test": VerifyManagementCVX, + "eos_data": [{"clusterStatus": {}}], + "inputs": {"enabled": False}, + "expected": {"result": "failure", "messages": ["Management CVX status is not valid: None"]}, + }, ]