Skip to content

Commit

Permalink
feat(anta.tests): Added testcase to validate STUN server status (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB authored Jun 28, 2024
1 parent 3382dfe commit 8129184
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
39 changes: 39 additions & 0 deletions anta/tests/stun.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,42 @@ def test(self) -> None:
if actual_stun_data != expected_stun_data:
failed_log = get_failed_logs(expected_stun_data, actual_stun_data)
self.result.is_failure(f"For STUN source `{source_address}:{source_port}`:{failed_log}")


class VerifyStunServer(AntaTest):
"""
Verifies the STUN server status is enabled and running.
Expected Results
----------------
* Success: The test will pass if the STUN server status is enabled and running.
* Failure: The test will fail if the STUN server is disabled or not running.
Examples
--------
```yaml
anta.tests.stun:
- VerifyStunServer:
```
"""

name = "VerifyStunServer"
description = "Verifies the STUN server status is enabled and running."
categories: ClassVar[list[str]] = ["stun"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show stun server status", revision=1)]

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyStunServer."""
command_output = self.instance_commands[0].json_output
status_disabled = not command_output.get("enabled")
not_running = command_output.get("pid") == 0

if status_disabled and not_running:
self.result.is_failure("STUN server status is disabled and not running.")
elif status_disabled:
self.result.is_failure("STUN server status is disabled.")
elif not_running:
self.result.is_failure("STUN server is not running.")
else:
self.result.is_success()
1 change: 1 addition & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ anta.tests.stun:
public_address: 100.64.3.21
source_port: 4500
public_port: 6006
- VerifyStunServer:

anta.tests.system:
- VerifyUptime:
Expand Down
59 changes: 58 additions & 1 deletion tests/units/anta_tests/test_stun.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from typing import Any

from anta.tests.stun import VerifyStunClient
from anta.tests.stun import VerifyStunClient, VerifyStunServer
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -173,4 +173,61 @@
],
},
},
{
"name": "success",
"test": VerifyStunServer,
"eos_data": [
{
"enabled": True,
"pid": 1895,
}
],
"inputs": {},
"expected": {"result": "success"},
},
{
"name": "failure-disabled",
"test": VerifyStunServer,
"eos_data": [
{
"enabled": False,
"pid": 1895,
}
],
"inputs": {},
"expected": {
"result": "failure",
"messages": ["STUN server status is disabled."],
},
},
{
"name": "failure-not-running",
"test": VerifyStunServer,
"eos_data": [
{
"enabled": True,
"pid": 0,
}
],
"inputs": {},
"expected": {
"result": "failure",
"messages": ["STUN server is not running."],
},
},
{
"name": "failure-not-running-disabled",
"test": VerifyStunServer,
"eos_data": [
{
"enabled": False,
"pid": 0,
}
],
"inputs": {},
"expected": {
"result": "failure",
"messages": ["STUN server status is disabled and not running."],
},
},
]

0 comments on commit 8129184

Please sign in to comment.