| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""test anta.models.py.""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import asyncio |
| 11 |
import sys |
| 12 |
from typing import TYPE_CHECKING, Any, ClassVar |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from anta.decorators import deprecated_test, skip_on_platforms |
| 17 |
from anta.models import AntaCommand, AntaTemplate, AntaTest |
| 18 |
from anta.result_manager.models import AntaTestStatus |
| 19 |
from tests.units.conftest import DEVICE_HW_MODEL |
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
from anta.device import AntaDevice |
| 23 |
|
| 24 |
|
| 25 |
class FakeTest(AntaTest): |
| 26 |
"""ANTA test that always succeed.""" |
| 27 |
|
| 28 |
categories: ClassVar[list[str]] = [] |
| 29 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 30 |
|
| 31 |
@AntaTest.anta_test |
| 32 |
def test(self) -> None: |
| 33 |
"""Test function.""" |
| 34 |
self.result.is_success() |
| 35 |
|
| 36 |
|
| 37 |
class FakeTestWithFailedCommand(AntaTest): |
| 38 |
"""ANTA test with a command that failed.""" |
| 39 |
|
| 40 |
categories: ClassVar[list[str]] = [] |
| 41 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show version", errors=["failed command"])] |
| 42 |
|
| 43 |
@AntaTest.anta_test |
| 44 |
def test(self) -> None: |
| 45 |
"""Test function.""" |
| 46 |
self.result.is_success() |
| 47 |
|
| 48 |
|
| 49 |
class FakeTestWithUnsupportedCommand(AntaTest): |
| 50 |
"""ANTA test with an unsupported command.""" |
| 51 |
|
| 52 |
categories: ClassVar[list[str]] = [] |
| 53 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [ |
| 54 |
AntaCommand( |
| 55 |
command="show hardware counter drop", |
| 56 |
errors=["Unavailable command (not supported on this hardware platform) (at token 2: 'counter')"], |
| 57 |
) |
| 58 |
] |
| 59 |
|
| 60 |
@AntaTest.anta_test |
| 61 |
def test(self) -> None: |
| 62 |
"""Test function.""" |
| 63 |
self.result.is_success() |
| 64 |
|
| 65 |
|
| 66 |
class FakeTestWithKnownEOSError(AntaTest): |
| 67 |
"""ANTA test triggering a known EOS Error that should translate to failure of the test.""" |
| 68 |
|
| 69 |
categories: ClassVar[list[str]] = [] |
| 70 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [ |
| 71 |
AntaCommand( |
| 72 |
command="show bgp evpn route-type mac-ip aa:c1:ab:de:50:ad vni 10010", |
| 73 |
errors=["BGP inactive"], |
| 74 |
) |
| 75 |
] |
| 76 |
|
| 77 |
@AntaTest.anta_test |
| 78 |
def test(self) -> None: |
| 79 |
"""Test function.""" |
| 80 |
self.result.is_success() |
| 81 |
|
| 82 |
|
| 83 |
class FakeTestWithInput(AntaTest): |
| 84 |
"""ANTA test with inputs that always succeed.""" |
| 85 |
|
| 86 |
categories: ClassVar[list[str]] = [] |
| 87 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 88 |
|
| 89 |
class Input(AntaTest.Input): |
| 90 |
"""Inputs for FakeTestWithInput test.""" |
| 91 |
|
| 92 |
string: str |
| 93 |
|
| 94 |
@AntaTest.anta_test |
| 95 |
def test(self) -> None: |
| 96 |
"""Test function.""" |
| 97 |
self.result.is_success(self.inputs.string) |
| 98 |
|
| 99 |
|
| 100 |
class FakeTestWithTemplate(AntaTest): |
| 101 |
"""ANTA test with template that always succeed.""" |
| 102 |
|
| 103 |
categories: ClassVar[list[str]] = [] |
| 104 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 105 |
|
| 106 |
class Input(AntaTest.Input): |
| 107 |
"""Inputs for FakeTestWithTemplate test.""" |
| 108 |
|
| 109 |
interface: str |
| 110 |
|
| 111 |
def render(self, template: AntaTemplate) -> list[AntaCommand]: |
| 112 |
"""Render function.""" |
| 113 |
return [template.render(interface=self.inputs.interface)] |
| 114 |
|
| 115 |
@AntaTest.anta_test |
| 116 |
def test(self) -> None: |
| 117 |
"""Test function.""" |
| 118 |
self.result.is_success(self.instance_commands[0].command) |
| 119 |
|
| 120 |
|
| 121 |
class FakeTestWithTemplateNoRender(AntaTest): |
| 122 |
"""ANTA test with template that miss the render() method.""" |
| 123 |
|
| 124 |
categories: ClassVar[list[str]] = [] |
| 125 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 126 |
|
| 127 |
class Input(AntaTest.Input): |
| 128 |
"""Inputs for FakeTestWithTemplateNoRender test.""" |
| 129 |
|
| 130 |
interface: str |
| 131 |
|
| 132 |
@AntaTest.anta_test |
| 133 |
def test(self) -> None: |
| 134 |
"""Test function.""" |
| 135 |
self.result.is_success(self.instance_commands[0].command) |
| 136 |
|
| 137 |
|
| 138 |
class FakeTestWithTemplateBadRender1(AntaTest): |
| 139 |
"""ANTA test with template that raises a AntaTemplateRenderError exception.""" |
| 140 |
|
| 141 |
categories: ClassVar[list[str]] = [] |
| 142 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 143 |
|
| 144 |
class Input(AntaTest.Input): |
| 145 |
"""Inputs for FakeTestWithTemplateBadRender1 test.""" |
| 146 |
|
| 147 |
interface: str |
| 148 |
|
| 149 |
def render(self, template: AntaTemplate) -> list[AntaCommand]: |
| 150 |
"""Render function.""" |
| 151 |
return [template.render(wrong_template_param=self.inputs.interface)] |
| 152 |
|
| 153 |
@AntaTest.anta_test |
| 154 |
def test(self) -> None: |
| 155 |
"""Test function.""" |
| 156 |
self.result.is_success(self.instance_commands[0].command) |
| 157 |
|
| 158 |
|
| 159 |
class FakeTestWithTemplateBadRender2(AntaTest): |
| 160 |
"""ANTA test with template that raises an arbitrary exception in render().""" |
| 161 |
|
| 162 |
categories: ClassVar[list[str]] = [] |
| 163 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 164 |
|
| 165 |
class Input(AntaTest.Input): |
| 166 |
"""Inputs for FakeTestWithTemplateBadRender2 test.""" |
| 167 |
|
| 168 |
interface: str |
| 169 |
|
| 170 |
def render(self, template: AntaTemplate) -> list[AntaCommand]: |
| 171 |
"""Render function.""" |
| 172 |
raise RuntimeError(template) |
| 173 |
|
| 174 |
@AntaTest.anta_test |
| 175 |
def test(self) -> None: |
| 176 |
"""Test function.""" |
| 177 |
self.result.is_success(self.instance_commands[0].command) |
| 178 |
|
| 179 |
|
| 180 |
class FakeTestWithTemplateBadRender3(AntaTest): |
| 181 |
"""ANTA test with template that gives extra template parameters in render().""" |
| 182 |
|
| 183 |
categories: ClassVar[list[str]] = [] |
| 184 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 185 |
|
| 186 |
class Input(AntaTest.Input): |
| 187 |
"""Inputs for FakeTestWithTemplateBadRender3 test.""" |
| 188 |
|
| 189 |
interface: str |
| 190 |
|
| 191 |
def render(self, template: AntaTemplate) -> list[AntaCommand]: |
| 192 |
"""Render function.""" |
| 193 |
return [template.render(interface=self.inputs.interface, extra="blah")] |
| 194 |
|
| 195 |
@AntaTest.anta_test |
| 196 |
def test(self) -> None: |
| 197 |
"""Test function.""" |
| 198 |
self.result.is_success(self.instance_commands[0].command) |
| 199 |
|
| 200 |
|
| 201 |
class FakeTestWithTemplateBadTest(AntaTest): |
| 202 |
"""ANTA test with template that tries to access an undefined template parameter in test().""" |
| 203 |
|
| 204 |
categories: ClassVar[list[str]] = [] |
| 205 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] |
| 206 |
|
| 207 |
class Input(AntaTest.Input): |
| 208 |
"""Inputs for FakeTestWithTemplateBadTest test.""" |
| 209 |
|
| 210 |
interface: str |
| 211 |
|
| 212 |
def render(self, template: AntaTemplate) -> list[AntaCommand]: |
| 213 |
"""Render function.""" |
| 214 |
return [template.render(interface=self.inputs.interface)] |
| 215 |
|
| 216 |
@AntaTest.anta_test |
| 217 |
def test(self) -> None: |
| 218 |
"""Test function.""" |
| 219 |
|
| 220 |
self.result.is_success(self.instance_commands[0].params.wrong_template_param) |
| 221 |
|
| 222 |
|
| 223 |
class SkipOnPlatformTest(AntaTest): |
| 224 |
"""ANTA test that is skipped.""" |
| 225 |
|
| 226 |
categories: ClassVar[list[str]] = [] |
| 227 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 228 |
|
| 229 |
@skip_on_platforms([DEVICE_HW_MODEL]) |
| 230 |
@AntaTest.anta_test |
| 231 |
def test(self) -> None: |
| 232 |
"""Test function.""" |
| 233 |
self.result.is_success() |
| 234 |
|
| 235 |
|
| 236 |
class UnSkipOnPlatformTest(AntaTest): |
| 237 |
"""ANTA test that is skipped.""" |
| 238 |
|
| 239 |
categories: ClassVar[list[str]] = [] |
| 240 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 241 |
|
| 242 |
@skip_on_platforms(["dummy"]) |
| 243 |
@AntaTest.anta_test |
| 244 |
def test(self) -> None: |
| 245 |
"""Test function.""" |
| 246 |
self.result.is_success() |
| 247 |
|
| 248 |
|
| 249 |
class SkipOnPlatformTestWithInput(AntaTest): |
| 250 |
"""ANTA test skipped on platforms but with Input.""" |
| 251 |
|
| 252 |
categories: ClassVar[list[str]] = [] |
| 253 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 254 |
|
| 255 |
class Input(AntaTest.Input): |
| 256 |
"""Inputs for SkipOnPlatformTestWithInput test.""" |
| 257 |
|
| 258 |
string: str |
| 259 |
|
| 260 |
@skip_on_platforms([DEVICE_HW_MODEL]) |
| 261 |
@AntaTest.anta_test |
| 262 |
def test(self) -> None: |
| 263 |
"""Test function.""" |
| 264 |
self.result.is_success(self.inputs.string) |
| 265 |
|
| 266 |
|
| 267 |
class DeprecatedTestWithoutNewTest(AntaTest): |
| 268 |
"""ANTA test that is deprecated without new test.""" |
| 269 |
|
| 270 |
categories: ClassVar[list[str]] = [] |
| 271 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 272 |
|
| 273 |
@deprecated_test() |
| 274 |
@AntaTest.anta_test |
| 275 |
def test(self) -> None: |
| 276 |
"""Test function.""" |
| 277 |
self.result.is_success() |
| 278 |
|
| 279 |
|
| 280 |
class DeprecatedTestWithNewTest(AntaTest): |
| 281 |
"""ANTA test that is deprecated with new test.""" |
| 282 |
|
| 283 |
categories: ClassVar[list[str]] = [] |
| 284 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 285 |
|
| 286 |
@deprecated_test(new_tests=["NewTest"]) |
| 287 |
@AntaTest.anta_test |
| 288 |
def test(self) -> None: |
| 289 |
"""Test function.""" |
| 290 |
self.result.is_success() |
| 291 |
|
| 292 |
|
| 293 |
class FakeTestWithMissingTest(AntaTest): |
| 294 |
"""ANTA test with missing test() method implementation.""" |
| 295 |
|
| 296 |
categories: ClassVar[list[str]] = [] |
| 297 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 298 |
|
| 299 |
|
| 300 |
ANTATEST_DATA: dict[tuple[type[AntaTest], str], Any] = { |
| 301 |
(FakeTest, "no input"): {"inputs": None, "expected": {"__init__": {"result": "unset"}, "test": {"result": "success"}}}, |
| 302 |
(FakeTest, "extra input"): { |
| 303 |
"inputs": {"string": "culpa! veniam quas quas veniam molestias, esse"}, |
| 304 |
"expected": {"__init__": {"result": "error", "messages": ["Extra inputs are not permitted"]}, "test": {"result": "error"}}, |
| 305 |
}, |
| 306 |
(FakeTestWithInput, "no input"): {"inputs": None, "expected": {"__init__": {"result": "error", "messages": ["Field required"]}, "test": {"result": "error"}}}, |
| 307 |
(FakeTestWithInput, "wrong input type"): { |
| 308 |
"inputs": {"string": 1}, |
| 309 |
"expected": {"__init__": {"result": "error", "messages": ["Input should be a valid string"]}, "test": {"result": "error"}}, |
| 310 |
}, |
| 311 |
(FakeTestWithInput, "good input"): { |
| 312 |
"inputs": {"string": "culpa! veniam quas quas veniam molestias, esse"}, |
| 313 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "success", "messages": ["culpa! veniam quas quas veniam molestias, esse"]}}, |
| 314 |
}, |
| 315 |
(FakeTestWithTemplate, "good input"): { |
| 316 |
"inputs": {"interface": "Ethernet1"}, |
| 317 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "success", "messages": ["show interface Ethernet1"]}}, |
| 318 |
}, |
| 319 |
(FakeTestWithTemplate, "wrong input type"): { |
| 320 |
"inputs": {"interface": 1}, |
| 321 |
"expected": {"__init__": {"result": "error", "messages": ["Input should be a valid string"]}, "test": {"result": "error"}}, |
| 322 |
}, |
| 323 |
(FakeTestWithTemplateNoRender, "wrong render definition"): { |
| 324 |
"inputs": {"interface": "Ethernet1"}, |
| 325 |
"expected": { |
| 326 |
"__init__": { |
| 327 |
"result": "error", |
| 328 |
"messages": ["AntaTemplate are provided but render() method has not been implemented for tests.units.test_models.FakeTestWithTemplateNoRender"], |
| 329 |
}, |
| 330 |
"test": {"result": "error"}, |
| 331 |
}, |
| 332 |
}, |
| 333 |
(FakeTestWithTemplateBadRender1, "AntaTemplateRenderError"): { |
| 334 |
"inputs": {"interface": "Ethernet1"}, |
| 335 |
"expected": { |
| 336 |
"__init__": { |
| 337 |
"result": "error", |
| 338 |
"messages": ["Cannot render template {template='show interface {interface}' version='latest' revision=None ofmt='json' use_cache=True}"], |
| 339 |
}, |
| 340 |
"test": {"result": "error"}, |
| 341 |
}, |
| 342 |
}, |
| 343 |
(FakeTestWithTemplateBadRender2, "RuntimeError in render()"): { |
| 344 |
"inputs": {"interface": "Ethernet1"}, |
| 345 |
"expected": { |
| 346 |
"__init__": {"result": "error", "messages": ["Exception in tests.units.test_models.FakeTestWithTemplateBadRender2.render(): RuntimeError"]}, |
| 347 |
"test": {"result": "error"}, |
| 348 |
}, |
| 349 |
}, |
| 350 |
(FakeTestWithTemplateBadRender3, "Extra template parameters in render()"): { |
| 351 |
"inputs": {"interface": "Ethernet1"}, |
| 352 |
"expected": { |
| 353 |
"__init__": { |
| 354 |
"result": "error", |
| 355 |
"messages": [ |
| 356 |
"Exception in tests.units.test_models.FakeTestWithTemplateBadRender3.render(): ValidationError: 1 validation error for AntaParams\nextra\n" |
| 357 |
" Extra inputs are not permitted [type=extra_forbidden, input_value='blah', input_type=str]\n" |
| 358 |
], |
| 359 |
}, |
| 360 |
"test": {"result": "error"}, |
| 361 |
}, |
| 362 |
}, |
| 363 |
(FakeTestWithTemplateBadTest, "Access undefined template param in test()"): { |
| 364 |
"inputs": {"interface": "Ethernet1"}, |
| 365 |
"expected": { |
| 366 |
"__init__": {"result": "unset"}, |
| 367 |
"test": {"result": "error", "messages": ["AttributeError: 'AntaParams' object has no attribute 'wrong_template_param'"]}, |
| 368 |
}, |
| 369 |
}, |
| 370 |
(UnSkipOnPlatformTest, "unskip on platforms"): {"inputs": None, "expected": {"__init__": {"result": "unset"}, "test": {"result": "success"}}}, |
| 371 |
(SkipOnPlatformTest, "skip on platforms, unset"): {"inputs": None, "expected": {"__init__": {"result": "unset"}, "test": {"result": "skipped"}}}, |
| 372 |
(SkipOnPlatformTestWithInput, "skip on platforms, not unset"): { |
| 373 |
"inputs": None, |
| 374 |
"expected": {"__init__": {"result": "error", "messages": ["Field required"]}, "test": {"result": "error"}}, |
| 375 |
}, |
| 376 |
(DeprecatedTestWithoutNewTest, "deprecate test without new test"): { |
| 377 |
"inputs": None, |
| 378 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "success"}}, |
| 379 |
}, |
| 380 |
(DeprecatedTestWithNewTest, "deprecate test with new test"): {"inputs": None, "expected": {"__init__": {"result": "unset"}, "test": {"result": "success"}}}, |
| 381 |
(FakeTestWithFailedCommand, "failed command"): { |
| 382 |
"inputs": None, |
| 383 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "error", "messages": ["show version has failed: failed command"]}}, |
| 384 |
}, |
| 385 |
(FakeTestWithUnsupportedCommand, "unsupported command"): { |
| 386 |
"inputs": None, |
| 387 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "skipped", "messages": ["'show hardware counter drop' is not supported on pytest"]}}, |
| 388 |
}, |
| 389 |
(FakeTestWithKnownEOSError, "known EOS error command"): { |
| 390 |
"inputs": None, |
| 391 |
"expected": {"__init__": {"result": "unset"}, "test": {"result": "failure", "messages": ["BGP inactive"]}}, |
| 392 |
}, |
| 393 |
} |
| 394 |
|
| 395 |
BLACKLIST_COMMANDS_PARAMS = ["reload", "reload now", "reload --force", "write", "wr mem", "write memory", "conf t", "configure terminal", "configure session"] |
| 396 |
|
| 397 |
|
| 398 |
class TestAntaTest: |
| 399 |
"""Test for anta.models.AntaTest.""" |
| 400 |
|
| 401 |
def test__init_subclass__(self) -> None: |
| 402 |
"""Test __init_subclass__.""" |
| 403 |
with pytest.raises(AttributeError) as exec_info: |
| 404 |
|
| 405 |
class _WrongTestNoCategories(AntaTest): |
| 406 |
"""ANTA test that is missing categories.""" |
| 407 |
|
| 408 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 409 |
|
| 410 |
@AntaTest.anta_test |
| 411 |
def test(self) -> None: |
| 412 |
self.result.is_success() |
| 413 |
|
| 414 |
assert exec_info.value.args[0] == "Class tests.units.test_models._WrongTestNoCategories is missing required class attribute(s): categories" |
| 415 |
|
| 416 |
with pytest.raises(AttributeError) as exec_info: |
| 417 |
|
| 418 |
class _WrongTestNoCommands(AntaTest): |
| 419 |
"""ANTA test that is missing commands.""" |
| 420 |
|
| 421 |
categories: ClassVar[list[str]] = [] |
| 422 |
|
| 423 |
@AntaTest.anta_test |
| 424 |
def test(self) -> None: |
| 425 |
self.result.is_success() |
| 426 |
|
| 427 |
assert exec_info.value.args[0] == "Class tests.units.test_models._WrongTestNoCommands is missing required class attribute(s): commands" |
| 428 |
|
| 429 |
with pytest.raises( |
| 430 |
AttributeError, |
| 431 |
match=r"Cannot set the description for class _WrongTestNoDescription, either set it in the class definition or add a docstring to the class.", |
| 432 |
): |
| 433 |
|
| 434 |
class _WrongTestNoDescription(AntaTest): |
| 435 |
|
| 436 |
|
| 437 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 438 |
categories: ClassVar[list[str]] = [] |
| 439 |
|
| 440 |
@AntaTest.anta_test |
| 441 |
def test(self) -> None: |
| 442 |
self.result.is_success() |
| 443 |
|
| 444 |
class _TestOverwriteNameAndDescription(AntaTest): |
| 445 |
"""ANTA test where both the test name and description are overwritten in the class definition.""" |
| 446 |
|
| 447 |
name: ClassVar[str] = "CustomName" |
| 448 |
description: ClassVar[str] = "Custom description" |
| 449 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 450 |
categories: ClassVar[list[str]] = [] |
| 451 |
|
| 452 |
@AntaTest.anta_test |
| 453 |
def test(self) -> None: |
| 454 |
self.result.is_success() |
| 455 |
|
| 456 |
assert _TestOverwriteNameAndDescription.name == "CustomName" |
| 457 |
assert _TestOverwriteNameAndDescription.description == "Custom description" |
| 458 |
|
| 459 |
def test_abc(self) -> None: |
| 460 |
"""Test that an error is raised if AntaTest is not implemented.""" |
| 461 |
with pytest.raises(TypeError) as exec_info: |
| 462 |
FakeTestWithMissingTest() |
| 463 |
msg = ( |
| 464 |
"Can't instantiate abstract class FakeTestWithMissingTest without an implementation for abstract method 'test'" |
| 465 |
if sys.version_info >= (3, 12) |
| 466 |
else "Can't instantiate abstract class FakeTestWithMissingTest with abstract method test" |
| 467 |
) |
| 468 |
assert exec_info.value.args[0] == msg |
| 469 |
|
| 470 |
def _assert_test(self, test: AntaTest, expected: dict[str, Any]) -> None: |
| 471 |
assert test.result.result == expected["result"] |
| 472 |
if "messages" in expected: |
| 473 |
assert len(test.result.messages) == len(expected["messages"]) |
| 474 |
for result_msg, expected_msg in zip(test.result.messages, expected["messages"], strict=True): |
| 475 |
assert expected_msg in result_msg |
| 476 |
|
| 477 |
@pytest.mark.parametrize( |
| 478 |
"data", ANTATEST_DATA.items(), ids=[f"{anta_test.__module__}.{anta_test.__name__}-{unit_test_name}" for (anta_test, unit_test_name) in ANTATEST_DATA] |
| 479 |
) |
| 480 |
def test__init__(self, device: AntaDevice, data: tuple[tuple[type[AntaTest], str], Any]) -> None: |
| 481 |
"""Test the AntaTest constructor.""" |
| 482 |
(anta_test, test_data) = data |
| 483 |
expected = test_data["expected"]["__init__"] |
| 484 |
test = anta_test[0](device, inputs=test_data["inputs"]) |
| 485 |
self._assert_test(test, expected) |
| 486 |
|
| 487 |
@pytest.mark.parametrize( |
| 488 |
"data", ANTATEST_DATA.items(), ids=[f"{anta_test.__module__}.{anta_test.__name__}-{unit_test_name}" for (anta_test, unit_test_name) in ANTATEST_DATA] |
| 489 |
) |
| 490 |
def test_test(self, device: AntaDevice, data: tuple[tuple[type[AntaTest], str], Any]) -> None: |
| 491 |
"""Test the AntaTest.test method.""" |
| 492 |
(anta_test, test_data) = data |
| 493 |
expected = test_data["expected"]["test"] |
| 494 |
test = anta_test[0](device, inputs=test_data["inputs"]) |
| 495 |
asyncio.run(test.test()) |
| 496 |
self._assert_test(test, expected) |
| 497 |
|
| 498 |
@pytest.mark.parametrize("command", BLACKLIST_COMMANDS_PARAMS) |
| 499 |
def test_blacklist(self, device: AntaDevice, command: str) -> None: |
| 500 |
"""Test that blacklisted commands are not collected.""" |
| 501 |
|
| 502 |
class FakeTestWithBlacklist(AntaTest): |
| 503 |
"""Fake Test for blacklist.""" |
| 504 |
|
| 505 |
categories: ClassVar[list[str]] = [] |
| 506 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command=command)] |
| 507 |
|
| 508 |
@AntaTest.anta_test |
| 509 |
def test(self) -> None: |
| 510 |
self.result.is_success() |
| 511 |
|
| 512 |
test = FakeTestWithBlacklist(device) |
| 513 |
asyncio.run(test.test()) |
| 514 |
assert test.result.result == AntaTestStatus.ERROR |
| 515 |
assert f"<{command}> is blocked for security reason" in test.result.messages |
| 516 |
assert test.instance_commands[0].collected is False |
| 517 |
|
| 518 |
@pytest.mark.parametrize( |
| 519 |
("categories", "description", "custom_field"), |
| 520 |
[ |
| 521 |
pytest.param(["hardware"], "a description", "a custom field", id="all set"), |
| 522 |
pytest.param(["hardware"], "a description", None, id="no custom field"), |
| 523 |
pytest.param(["hardware"], None, "a custom field", id="no description"), |
| 524 |
pytest.param([], "a description", "a custom field", id="empty categories"), |
| 525 |
pytest.param(None, "a description", "a custom field", id="no categories"), |
| 526 |
], |
| 527 |
) |
| 528 |
def test_result_overwrite(self, device: AntaDevice, categories: list[str] | None, description: str | None, custom_field: str) -> None: |
| 529 |
"""Test the AntaTest.Input.ResultOverwrite model.""" |
| 530 |
result_overwrite = {"categories": categories, "description": description, "custom_field": custom_field} |
| 531 |
test = FakeTest(device, inputs={"result_overwrite": result_overwrite}) |
| 532 |
asyncio.run(test.test()) |
| 533 |
|
| 534 |
assert test.result.result == AntaTestStatus.SUCCESS |
| 535 |
|
| 536 |
for category in categories or []: |
| 537 |
assert category in test.result.categories |
| 538 |
if description: |
| 539 |
assert test.result.description == description |
| 540 |
if custom_field: |
| 541 |
assert test.result.custom_field == "a custom field" |
| 542 |
|
| 543 |
|
| 544 |
class TestAntaCommand: |
| 545 |
"""Test for anta.models.AntaCommand.""" |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
def test_empty_output_access(self) -> None: |
| 550 |
"""Test for both json and text ofmt.""" |
| 551 |
json_cmd = AntaCommand(command="show dummy") |
| 552 |
text_cmd = AntaCommand(command="show dummy", ofmt="text") |
| 553 |
msg = "There is no output for command 'show dummy'" |
| 554 |
with pytest.raises(RuntimeError, match=msg): |
| 555 |
json_cmd.json_output |
| 556 |
with pytest.raises(RuntimeError, match=msg): |
| 557 |
text_cmd.text_output |
| 558 |
|
| 559 |
def test_wrong_format_output_access(self) -> None: |
| 560 |
"""Test for both json and text ofmt.""" |
| 561 |
json_cmd = AntaCommand(command="show dummy", output={}) |
| 562 |
json_cmd_2 = AntaCommand(command="show dummy", output="not_json") |
| 563 |
text_cmd = AntaCommand(command="show dummy", ofmt="text", output="blah") |
| 564 |
text_cmd_2 = AntaCommand(command="show dummy", ofmt="text", output={"not_a": "string"}) |
| 565 |
msg = "Output of command 'show dummy' is invalid" |
| 566 |
with pytest.raises(RuntimeError, match=msg): |
| 567 |
json_cmd.text_output |
| 568 |
with pytest.raises(RuntimeError, match=msg): |
| 569 |
text_cmd.json_output |
| 570 |
with pytest.raises(RuntimeError, match=msg): |
| 571 |
json_cmd_2.text_output |
| 572 |
with pytest.raises(RuntimeError, match=msg): |
| 573 |
text_cmd_2.json_output |
| 574 |
|
| 575 |
def test_supported(self) -> None: |
| 576 |
"""Test the supported property.""" |
| 577 |
command = AntaCommand(command="show hardware counter drop", errors=["Unavailable command (not supported on this hardware platform) (at token 2: 'counter')"]) |
| 578 |
assert command.supported is False |
| 579 |
command = AntaCommand( |
| 580 |
command="show hardware counter drop", output={"totalAdverseDrops": 0, "totalCongestionDrops": 0, "totalPacketProcessorDrops": 0, "dropEvents": {}} |
| 581 |
) |
| 582 |
assert command.supported is True |
| 583 |
command = AntaCommand(command="show hardware counter drop") |
| 584 |
with pytest.raises(RuntimeError) as exec_info: |
| 585 |
command.supported |
| 586 |
assert exec_info.value.args[0] == "Command 'show hardware counter drop' has not been collected and has not returned an error. Call AntaDevice.collect()." |
| 587 |
|
| 588 |
def test_requires_privileges(self) -> None: |
| 589 |
"""Test the requires_privileges property.""" |
| 590 |
command = AntaCommand(command="show aaa methods accounting", errors=["Invalid input (privileged mode required)"]) |
| 591 |
assert command.requires_privileges is True |
| 592 |
command = AntaCommand( |
| 593 |
command="show aaa methods accounting", |
| 594 |
output={ |
| 595 |
"commandsAcctMethods": {"privilege0-15": {"defaultMethods": [], "consoleMethods": []}}, |
| 596 |
"execAcctMethods": {"exec": {"defaultMethods": [], "consoleMethods": []}}, |
| 597 |
"systemAcctMethods": {"system": {"defaultMethods": [], "consoleMethods": []}}, |
| 598 |
"dot1xAcctMethods": {"dot1x": {"defaultMethods": [], "consoleMethods": []}}, |
| 599 |
}, |
| 600 |
) |
| 601 |
assert command.requires_privileges is False |
| 602 |
command = AntaCommand(command="show aaa methods accounting") |
| 603 |
with pytest.raises( |
| 604 |
RuntimeError, match=r"Command 'show aaa methods accounting' has not been collected and has not returned an error. Call AntaDevice.collect()." |
| 605 |
): |
| 606 |
command.requires_privileges |
| 607 |
|
| 608 |
@pytest.mark.parametrize( |
| 609 |
("command_str", "error", "is_known"), |
| 610 |
[ |
| 611 |
("show ip interface Ethernet1", "Ethernet1 does not support IP", True), |
| 612 |
("ping vrf MGMT 1.1.1.1 source Management0 size 100 df-bit repeat 2", "VRF 'MGMT' is not active", True), |
| 613 |
("ping vrf MGMT 1.1.1.1 source Management1 size 100 df-bit repeat 2", "No source interface Management1", True), |
| 614 |
("show bgp evpn route-type mac-ip aa:c1:ab:de:50:ad vni 10010", "BGP inactive", True), |
| 615 |
("show isis BLAH neighbors", "IS-IS (BLAH) is disabled because: IS-IS Network Entity Title (NET) configuration is not present", True), |
| 616 |
("show ip interface Ethernet1", None, False), |
| 617 |
], |
| 618 |
) |
| 619 |
def test_returned_known_eos_error(self, command_str: str, error: str | None, is_known: bool) -> None: |
| 620 |
"""Test the returned_known_eos_error property.""" |
| 621 |
|
| 622 |
command = AntaCommand(command=command_str, errors=[error] if error else [], output=None if error else "{}") |
| 623 |
assert command.returned_known_eos_error is is_known |
| 624 |
|
| 625 |
def test_returned_known_eos_error_failure(self) -> None: |
| 626 |
"""Test the returned_known_eos_error property unset.""" |
| 627 |
command = AntaCommand(command="show ip interface Ethernet1") |
| 628 |
with pytest.raises( |
| 629 |
RuntimeError, match=r"Command 'show ip interface Ethernet1' has not been collected and has not returned an error. Call AntaDevice.collect()." |
| 630 |
): |
| 631 |
command.returned_known_eos_error |