Skip to content

Commit

Permalink
Fix: Catalog issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc committed Nov 15, 2023
1 parent 107910a commit 0c60d34
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
17 changes: 17 additions & 0 deletions anta/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,20 @@ def parse(filename: str | Path) -> AntaCatalog:
message = f"Unable to parse ANTA Test Catalog file '{filename}'"
anta_log_exception(e, message, logger)
raise

if data is None:
logger.warning("Catalog file at %s is empty", filename)
return AntaCatalog([], filename=filename)

if not isinstance(data, dict):
raise ValueError(f"Parsed data in {filename} does not have the correct format, Aborting...")

try:
catalog_data = AntaCatalogFile(**data)
except ValidationError as e:
anta_log_exception(e, f"Test catalog '{filename}' is invalid!", logger)
raise

tests: list[AntaTestDefinition] = []
for t in catalog_data.root.values():
tests.extend(t)
Expand All @@ -244,6 +253,14 @@ def from_dict(data: RawCatalogInput) -> AntaCatalog:
data: Python dictionary used to instantiate the AntaCatalog instance
"""
tests: list[AntaTestDefinition] = []

if data is None:
logger.warning("Catalog input data is empty")
return AntaCatalog([])

if not isinstance(data, dict):
raise ValueError(f"Wrong input type for catalog data, must be a dict, got {type(data)}")

try:
catalog_data = AntaCatalogFile(**data) # type: ignore[arg-type]
except ValidationError as e:
Expand Down
1 change: 1 addition & 0 deletions tests/data/test_catalog_wrong_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Not a string"
Empty file.
25 changes: 21 additions & 4 deletions tests/units/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
(VerifyL3MTU, {"mtu": 1500, "filters": {"tags": ["demo"]}}),
],
},
{
"name": "test_empty_catalog",
"filename": "test_empty_catalog.yml",
"tests": [],
},
]
CATALOG_PARSE_FAIL_DATA: list[dict[str, Any]] = [
{
Expand Down Expand Up @@ -102,13 +107,19 @@
"filename": "test_catalog_test_definition_multiple_dicts.yml",
"error": "Value error, AntaTestDefinition must be a dictionary with a single entry",
},
{"name": "wrong_type_after_parsing", "filename": "test_catalog_wrong_type.yml", "error": " does not have the correct format, Aborting..."},
]
CATALOG_FROM_DICT_FAIL_DATA: list[dict[str, Any]] = [
{
"name": "undefined_tests",
"filename": "test_catalog_with_undefined_tests.yml",
"error": "FakeTest is not defined in Python module <module 'anta.tests.software' from",
},
{
"name": "wrong_type",
"filename": "test_catalog_wrong_type.yml",
"error": "Wrong input type for catalog data, must be a dict, got <class 'str'>",
},
]
CATALOG_FROM_LIST_FAIL_DATA: list[dict[str, Any]] = [
{
Expand Down Expand Up @@ -209,9 +220,12 @@ def test_parse_fail(self, catalog_data: dict[str, Any]) -> None:
"""
Errors when instantiating AntaCatalog from a file
"""
with pytest.raises(ValidationError) as exec_info:
with pytest.raises((ValidationError, ValueError)) as exec_info:
AntaCatalog.parse(str(DATA_DIR / catalog_data["filename"]))
assert catalog_data["error"] in exec_info.value.errors()[0]["msg"]
if exec_info.type == ValidationError:
assert catalog_data["error"] in exec_info.value.errors()[0]["msg"]
else:
assert catalog_data["error"] in str(exec_info)

def test_parse_fail_parsing(self, caplog: pytest.LogCaptureFixture) -> None:
"""
Expand Down Expand Up @@ -241,9 +255,12 @@ def test_from_dict_fail(self, catalog_data: dict[str, Any]) -> None:
"""
with open(file=str(DATA_DIR / catalog_data["filename"]), mode="r", encoding="UTF-8") as file:
data = safe_load(file)
with pytest.raises(ValidationError) as exec_info:
with pytest.raises((ValidationError, ValueError)) as exec_info:
AntaCatalog.from_dict(data)
assert catalog_data["error"] in exec_info.value.errors()[0]["msg"]
if exec_info.type == ValidationError:
assert catalog_data["error"] in exec_info.value.errors()[0]["msg"]
else:
assert catalog_data["error"] in str(exec_info)

def test_filename(self) -> None:
"""
Expand Down

0 comments on commit 0c60d34

Please sign in to comment.