[VVP] Add test for R-100260 and fix mapping
[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 #
39
40 """parametrizers
41 """
42
43 from os import path, listdir
44 import re
45 from tests import cached_yaml as yaml
46 import pytest
47 from tests.helpers import get_parsed_yml_for_yaml_files, check_basename_ending
48 from tests.utils.nested_files import get_nested_files
49
50 VERSION = "1.0.0"
51
52 # pylint: disable=invalid-name
53
54
55 def get_template_dir(metafunc):
56     """
57     returns template_dir, either as its passed in on CLI
58     or, during --self-test, the directory whos name matches
59     the current tests module name
60     """
61     if metafunc.config.getoption("template_dir") is None:
62         return path.join(
63             path.dirname(metafunc.module.__file__),
64             "fixtures",
65             metafunc.function.__module__.split(".")[-1],
66         )
67     else:
68         return metafunc.config.getoption("template_dir")[0]
69
70
71 def list_filenames_in_template_dir(
72     metafunc, extensions, template_type="", sub_dirs=None
73 ):
74     """
75     returns the filenames in a template_dir, either as its passed in
76     on CLI or, during --self-test, the directory whos name matches
77     the current tests module name
78     """
79     sub_dirs = [] if sub_dirs is None else sub_dirs
80     template_dir = get_template_dir(metafunc)
81     filenames = []
82     if metafunc.config.getoption("self_test"):
83         filenames = [
84             path.join(template_dir, s, f)
85             for s in sub_dirs
86             for f in listdir(path.join(template_dir, s))
87             if path.isfile(path.join(template_dir, s, f))
88             and path.splitext(f)[-1] in extensions
89             and check_basename_ending(template_type, path.splitext(f)[0])
90         ]
91     else:
92         filenames = [
93             path.join(template_dir, f)
94             for f in listdir(template_dir)
95             if path.isfile(path.join(template_dir, f))
96             and path.splitext(f)[-1] in extensions
97             and check_basename_ending(template_type, path.splitext(f)[0])
98         ]
99     return filenames
100
101
102 def list_template_dir(
103     metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
104 ):
105     """
106     returns the filenames excluding the nested files for a template_dir,
107     either as its passed in on CLI or, during --self-test, the
108     directory whos name matches the current tests module name
109     """
110     sub_dirs = [] if sub_dirs is None else sub_dirs
111     filenames = []
112     nested_files = []
113     filenames = list_filenames_in_template_dir(
114         metafunc, extensions, template_type, sub_dirs
115     )
116     if exclude_nested:
117         nested_files = get_nested_files(filenames)
118     return list(set(filenames) - set(nested_files))
119
120
121 def get_filenames_list(
122     metafunc, extensions=None, exclude_nested=False, template_type=""
123 ):
124     """
125     returns the filename fixtures for the template dir, either as by how its
126     passed in on CLI or, during --self-test, the directory whos name
127     matches the current tests module name
128     """
129     extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
130     if metafunc.config.getoption("self_test"):
131         filenames_list = list_template_dir(
132             metafunc, extensions, exclude_nested, template_type, ["pass"]
133         )
134         filenames_list += [
135             pytest.mark.xfail(f, strict=True)
136             for f in list_template_dir(
137                 metafunc, extensions, exclude_nested, template_type, ["fail"]
138             )
139         ]
140     else:
141         filenames_list = list_template_dir(
142             metafunc, extensions, exclude_nested, template_type
143         )
144
145     return filenames_list
146
147
148 def get_filenames_lists(
149     metafunc, extensions=None, exclude_nested=False, template_type=""
150 ):
151     """
152     returns the list of files in the template dir, either as by how its
153     passed in on CLI or, during --self-test, the directory whos name
154     matches the current tests module name
155     """
156     extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
157     filenames_lists = []
158     if metafunc.config.getoption("self_test"):
159         filenames_lists.append(
160             list_template_dir(
161                 metafunc, extensions, exclude_nested, template_type, ["pass"]
162             )
163         )
164         filenames_lists.append(
165             pytest.mark.xfail(
166                 list_template_dir(
167                     metafunc, extensions, exclude_nested, template_type, ["fail"]
168                 ),
169                 strict=True,
170             )
171         )
172     else:
173         filenames_lists.append(
174             list_template_dir(metafunc, extensions, exclude_nested, template_type)
175         )
176     return filenames_lists
177
178
179 def get_parsed_yaml_files(
180     metafunc, extensions, exclude_nested=True, template_type="", sections=None
181 ):
182     """
183     returns the list of parsed yaml files in the specified template dir,
184     either as by how its passed in on CLI or, during --self-test, the
185     directory whos name matches the current tests module name
186     """
187     sections = [] if sections is None else sections
188     extensions = [".yaml", ".yml"]
189
190     if metafunc.config.getoption("self_test"):
191         yaml_files = list_template_dir(
192             metafunc, extensions, exclude_nested, template_type, ["pass"]
193         )
194         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
195
196         yaml_files = list_template_dir(
197             metafunc, extensions, exclude_nested, template_type, ["fail"]
198         )
199         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
200         parsed_yml_list += [
201             pytest.mark.xfail(parsed_yml, strict=True) for parsed_yml in parsed_yml_list
202         ]
203     else:
204         yaml_files = list_template_dir(metafunc, extensions)
205         parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
206     return parsed_yml_list
207
208
209 def parametrize_filenames(metafunc):
210     """
211     This param runs tests all files in the template dir
212     """
213     filenames = get_filenames_lists(metafunc)
214     metafunc.parametrize("filenames", filenames)
215
216
217 def parametrize_filename(metafunc):
218     """
219     This param runs tests once for every file in the template dir
220     """
221     filenames = get_filenames_list(metafunc)
222     metafunc.parametrize("filename", filenames)
223
224
225 def parametrize_yaml_files(metafunc):
226     """
227     This param runs tests for the yaml files in the template dir
228     """
229     yaml_files = get_filenames_lists(metafunc, [".yaml", ".yml"], False)
230     metafunc.parametrize("yaml_files", yaml_files)
231
232
233 def parametrize_yaml_file(metafunc):
234     """
235     This param runs tests for every yaml file in the template dir
236     """
237     yaml_files = get_filenames_list(metafunc, [".yaml", ".yml"], False)
238     metafunc.parametrize("yaml_file", yaml_files)
239
240
241 def parametrize_templates(metafunc):
242     """
243     This param runs tests for the template in the template dir
244     """
245     templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True)
246     metafunc.parametrize("templates", templates)
247
248
249 def parametrize_template(metafunc):
250     """
251     This param runs tests for every template in the template dir
252     """
253     templates = get_filenames_list(metafunc, [".yaml", ".yml"], True)
254     metafunc.parametrize("template", templates)
255
256
257 def parametrize_parsed_yaml_file(metafunc):
258     """
259     This param runs tests for a parsed version of each yaml file
260     in the template dir
261     """
262     parsed_yaml_files = get_parsed_yaml_files(metafunc, [".yaml", ".yml"], False)
263     metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
264
265
266 def parametrize_heat_templates(metafunc):
267     """
268     This param runs tests for all heat templates in the template dir
269     """
270     heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
271     metafunc.parametrize("heat_templates", heat_templates)
272
273
274 def parametrize_heat_template(metafunc):
275     """
276     This param runs tests for every heat template in the template dir
277     """
278     heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
279     metafunc.parametrize("heat_template", heat_templates)
280
281
282 def parametrize_volume_templates(metafunc):
283     """
284     This param runs tests for all volume templates in the template dir
285     """
286     volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
287     metafunc.parametrize("volume_templates", volume_templates)
288
289
290 def parametrize_volume_template(metafunc):
291     """
292
293     This param runs tests for every volume template in the template dir
294     """
295     volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
296     metafunc.parametrize("volume_template", volume_templates)
297
298
299 def parametrize_environment_files(metafunc):
300     """
301     This param runs tests for all environment files in the template dir
302     """
303     env_files = get_filenames_lists(metafunc, [".env"])
304     metafunc.parametrize("env_files", env_files)
305
306
307 def parametrize_environment_file(metafunc):
308     """
309     This param runs tests for every environment file in the template dir
310     """
311     env_files = get_filenames_list(metafunc, [".env"])
312     metafunc.parametrize("env_file", env_files)
313
314
315 def parametrize_parsed_environment_file(metafunc):
316     """
317     This param runs tests for every parsed environment file
318     in the template dir
319     """
320     parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
321     metafunc.parametrize("parsed_env_file", parsed_env_files)
322
323
324 def parametrize_template_dir(metafunc):
325     """
326     This param passes a  the template_dir as passed in on CLI
327     or, during --self-test, passes in the sub directories of
328     template_dir/pass/ and template_dir/fail
329     template_dir = get_template_dir(metafunc)
330     """
331     template_dir = get_template_dir(metafunc)
332
333     if metafunc.config.getoption("self_test"):
334         dirs = [
335             path.join(template_dir, s, t)
336             for s in ["pass"]
337             for t in listdir(path.join(template_dir, s))
338             if path.isdir(path.join(template_dir, s, t))
339         ]
340
341         dirs += [
342             pytest.mark.xfail(path.join(template_dir, s, t))
343             for s in ["fail"]
344             for t in listdir(path.join(template_dir, s))
345             if path.isdir(path.join(template_dir, s, t))
346         ]
347     else:
348         dirs = [template_dir]
349
350     metafunc.parametrize("template_dir", dirs)
351
352
353 def parametrize_environment_pair(metafunc, template_type=""):
354     """
355     Define a list of pairs of parsed yaml from the heat templates and
356     environment files
357     """
358     pairs = []
359     if metafunc.config.getoption("self_test"):
360         sub_dirs = ["pass", "fail"]
361         env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
362         yaml_files = list_template_dir(
363             metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
364         )
365     else:
366         env_files = list_template_dir(metafunc, [".env"], True, template_type)
367         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
368
369     for filename in env_files:
370         basename = path.splitext(filename)[0]
371         if basename + ".yml" in yaml_files:
372             yfilename = basename + ".yml"
373         else:
374             yfilename = basename + ".yaml"
375
376         try:
377             with open(filename) as fh:
378                 eyml = yaml.load(fh)
379             with open(yfilename) as fh:
380                 yyml = yaml.load(fh)
381
382             if "fail" in filename:
383                 pairs.append(
384                     pytest.mark.xfail(
385                         {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
386                     )
387                 )
388             else:
389                 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
390
391         except yaml.YAMLError as e:
392             print(e)  # pylint: disable=superfluous-parens
393
394     metafunc.parametrize("environment_pair", pairs)
395
396
397 def parametrize_heat_volume_pair(metafunc):
398     """
399     Define a list of pairs of parsed yaml from the a heat and volume
400     template
401     """
402     pairs = []
403     if metafunc.config.getoption("self_test"):
404         sub_dirs = ["pass", "fail"]
405         volume_files = list_template_dir(
406             metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
407         )
408         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
409     else:
410         volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
411         yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
412
413     pattern = re.compile(r"\_volume$")
414     for vfilename in volume_files:
415         basename = pattern.sub("", path.splitext(vfilename)[0])
416         if basename + ".yml" in yaml_files:
417             yfilename = basename + ".yml"
418         else:
419             yfilename = basename + ".yaml"
420
421         try:
422             with open(vfilename) as fh:
423                 vyml = yaml.load(fh)
424             with open(yfilename) as fh:
425                 yyml = yaml.load(fh)
426
427             if "fail" in vfilename:
428                 pairs.append(
429                     pytest.mark.xfail(
430                         {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
431                     )
432                 )
433             else:
434                 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
435
436         except yaml.YAMLError as e:
437             print(e)  # pylint: disable=superfluous-parens
438
439     metafunc.parametrize("heat_volume_pair", pairs)