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.
41 from os import path, listdir
45 from .helpers import get_parsed_yml_for_yaml_files, check_basename_ending
46 from .utils.nested_files import get_list_of_nested_files
49 def get_template_dir(metafunc):
51 returns template_dir, either as its passed in on CLI
52 or, during --self-test, the directory whos name matches
53 the current tests module name
55 if metafunc.config.getoption('template_dir') is None:
57 path.dirname(path.realpath(__file__)),
59 metafunc.function.__module__.split('.')[-1])
61 return metafunc.config.getoption('template_dir')[0]
64 def get_nested_files(filenames):
66 returns all the nested files for a set of filenames
69 for filename in filenames:
71 with open(filename) as fh:
73 if "resources" not in yml:
75 nested_files.extend(get_list_of_nested_files(
76 yml["resources"], path.dirname(filename)))
77 except Exception as e:
83 def list_filenames_in_template_dir(metafunc, extensions, template_type='',
86 returns the filenames in a template_dir, either as its passed in
87 on CLI or, during --self-test, the directory whos name matches
88 the current tests module name
90 template_dir = get_template_dir(metafunc)
94 if metafunc.config.getoption('self_test'):
95 filenames = [path.join(template_dir, s, f)
97 for f in listdir(path.join(template_dir, s))
98 if path.isfile(path.join(template_dir, s, f)) and
99 path.splitext(f)[-1] in extensions and
100 check_basename_ending(template_type,
101 path.splitext(f)[0])]
103 filenames = [path.join(template_dir, f)
104 for f in listdir(template_dir)
105 if path.isfile(path.join(template_dir, f)) and
106 path.splitext(f)[-1] in extensions and
107 check_basename_ending(template_type,
108 path.splitext(f)[0])]
110 except Exception as e:
116 def list_template_dir(metafunc, extensions, exclude_nested=True,
117 template_type='', sub_dirs=[]):
119 returns the filenames excluding the nested files for a template_dir,
120 either as its passed in on CLI or, during --self-test, the
121 directory whos name matches the current tests module name
127 filenames = list_filenames_in_template_dir(metafunc, extensions,
128 template_type, sub_dirs)
130 nested_files = get_nested_files(filenames)
131 except Exception as e:
134 return list(set(filenames) - set(nested_files))
137 def get_filenames_list(metafunc, extensions=[".yaml", ".yml", ".env"],
138 exclude_nested=False, template_type=''):
140 returns the filename fixtures for the template dir, either as by how its
141 passed in on CLI or, during --self-test, the directory whos name
142 matches the current tests module name
144 if metafunc.config.getoption('self_test'):
145 filenames_list = list_template_dir(metafunc,
150 filenames_list += [pytest.mark.xfail(f, strict=True)
151 for f in list_template_dir(metafunc,
157 filenames_list = list_template_dir(metafunc,
162 return filenames_list
165 def get_filenames_lists(metafunc, extensions=[".yaml", ".yml", ".env"],
166 exclude_nested=False, template_type=''):
168 returns the list of files in the template dir, either as by how its
169 passed in on CLI or, during --self-test, the directory whos name
170 matches the current tests module name
173 if metafunc.config.getoption('self_test'):
174 filenames_lists.append(list_template_dir(metafunc,
179 filenames_lists.append(pytest.mark.xfail(
180 list_template_dir(metafunc,
187 filenames_lists.append(list_template_dir(metafunc,
192 return filenames_lists
195 def get_parsed_yaml_files(metafunc, extensions, exclude_nested=True,
196 template_type='', sections=[]):
198 returns the list of parsed yaml files in the specified template dir,
199 either as by how its passed in on CLI or, during --self-test, the
200 directory whos name matches the current tests module name
202 extensions = [".yaml", ".yml"]
204 if metafunc.config.getoption('self_test'):
205 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
206 template_type, ['pass'])
207 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
210 yaml_files = list_template_dir(metafunc, extensions, exclude_nested,
211 template_type, ['fail'])
212 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
214 parsed_yml_list += [pytest.mark.xfail(parsed_yml, strict=True)
215 for parsed_yml in parsed_yml_list]
217 yaml_files = list_template_dir(metafunc, extensions)
218 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files,
221 return parsed_yml_list
224 def parametrize_filenames(metafunc):
226 This param runs tests all files in the template dir
228 filenames = get_filenames_lists(metafunc)
229 metafunc.parametrize('filenames', filenames)
232 def parametrize_filename(metafunc):
234 This param runs tests once for every file in the template dir
236 filenames = get_filenames_list(metafunc)
237 metafunc.parametrize('filename', filenames)
240 def parametrize_yaml_files(metafunc):
242 This param runs tests for the yaml files in the template dir
244 yaml_files = get_filenames_lists(metafunc, ['.yaml', '.yml'], False)
245 metafunc.parametrize("yaml_files", yaml_files)
248 def parametrize_yaml_file(metafunc):
250 This param runs tests for every yaml file in the template dir
252 yaml_files = get_filenames_list(metafunc, ['.yaml', '.yml'], False)
253 metafunc.parametrize('yaml_file', yaml_files)
256 def parametrize_templates(metafunc):
258 This param runs tests for the template in the template dir
260 templates = get_filenames_lists(metafunc, ['.yaml', '.yml'], True)
261 metafunc.parametrize("templates", templates)
264 def parametrize_template(metafunc):
266 This param runs tests for every template in the template dir
268 templates = get_filenames_list(metafunc, ['.yaml', '.yml'], True)
269 metafunc.parametrize('template', templates)
272 def parametrize_parsed_yaml_file(metafunc):
274 This param runs tests for a parsed version of each yaml file
277 parsed_yaml_files = get_parsed_yaml_files(metafunc, ['.yaml', '.yml'],
279 metafunc.parametrize('parsed_yaml_file', parsed_yaml_files)
282 def parametrize_heat_templates(metafunc):
284 This param runs tests for all heat templates in the template dir
286 heat_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
288 metafunc.parametrize('heat_templates', heat_templates)
291 def parametrize_heat_template(metafunc):
293 This param runs tests for every heat template in the template dir
295 heat_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
297 metafunc.parametrize('heat_template', heat_templates)
300 def parametrize_volume_templates(metafunc):
302 This param runs tests for all volume templates in the template dir
304 volume_templates = get_filenames_lists(metafunc, ['.yaml', '.yml'],
306 metafunc.parametrize('volume_templates', volume_templates)
309 def parametrize_volume_template(metafunc):
312 This param runs tests for every volume template in the template dir
314 volume_templates = get_filenames_list(metafunc, ['.yaml', '.yml'],
316 metafunc.parametrize('volume_template', volume_templates)
319 def parametrize_environment_files(metafunc):
321 This param runs tests for all environment files in the template dir
323 env_files = get_filenames_lists(metafunc, ['.env'])
324 metafunc.parametrize('env_files', env_files)
327 def parametrize_environment_file(metafunc):
329 This param runs tests for every environment file in the template dir
331 env_files = get_filenames_list(metafunc, ['.env'])
332 metafunc.parametrize('env_file', env_files)
335 def parametrize_parsed_environment_file(metafunc):
337 This param runs tests for every parsed environment file
340 parsed_env_files = get_parsed_yaml_files(metafunc, ['.env'])
341 metafunc.parametrize('parsed_env_file', parsed_env_files)
344 def parametrize_template_dir(metafunc):
346 This param passes a the template_dir as passed in on CLI
347 or, during --self-test, passes in the sub directories of
348 template_dir/pass/ and template_dir/fail
349 template_dir = get_template_dir(metafunc)
351 template_dir = get_template_dir(metafunc)
353 if metafunc.config.getoption('self_test'):
354 dirs = [path.join(template_dir, s, t)
356 for t in listdir(path.join(template_dir, s))
357 if path.isdir(path.join(template_dir, s, t))]
359 dirs += [pytest.mark.xfail(path.join(template_dir, s, t))
361 for t in listdir(path.join(template_dir, s))
362 if path.isdir(path.join(template_dir, s, t))]
364 dirs = [template_dir]
366 metafunc.parametrize('template_dir', dirs)
369 def parametrize_environment_pair(metafunc, template_type=''):
371 Define a list of pairs of parsed yaml from the heat templates and
375 if metafunc.config.getoption('self_test'):
376 sub_dirs = ['pass', 'fail']
377 env_files = list_template_dir(metafunc, ['.env'], True,
378 template_type, sub_dirs)
379 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True,
380 template_type, sub_dirs)
382 env_files = list_template_dir(metafunc, ['.env'], True,
384 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
387 for filename in env_files:
388 basename = path.splitext(filename)[0]
389 if basename + '.yml' in yaml_files:
390 yfilename = basename + '.yml'
392 yfilename = basename + '.yaml'
395 with open(filename) as fh:
397 with open(yfilename) as fh:
400 if 'fail' in filename:
401 pairs.append(pytest.mark.xfail({"name": basename,
406 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
408 except Exception as e:
411 metafunc.parametrize('environment_pair', pairs)
414 def parametrize_heat_volume_pair(metafunc):
416 Define a list of pairs of parsed yaml from the a heat and volume
420 if metafunc.config.getoption('self_test'):
421 sub_dirs = ['pass', 'fail']
422 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
423 True, 'volume', sub_dirs)
424 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'],
427 volume_files = list_template_dir(metafunc, ['.yaml', '.yml'],
429 yaml_files = list_template_dir(metafunc, ['.yaml', '.yml'], True)
431 pattern = re.compile(r'\_volume$')
432 for vfilename in volume_files:
433 basename = pattern.sub('', path.splitext(vfilename)[0])
434 if basename + '.yml' in yaml_files:
435 yfilename = basename + '.yml'
437 yfilename = basename + '.yaml'
440 with open(vfilename) as fh:
442 with open(yfilename) as fh:
445 if 'fail' in vfilename:
446 pairs.append(pytest.mark.xfail({"name": basename,
451 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
453 except Exception as e:
456 metafunc.parametrize('heat_volume_pair', pairs)