File: test_snmp.py

package info (click to toggle)
anta 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,048 kB
  • sloc: python: 48,164; sh: 28; javascript: 9; makefile: 4
file content (192 lines) | stat: -rw-r--r-- 7,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Tests for anta.input_models.snmp.py."""

# pylint: disable=C0302
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from pydantic import ValidationError

from anta.input_models.snmp import SnmpGroup
from anta.tests.snmp import VerifySnmpNotificationHost, VerifySnmpUser

if TYPE_CHECKING:
    from anta.custom_types import SnmpVersion, SnmpVersionV3AuthType
    from anta.input_models.snmp import SnmpHost, SnmpUser


class TestVerifySnmpUserInput:
    """Test anta.tests.snmp.VerifySnmpUser.Input."""

    @pytest.mark.parametrize(
        ("snmp_users"),
        [
            pytest.param([{"username": "test", "group_name": "abc", "version": "v1", "auth_type": None, "priv_type": None}], id="valid-v1"),
            pytest.param([{"username": "test", "group_name": "abc", "version": "v2c", "auth_type": None, "priv_type": None}], id="valid-v2c"),
            pytest.param([{"username": "test", "group_name": "abc", "version": "v3", "auth_type": "SHA", "priv_type": "AES-128"}], id="valid-v3"),
        ],
    )
    def test_valid(self, snmp_users: list[SnmpUser]) -> None:
        """Test VerifySnmpUser.Input valid inputs."""
        VerifySnmpUser.Input(snmp_users=snmp_users)

    @pytest.mark.parametrize(
        ("snmp_users"),
        [
            pytest.param([{"username": "test", "group_name": "abc", "version": "v3", "auth_type": None, "priv_type": None}], id="invalid-v3"),
        ],
    )
    def test_invalid(self, snmp_users: list[SnmpUser]) -> None:
        """Test VerifySnmpUser.Input invalid inputs."""
        with pytest.raises(ValidationError):
            VerifySnmpUser.Input(snmp_users=snmp_users)


class TestSnmpHost:
    """Test anta.input_models.snmp.SnmpHost."""

    @pytest.mark.parametrize(
        ("notification_hosts"),
        [
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v1",
                        "udp_port": 162,
                        "community_string": "public",
                        "user": None,
                    }
                ],
                id="valid-v1",
            ),
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v2c",
                        "udp_port": 162,
                        "community_string": "public",
                        "user": None,
                    }
                ],
                id="valid-v2c",
            ),
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v3",
                        "udp_port": 162,
                        "community_string": None,
                        "user": "public",
                    }
                ],
                id="valid-v3",
            ),
        ],
    )
    def test_valid(self, notification_hosts: list[SnmpHost]) -> None:
        """Test VerifySnmpNotificationHost.Input valid inputs."""
        VerifySnmpNotificationHost.Input(notification_hosts=notification_hosts)

    @pytest.mark.parametrize(
        ("notification_hosts"),
        [
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": None,
                        "udp_port": 162,
                        "community_string": None,
                        "user": None,
                    }
                ],
                id="invalid-version",
            ),
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v1",
                        "udp_port": 162,
                        "community_string": None,
                        "user": None,
                    }
                ],
                id="invalid-community-string-version-v1",
            ),
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v2c",
                        "udp_port": 162,
                        "community_string": None,
                        "user": None,
                    }
                ],
                id="invalid-community-string-version-v2c",
            ),
            pytest.param(
                [
                    {
                        "hostname": "192.168.1.100",
                        "vrf": "test",
                        "notification_type": "trap",
                        "version": "v3",
                        "udp_port": 162,
                        "community_string": None,
                        "user": None,
                    }
                ],
                id="invalid-user-version-v3",
            ),
        ],
    )
    def test_invalid(self, notification_hosts: list[SnmpHost]) -> None:
        """Test VerifySnmpNotificationHost.Input invalid inputs."""
        with pytest.raises(ValidationError):
            VerifySnmpNotificationHost.Input(notification_hosts=notification_hosts)


class TestSnmpGroupInput:
    """Test anta.input_models.snmp.SnmpGroup."""

    @pytest.mark.parametrize(
        ("group_name", "version", "read_view", "write_view", "notify_view", "authentication"),
        [
            pytest.param("group1", "v3", "", "write_1", None, "auth", id="snmp-auth"),
        ],
    )
    def test_valid(self, group_name: str, read_view: str, version: SnmpVersion, write_view: str, notify_view: str, authentication: SnmpVersionV3AuthType) -> None:
        """Test SnmpGroup valid inputs."""
        SnmpGroup(group_name=group_name, version=version, read_view=read_view, write_view=write_view, notify_view=notify_view, authentication=authentication)

    @pytest.mark.parametrize(
        ("group_name", "version", "read_view", "write_view", "notify_view", "authentication"),
        [
            pytest.param("group1", "v3", "", "write_1", None, None, id="snmp-invalid-auth"),
        ],
    )
    def test_invalid(self, group_name: str, read_view: str, version: SnmpVersion, write_view: str, notify_view: str, authentication: SnmpVersionV3AuthType) -> None:
        """Test SnmpGroup invalid inputs."""
        with pytest.raises(ValidationError):
            SnmpGroup(group_name=group_name, version=version, read_view=read_view, write_view=write_view, notify_view=notify_view, authentication=authentication)