| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.input_models.snmp.py.""" |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from typing import TYPE_CHECKING |
| 10 |
|
| 11 |
import pytest |
| 12 |
from pydantic import ValidationError |
| 13 |
|
| 14 |
from anta.input_models.snmp import SnmpGroup |
| 15 |
from anta.tests.snmp import VerifySnmpNotificationHost, VerifySnmpUser |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from anta.custom_types import SnmpVersion, SnmpVersionV3AuthType |
| 19 |
from anta.input_models.snmp import SnmpHost, SnmpUser |
| 20 |
|
| 21 |
|
| 22 |
class TestVerifySnmpUserInput: |
| 23 |
"""Test anta.tests.snmp.VerifySnmpUser.Input.""" |
| 24 |
|
| 25 |
@pytest.mark.parametrize( |
| 26 |
("snmp_users"), |
| 27 |
[ |
| 28 |
pytest.param([{"username": "test", "group_name": "abc", "version": "v1", "auth_type": None, "priv_type": None}], id="valid-v1"), |
| 29 |
pytest.param([{"username": "test", "group_name": "abc", "version": "v2c", "auth_type": None, "priv_type": None}], id="valid-v2c"), |
| 30 |
pytest.param([{"username": "test", "group_name": "abc", "version": "v3", "auth_type": "SHA", "priv_type": "AES-128"}], id="valid-v3"), |
| 31 |
], |
| 32 |
) |
| 33 |
def test_valid(self, snmp_users: list[SnmpUser]) -> None: |
| 34 |
"""Test VerifySnmpUser.Input valid inputs.""" |
| 35 |
VerifySnmpUser.Input(snmp_users=snmp_users) |
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
("snmp_users"), |
| 39 |
[ |
| 40 |
pytest.param([{"username": "test", "group_name": "abc", "version": "v3", "auth_type": None, "priv_type": None}], id="invalid-v3"), |
| 41 |
], |
| 42 |
) |
| 43 |
def test_invalid(self, snmp_users: list[SnmpUser]) -> None: |
| 44 |
"""Test VerifySnmpUser.Input invalid inputs.""" |
| 45 |
with pytest.raises(ValidationError): |
| 46 |
VerifySnmpUser.Input(snmp_users=snmp_users) |
| 47 |
|
| 48 |
|
| 49 |
class TestSnmpHost: |
| 50 |
"""Test anta.input_models.snmp.SnmpHost.""" |
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
("notification_hosts"), |
| 54 |
[ |
| 55 |
pytest.param( |
| 56 |
[ |
| 57 |
{ |
| 58 |
"hostname": "192.168.1.100", |
| 59 |
"vrf": "test", |
| 60 |
"notification_type": "trap", |
| 61 |
"version": "v1", |
| 62 |
"udp_port": 162, |
| 63 |
"community_string": "public", |
| 64 |
"user": None, |
| 65 |
} |
| 66 |
], |
| 67 |
id="valid-v1", |
| 68 |
), |
| 69 |
pytest.param( |
| 70 |
[ |
| 71 |
{ |
| 72 |
"hostname": "192.168.1.100", |
| 73 |
"vrf": "test", |
| 74 |
"notification_type": "trap", |
| 75 |
"version": "v2c", |
| 76 |
"udp_port": 162, |
| 77 |
"community_string": "public", |
| 78 |
"user": None, |
| 79 |
} |
| 80 |
], |
| 81 |
id="valid-v2c", |
| 82 |
), |
| 83 |
pytest.param( |
| 84 |
[ |
| 85 |
{ |
| 86 |
"hostname": "192.168.1.100", |
| 87 |
"vrf": "test", |
| 88 |
"notification_type": "trap", |
| 89 |
"version": "v3", |
| 90 |
"udp_port": 162, |
| 91 |
"community_string": None, |
| 92 |
"user": "public", |
| 93 |
} |
| 94 |
], |
| 95 |
id="valid-v3", |
| 96 |
), |
| 97 |
], |
| 98 |
) |
| 99 |
def test_valid(self, notification_hosts: list[SnmpHost]) -> None: |
| 100 |
"""Test VerifySnmpNotificationHost.Input valid inputs.""" |
| 101 |
VerifySnmpNotificationHost.Input(notification_hosts=notification_hosts) |
| 102 |
|
| 103 |
@pytest.mark.parametrize( |
| 104 |
("notification_hosts"), |
| 105 |
[ |
| 106 |
pytest.param( |
| 107 |
[ |
| 108 |
{ |
| 109 |
"hostname": "192.168.1.100", |
| 110 |
"vrf": "test", |
| 111 |
"notification_type": "trap", |
| 112 |
"version": None, |
| 113 |
"udp_port": 162, |
| 114 |
"community_string": None, |
| 115 |
"user": None, |
| 116 |
} |
| 117 |
], |
| 118 |
id="invalid-version", |
| 119 |
), |
| 120 |
pytest.param( |
| 121 |
[ |
| 122 |
{ |
| 123 |
"hostname": "192.168.1.100", |
| 124 |
"vrf": "test", |
| 125 |
"notification_type": "trap", |
| 126 |
"version": "v1", |
| 127 |
"udp_port": 162, |
| 128 |
"community_string": None, |
| 129 |
"user": None, |
| 130 |
} |
| 131 |
], |
| 132 |
id="invalid-community-string-version-v1", |
| 133 |
), |
| 134 |
pytest.param( |
| 135 |
[ |
| 136 |
{ |
| 137 |
"hostname": "192.168.1.100", |
| 138 |
"vrf": "test", |
| 139 |
"notification_type": "trap", |
| 140 |
"version": "v2c", |
| 141 |
"udp_port": 162, |
| 142 |
"community_string": None, |
| 143 |
"user": None, |
| 144 |
} |
| 145 |
], |
| 146 |
id="invalid-community-string-version-v2c", |
| 147 |
), |
| 148 |
pytest.param( |
| 149 |
[ |
| 150 |
{ |
| 151 |
"hostname": "192.168.1.100", |
| 152 |
"vrf": "test", |
| 153 |
"notification_type": "trap", |
| 154 |
"version": "v3", |
| 155 |
"udp_port": 162, |
| 156 |
"community_string": None, |
| 157 |
"user": None, |
| 158 |
} |
| 159 |
], |
| 160 |
id="invalid-user-version-v3", |
| 161 |
), |
| 162 |
], |
| 163 |
) |
| 164 |
def test_invalid(self, notification_hosts: list[SnmpHost]) -> None: |
| 165 |
"""Test VerifySnmpNotificationHost.Input invalid inputs.""" |
| 166 |
with pytest.raises(ValidationError): |
| 167 |
VerifySnmpNotificationHost.Input(notification_hosts=notification_hosts) |
| 168 |
|
| 169 |
|
| 170 |
class TestSnmpGroupInput: |
| 171 |
"""Test anta.input_models.snmp.SnmpGroup.""" |
| 172 |
|
| 173 |
@pytest.mark.parametrize( |
| 174 |
("group_name", "version", "read_view", "write_view", "notify_view", "authentication"), |
| 175 |
[ |
| 176 |
pytest.param("group1", "v3", "", "write_1", None, "auth", id="snmp-auth"), |
| 177 |
], |
| 178 |
) |
| 179 |
def test_valid(self, group_name: str, read_view: str, version: SnmpVersion, write_view: str, notify_view: str, authentication: SnmpVersionV3AuthType) -> None: |
| 180 |
"""Test SnmpGroup valid inputs.""" |
| 181 |
SnmpGroup(group_name=group_name, version=version, read_view=read_view, write_view=write_view, notify_view=notify_view, authentication=authentication) |
| 182 |
|
| 183 |
@pytest.mark.parametrize( |
| 184 |
("group_name", "version", "read_view", "write_view", "notify_view", "authentication"), |
| 185 |
[ |
| 186 |
pytest.param("group1", "v3", "", "write_1", None, None, id="snmp-invalid-auth"), |
| 187 |
], |
| 188 |
) |
| 189 |
def test_invalid(self, group_name: str, read_view: str, version: SnmpVersion, write_view: str, notify_view: str, authentication: SnmpVersionV3AuthType) -> None: |
| 190 |
"""Test SnmpGroup invalid inputs.""" |
| 191 |
with pytest.raises(ValidationError): |
| 192 |
SnmpGroup(group_name=group_name, version=version, read_view=read_view, write_view=write_view, notify_view=notify_view, authentication=authentication) |