diff options
Diffstat (limited to '')
-rw-r--r-- | tests/units/inventory/__init__.py | 1 | ||||
-rw-r--r-- | tests/units/inventory/test_inventory.py | 19 | ||||
-rw-r--r-- | tests/units/inventory/test_models.py | 76 |
3 files changed, 48 insertions, 48 deletions
diff --git a/tests/units/inventory/__init__.py b/tests/units/inventory/__init__.py index e772bee..70fbdda 100644 --- a/tests/units/inventory/__init__.py +++ b/tests/units/inventory/__init__.py @@ -1,3 +1,4 @@ # 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. +"""Tests for inventory submodule.""" diff --git a/tests/units/inventory/test_inventory.py b/tests/units/inventory/test_inventory.py index 7c62b5c..430ca21 100644 --- a/tests/units/inventory/test_inventory.py +++ b/tests/units/inventory/test_inventory.py @@ -2,23 +2,25 @@ # Use of this source code is governed by the Apache License 2.0 # that can be found in the LICENSE file. """ANTA Inventory unit tests.""" + from __future__ import annotations -import logging -from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING, Any import pytest import yaml from pydantic import ValidationError from anta.inventory import AntaInventory -from anta.inventory.exceptions import InventoryIncorrectSchema, InventoryRootKeyError +from anta.inventory.exceptions import InventoryIncorrectSchemaError, InventoryRootKeyError from tests.data.json_data import ANTA_INVENTORY_TESTS_INVALID, ANTA_INVENTORY_TESTS_VALID from tests.lib.utils import generate_test_ids_dict +if TYPE_CHECKING: + from pathlib import Path + -class Test_AntaInventory: +class TestAntaInventory: """Test AntaInventory class.""" def create_inventory(self, content: str, tmp_path: Path) -> str: @@ -31,7 +33,7 @@ class Test_AntaInventory: def check_parameter(self, parameter: str, test_definition: dict[Any, Any]) -> bool: """Check if parameter is configured in testbed.""" - return "parameters" in test_definition and parameter in test_definition["parameters"].keys() + return "parameters" in test_definition and parameter in test_definition["parameters"] @pytest.mark.parametrize("test_definition", ANTA_INVENTORY_TESTS_VALID, ids=generate_test_ids_dict) def test_init_valid(self, test_definition: dict[str, Any], tmp_path: Path) -> None: @@ -55,8 +57,7 @@ class Test_AntaInventory: try: AntaInventory.parse(filename=inventory_file, username="arista", password="arista123") except ValidationError as exc: - logging.error("Exceptions is: %s", str(exc)) - assert False + raise AssertionError from exc @pytest.mark.parametrize("test_definition", ANTA_INVENTORY_TESTS_INVALID, ids=generate_test_ids_dict) def test_init_invalid(self, test_definition: dict[str, Any], tmp_path: Path) -> None: @@ -77,5 +78,5 @@ class Test_AntaInventory: """ inventory_file = self.create_inventory(content=test_definition["input"], tmp_path=tmp_path) - with pytest.raises((InventoryIncorrectSchema, InventoryRootKeyError, ValidationError)): + with pytest.raises((InventoryIncorrectSchemaError, InventoryRootKeyError, ValidationError)): AntaInventory.parse(filename=inventory_file, username="arista", password="arista123") diff --git a/tests/units/inventory/test_models.py b/tests/units/inventory/test_models.py index 83f151c..0dccfb8 100644 --- a/tests/units/inventory/test_models.py +++ b/tests/units/inventory/test_models.py @@ -2,6 +2,7 @@ # Use of this source code is governed by the Apache License 2.0 # that can be found in the LICENSE file. """ANTA Inventory models unit tests.""" + from __future__ import annotations import logging @@ -30,7 +31,7 @@ from tests.data.json_data import ( from tests.lib.utils import generate_test_ids_dict -class Test_InventoryUnitModels: +class TestInventoryUnitModels: """Test components of AntaInventoryInput model.""" @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_HOST_VALID, ids=generate_test_ids_dict) @@ -51,9 +52,8 @@ class Test_InventoryUnitModels: host_inventory = AntaInventoryHost(host=test_definition["input"]) except ValidationError as exc: logging.warning("Error: %s", str(exc)) - assert False - else: - assert test_definition["input"] == str(host_inventory.host) + raise AssertionError from exc + assert test_definition["input"] == str(host_inventory.host) @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_HOST_INVALID, ids=generate_test_ids_dict) def test_anta_inventory_host_invalid(self, test_definition: dict[str, Any]) -> None: @@ -110,9 +110,8 @@ class Test_InventoryUnitModels: network_inventory = AntaInventoryNetwork(network=test_definition["input"]) except ValidationError as exc: logging.warning("Error: %s", str(exc)) - assert False - else: - assert test_definition["input"] == str(network_inventory.network) + raise AssertionError from exc + assert test_definition["input"] == str(network_inventory.network) @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_NETWORK_INVALID, ids=generate_test_ids_dict) def test_anta_inventory_network_invalid(self, test_definition: dict[str, Any]) -> None: @@ -133,11 +132,11 @@ class Test_InventoryUnitModels: except ValidationError as exc: logging.warning("Error: %s", str(exc)) else: - assert False + raise AssertionError @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_NETWORK_CACHE, ids=generate_test_ids_dict) def test_anta_inventory_network_cache(self, test_definition: dict[str, Any]) -> None: - """Test network disable_cache + """Test network disable_cache. Test structure: --------------- @@ -176,10 +175,9 @@ class Test_InventoryUnitModels: ) except ValidationError as exc: logging.warning("Error: %s", str(exc)) - assert False - else: - assert test_definition["input"]["start"] == str(range_inventory.start) - assert test_definition["input"]["end"] == str(range_inventory.end) + raise AssertionError from exc + assert test_definition["input"]["start"] == str(range_inventory.start) + assert test_definition["input"]["end"] == str(range_inventory.end) @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_RANGE_INVALID, ids=generate_test_ids_dict) def test_anta_inventory_range_invalid(self, test_definition: dict[str, Any]) -> None: @@ -203,11 +201,11 @@ class Test_InventoryUnitModels: except ValidationError as exc: logging.warning("Error: %s", str(exc)) else: - assert False + raise AssertionError @pytest.mark.parametrize("test_definition", INVENTORY_MODEL_RANGE_CACHE, ids=generate_test_ids_dict) def test_anta_inventory_range_cache(self, test_definition: dict[str, Any]) -> None: - """Test range disable_cache + """Test range disable_cache. Test structure: --------------- @@ -221,22 +219,23 @@ class Test_InventoryUnitModels: """ if "disable_cache" in test_definition["input"]: range_inventory = AntaInventoryRange( - start=test_definition["input"]["start"], end=test_definition["input"]["end"], disable_cache=test_definition["input"]["disable_cache"] + start=test_definition["input"]["start"], + end=test_definition["input"]["end"], + disable_cache=test_definition["input"]["disable_cache"], ) else: range_inventory = AntaInventoryRange(start=test_definition["input"]["start"], end=test_definition["input"]["end"]) assert test_definition["expected_result"] == range_inventory.disable_cache -class Test_AntaInventoryInputModel: +class TestAntaInventoryInputModel: """Unit test of AntaInventoryInput model.""" def test_inventory_input_structure(self) -> None: """Test inventory keys are those expected.""" - inventory = AntaInventoryInput() logging.info("Inventory keys are: %s", str(inventory.model_dump().keys())) - assert all(elem in inventory.model_dump().keys() for elem in ["hosts", "networks", "ranges"]) + assert all(elem in inventory.model_dump() for elem in ["hosts", "networks", "ranges"]) @pytest.mark.parametrize("inventory_def", INVENTORY_MODEL_VALID, ids=generate_test_ids_dict) def test_anta_inventory_intput_valid(self, inventory_def: dict[str, Any]) -> None: @@ -265,10 +264,9 @@ class Test_AntaInventoryInputModel: inventory = AntaInventoryInput(**inventory_def["input"]) except ValidationError as exc: logging.warning("Error: %s", str(exc)) - assert False - else: - logging.info("Checking if all root keys are correctly lodaded") - assert all(elem in inventory.model_dump().keys() for elem in inventory_def["input"].keys()) + raise AssertionError from exc + logging.info("Checking if all root keys are correctly lodaded") + assert all(elem in inventory.model_dump() for elem in inventory_def["input"]) @pytest.mark.parametrize("inventory_def", INVENTORY_MODEL_INVALID, ids=generate_test_ids_dict) def test_anta_inventory_intput_invalid(self, inventory_def: dict[str, Any]) -> None: @@ -294,19 +292,19 @@ class Test_AntaInventoryInputModel: """ try: - if "hosts" in inventory_def["input"].keys(): + if "hosts" in inventory_def["input"]: logging.info( "Loading %s into AntaInventoryInput hosts section", str(inventory_def["input"]["hosts"]), ) AntaInventoryInput(hosts=inventory_def["input"]["hosts"]) - if "networks" in inventory_def["input"].keys(): + if "networks" in inventory_def["input"]: logging.info( "Loading %s into AntaInventoryInput networks section", str(inventory_def["input"]["networks"]), ) AntaInventoryInput(networks=inventory_def["input"]["networks"]) - if "ranges" in inventory_def["input"].keys(): + if "ranges" in inventory_def["input"]: logging.info( "Loading %s into AntaInventoryInput ranges section", str(inventory_def["input"]["ranges"]), @@ -315,10 +313,10 @@ class Test_AntaInventoryInputModel: except ValidationError as exc: logging.warning("Error: %s", str(exc)) else: - assert False + raise AssertionError -class Test_InventoryDeviceModel: +class TestInventoryDeviceModel: """Unit test of InventoryDevice model.""" @pytest.mark.parametrize("test_definition", INVENTORY_DEVICE_MODEL_VALID, ids=generate_test_ids_dict) @@ -349,12 +347,12 @@ class Test_InventoryDeviceModel: if test_definition["expected_result"] == "invalid": pytest.skip("Not concerned by the test") - for entity in test_definition["input"]: - try: + try: + for entity in test_definition["input"]: AsyncEOSDevice(**entity) - except TypeError as exc: - logging.warning("Error: %s", str(exc)) - assert False + except TypeError as exc: + logging.warning("Error: %s", str(exc)) + raise AssertionError from exc @pytest.mark.parametrize("test_definition", INVENTORY_DEVICE_MODEL_INVALID, ids=generate_test_ids_dict) def test_inventory_device_invalid(self, test_definition: dict[str, Any]) -> None: @@ -384,10 +382,10 @@ class Test_InventoryDeviceModel: if test_definition["expected_result"] == "valid": pytest.skip("Not concerned by the test") - for entity in test_definition["input"]: - try: + try: + for entity in test_definition["input"]: AsyncEOSDevice(**entity) - except TypeError as exc: - logging.info("Error: %s", str(exc)) - else: - assert False + except TypeError as exc: + logging.info("Error: %s", str(exc)) + else: + raise AssertionError |