[VVP] Added new three new reports
[vvp/validation-scripts.git] / ice_validator / tests / parametrizers.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 #
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 """parametrizers
42 """
43
44 from os import path, listdir
45 import re
46 import yaml
47 import pytest
48 from .helpers import get_parsed_yml_for_yaml_files, check_basename_ending
49 from .utils.nested_files import get_list_of_nested_files
50
51 VERSION = "1.0.0"
52
53
54 # pylint: disable=invalid-name
55
56
57 def get_template_dir(metafunc):
58     """
59     returns template_dir, either as its passed in on CLI
60     or, during --self-test, the directory whos name matches
61     the current tests module name
62     """
63     if metafunc.config.getoption("template_dir") is None:
64         return path.join(
65             path.dirname(path.realpath(__file__)),
66             "fixtures",
67             metafunc.function.__module__.split(".")[-1],
68         )
69     else:
70         return metafunc.config.getoption("template_dir")[0]
71
72
73 def get_nested_files(filenames):
74     """
75     returns all the nested files for a set of filenames
76     """
77     nested_files = []
78     for filename in filenames:
79         try:
80             with open(filename) as fh:
81                 yml = yaml.load(fh)
82             if "resources" not in yml:
83                 continue
84             nested_files.extend(
85                 get_list_of_nested_files(yml["resources"], path.dirname(filename))
86             )
87         except yaml.YAMLError as e:
88             print(e)  # pylint: disable=superfluous-parens
89             continue
90     return nested_files
91
92
93 def list_filenames_in_template_dir(
94         metafunc, extensions, template_type="", sub_dirs=None
95 ):
96     """
97     returns the filenames in a template_dir, either as its passed in
98     on CLI or, during --self-test, the directory whos name matches
99     the current tests module name
100     """
101     sub_dirs = [] if sub_dirs is None else sub_dirs
102     template_dir = get_template_dir(metafunc)
103     filenames = []
104     if metafunc.config.getoption("self_test"):
105         filenames = [
106             path.join(template_dir, s, f)
107             for s in sub_dirs
108             for f in listdir(path.join(template_dir, s))
109             if (path.isfile(path.join(template_dir, s, f)) and
110                 path.splitext(f)[-1] in extensions and
111                 check_basename_ending(template_type, path.splitext(f)[0]))
112         ]
113     else:
114         filenames = [
115             path.join(template_dir, f)
116             for f in listdir(template_dir)
117             if (path.isfile(path.join(template_dir, f)) and
118                 path.splitext(f)[-1] in extensions and
119                 check_basename_ending(template_type, path.splitext(f)[0]))
120         ]
121     return filenames
122
123
124 def list_template_dir(
125         metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
126 ):
127     """
128     returns the filenames excluding the nested files for a template_dir,
129     either as its passed in on CLI or, during --self-test, the
130     directory whos name matches the current tests module name
131     """
132     sub_dirs = [] if sub_dirs is None else sub_dirs
133     filenames = []
134     nested_files = []
135     filenames = list_filenames_in_template_dir(
136         metafunc, extensions, template_type, sub_dirs
137     )
138     if exclude_nested:
139         nested_files = get_nested_files(filenames)
140     return list(set(filenames) - set(nested_files))
141
142
143 def get_filenames_list(
144         metafunc, extensions=None, exclude_nested=False, template_type=""
145 ):
146     """
147     returns the filename fixtures for the template dir, either as by how its
148     passed in on CLI or, during --self-test, the directory whos name
149     matches the current tests module name
150     """
151     extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
152     if metafunc.config.getoption("self_test"):
153         filenames_list = list_template_dir(
154             metafunc, extensions, exclude_nested, template_type, ["pass"]
155         )
156         filenames_list += [
157             pytest.mark.xfail(f, strict=True)
158             for f in list_template_dir(
159                 metafunc, extensions, exclude_nested, template_type, ["fail"]
160             )
161         ]
162     else:
163         filenames_list = list_template_dir(
164             metafunc, extensions, exclude_nested, template_type
165         )
166
167     return filenames_list
168
169
170 def get_filenames_lists(
171         metafunc, extensions=None, exclude_nested=False, template_type=""
172 ):
173     """
174     returns the list of files in the template dir, either as by how its
175     passed in on CLI or, during --self-test, the directory whos name
176     matches the current tests module name
177     """
178     extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
179     filenames_lists = []
180     if metafunc.config.getoption("self_test"):
181         filenames_lists.append(
182             list_template_dir(
183                 metafunc, extensions, exclude_nested, template_type, ["pass"]
184             )
185         )
186         filenames_lists.append(
187             pytest.mark.xfail(
188                 list_template_dir(
189                     metafunc, extensions, exclude_nested, template_type, ["fail"]
190                 ),
191                 strict=True,
192             )
193         )
194     else:
195         filenames_lists.append(
196             list_template_dir(metafunc, extensions, exclude_nested, template_type)
197         )
198     return filenames_lists
199
200
201 def get_parsed_yaml_files(
202         metafunc, extensions, exclude_nested=True, template_type="", sections=None
203 ):
204     """
205     returns the list of parsed yaml files in the specified template dir,
206     either as by how its passed in on CLI or, during --self-test, the
207     directory whos name matches the current tests module name
208     """
209     sections = [] if sections is None else sections
210     extensions = [".yaml", ".yml"]
211
212     if metafunc.config.getoption("self_test"):
213         yaml_files = list_template_dir(
214             metafunc, extensions, exclude_nested, template_type, ["pass"]
215         )
216         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
217
218         yaml_files = list_template_dir(
219             metafunc, extensions, exclude_nested, template_type, ["fail"]
220         )
221         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
222         parsed_yml_list += [
223             pytest.mark.xfail(parsed_yml, strict=True) for parsed_yml in parsed_yml_list
224         ]
225     else:
226         yaml_files = list_template_dir(metafunc, extensions)
227         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
228     return parsed_yml_list
229
230
231 def parametrize_filenames(metafunc):
232     """
233     This param runs tests all files in the template dir
234     """
235     filenames = get_filenames_lists(metafunc)
236     metafunc.parametrize("filenames", filenames)
237
238
239 def parametrize_filename(metafunc):
240     """
241     This param runs tests once for every file in the template dir
242     """
243     filenames = get_filenames_list(metafunc)
244     metafunc.parametrize("filename", filenames)
245
246
247 def parametrize_yaml_files(metafunc):
248     """
249     This param runs tests for the yaml files in the template dir
250     """
251     yaml_files = get_filenames_lists(metafunc, [".yaml", ".yml"], False)
252     metafunc.parametrize("yaml_files", yaml_files)
253
254
255 def parametrize_yaml_file(metafunc):
256     """
257     This param runs tests for every yaml file in the template dir
258     """
259     yaml_files = get_filenames_list(metafunc, [".yaml", ".yml"], False)
260     metafunc.parametrize("yaml_file", yaml_files)
261
262
263 def parametrize_templates(metafunc):
264     """
265     This param runs tests for the template in the template dir
266     """
267     templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True)
268     metafunc.parametrize("templates", templates)
269
270
271 def parametrize_template(metafunc):
272     """
273     This param runs tests for every template in the template dir
274     """
275     templates = get_filenames_list(metafunc, [".yaml", ".yml"], True)
276     metafunc.parametrize("template", templates)
277
278
279 def parametrize_parsed_yaml_file(metafunc):
280     """
281     This param runs tests for a parsed version of each yaml file
282     in the template dir
283     """
284     parsed_yaml_files = get_parsed_yaml_files(metafunc, [".yaml", ".yml"], False)
285     metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
286
287
288 def parametrize_heat_templates(metafunc):
289     """
290     This param runs tests for all heat templates in the template dir
291     """
292     heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
293     metafunc.parametrize("heat_templates", heat_templates)
294
295
296 def parametrize_heat_template(metafunc):
297     """
298     This param runs tests for every heat template in the template dir
299     """
300     heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
301     metafunc.parametrize("heat_template", heat_templates)
302
303
304 def parametrize_volume_templates(metafunc):
305     """
306     This param runs tests for all volume templates in the template dir
307     """
308     volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
309     metafunc.parametrize("volume_templates", volume_templates)
310
311
312 def parametrize_volume_template(metafunc):
313     """
314
315     This param runs tests for every volume template in the template dir
316     """
317     volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
318     metafunc.parametrize("volume_template", volume_templates)
319
320
321 def parametrize_environment_files(metafunc):
322     """
323     This param runs tests for all environment files in the template dir
324     """
325     env_files = get_filenames_lists(metafunc, [".env"])
326     metafunc.parametrize("env_files", env_files)
327
328
329 def parametrize_environment_file(metafunc):
330     """
331     This param runs tests for every environment file in the template dir
332     """
333     env_files = get_filenames_list(metafunc, [".env"])
334     metafunc.parametrize("env_file", env_files)
335
336
337 def parametrize_parsed_environment_file(metafunc):
338     """
339     This param runs tests for every parsed environment file
340     in the template dir
341     """
342     parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
343     metafunc.parametrize("parsed_env_file", parsed_env_files)
344
345
346 def parametrize_template_dir(metafunc):
347     """
348     This param passes a  the template_dir as passed in on CLI
349     or, during --self-test, passes in the sub directories of
350     template_dir/pass/ and template_dir/fail
351     template_dir = get_template_dir(metafunc)
352     """
353     template_dir = get_template_dir(metafunc)
354
355     if metafunc.config.getoption("self_test"):
356         dirs = [
357             path.join(template_dir, s, t)
358             for s in ["pass"]
359             for t in listdir(path.join(template_dir, s))
360             if path.isdir(path.join(template_dir, s, t))
361         ]
362
363         dirs += [
364             pytest.mark.xfail(path.join(template_dir, s, t))
365             for s in ["fail"]
366             for t in listdir(path.join(template_dir, s))
367             if path.isdir(path.join(template_dir, s, t))
368         ]
369     else:
370         dirs = [template_dir]
371
372     metafunc.parametrize("template_dir", dirs)
373
374
375 def parametrize_environment_pair(metafunc, template_type=""):
376     """
377     Define a list of pairs of parsed yaml from the heat templates and
378     environment files
379     """
380     pairs = []
381     if metafunc.config.getoption("self_test"):
382         sub_dirs = ["pass", "fail"]
383         env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
384         yaml_files = list_template_dir(
385             metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
386         )
387     else:
388         env_files = list_template_dir(metafunc, [".env"], True, template_type)
389         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
390
391     for filename in env_files:
392         basename = path.splitext(filename)[0]
393         if basename + ".yml" in yaml_files:
394             yfilename = basename + ".yml"
395         else:
396             yfilename = basename + ".yaml"
397
398         try:
399             with open(filename) as fh:
400                 eyml = yaml.load(fh)
401             with open(yfilename) as fh:
402                 yyml = yaml.load(fh)
403
404             if "fail" in filename:
405                 pairs.append(
406                     pytest.mark.xfail(
407                         {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
408                     )
409                 )
410             else:
411                 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
412
413         except yaml.YAMLError as e:
414             print(e)  # pylint: disable=superfluous-parens
415
416     metafunc.parametrize("environment_pair", pairs)
417
418
419 def parametrize_heat_volume_pair(metafunc):
420     """
421     Define a list of pairs of parsed yaml from the a heat and volume
422     template
423     """
424     pairs = []
425     if metafunc.config.getoption("self_test"):
426         sub_dirs = ["pass", "fail"]
427         volume_files = list_template_dir(
428             metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
429         )
430         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
431     else:
432         volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
433         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
434
435     pattern = re.compile(r"\_volume$")
436     for vfilename in volume_files:
437         basename = pattern.sub("", path.splitext(vfilename)[0])
438         if basename + ".yml" in yaml_files:
439             yfilename = basename + ".yml"
440         else:
441             yfilename = basename + ".yaml"
442
443         try:
444             with open(vfilename) as fh:
445                 vyml = yaml.load(fh)
446             with open(yfilename) as fh:
447                 yyml = yaml.load(fh)
448
449             if "fail" in vfilename:
450                 pairs.append(
451                     pytest.mark.xfail(
452                         {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
453                     )
454                 )
455             else:
456                 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
457
458         except yaml.YAMLError as e:
459             print(e)  # pylint: disable=superfluous-parens
460
461     metafunc.parametrize("heat_volume_pair", pairs)