summaryrefslogtreecommitdiffstats
path: root/tests/units/test_models.py
blob: c0585a491b2a2979b2eedfacec994ef1dd033852 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""
test anta.models.py
"""
# Mypy does not understand AntaTest.Input typing
# mypy: disable-error-code=attr-defined
from __future__ import annotations

import asyncio
from typing import Any

import pytest

from anta.decorators import deprecated_test, skip_on_platforms
from anta.device import AntaDevice
from anta.models import AntaCommand, AntaTemplate, AntaTest
from tests.lib.fixture import DEVICE_HW_MODEL
from tests.lib.utils import generate_test_ids


class FakeTest(AntaTest):
    """ANTA test that always succeed"""

    name = "FakeTest"
    description = "ANTA test that always succeed"
    categories = []
    commands = []

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class FakeTestWithFailedCommand(AntaTest):
    """ANTA test with a command that failed"""

    name = "FakeTestWithFailedCommand"
    description = "ANTA test with a command that failed"
    categories = []
    commands = [AntaCommand(command="show version", errors=["failed command"])]

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class FakeTestWithUnsupportedCommand(AntaTest):
    """ANTA test with an unsupported command"""

    name = "FakeTestWithUnsupportedCommand"
    description = "ANTA test with an unsupported command"
    categories = []
    commands = [AntaCommand(command="show hardware counter drop", errors=["Unavailable command (not supported on this hardware platform) (at token 2: 'counter')"])]

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class FakeTestWithInput(AntaTest):
    """ANTA test with inputs that always succeed"""

    name = "FakeTestWithInput"
    description = "ANTA test with inputs that always succeed"
    categories = []
    commands = []

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        string: str

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.inputs.string)


class FakeTestWithTemplate(AntaTest):
    """ANTA test with template that always succeed"""

    name = "FakeTestWithTemplate"
    description = "ANTA test with template that always succeed"
    categories = []
    commands = [AntaTemplate(template="show interface {interface}")]

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        interface: str

    def render(self, template: AntaTemplate) -> list[AntaCommand]:
        return [template.render(interface=self.inputs.interface)]

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.instance_commands[0].command)


class FakeTestWithTemplateNoRender(AntaTest):
    """ANTA test with template that miss the render() method"""

    name = "FakeTestWithTemplateNoRender"
    description = "ANTA test with template that miss the render() method"
    categories = []
    commands = [AntaTemplate(template="show interface {interface}")]

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        interface: str

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.instance_commands[0].command)


class FakeTestWithTemplateBadRender1(AntaTest):
    """ANTA test with template that raises a AntaTemplateRenderError exception"""

    name = "FakeTestWithTemplateBadRender"
    description = "ANTA test with template that raises a AntaTemplateRenderError exception"
    categories = []
    commands = [AntaTemplate(template="show interface {interface}")]

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        interface: str

    def render(self, template: AntaTemplate) -> list[AntaCommand]:
        return [template.render(wrong_template_param=self.inputs.interface)]

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.instance_commands[0].command)


class FakeTestWithTemplateBadRender2(AntaTest):
    """ANTA test with template that raises an arbitrary exception"""

    name = "FakeTestWithTemplateBadRender2"
    description = "ANTA test with template that raises an arbitrary exception"
    categories = []
    commands = [AntaTemplate(template="show interface {interface}")]

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        interface: str

    def render(self, template: AntaTemplate) -> list[AntaCommand]:
        raise Exception()  # pylint: disable=broad-exception-raised

    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.instance_commands[0].command)


class SkipOnPlatformTest(AntaTest):
    """ANTA test that is skipped"""

    name = "SkipOnPlatformTest"
    description = "ANTA test that is skipped on a specific platform"
    categories = []
    commands = []

    @skip_on_platforms([DEVICE_HW_MODEL])
    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class UnSkipOnPlatformTest(AntaTest):
    """ANTA test that is skipped"""

    name = "UnSkipOnPlatformTest"
    description = "ANTA test that is skipped on a specific platform"
    categories = []
    commands = []

    @skip_on_platforms(["dummy"])
    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class SkipOnPlatformTestWithInput(AntaTest):
    """ANTA test skipped on platforms but with Input"""

    name = "SkipOnPlatformTestWithInput"
    description = "ANTA test skipped on platforms but with Input"
    categories = []
    commands = []

    class Input(AntaTest.Input):  # pylint: disable=missing-class-docstring
        string: str

    @skip_on_platforms([DEVICE_HW_MODEL])
    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success(self.inputs.string)


class DeprecatedTestWithoutNewTest(AntaTest):
    """ANTA test that is deprecated without new test"""

    name = "DeprecatedTestWitouthNewTest"
    description = "ANTA test that is deprecated without new test"
    categories = []
    commands = []

    @deprecated_test()
    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


class DeprecatedTestWithNewTest(AntaTest):
    """ANTA test that is deprecated with new test."""

    name = "DeprecatedTestWithNewTest"
    description = "ANTA deprecated test with New Test"
    categories = []
    commands = []

    @deprecated_test(new_tests=["NewTest"])
    @AntaTest.anta_test
    def test(self) -> None:
        self.result.is_success()


ANTATEST_DATA: list[dict[str, Any]] = [
    {"name": "no input", "test": FakeTest, "inputs": None, "expected": {"__init__": {"result": "unset"}, "test": {"result": "success"}}},
    {
        "name": "extra input",
        "test": FakeTest,
        "inputs": {"string": "culpa! veniam quas quas veniam molestias, esse"},
        "expected": {"__init__": {"result": "error", "messages": ["Extra inputs are not permitted"]}, "test": {"result": "error"}},
    },
    {
        "name": "no input",
        "test": FakeTestWithInput,
        "inputs": None,
        "expected": {"__init__": {"result": "error", "messages": ["Field required"]}, "test": {"result": "error"}},
    },
    {
        "name": "wrong input type",
        "test": FakeTestWithInput,
        "inputs": {"string": 1},
        "expected": {"__init__": {"result": "error", "messages": ["Input should be a valid string"]}, "test": {"result": "error"}},
    },
    {
        "name": "good input",
        "test": FakeTestWithInput,
        "inputs": {"string": "culpa! veniam quas quas veniam molestias, esse"},
        "expected": {"__init__": {"result": "unset"}, "test": {"result": "success", "messages": ["culpa! veniam quas quas veniam molestias, esse"]}},
    },
    {
        "name": "good input",
        "test": FakeTestWithTemplate,
        "inputs": {"interface": "Ethernet1"},
        "expected": {"__init__": {"result": "unset"}, "test": {"result": "success", "messages": ["show interface Ethernet1"]}},
    },
    {
        "name": "wrong input type",
        "test": FakeTestWithTemplate,
        "inputs": {"interface": 1},
        "expected": {"__init__": {"result": "error", "messages": ["Input should be a valid string"]}, "test": {"result": "error"}},
    },
    {
        "name": "wrong render definition",
        "test": FakeTestWithTemplateNoRender,
        "inputs": {"interface": "Ethernet1"},
        "expected": {
            "__init__": {
                "result": "error",
                "messages": ["AntaTemplate are provided but render() method has not been implemented for tests.units.test_models.FakeTestWithTemplateNoRender"],
            },
            "test": {"result": "error"},
        },
    },
    {
        "name": "AntaTemplateRenderError",
        "test": FakeTestWithTemplateBadRender1,
        "inputs": {"interface": "Ethernet1"},
        "expected": {
            "__init__": {
                "result": "error",
                "messages": ["Cannot render template {template='show interface {interface}' version='latest' revision=None ofmt='json' use_cache=True}"],
            },
            "test": {"result": "error"},
        },
    },
    {
        "name": "Exception in render()",
        "test": FakeTestWithTemplateBadRender2,
        "inputs": {"interface": "Ethernet1"},
        "expected": {
            "__init__": {
                "result": "error",
                "messages": ["Exception in tests.units.test_models.FakeTestWithTemplateBadRender2.render(): Exception"],
            },
            "test": {"result": "error"},
        },
    },
    {
        "name": "unskip on platforms",
        "test": UnSkipOnPlatformTest,
        "inputs": None,
        "expected": {
            "__init__": {"result": "unset"},
            "test": {"result": "success"},
        },
    },
    {
        "name": "skip on platforms, unset",
        "test": SkipOnPlatformTest,
        "inputs": None,
        "expected": {
            "__init__": {"result": "unset"},
            "test": {"result": "skipped"},
        },
    },
    {
        "name": "skip on platforms, not unset",
        "test": SkipOnPlatformTestWithInput,
        "inputs": None,
        "expected": {"__init__": {"result": "error", "messages": ["Field required"]}, "test": {"result": "error"}},
    },
    {
        "name": "deprecate test without new test",
        "test": DeprecatedTestWithoutNewTest,
        "inputs": None,
        "expected": {
            "__init__": {"result": "unset"},
            "test": {"result": "success"},
        },
    },
    {
        "name": "deprecate test with new test",
        "test": DeprecatedTestWithNewTest,
        "inputs": None,
        "expected": {
            "__init__": {"result": "unset"},
            "test": {"result": "success"},
        },
    },
    {
        "name": "failed command",
        "test": FakeTestWithFailedCommand,
        "inputs": None,
        "expected": {"__init__": {"result": "unset"}, "test": {"result": "error", "messages": ["show version has failed: failed command"]}},
    },
    {
        "name": "unsupported command",
        "test": FakeTestWithUnsupportedCommand,
        "inputs": None,
        "expected": {
            "__init__": {"result": "unset"},
            "test": {"result": "skipped", "messages": ["Skipped because show hardware counter drop is not supported on pytest"]},
        },
    },
]


class Test_AntaTest:
    """
    Test for anta.models.AntaTest
    """

    def test__init_subclass__name(self) -> None:
        """Test __init_subclass__"""
        # Pylint detects all the classes in here as unused which is on purpose
        # pylint: disable=unused-variable
        with pytest.raises(NotImplementedError) as exec_info:

            class WrongTestNoName(AntaTest):
                """ANTA test that is missing a name"""

                description = "ANTA test that is missing a name"
                categories = []
                commands = []

                @AntaTest.anta_test
                def test(self) -> None:
                    self.result.is_success()

        assert exec_info.value.args[0] == "Class tests.units.test_models.WrongTestNoName is missing required class attribute name"

        with pytest.raises(NotImplementedError) as exec_info:

            class WrongTestNoDescription(AntaTest):
                """ANTA test that is missing a description"""

                name = "WrongTestNoDescription"
                categories = []
                commands = []

                @AntaTest.anta_test
                def test(self) -> None:
                    self.result.is_success()

        assert exec_info.value.args[0] == "Class tests.units.test_models.WrongTestNoDescription is missing required class attribute description"

        with pytest.raises(NotImplementedError) as exec_info:

            class WrongTestNoCategories(AntaTest):
                """ANTA test that is missing categories"""

                name = "WrongTestNoCategories"
                description = "ANTA test that is missing categories"
                commands = []

                @AntaTest.anta_test
                def test(self) -> None:
                    self.result.is_success()

        assert exec_info.value.args[0] == "Class tests.units.test_models.WrongTestNoCategories is missing required class attribute categories"

        with pytest.raises(NotImplementedError) as exec_info:

            class WrongTestNoCommands(AntaTest):
                """ANTA test that is missing commands"""

                name = "WrongTestNoCommands"
                description = "ANTA test that is missing commands"
                categories = []

                @AntaTest.anta_test
                def test(self) -> None:
                    self.result.is_success()

        assert exec_info.value.args[0] == "Class tests.units.test_models.WrongTestNoCommands is missing required class attribute commands"

    def _assert_test(self, test: AntaTest, expected: dict[str, Any]) -> None:
        assert test.result.result == expected["result"]
        if "messages" in expected:
            for result_msg, expected_msg in zip(test.result.messages, expected["messages"]):  # NOTE: zip(strict=True) has been added in Python 3.10
                assert expected_msg in result_msg

    @pytest.mark.parametrize("data", ANTATEST_DATA, ids=generate_test_ids(ANTATEST_DATA))
    def test__init__(self, device: AntaDevice, data: dict[str, Any]) -> None:
        """Test the AntaTest constructor"""
        expected = data["expected"]["__init__"]
        test = data["test"](device, inputs=data["inputs"])
        self._assert_test(test, expected)

    @pytest.mark.parametrize("data", ANTATEST_DATA, ids=generate_test_ids(ANTATEST_DATA))
    def test_test(self, device: AntaDevice, data: dict[str, Any]) -> None:
        """Test the AntaTest.test method"""
        expected = data["expected"]["test"]
        test = data["test"](device, inputs=data["inputs"])
        asyncio.run(test.test())
        self._assert_test(test, expected)


ANTATEST_BLACKLIST_DATA = ["reload", "reload --force", "write", "wr mem"]


@pytest.mark.parametrize("data", ANTATEST_BLACKLIST_DATA)
def test_blacklist(device: AntaDevice, data: str) -> None:
    """Test for blacklisting function."""

    class FakeTestWithBlacklist(AntaTest):
        """Fake Test for blacklist"""

        name = "FakeTestWithBlacklist"
        description = "ANTA test that has blacklisted command"
        categories = []
        commands = [AntaCommand(command=data)]

        @AntaTest.anta_test
        def test(self) -> None:
            self.result.is_success()

    test_instance = FakeTestWithBlacklist(device)

    # Run the test() method
    asyncio.run(test_instance.test())
    assert test_instance.result.result == "error"