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
46 from tests import cached_yaml as yaml
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(metafunc,
96 returns the filenames in a template_dir, either as its passed in
97 on CLI or, during --self-test, the directory whos name matches
98 the current tests module name
100 sub_dirs = [] if sub_dirs is None else sub_dirs
101 template_dir = get_template_dir(metafunc)
103 if metafunc.config.getoption('self_test'):
104 filenames = [path.join(template_dir, s, f)
106 for f in listdir(path.join(template_dir, s))
107 if path.isfile(path.join(template_dir, s, f))
108 and path.splitext(f)[-1] in extensions
109 and check_basename_ending(template_type,
110 path.splitext(f)[0])]
112 filenames = [path.join(template_dir, f)
113 for f in listdir(template_dir)
114 if path.isfile(path.join(template_dir, f))
115 and path.splitext(f)[-1] in extensions
116 and check_basename_ending(template_type,
117 path.splitext(f)[0])]
121 def list_template_dir(metafunc,
127 returns the filenames excluding the nested files for a template_dir,
128 either as its passed in on CLI or, during --self-test, the
129 directory whos name matches the current tests module name
131 sub_dirs = [] if sub_dirs is None else sub_dirs
134 filenames = list_filenames_in_template_dir(metafunc,
139 nested_files = get_nested_files(filenames)
140 return list(set(filenames) - set(nested_files))
143 def get_filenames_list(metafunc,
145 exclude_nested=False,
148 returns the filename fixtures for the template dir, either as by how its
149 passed in on CLI or, during --self-test, the directory whos name
150 matches the current tests module name
152 extensions = [".yaml",
154 ".env"] if extensions is None else extensions
155 if metafunc.config.getoption('self_test'):
156 filenames_list = list_template_dir(metafunc,
161 filenames_list += [pytest.mark.xfail(f, strict=True)
162 for f in list_template_dir(metafunc,
168 filenames_list = list_template_dir(metafunc,
173 return filenames_list
176 def get_filenames_lists(metafunc,
178 exclude_nested=False,
181 returns the list of files in the template dir, either as by how its
182 passed in on CLI or, during --self-test, the directory whos name
183 matches the current tests module name
185 extensions = [".yaml",
187 ".env"] if extensions is None else extensions
189 if metafunc.config.getoption('self_test'):
190 filenames_lists.append(list_template_dir(metafunc,
195 filenames_lists.append(pytest.mark.xfail(
196 list_template_dir(metafunc,
200 ['fail']), strict=True))
202 filenames_lists.append(list_template_dir(metafunc,
206 return filenames_lists
209 def get_parsed_yaml_files(metafunc,
215 returns the list of parsed yaml files in the specified template dir,
216 either as by how its passed in on CLI or, during --self-test, the
217 directory whos name matches the current tests module name
219 sections = [] if sections is None else sections
220 extensions = [".yaml", ".yml"]
222 if metafunc.config.getoption('self_test'):
223 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
224 template_type, ['pass'])
225 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
228 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
229 template_type, ['fail'])
230 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
232 parsed_yml_list += [pytest.mark.xfail(parsed_yml, strict=True)
233 for parsed_yml in parsed_yml_list]
235 yaml_files = list_template_dir(metafunc, extensions)
236 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
238 return parsed_yml_list
241 def parametrize_filenames(metafunc):
243 This param runs tests all files in the template dir
245 filenames = get_filenames_lists(metafunc)
246 metafunc.parametrize('filenames', filenames)
249 def parametrize_filename(metafunc):
251 This param runs tests once for every file in the template dir
253 filenames = get_filenames_list(metafunc)
254 metafunc.parametrize('filename', filenames)
257 def parametrize_yaml_files(metafunc):
259 This param runs tests for the yaml files in the template dir
261 yaml_files = get_filenames_lists(metafunc, ['.yaml', '.yml'], False)
262 metafunc.parametrize("yaml_files", yaml_files)
265 def parametrize_yaml_file(metafunc):
267 This param runs tests for every yaml file in the template dir
269 yaml_files = get_filenames_list(metafunc, ['.yaml', '.yml'], False)
270 metafunc.parametrize('yaml_file', yaml_files)
273 def parametrize_templates(metafunc):
275 This param runs tests for the template in the template dir
277 templates = get_filenames_lists(metafunc, ['.yaml', '.yml'], True)
278 metafunc.parametrize("templates", templates)
281 def parametrize_template(metafunc):
283 This param runs tests for every template in the template dir
285 templates = get_filenames_list(metafunc, ['.yaml', '.yml'], True)
286 metafunc.parametrize('template', templates)
289 def parametrize_parsed_yaml_file(metafunc):
291 This param runs tests for a parsed version of each yaml file
294 parsed_yaml_files = get_parsed_yaml_files(metafunc, ['.yaml', '.yml'],
296 metafunc.parametrize('parsed_yaml_file', parsed_yaml_files)
299 def parametrize_heat_templates(metafunc):
301 This param runs tests for all heat templates in the template dir
303 heat_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
305 metafunc.parametrize('heat_templates', heat_templates)
308 def parametrize_heat_template(metafunc):
310 This param runs tests for every heat template in the template dir
312 heat_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
314 metafunc.parametrize('heat_template', heat_templates)
317 def parametrize_volume_templates(metafunc):
319 This param runs tests for all volume templates in the template dir
321 volume_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
323 metafunc.parametrize('volume_templates', volume_templates)
326 def parametrize_volume_template(metafunc):
329 This param runs tests for every volume template in the template dir
331 volume_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
333 metafunc.parametrize('volume_template', volume_templates)
336 def parametrize_environment_files(metafunc):
338 This param runs tests for all environment files in the template dir
340 env_files = get_filenames_lists(metafunc, ['.env'])
341 metafunc.parametrize('env_files', env_files)
344 def parametrize_environment_file(metafunc):
346 This param runs tests for every environment file in the template dir
348 env_files = get_filenames_list(metafunc, ['.env'])
349 metafunc.parametrize('env_file', env_files)
352 def parametrize_parsed_environment_file(metafunc):
354 This param runs tests for every parsed environment file
357 parsed_env_files = get_parsed_yaml_files(metafunc, ['.env'])
358 metafunc.parametrize('parsed_env_file', parsed_env_files)
361 def parametrize_template_dir(metafunc):
363 This param passes a the template_dir as passed in on CLI
364 or, during --self-test, passes in the sub directories of
365 template_dir/pass/ and template_dir/fail
366 template_dir = get_template_dir(metafunc)
368 template_dir = get_template_dir(metafunc)
370 if metafunc.config.getoption('self_test'):
371 dirs = [path.join(template_dir, s, t)
373 for t in listdir(path.join(template_dir, s))
374 if path.isdir(path.join(template_dir, s, t))]
376 dirs += [pytest.mark.xfail(path.join(template_dir, s, t))
378 for t in listdir(path.join(template_dir, s))
379 if path.isdir(path.join(template_dir, s, t))]
381 dirs = [template_dir]
383 metafunc.parametrize('template_dir', dirs)
386 def parametrize_environment_pair(metafunc, template_type=''):
388 Define a list of pairs of parsed yaml from the heat templates and
392 if metafunc.config.getoption('self_test'):
393 sub_dirs = ['pass', 'fail']
394 env_files = list_template_dir(metafunc, ['.env'], True,
395 template_type, sub_dirs)
396 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True,
397 template_type, sub_dirs)
399 env_files = list_template_dir(metafunc, ['.env'], True,
401 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
404 for filename in env_files:
405 basename = path.splitext(filename)[0]
406 if basename + '.yml' in yaml_files:
407 yfilename = basename + '.yml'
409 yfilename = basename + '.yaml'
412 with open(filename) as fh:
414 with open(yfilename) as fh:
417 if 'fail' in filename:
418 pairs.append(pytest.mark.xfail({"name": basename,
423 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
425 except yaml.YAMLError as e:
426 print(e) # pylint: disable=superfluous-parens
428 metafunc.parametrize('environment_pair', pairs)
431 def parametrize_heat_volume_pair(metafunc):
433 Define a list of pairs of parsed yaml from the a heat and volume
437 if metafunc.config.getoption('self_test'):
438 sub_dirs = ['pass', 'fail']
439 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
440 True, 'volume', sub_dirs)
441 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
444 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
446 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True)
448 pattern = re.compile(r'\_volume$')
449 for vfilename in volume_files:
450 basename = pattern.sub('', path.splitext(vfilename)[0])
451 if basename + '.yml' in yaml_files:
452 yfilename = basename + '.yml'
454 yfilename = basename + '.yaml'
457 with open(vfilename) as fh:
459 with open(yfilename) as fh:
462 if 'fail' in vfilename:
463 pairs.append(pytest.mark.xfail({"name": basename,
468 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
470 except yaml.YAMLError as e:
471 print(e) # pylint: disable=superfluous-parens
473 metafunc.parametrize('heat_volume_pair', pairs)