-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathloader.py
More file actions
112 lines (94 loc) · 4.16 KB
/
loader.py
File metadata and controls
112 lines (94 loc) · 4.16 KB
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
from __future__ import annotations
from collections.abc import Iterable
from typing import Any
import pytest
import yaml
from pytest import Item
from .exceptions import InvalidResourceConfigError
from .items import (
InfrahubCheckIntegrationItem,
InfrahubCheckSmokeItem,
InfrahubCheckUnitProcessItem,
InfrahubGraphQLQueryIntegrationItem,
InfrahubGraphQLQuerySmokeItem,
InfrahubItem,
InfrahubJinja2TransformIntegrationItem,
InfrahubJinja2TransformSmokeItem,
InfrahubJinja2TransformUnitRenderItem,
InfrahubPythonTransformIntegrationItem,
InfrahubPythonTransformSmokeItem,
InfrahubPythonTransformUnitProcessItem,
)
from .models import InfrahubTestFileV1, InfrahubTestGroup
MARKER_MAPPING = {
"Check": pytest.mark.infrahub_check,
"GraphQLQuery": pytest.mark.infrahub_graphql_query,
"Jinja2Transform": pytest.mark.infrahub_jinja2_transform,
"PythonTransform": pytest.mark.infrahub_python_transform,
}
CONFIG_MAPPING = {
"Check": "get_check_definition",
"GraphQLQuery": None,
"Jinja2Transform": "get_jinja2_transform",
"PythonTransform": "get_python_transform",
}
ITEMS_MAPPING = {
"check-smoke": InfrahubCheckSmokeItem,
"check-unit-process": InfrahubCheckUnitProcessItem,
"check-integration": InfrahubCheckIntegrationItem,
"graphql-query-smoke": InfrahubGraphQLQuerySmokeItem,
"graphql-query-integration": InfrahubGraphQLQueryIntegrationItem,
"jinja2-transform-smoke": InfrahubJinja2TransformSmokeItem,
"jinja2-transform-unit-render": InfrahubJinja2TransformUnitRenderItem,
"jinja2-transform-integration": InfrahubJinja2TransformIntegrationItem,
"python-transform-smoke": InfrahubPythonTransformSmokeItem,
"python-transform-unit-process": InfrahubPythonTransformUnitProcessItem,
"python-transform-integration": InfrahubPythonTransformIntegrationItem,
}
class InfrahubYamlFile(pytest.File):
def get_resource_config(self, group: InfrahubTestGroup) -> Any | None:
"""Retrieve the resource configuration to apply to all tests in a group."""
resource_config_function = CONFIG_MAPPING.get(group.resource)
resource_config = None
if resource_config_function is not None:
func = getattr(self.session.infrahub_repo_config, resource_config_function) # type:ignore[attr-defined]
try:
resource_config = func(group.resource_name)
except KeyError:
# Ignore error and just return None
pass
return resource_config
def collect_group(self, group: InfrahubTestGroup) -> Iterable[Item]:
"""Collect all items for a group."""
marker = MARKER_MAPPING[group.resource]
resource_config = self.get_resource_config(group)
for test in group.tests:
item_class: type[pytest.Item] = ITEMS_MAPPING[test.spec.kind] # type: ignore[assignment]
item: InfrahubItem = item_class.from_parent(
name=f"{marker.markname}__{group.resource_name}__{test.name}",
parent=self,
resource_name=group.resource_name,
resource_config=resource_config,
test=test,
) # type: ignore[assignment]
# If item does not pass validation, mark it to be skipped
try:
item.validate_resource_config()
except InvalidResourceConfigError as exc:
item.add_marker(pytest.mark.skip(reason=str(exc)))
item.add_marker(pytest.mark.infrahub)
item.add_marker(marker)
if "smoke" in test.spec.kind:
item.add_marker(pytest.mark.infrahub_smoke)
if "unit" in test.spec.kind:
item.add_marker(pytest.mark.infrahub_unit)
if "integration" in test.spec.kind:
item.add_marker(pytest.mark.infrahub_integration)
yield item
def collect(self) -> Iterable[Item]:
raw = yaml.safe_load(self.path.open(encoding="utf-8"))
if not raw or "infrahub_tests" not in raw:
return
content = InfrahubTestFileV1(**raw)
for test_group in content.infrahub_tests:
yield from self.collect_group(test_group)