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
54 # pylint: disable=invalid-name
57 def get_template_dir(metafunc):
59 returns template_dir, either as its passed in on CLI
60 or, during --self-test, the directory whos name matches
61 the current tests module name
63 if metafunc.config.getoption("template_dir") is None:
65 path.dirname(path.realpath(__file__)),
67 metafunc.function.__module__.split(".")[-1],
70 return metafunc.config.getoption("template_dir")[0]
73 def get_nested_files(filenames):
75 returns all the nested files for a set of filenames
78 for filename in filenames:
80 with open(filename) as fh:
82 if "resources" not in yml:
85 get_list_of_nested_files(yml["resources"], path.dirname(filename))
87 except yaml.YAMLError as e:
88 print(e) # pylint: disable=superfluous-parens
93 def list_filenames_in_template_dir(
94 metafunc, extensions, template_type="", sub_dirs=None
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"):
106 path.join(template_dir, s, f)
108 for f in listdir(path.join(template_dir, s))
109 if (path.isfile(path.join(template_dir, s, f)) and
110 path.splitext(f)[-1] in extensions and
111 check_basename_ending(template_type, path.splitext(f)[0]))
115 path.join(template_dir, f)
116 for f in listdir(template_dir)
117 if (path.isfile(path.join(template_dir, f)) and
118 path.splitext(f)[-1] in extensions and
119 check_basename_ending(template_type, path.splitext(f)[0]))
124 def list_template_dir(
125 metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
128 returns the filenames excluding the nested files for a template_dir,
129 either as its passed in on CLI or, during --self-test, the
130 directory whos name matches the current tests module name
132 sub_dirs = [] if sub_dirs is None else sub_dirs
135 filenames = list_filenames_in_template_dir(
136 metafunc, extensions, template_type, sub_dirs
139 nested_files = get_nested_files(filenames)
140 return list(set(filenames) - set(nested_files))
143 def get_filenames_list(
144 metafunc, extensions=None, exclude_nested=False, template_type=""
147 returns the filename fixtures for the template dir, either as by how its
148 passed in on CLI or, during --self-test, the directory whos name
149 matches the current tests module name
151 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
152 if metafunc.config.getoption("self_test"):
153 filenames_list = list_template_dir(
154 metafunc, extensions, exclude_nested, template_type, ["pass"]
157 pytest.mark.xfail(f, strict=True)
158 for f in list_template_dir(
159 metafunc, extensions, exclude_nested, template_type, ["fail"]
163 filenames_list = list_template_dir(
164 metafunc, extensions, exclude_nested, template_type
167 return filenames_list
170 def get_filenames_lists(
171 metafunc, extensions=None, exclude_nested=False, template_type=""
174 returns the list of files in the template dir, either as by how its
175 passed in on CLI or, during --self-test, the directory whos name
176 matches the current tests module name
178 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
180 if metafunc.config.getoption("self_test"):
181 filenames_lists.append(
183 metafunc, extensions, exclude_nested, template_type, ["pass"]
186 filenames_lists.append(
189 metafunc, extensions, exclude_nested, template_type, ["fail"]
195 filenames_lists.append(
196 list_template_dir(metafunc, extensions, exclude_nested, template_type)
198 return filenames_lists
201 def get_parsed_yaml_files(
202 metafunc, extensions, exclude_nested=True, template_type="", sections=None
205 returns the list of parsed yaml files in the specified template dir,
206 either as by how its passed in on CLI or, during --self-test, the
207 directory whos name matches the current tests module name
209 sections = [] if sections is None else sections
210 extensions = [".yaml", ".yml"]
212 if metafunc.config.getoption("self_test"):
213 yaml_files = list_template_dir(
214 metafunc, extensions, exclude_nested, template_type, ["pass"]
216 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
218 yaml_files = list_template_dir(
219 metafunc, extensions, exclude_nested, template_type, ["fail"]
221 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
223 pytest.mark.xfail(parsed_yml, strict=True) for parsed_yml in parsed_yml_list
226 yaml_files = list_template_dir(metafunc, extensions)
227 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
228 return parsed_yml_list
231 def parametrize_filenames(metafunc):
233 This param runs tests all files in the template dir
235 filenames = get_filenames_lists(metafunc)
236 metafunc.parametrize("filenames", filenames)
239 def parametrize_filename(metafunc):
241 This param runs tests once for every file in the template dir
243 filenames = get_filenames_list(metafunc)
244 metafunc.parametrize("filename", filenames)
247 def parametrize_yaml_files(metafunc):
249 This param runs tests for the yaml files in the template dir
251 yaml_files = get_filenames_lists(metafunc, [".yaml", ".yml"], False)
252 metafunc.parametrize("yaml_files", yaml_files)
255 def parametrize_yaml_file(metafunc):
257 This param runs tests for every yaml file in the template dir
259 yaml_files = get_filenames_list(metafunc, [".yaml", ".yml"], False)
260 metafunc.parametrize("yaml_file", yaml_files)
263 def parametrize_templates(metafunc):
265 This param runs tests for the template in the template dir
267 templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True)
268 metafunc.parametrize("templates", templates)
271 def parametrize_template(metafunc):
273 This param runs tests for every template in the template dir
275 templates = get_filenames_list(metafunc, [".yaml", ".yml"], True)
276 metafunc.parametrize("template", templates)
279 def parametrize_parsed_yaml_file(metafunc):
281 This param runs tests for a parsed version of each yaml file
284 parsed_yaml_files = get_parsed_yaml_files(metafunc, [".yaml", ".yml"], False)
285 metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
288 def parametrize_heat_templates(metafunc):
290 This param runs tests for all heat templates in the template dir
292 heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
293 metafunc.parametrize("heat_templates", heat_templates)
296 def parametrize_heat_template(metafunc):
298 This param runs tests for every heat template in the template dir
300 heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
301 metafunc.parametrize("heat_template", heat_templates)
304 def parametrize_volume_templates(metafunc):
306 This param runs tests for all volume templates in the template dir
308 volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
309 metafunc.parametrize("volume_templates", volume_templates)
312 def parametrize_volume_template(metafunc):
315 This param runs tests for every volume template in the template dir
317 volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
318 metafunc.parametrize("volume_template", volume_templates)
321 def parametrize_environment_files(metafunc):
323 This param runs tests for all environment files in the template dir
325 env_files = get_filenames_lists(metafunc, [".env"])
326 metafunc.parametrize("env_files", env_files)
329 def parametrize_environment_file(metafunc):
331 This param runs tests for every environment file in the template dir
333 env_files = get_filenames_list(metafunc, [".env"])
334 metafunc.parametrize("env_file", env_files)
337 def parametrize_parsed_environment_file(metafunc):
339 This param runs tests for every parsed environment file
342 parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
343 metafunc.parametrize("parsed_env_file", parsed_env_files)
346 def parametrize_template_dir(metafunc):
348 This param passes a the template_dir as passed in on CLI
349 or, during --self-test, passes in the sub directories of
350 template_dir/pass/ and template_dir/fail
351 template_dir = get_template_dir(metafunc)
353 template_dir = get_template_dir(metafunc)
355 if metafunc.config.getoption("self_test"):
357 path.join(template_dir, s, t)
359 for t in listdir(path.join(template_dir, s))
360 if path.isdir(path.join(template_dir, s, t))
364 pytest.mark.xfail(path.join(template_dir, s, t))
366 for t in listdir(path.join(template_dir, s))
367 if path.isdir(path.join(template_dir, s, t))
370 dirs = [template_dir]
372 metafunc.parametrize("template_dir", dirs)
375 def parametrize_environment_pair(metafunc, template_type=""):
377 Define a list of pairs of parsed yaml from the heat templates and
381 if metafunc.config.getoption("self_test"):
382 sub_dirs = ["pass", "fail"]
383 env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
384 yaml_files = list_template_dir(
385 metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
388 env_files = list_template_dir(metafunc, [".env"], True, template_type)
389 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
391 for filename in env_files:
392 basename = path.splitext(filename)[0]
393 if basename + ".yml" in yaml_files:
394 yfilename = basename + ".yml"
396 yfilename = basename + ".yaml"
399 with open(filename) as fh:
401 with open(yfilename) as fh:
404 if "fail" in filename:
407 {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
411 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
413 except yaml.YAMLError as e:
414 print(e) # pylint: disable=superfluous-parens
416 metafunc.parametrize("environment_pair", pairs)
419 def parametrize_heat_volume_pair(metafunc):
421 Define a list of pairs of parsed yaml from the a heat and volume
425 if metafunc.config.getoption("self_test"):
426 sub_dirs = ["pass", "fail"]
427 volume_files = list_template_dir(
428 metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
430 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
432 volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
433 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
435 pattern = re.compile(r"\_volume$")
436 for vfilename in volume_files:
437 basename = pattern.sub("", path.splitext(vfilename)[0])
438 if basename + ".yml" in yaml_files:
439 yfilename = basename + ".yml"
441 yfilename = basename + ".yaml"
444 with open(vfilename) as fh:
446 with open(yfilename) as fh:
449 if "fail" in vfilename:
452 {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
456 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
458 except yaml.YAMLError as e:
459 print(e) # pylint: disable=superfluous-parens
461 metafunc.parametrize("heat_volume_pair", pairs)