diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-23 05:06:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-23 05:06:51 +0000 |
commit | 57f704845136d9d0f0266c880cfa1dd7b902e880 (patch) | |
tree | 1c7ffc0d0bf371b2e3d425efbb1c055a24340dc7 /tests/units/test_catalog.py | |
parent | Releasing debian version 0.14.0-2. (diff) | |
download | anta-57f704845136d9d0f0266c880cfa1dd7b902e880.tar.xz anta-57f704845136d9d0f0266c880cfa1dd7b902e880.zip |
Merging upstream version 0.15.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/units/test_catalog.py')
-rw-r--r-- | tests/units/test_catalog.py | 95 |
1 files changed, 81 insertions, 14 deletions
diff --git a/tests/units/test_catalog.py b/tests/units/test_catalog.py index 8de6382..1c7ca8a 100644 --- a/tests/units/test_catalog.py +++ b/tests/units/test_catalog.py @@ -12,7 +12,7 @@ import pytest from pydantic import ValidationError from yaml import safe_load -from anta.catalog import AntaCatalog, AntaTestDefinition +from anta.catalog import AntaCatalog, AntaCatalogFile, AntaTestDefinition from anta.models import AntaTest from anta.tests.interfaces import VerifyL3MTU from anta.tests.mlag import VerifyMlagStatus @@ -76,6 +76,11 @@ INIT_CATALOG_DATA: list[dict[str, Any]] = [ "filename": "test_empty_catalog.yml", "tests": [], }, + { + "name": "test_empty_dict_catalog", + "filename": "test_empty_dict_catalog.yml", + "tests": [], + }, ] CATALOG_PARSE_FAIL_DATA: list[dict[str, Any]] = [ { @@ -160,7 +165,6 @@ CATALOG_FROM_LIST_FAIL_DATA: list[dict[str, Any]] = [ "error": "FakeTestWithInput test inputs are not valid: 1 validation error for Input\n\tstring\n\t Input should be a valid string", }, ] - TESTS_SETTER_FAIL_DATA: list[dict[str, Any]] = [ { "name": "not_a_list", @@ -181,7 +185,7 @@ class TestAntaCatalog: @pytest.mark.parametrize("catalog_data", INIT_CATALOG_DATA, ids=generate_test_ids_list(INIT_CATALOG_DATA)) def test_parse(self, catalog_data: dict[str, Any]) -> None: """Instantiate AntaCatalog from a file.""" - catalog: AntaCatalog = AntaCatalog.parse(str(DATA_DIR / catalog_data["filename"])) + catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / catalog_data["filename"]) assert len(catalog.tests) == len(catalog_data["tests"]) for test_id, (test, inputs_data) in enumerate(catalog_data["tests"]): @@ -221,7 +225,7 @@ class TestAntaCatalog: def test_parse_fail(self, catalog_data: dict[str, Any]) -> None: """Errors when instantiating AntaCatalog from a file.""" with pytest.raises((ValidationError, TypeError)) as exec_info: - AntaCatalog.parse(str(DATA_DIR / catalog_data["filename"])) + AntaCatalog.parse(DATA_DIR / catalog_data["filename"]) if isinstance(exec_info.value, ValidationError): assert catalog_data["error"] in exec_info.value.errors()[0]["msg"] else: @@ -230,7 +234,7 @@ class TestAntaCatalog: def test_parse_fail_parsing(self, caplog: pytest.LogCaptureFixture) -> None: """Errors when instantiating AntaCatalog from a file.""" with pytest.raises(FileNotFoundError) as exec_info: - AntaCatalog.parse(str(DATA_DIR / "catalog_does_not_exist.yml")) + AntaCatalog.parse(DATA_DIR / "catalog_does_not_exist.yml") assert "No such file or directory" in str(exec_info) assert len(caplog.record_tuples) >= 1 _, _, message = caplog.record_tuples[0] @@ -284,16 +288,79 @@ class TestAntaCatalog: catalog.tests = catalog_data["tests"] assert catalog_data["error"] in str(exec_info) + def test_build_indexes_all(self) -> None: + """Test AntaCatalog.build_indexes().""" + catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_with_tags.yml") + catalog.build_indexes() + assert len(catalog.tests_without_tags) == 5 + assert "leaf" in catalog.tag_to_tests + assert len(catalog.tag_to_tests["leaf"]) == 3 + all_unique_tests = catalog.tests_without_tags + for tests in catalog.tag_to_tests.values(): + all_unique_tests.update(tests) + assert len(all_unique_tests) == 11 + assert catalog.indexes_built is True + + def test_build_indexes_filtered(self) -> None: + """Test AntaCatalog.build_indexes().""" + catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_with_tags.yml") + catalog.build_indexes({"VerifyUptime", "VerifyCoredump", "VerifyL3MTU"}) + assert "leaf" in catalog.tag_to_tests + assert len(catalog.tag_to_tests["leaf"]) == 1 + assert len(catalog.tests_without_tags) == 1 + all_unique_tests = catalog.tests_without_tags + for tests in catalog.tag_to_tests.values(): + all_unique_tests.update(tests) + assert len(all_unique_tests) == 4 + assert catalog.indexes_built is True + def test_get_tests_by_tags(self) -> None: """Test AntaCatalog.get_tests_by_tags().""" - catalog: AntaCatalog = AntaCatalog.parse(str(DATA_DIR / "test_catalog_with_tags.yml")) - tests: list[AntaTestDefinition] = catalog.get_tests_by_tags(tags={"leaf"}) + catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_with_tags.yml") + catalog.build_indexes() + tests: set[AntaTestDefinition] = catalog.get_tests_by_tags(tags={"leaf"}) assert len(tests) == 3 - tests = catalog.get_tests_by_tags(tags={"leaf"}, strict=True) - assert len(tests) == 2 + tests = catalog.get_tests_by_tags(tags={"leaf", "spine"}, strict=True) + assert len(tests) == 1 - def test_get_tests_by_names(self) -> None: - """Test AntaCatalog.get_tests_by_tags().""" - catalog: AntaCatalog = AntaCatalog.parse(str(DATA_DIR / "test_catalog_with_tags.yml")) - tests: list[AntaTestDefinition] = catalog.get_tests_by_names(names={"VerifyUptime", "VerifyCoredump"}) - assert len(tests) == 3 + def test_merge(self) -> None: + """Test AntaCatalog.merge().""" + catalog1: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog.yml") + assert len(catalog1.tests) == 1 + catalog2: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog.yml") + assert len(catalog2.tests) == 1 + catalog3: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_medium.yml") + assert len(catalog3.tests) == 228 + + assert len(catalog1.merge(catalog2).tests) == 2 + assert len(catalog1.tests) == 1 + assert len(catalog2.tests) == 1 + + assert len(catalog2.merge(catalog3).tests) == 229 + assert len(catalog2.tests) == 1 + assert len(catalog3.tests) == 228 + + def test_dump(self) -> None: + """Test AntaCatalog.dump().""" + catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog.yml") + assert len(catalog.tests) == 1 + file: AntaCatalogFile = catalog.dump() + assert sum(len(tests) for tests in file.root.values()) == 1 + + catalog = AntaCatalog.parse(DATA_DIR / "test_catalog_medium.yml") + assert len(catalog.tests) == 228 + file = catalog.dump() + assert sum(len(tests) for tests in file.root.values()) == 228 + + +class TestAntaCatalogFile: # pylint: disable=too-few-public-methods + """Test for anta.catalog.AntaCatalogFile.""" + + def test_yaml(self) -> None: + """Test AntaCatalogFile.yaml().""" + file = DATA_DIR / "test_catalog_medium.yml" + catalog = AntaCatalog.parse(file) + assert len(catalog.tests) == 228 + catalog_yaml_str = catalog.dump().yaml() + with file.open(encoding="UTF-8") as f: + assert catalog_yaml_str == f.read() |