-
Notifications
You must be signed in to change notification settings - Fork 32
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): Add test for Maintenance mode #1067
base: main
Are you sure you want to change the base?
Changes from all commits
5890528
d81bcd2
5b5b51c
c3aee91
e1a31b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -345,3 +345,68 @@ def test(self) -> None: | |||||||
|
||||||||
if act_condition != exp_condition or act_stratum != exp_stratum: | ||||||||
self.result.is_failure(f"{ntp_server} - Bad association - Condition: {act_condition}, Stratum: {act_stratum}") | ||||||||
|
||||||||
|
||||||||
class VerifyMaintenance(AntaTest): | ||||||||
"""Verifies that the device is not currently under or entering maintenance. | ||||||||
|
||||||||
Expected Results | ||||||||
---------------- | ||||||||
* Success: The test will pass if the device is not under or entering maintenance. | ||||||||
* Failure: The test will fail if the device is under or entering maintenance. | ||||||||
|
||||||||
Examples | ||||||||
-------- | ||||||||
```yaml | ||||||||
anta.tests.system: | ||||||||
- VerifyMaintenance: | ||||||||
``` | ||||||||
""" | ||||||||
|
||||||||
categories: ClassVar[list[str]] = ["Maintenance"] | ||||||||
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show maintenance", revision=1)] | ||||||||
|
||||||||
@AntaTest.anta_test | ||||||||
def test(self) -> None: | ||||||||
"""Main test function for VerifyMaintenance.""" | ||||||||
self.result.is_success() | ||||||||
|
||||||||
# If units is not empty we have to examine the output for details. | ||||||||
if units := get_value(self.instance_commands[0].json_output, "units"): | ||||||||
unitsundermaintenance = [] | ||||||||
unitsenteringmaintenance = [] | ||||||||
under = False | ||||||||
entering = False | ||||||||
Comment on lines
+378
to
+379
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the flags as you just need to check if the lists are empty or not |
||||||||
causes = [] | ||||||||
# Iterate over units, check for units under or entering maintenance, and examine the causes. | ||||||||
for unit, info in units.items(): | ||||||||
if info["state"] == "underMaintenance": | ||||||||
unitsundermaintenance.append(unit) | ||||||||
under = True | ||||||||
Comment on lines
+384
to
+385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
elif info["state"] == "maintenanceModeEnter": | ||||||||
unitsenteringmaintenance.append(unit) | ||||||||
entering = True | ||||||||
Comment on lines
+387
to
+388
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if info["adminState"] == "underMaintenance" and "Quiesce is configured" not in causes: | ||||||||
causes.append("Quiesce is configured") | ||||||||
if info["onBootMaintenance"] and "On-boot maintenance is configured" not in causes: | ||||||||
causes.append("On-boot maintenance is configured") | ||||||||
if info["intfsViolatingTrafficThreshold"] and "Interface traffic threshold violation" not in causes: | ||||||||
causes.append("Interface traffic threshold violation") | ||||||||
|
||||||||
# This can occur if maintenance is configured but no unit is configured with 'quiesce'. | ||||||||
if not under and not entering and not causes: | ||||||||
self.result.is_success() | ||||||||
Comment on lines
+397
to
+398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you already set is_success at the beginning so you don't need this. you can just return |
||||||||
|
||||||||
# Building the error message. | ||||||||
else: | ||||||||
message = "" | ||||||||
unitsundermaintenance = ", ".join(unitsundermaintenance) | ||||||||
unitsenteringmaintenance = " ".join(unitsenteringmaintenance) | ||||||||
causes = ", ".join(causes) | ||||||||
if under: | ||||||||
message += f"Units under maintenance: '{unitsundermaintenance}'. " | ||||||||
if entering: | ||||||||
message += f"Units entering maintenance: '{unitsenteringmaintenance}'. " | ||||||||
if len(causes) > 0: | ||||||||
message += f"Possible causes: {causes}" | ||||||||
self.result.is_failure(message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.