-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_plugin.py
More file actions
291 lines (254 loc) · 7.79 KB
/
Copy pathtest_plugin.py
File metadata and controls
291 lines (254 loc) · 7.79 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def test_help_message(pytester):
"""Make sure that the plugin is loaded by capturing an option it adds in the help message."""
result = pytester.runpytest("--help")
result.stdout.fnmatch_lines(["*Infrahub configuration file for the repository*"])
def test_without_config(pytester):
"""Make sure 0 tests run when test file is not found."""
result = pytester.runpytest()
result.assert_outcomes()
def test_emptyconfig(pytester):
"""Make sure that the plugin load the test file properly."""
pytester.makefile(
".yml",
test_empty="""
---
version: "1.0"
infrahub_tests: []
""",
)
result = pytester.runpytest()
result.assert_outcomes()
def test_jinja2_transform_config_missing_directory(pytester):
"""Make sure tests raise errors if directories are not found."""
pytester.makefile(
".yml",
test_jinja2_transform="""
---
version: "1.0"
infrahub_tests:
- resource: "Jinja2Transform"
resource_name: "bgp_config"
tests:
- name: "base"
expect: PASS
spec:
kind: "jinja2-transform-unit-render"
directory: bgp_config/base
""",
)
pytester.makefile(
".yml",
infrahub_config="""
---
schemas:
- schemas/demo_edge_fabric.yml
jinja2_transforms:
- name: bgp_config
description: "Template for BGP config base"
query: "bgp_sessions"
template_path: "templates/bgp_config.j2"
""",
)
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
result.assert_outcomes(errors=1)
def test_jinja2_transform_config_missing_input(pytester):
"""Make sure tests raise errors if no inputs are provided."""
pytester.makefile(
".yml",
test_jinja2_transform="""
---
version: "1.0"
infrahub_tests:
- resource: "Jinja2Transform"
resource_name: "bgp_config"
tests:
- name: "base"
expect: PASS
spec:
kind: "jinja2-transform-unit-render"
directory: bgp_config/base
""",
)
pytester.makefile(
".yml",
infrahub_config="""
---
schemas:
- schemas/demo_edge_fabric.yml
jinja2_transforms:
- name: bgp_config
description: "Template for BGP config base"
query: "bgp_sessions"
template_path: "templates/bgp_config.j2"
""",
)
pytester.mkdir("bgp_config")
pytester.mkdir("bgp_config/base")
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
result.assert_outcomes(errors=1)
def test_jinja2_transform_no_expected_output(pytester):
"""Make sure tests succeed if no expect outputs are provided."""
pytester.makefile(
".yml",
test_jinja2_transform="""
---
version: "1.0"
infrahub_tests:
- resource: "Jinja2Transform"
resource_name: "bgp_config"
tests:
- name: "base"
expect: PASS
spec:
kind: "jinja2-transform-unit-render"
directory: bgp_config/base
""",
)
pytester.makefile(
".yml",
infrahub_config="""
---
schemas:
- schemas/demo_edge_fabric.yml
jinja2_transforms:
- name: bgp_config
description: "Template for BGP config base"
query: "bgp_sessions"
template_path: "templates/bgp_config.j2"
""",
)
pytester.mkdir("bgp_config")
test_dir = pytester.mkdir("bgp_config/base")
test_input = pytester.makefile(".json", input='{"data": {}}')
pytester.run("mv", test_input, test_dir)
template_dir = pytester.mkdir("templates")
template = pytester.makefile(
".j2",
bgp_config="""
protocols {
bgp {
log-up-down;
bgp-error-tolerance;
}
}
""",
)
pytester.run("mv", template, template_dir)
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
result.assert_outcomes(passed=1)
def test_jinja2_transform_unexpected_output(pytester):
"""Make sure tests fail if the expected and computed outputs don't match."""
pytester.makefile(
".yml",
test_jinja2_transform="""
---
version: "1.0"
infrahub_tests:
- resource: "Jinja2Transform"
resource_name: "bgp_config"
tests:
- name: "base"
expect: PASS
spec:
kind: "jinja2-transform-unit-render"
directory: bgp_config/base
""",
)
pytester.makefile(
".yml",
infrahub_config="""
---
schemas:
- schemas/demo_edge_fabric.yml
jinja2_transforms:
- name: bgp_config
description: "Template for BGP config base"
query: "bgp_sessions"
template_path: "templates/bgp_config.j2"
""",
)
pytester.mkdir("bgp_config")
test_dir = pytester.mkdir("bgp_config/base")
test_input = pytester.makefile(".json", input='{"data": {}}')
test_output = pytester.makefile(
".txt",
output="""
protocols {
bgp {
group ipv4-ibgp {
peer-as 64545;
}
log-up-down;
bgp-error-tolerance;
}
}
""",
)
pytester.run("mv", test_input, test_dir)
pytester.run("mv", test_output, test_dir)
template_dir = pytester.mkdir("templates")
template = pytester.makefile(
".j2",
bgp_config="""
protocols {
bgp {
log-up-down;
bgp-error-tolerance;
}
}
""",
)
pytester.run("mv", template, template_dir)
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
result.assert_outcomes(failed=1)
def test_python_transform(pytester):
pytester.makefile(
".yml",
test_python_transform="""
---
version: "1.0"
infrahub_tests:
- resource: "PythonTransform"
resource_name: "device_config"
tests:
- name: "base_config"
expect: PASS
spec:
kind: "python-transform-unit-process"
directory: device_config/base_config
""",
)
pytester.makefile(
".yml",
infrahub_config="""
---
schemas:
- schemas/dcim.yml
python_transforms:
- name: device_config
class_name: "DeviceConfig"
file_path: "transforms/device_config.py"
""",
)
test_input = pytester.makefile(
".json", input='{"data": { "InfraDevice": { "edges": [ { "node": { "name": {"value": "atl1-edge1"} } } ] } } }'
)
test_output = pytester.makefile(".json", output='{"hostname": "atl1-edge1"}')
test_template = pytester.makefile(
".py",
device_config="""
from infrahub_sdk.transforms import InfrahubTransform
class DeviceConfig(InfrahubTransform):
query = "device_config"
async def transform(self, data):
return {"hostname": data["InfraDevice"]["edges"][0]["node"]["name"]["value"]}
""",
)
pytester.mkdir("device_config")
test_dir = pytester.mkdir("device_config/base_config")
pytester.run("mv", test_input, test_dir)
pytester.run("mv", test_output, test_dir)
transform_dir = pytester.mkdir("transforms")
pytester.run("mv", test_template, transform_dir)
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
result.assert_outcomes(passed=1)