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.
40 from os import path, listdir
44 from .helpers import get_parsed_yml_for_yaml_files, check_basename_ending
45 from .utils.nested_files import get_list_of_nested_files
49 # pylint: disable=invalid-name
52 def get_template_dir(metafunc):
54 returns template_dir, either as its passed in on CLI
55 or, during --self-test, the directory whos name matches
56 the current tests module name
58 if metafunc.config.getoption('template_dir') is None:
60 path.dirname(path.realpath(__file__)),
62 metafunc.function.__module__.split('.')[-1])
64 return metafunc.config.getoption('template_dir')[0]
67 def get_nested_files(filenames):
69 returns all the nested files for a set of filenames
72 for filename in filenames:
74 with open(filename) as fh:
76 if "resources" not in yml:
78 nested_files.extend(get_list_of_nested_files(
80 path.dirname(filename)))
81 except yaml.YAMLError as e:
82 print(e) # pylint: disable=superfluous-parens
87 def list_filenames_in_template_dir(
93 returns the filenames in a template_dir, either as its passed in
94 on CLI or, during --self-test, the directory whos name matches
95 the current tests module name
97 sub_dirs = [] if sub_dirs is None else sub_dirs
98 template_dir = get_template_dir(metafunc)
100 if metafunc.config.getoption('self_test'):
101 filenames = [path.join(template_dir, s, f)
103 for f in listdir(path.join(template_dir, s))
104 if path.isfile(path.join(template_dir, s, f))
105 and path.splitext(f)[-1] in extensions
106 and check_basename_ending(
108 path.splitext(f)[0])]
110 filenames = [path.join(template_dir, f)
111 for f in listdir(template_dir)
112 if path.isfile(path.join(template_dir, f))
113 and path.splitext(f)[-1] in extensions
114 and check_basename_ending(
116 path.splitext(f)[0])]
120 def list_template_dir(
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(
140 nested_files = get_nested_files(filenames)
141 return list(set(filenames) - set(nested_files))
144 def get_filenames_list(
147 exclude_nested=False,
150 returns the filename fixtures for the template dir, either as by how its
151 passed in on CLI or, during --self-test, the directory whos name
152 matches the current tests module name
158 ] if extensions is None else extensions
159 if metafunc.config.getoption('self_test'):
160 filenames_list = list_template_dir(metafunc,
165 filenames_list += [pytest.mark.xfail(f, strict=True)
166 for f in list_template_dir(metafunc,
172 filenames_list = list_template_dir(metafunc,
177 return filenames_list
180 def get_filenames_lists(
183 exclude_nested=False,
186 returns the list of files in the template dir, either as by how its
187 passed in on CLI or, during --self-test, the directory whos name
188 matches the current tests module name
194 ] if extensions is None else extensions
196 if metafunc.config.getoption('self_test'):
197 filenames_lists.append(list_template_dir(metafunc,
202 filenames_lists.append(pytest.mark.xfail(
203 list_template_dir(metafunc,
210 filenames_lists.append(list_template_dir(metafunc,
214 return filenames_lists
217 def get_parsed_yaml_files(
224 returns the list of parsed yaml files in the specified template dir,
225 either as by how its passed in on CLI or, during --self-test, the
226 directory whos name matches the current tests module name
228 sections = [] if sections is None else sections
229 extensions = [".yaml", ".yml"]
231 if metafunc.config.getoption('self_test'):
232 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
233 template_type, ['pass'])
234 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
237 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
238 template_type, ['fail'])
239 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
241 parsed_yml_list += [pytest.mark.xfail(parsed_yml, strict=True)
242 for parsed_yml in parsed_yml_list]
244 yaml_files = list_template_dir(metafunc, extensions)
245 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
247 return parsed_yml_list
250 def parametrize_filenames(metafunc):
252 This param runs tests all files in the template dir
254 filenames = get_filenames_lists(metafunc)
255 metafunc.parametrize('filenames', filenames)
258 def parametrize_filename(metafunc):
260 This param runs tests once for every file in the template dir
262 filenames = get_filenames_list(metafunc)
263 metafunc.parametrize('filename', filenames)
266 def parametrize_yaml_files(metafunc):
268 This param runs tests for the yaml files in the template dir
270 yaml_files = get_filenames_lists(metafunc, ['.yaml', '.yml'], False)
271 metafunc.parametrize("yaml_files", yaml_files)
274 def parametrize_yaml_file(metafunc):
276 This param runs tests for every yaml file in the template dir
278 yaml_files = get_filenames_list(metafunc, ['.yaml', '.yml'], False)
279 metafunc.parametrize('yaml_file', yaml_files)
282 def parametrize_templates(metafunc):
284 This param runs tests for the template in the template dir
286 templates = get_filenames_lists(metafunc, ['.yaml', '.yml'], True)
287 metafunc.parametrize("templates", templates)
290 def parametrize_template(metafunc):
292 This param runs tests for every template in the template dir
294 templates = get_filenames_list(metafunc, ['.yaml', '.yml'], True)
295 metafunc.parametrize('template', templates)
298 def parametrize_parsed_yaml_file(metafunc):
300 This param runs tests for a parsed version of each yaml file
303 parsed_yaml_files = get_parsed_yaml_files(metafunc, ['.yaml', '.yml'],
305 metafunc.parametrize('parsed_yaml_file', parsed_yaml_files)
308 def parametrize_heat_templates(metafunc):
310 This param runs tests for all heat templates in the template dir
312 heat_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
314 metafunc.parametrize('heat_templates', heat_templates)
317 def parametrize_heat_template(metafunc):
319 This param runs tests for every heat template in the template dir
321 heat_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
323 metafunc.parametrize('heat_template', heat_templates)
326 def parametrize_volume_templates(metafunc):
328 This param runs tests for all volume templates in the template dir
330 volume_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
332 metafunc.parametrize('volume_templates', volume_templates)
335 def parametrize_volume_template(metafunc):
338 This param runs tests for every volume template in the template dir
340 volume_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
342 metafunc.parametrize('volume_template', volume_templates)
345 def parametrize_environment_files(metafunc):
347 This param runs tests for all environment files in the template dir
349 env_files = get_filenames_lists(metafunc, ['.env'])
350 metafunc.parametrize('env_files', env_files)
353 def parametrize_environment_file(metafunc):
355 This param runs tests for every environment file in the template dir
357 env_files = get_filenames_list(metafunc, ['.env'])
358 metafunc.parametrize('env_file', env_files)
361 def parametrize_parsed_environment_file(metafunc):
363 This param runs tests for every parsed environment file
366 parsed_env_files = get_parsed_yaml_files(metafunc, ['.env'])
367 metafunc.parametrize('parsed_env_file', parsed_env_files)
370 def parametrize_template_dir(metafunc):
372 This param passes a the template_dir as passed in on CLI
373 or, during --self-test, passes in the sub directories of
374 template_dir/pass/ and template_dir/fail
375 template_dir = get_template_dir(metafunc)
377 template_dir = get_template_dir(metafunc)
379 if metafunc.config.getoption('self_test'):
380 dirs = [path.join(template_dir, s, t)
382 for t in listdir(path.join(template_dir, s))
383 if path.isdir(path.join(template_dir, s, t))]
385 dirs += [pytest.mark.xfail(path.join(template_dir, s, t))
387 for t in listdir(path.join(template_dir, s))
388 if path.isdir(path.join(template_dir, s, t))]
390 dirs = [template_dir]
392 metafunc.parametrize('template_dir', dirs)
395 def parametrize_environment_pair(metafunc, template_type=''):
397 Define a list of pairs of parsed yaml from the heat templates and
401 if metafunc.config.getoption('self_test'):
402 sub_dirs = ['pass', 'fail']
403 env_files = list_template_dir(metafunc, ['.env'], True,
404 template_type, sub_dirs)
405 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True,
406 template_type, sub_dirs)
408 env_files = list_template_dir(metafunc, ['.env'], True,
410 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
413 for filename in env_files:
414 basename = path.splitext(filename)[0]
415 if basename + '.yml' in yaml_files:
416 yfilename = basename + '.yml'
418 yfilename = basename + '.yaml'
421 with open(filename) as fh:
423 with open(yfilename) as fh:
426 if 'fail' in filename:
427 pairs.append(pytest.mark.xfail({"name": basename,
432 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
434 except yaml.YAMLError as e:
435 print(e) # pylint: disable=superfluous-parens
437 metafunc.parametrize('environment_pair', pairs)
440 def parametrize_heat_volume_pair(metafunc):
442 Define a list of pairs of parsed yaml from the a heat and volume
446 if metafunc.config.getoption('self_test'):
447 sub_dirs = ['pass', 'fail']
448 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
449 True, 'volume', sub_dirs)
450 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
453 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
455 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True)
457 pattern = re.compile(r'\_volume$')
458 for vfilename in volume_files:
459 basename = pattern.sub('', path.splitext(vfilename)[0])
460 if basename + '.yml' in yaml_files:
461 yfilename = basename + '.yml'
463 yfilename = basename + '.yaml'
466 with open(vfilename) as fh:
468 with open(yfilename) as fh:
471 if 'fail' in vfilename:
472 pairs.append(pytest.mark.xfail({"name": basename,
477 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
479 except yaml.YAMLError as e:
480 print(e) # pylint: disable=superfluous-parens
482 metafunc.parametrize('heat_volume_pair', pairs)