2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
44 from os import path, listdir
48 from .helpers import get_parsed_yml_for_yaml_files, check_basename_ending
49 from .utils.nested_files import get_list_of_nested_files
53 # pylint: disable=invalid-name
56 def get_template_dir(metafunc):
58 returns template_dir, either as its passed in on CLI
59 or, during --self-test, the directory whos name matches
60 the current tests module name
62 if metafunc.config.getoption('template_dir') is None:
64 path.dirname(path.realpath(__file__)),
66 metafunc.function.__module__.split('.')[-1])
68 return metafunc.config.getoption('template_dir')[0]
71 def get_nested_files(filenames):
73 returns all the nested files for a set of filenames
76 for filename in filenames:
78 with open(filename) as fh:
80 if "resources" not in yml:
82 nested_files.extend(get_list_of_nested_files(
84 path.dirname(filename)))
85 except yaml.YAMLError as e:
86 print(e) # pylint: disable=superfluous-parens
91 def list_filenames_in_template_dir(
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
101 sub_dirs = [] if sub_dirs is None else sub_dirs
102 template_dir = get_template_dir(metafunc)
104 if metafunc.config.getoption('self_test'):
105 filenames = [path.join(template_dir, s, f)
107 for f in listdir(path.join(template_dir, s))
108 if path.isfile(path.join(template_dir, s, f))
109 and path.splitext(f)[-1] in extensions
110 and check_basename_ending(
112 path.splitext(f)[0])]
114 filenames = [path.join(template_dir, f)
115 for f in listdir(template_dir)
116 if path.isfile(path.join(template_dir, f))
117 and path.splitext(f)[-1] in extensions
118 and check_basename_ending(
120 path.splitext(f)[0])]
124 def list_template_dir(
131 returns the filenames excluding the nested files for a template_dir,
132 either as its passed in on CLI or, during --self-test, the
133 directory whos name matches the current tests module name
135 sub_dirs = [] if sub_dirs is None else sub_dirs
138 filenames = list_filenames_in_template_dir(
144 nested_files = get_nested_files(filenames)
145 return list(set(filenames) - set(nested_files))
148 def get_filenames_list(
151 exclude_nested=False,
154 returns the filename fixtures for the template dir, either as by how its
155 passed in on CLI or, during --self-test, the directory whos name
156 matches the current tests module name
162 ] if extensions is None else extensions
163 if metafunc.config.getoption('self_test'):
164 filenames_list = list_template_dir(metafunc,
169 filenames_list += [pytest.mark.xfail(f, strict=True)
170 for f in list_template_dir(metafunc,
176 filenames_list = list_template_dir(metafunc,
181 return filenames_list
184 def get_filenames_lists(
187 exclude_nested=False,
190 returns the list of files in the template dir, either as by how its
191 passed in on CLI or, during --self-test, the directory whos name
192 matches the current tests module name
198 ] if extensions is None else extensions
200 if metafunc.config.getoption('self_test'):
201 filenames_lists.append(list_template_dir(metafunc,
206 filenames_lists.append(pytest.mark.xfail(
207 list_template_dir(metafunc,
214 filenames_lists.append(list_template_dir(metafunc,
218 return filenames_lists
221 def get_parsed_yaml_files(
228 returns the list of parsed yaml files in the specified template dir,
229 either as by how its passed in on CLI or, during --self-test, the
230 directory whos name matches the current tests module name
232 sections = [] if sections is None else sections
233 extensions = [".yaml", ".yml"]
235 if metafunc.config.getoption('self_test'):
236 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
237 template_type, ['pass'])
238 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
241 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
242 template_type, ['fail'])
243 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
245 parsed_yml_list += [pytest.mark.xfail(parsed_yml, strict=True)
246 for parsed_yml in parsed_yml_list]
248 yaml_files = list_template_dir(metafunc, extensions)
249 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
251 return parsed_yml_list
254 def parametrize_filenames(metafunc):
256 This param runs tests all files in the template dir
258 filenames = get_filenames_lists(metafunc)
259 metafunc.parametrize('filenames', filenames)
262 def parametrize_filename(metafunc):
264 This param runs tests once for every file in the template dir
266 filenames = get_filenames_list(metafunc)
267 metafunc.parametrize('filename', filenames)
270 def parametrize_yaml_files(metafunc):
272 This param runs tests for the yaml files in the template dir
274 yaml_files = get_filenames_lists(metafunc, ['.yaml', '.yml'], False)
275 metafunc.parametrize("yaml_files", yaml_files)
278 def parametrize_yaml_file(metafunc):
280 This param runs tests for every yaml file in the template dir
282 yaml_files = get_filenames_list(metafunc, ['.yaml', '.yml'], False)
283 metafunc.parametrize('yaml_file', yaml_files)
286 def parametrize_templates(metafunc):
288 This param runs tests for the template in the template dir
290 templates = get_filenames_lists(metafunc, ['.yaml', '.yml'], True)
291 metafunc.parametrize("templates", templates)
294 def parametrize_template(metafunc):
296 This param runs tests for every template in the template dir
298 templates = get_filenames_list(metafunc, ['.yaml', '.yml'], True)
299 metafunc.parametrize('template', templates)
302 def parametrize_parsed_yaml_file(metafunc):
304 This param runs tests for a parsed version of each yaml file
307 parsed_yaml_files = get_parsed_yaml_files(metafunc, ['.yaml', '.yml'],
309 metafunc.parametrize('parsed_yaml_file', parsed_yaml_files)
312 def parametrize_heat_templates(metafunc):
314 This param runs tests for all heat templates in the template dir
316 heat_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
318 metafunc.parametrize('heat_templates', heat_templates)
321 def parametrize_heat_template(metafunc):
323 This param runs tests for every heat template in the template dir
325 heat_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
327 metafunc.parametrize('heat_template', heat_templates)
330 def parametrize_volume_templates(metafunc):
332 This param runs tests for all volume templates in the template dir
334 volume_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
336 metafunc.parametrize('volume_templates', volume_templates)
339 def parametrize_volume_template(metafunc):
342 This param runs tests for every volume template in the template dir
344 volume_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
346 metafunc.parametrize('volume_template', volume_templates)
349 def parametrize_environment_files(metafunc):
351 This param runs tests for all environment files in the template dir
353 env_files = get_filenames_lists(metafunc, ['.env'])
354 metafunc.parametrize('env_files', env_files)
357 def parametrize_environment_file(metafunc):
359 This param runs tests for every environment file in the template dir
361 env_files = get_filenames_list(metafunc, ['.env'])
362 metafunc.parametrize('env_file', env_files)
365 def parametrize_parsed_environment_file(metafunc):
367 This param runs tests for every parsed environment file
370 parsed_env_files = get_parsed_yaml_files(metafunc, ['.env'])
371 metafunc.parametrize('parsed_env_file', parsed_env_files)
374 def parametrize_template_dir(metafunc):
376 This param passes a the template_dir as passed in on CLI
377 or, during --self-test, passes in the sub directories of
378 template_dir/pass/ and template_dir/fail
379 template_dir = get_template_dir(metafunc)
381 template_dir = get_template_dir(metafunc)
383 if metafunc.config.getoption('self_test'):
384 dirs = [path.join(template_dir, s, t)
386 for t in listdir(path.join(template_dir, s))
387 if path.isdir(path.join(template_dir, s, t))]
389 dirs += [pytest.mark.xfail(path.join(template_dir, s, t))
391 for t in listdir(path.join(template_dir, s))
392 if path.isdir(path.join(template_dir, s, t))]
394 dirs = [template_dir]
396 metafunc.parametrize('template_dir', dirs)
399 def parametrize_environment_pair(metafunc, template_type=''):
401 Define a list of pairs of parsed yaml from the heat templates and
405 if metafunc.config.getoption('self_test'):
406 sub_dirs = ['pass', 'fail']
407 env_files = list_template_dir(metafunc, ['.env'], True,
408 template_type, sub_dirs)
409 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True,
410 template_type, sub_dirs)
412 env_files = list_template_dir(metafunc, ['.env'], True,
414 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
417 for filename in env_files:
418 basename = path.splitext(filename)[0]
419 if basename + '.yml' in yaml_files:
420 yfilename = basename + '.yml'
422 yfilename = basename + '.yaml'
425 with open(filename) as fh:
427 with open(yfilename) as fh:
430 if 'fail' in filename:
431 pairs.append(pytest.mark.xfail({"name": basename,
436 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
438 except yaml.YAMLError as e:
439 print(e) # pylint: disable=superfluous-parens
441 metafunc.parametrize('environment_pair', pairs)
444 def parametrize_heat_volume_pair(metafunc):
446 Define a list of pairs of parsed yaml from the a heat and volume
450 if metafunc.config.getoption('self_test'):
451 sub_dirs = ['pass', 'fail']
452 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
453 True, 'volume', sub_dirs)
454 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
457 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
459 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True)
461 pattern = re.compile(r'\_volume$')
462 for vfilename in volume_files:
463 basename = pattern.sub('', path.splitext(vfilename)[0])
464 if basename + '.yml' in yaml_files:
465 yfilename = basename + '.yml'
467 yfilename = basename + '.yaml'
470 with open(vfilename) as fh:
472 with open(yfilename) as fh:
475 if 'fail' in vfilename:
476 pairs.append(pytest.mark.xfail({"name": basename,
481 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
483 except yaml.YAMLError as e:
484 print(e) # pylint: disable=superfluous-parens
486 metafunc.parametrize('heat_volume_pair', pairs)