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(metafunc.module.__file__),
66 metafunc.function.__module__.split(".")[-1],
69 return metafunc.config.getoption("template_dir")[0]
72 def file_is_a_nested_template(file):
73 directory = path.dirname(file)
75 for filename in listdir(directory):
76 if filename.endswith(".yaml") or filename.endswith(".yml"):
77 filename = "{}/{}".format(directory, filename)
79 with open(filename) as fh:
81 if "resources" not in yml:
84 get_list_of_nested_files(yml["resources"], path.dirname(filename))
86 except yaml.YAMLError as e:
87 print(e) # pylint: disable=superfluous-parens
89 return file in nested_files
92 def get_nested_files(filenames):
94 returns all the nested files for a set of filenames
97 for filename in filenames:
98 if file_is_a_nested_template(filename):
99 nested_files.append(filename)
103 def list_filenames_in_template_dir(
104 metafunc, extensions, template_type="", sub_dirs=None
107 returns the filenames in a template_dir, either as its passed in
108 on CLI or, during --self-test, the directory whos name matches
109 the current tests module name
111 sub_dirs = [] if sub_dirs is None else sub_dirs
112 template_dir = get_template_dir(metafunc)
114 if metafunc.config.getoption("self_test"):
116 path.join(template_dir, s, f)
118 for f in listdir(path.join(template_dir, s))
119 if path.isfile(path.join(template_dir, s, f))
120 and path.splitext(f)[-1] in extensions
121 and check_basename_ending(template_type, path.splitext(f)[0])
125 path.join(template_dir, f)
126 for f in listdir(template_dir)
127 if path.isfile(path.join(template_dir, f))
128 and path.splitext(f)[-1] in extensions
129 and check_basename_ending(template_type, path.splitext(f)[0])
134 def list_template_dir(
135 metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
138 returns the filenames excluding the nested files for a template_dir,
139 either as its passed in on CLI or, during --self-test, the
140 directory whos name matches the current tests module name
142 sub_dirs = [] if sub_dirs is None else sub_dirs
145 filenames = list_filenames_in_template_dir(
146 metafunc, extensions, template_type, sub_dirs
149 nested_files = get_nested_files(filenames)
150 return list(set(filenames) - set(nested_files))
153 def get_filenames_list(
154 metafunc, extensions=None, exclude_nested=False, template_type=""
157 returns the filename fixtures for the template dir, either as by how its
158 passed in on CLI or, during --self-test, the directory whos name
159 matches the current tests module name
161 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
162 if metafunc.config.getoption("self_test"):
163 filenames_list = list_template_dir(
164 metafunc, extensions, exclude_nested, template_type, ["pass"]
167 pytest.mark.xfail(f, strict=True)
168 for f in list_template_dir(
169 metafunc, extensions, exclude_nested, template_type, ["fail"]
173 filenames_list = list_template_dir(
174 metafunc, extensions, exclude_nested, template_type
177 return filenames_list
180 def get_filenames_lists(
181 metafunc, extensions=None, exclude_nested=False, template_type=""
184 returns the list of files in the template dir, either as by how its
185 passed in on CLI or, during --self-test, the directory whos name
186 matches the current tests module name
188 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
190 if metafunc.config.getoption("self_test"):
191 filenames_lists.append(
193 metafunc, extensions, exclude_nested, template_type, ["pass"]
196 filenames_lists.append(
199 metafunc, extensions, exclude_nested, template_type, ["fail"]
205 filenames_lists.append(
206 list_template_dir(metafunc, extensions, exclude_nested, template_type)
208 return filenames_lists
211 def get_parsed_yaml_files(
212 metafunc, extensions, exclude_nested=True, template_type="", sections=None
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(
224 metafunc, extensions, exclude_nested, template_type, ["pass"]
226 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
228 yaml_files = list_template_dir(
229 metafunc, extensions, exclude_nested, template_type, ["fail"]
231 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
233 pytest.mark.xfail(parsed_yml, strict=True) for parsed_yml in parsed_yml_list
236 yaml_files = list_template_dir(metafunc, extensions)
237 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
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"], False)
295 metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
298 def parametrize_heat_templates(metafunc):
300 This param runs tests for all heat templates in the template dir
302 heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
303 metafunc.parametrize("heat_templates", heat_templates)
306 def parametrize_heat_template(metafunc):
308 This param runs tests for every heat template in the template dir
310 heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
311 metafunc.parametrize("heat_template", heat_templates)
314 def parametrize_volume_templates(metafunc):
316 This param runs tests for all volume templates in the template dir
318 volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
319 metafunc.parametrize("volume_templates", volume_templates)
322 def parametrize_volume_template(metafunc):
325 This param runs tests for every volume template in the template dir
327 volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
328 metafunc.parametrize("volume_template", volume_templates)
331 def parametrize_environment_files(metafunc):
333 This param runs tests for all environment files in the template dir
335 env_files = get_filenames_lists(metafunc, [".env"])
336 metafunc.parametrize("env_files", env_files)
339 def parametrize_environment_file(metafunc):
341 This param runs tests for every environment file in the template dir
343 env_files = get_filenames_list(metafunc, [".env"])
344 metafunc.parametrize("env_file", env_files)
347 def parametrize_parsed_environment_file(metafunc):
349 This param runs tests for every parsed environment file
352 parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
353 metafunc.parametrize("parsed_env_file", parsed_env_files)
356 def parametrize_template_dir(metafunc):
358 This param passes a the template_dir as passed in on CLI
359 or, during --self-test, passes in the sub directories of
360 template_dir/pass/ and template_dir/fail
361 template_dir = get_template_dir(metafunc)
363 template_dir = get_template_dir(metafunc)
365 if metafunc.config.getoption("self_test"):
367 path.join(template_dir, s, t)
369 for t in listdir(path.join(template_dir, s))
370 if path.isdir(path.join(template_dir, s, t))
374 pytest.mark.xfail(path.join(template_dir, s, t))
376 for t in listdir(path.join(template_dir, s))
377 if path.isdir(path.join(template_dir, s, t))
380 dirs = [template_dir]
382 metafunc.parametrize("template_dir", dirs)
385 def parametrize_environment_pair(metafunc, template_type=""):
387 Define a list of pairs of parsed yaml from the heat templates and
391 if metafunc.config.getoption("self_test"):
392 sub_dirs = ["pass", "fail"]
393 env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
394 yaml_files = list_template_dir(
395 metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
398 env_files = list_template_dir(metafunc, [".env"], True, template_type)
399 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
401 for filename in env_files:
402 basename = path.splitext(filename)[0]
403 if basename + ".yml" in yaml_files:
404 yfilename = basename + ".yml"
406 yfilename = basename + ".yaml"
409 with open(filename) as fh:
411 with open(yfilename) as fh:
414 if "fail" in filename:
417 {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
421 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
423 except yaml.YAMLError as e:
424 print(e) # pylint: disable=superfluous-parens
426 metafunc.parametrize("environment_pair", pairs)
429 def parametrize_heat_volume_pair(metafunc):
431 Define a list of pairs of parsed yaml from the a heat and volume
435 if metafunc.config.getoption("self_test"):
436 sub_dirs = ["pass", "fail"]
437 volume_files = list_template_dir(
438 metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
440 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
442 volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
443 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
445 pattern = re.compile(r"\_volume$")
446 for vfilename in volume_files:
447 basename = pattern.sub("", path.splitext(vfilename)[0])
448 if basename + ".yml" in yaml_files:
449 yfilename = basename + ".yml"
451 yfilename = basename + ".yaml"
454 with open(vfilename) as fh:
456 with open(yfilename) as fh:
459 if "fail" in vfilename:
462 {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
466 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
468 except yaml.YAMLError as e:
469 print(e) # pylint: disable=superfluous-parens
471 metafunc.parametrize("heat_volume_pair", pairs)