Skip to content

Commit

Permalink
fixing the sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vitthalmagadum committed Jan 9, 2025
1 parent 3a4dc19 commit 1692ea8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
16 changes: 8 additions & 8 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,23 +421,21 @@ def test(self) -> None:
return

for host in self.inputs.notification_hosts:
vrf = host.vrf
vrf = "" if host.vrf == "default" else host.vrf
hostname = str(host.hostname)
notification_type = host.notification_type
version = host.version
udp_port = host.udp_port
community_string = host.community_string
user = host.user

host_details = next((host for host in snmp_hosts if (host.get("hostname") == hostname and host.get("protocolVersion") == version)), None)
host_details = next(
(host for host in snmp_hosts if (host.get("hostname") == hostname and host.get("protocolVersion") == version and host.get("vrf") == vrf)), None
)
# If expected SNMP hostname is not configured with the specified protocol version, test fails.
if not host_details:
self.result.is_failure(f"{host} Version: {version} - Not configured")
continue
actual_vrf = "default" if (vrf_name := host_details.get("vrf")) == "" else vrf_name

if actual_vrf != vrf:
self.result.is_failure(f"{host} - Incorrect VRF - Actual: {actual_vrf}")

# If actual notification type do not matches the expected value, test fails.
if notification_type != (actual_notification_type := get_value(host_details, "notificationType", "Not Found")):
Expand All @@ -449,8 +447,10 @@ def test(self) -> None:

# If SNMP protocol version is v1 or v2c and actual community string do not matches the expected value, test fails.
if version in ["v1", "v2c"] and community_string != (actual_community_string := get_value(host_details, "v1v2cParams.communityString", "Not Found")):
self.result.is_failure(f"{host} Version: {version} - Incorrect community string - Expected: {community_string} Actual: {actual_community_string}")
self.result.is_failure(
f"{host} Version: {version} - Incorrect community string - Expected: {community_string} Actual: {actual_community_string}"
)

# If SNMP protocol version is v3 and actual user do not matches the expected value, test fails.
if version == "v3" and user != (actual_user := get_value(host_details, "v3Params.user", "Not Found")):
elif version == "v3" and user != (actual_user := get_value(host_details, "v3Params.user", "Not Found")):
self.result.is_failure(f"{host} Version: {version} - Incorrect user - Expected: {user} Actual: {actual_user}")
36 changes: 0 additions & 36 deletions tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,40 +531,4 @@
},
"expected": {"result": "failure", "messages": ["Host: 192.168.1.100 VRF: default Version: v3 - Incorrect user - Expected: public Actual: private"]},
},
{
"name": "failure-incorrect-vrf",
"test": VerifySNMPNotificationHost,
"eos_data": [
{
"hosts": [
{
"hostname": "192.168.1.100",
"port": 162,
"vrf": "MGMT",
"notificationType": "trap",
"protocolVersion": "v3",
"v3Params": {"user": "public", "securityLevel": "authNoPriv"},
},
{
"hostname": "192.168.1.101",
"port": 162,
"vrf": "test",
"notificationType": "trap",
"protocolVersion": "v2c",
"v1v2cParams": {"communityString": "public"},
},
]
}
],
"inputs": {
"notification_hosts": [
{"hostname": "192.168.1.100", "vrf": "default", "notification_type": "trap", "version": "v3", "udp_port": 162, "user": "public"},
{"hostname": "192.168.1.101", "vrf": "MGMT", "notification_type": "trap", "version": "v2c", "udp_port": 162, "community_string": "public"},
]
},
"expected": {
"result": "failure",
"messages": ["Host: 192.168.1.100 VRF: default - Incorrect VRF - Actual: MGMT", "Host: 192.168.1.101 VRF: MGMT - Incorrect VRF - Actual: test"],
},
},
]

0 comments on commit 1692ea8

Please sign in to comment.