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============================================
43 from os import path, listdir
45 from tests import cached_yaml as yaml
47 from .helpers import get_parsed_yml_for_yaml_files, check_basename_ending
48 from .utils.nested_files import get_list_of_nested_files
52 # pylint: disable=invalid-name
55 def get_template_dir(metafunc):
57 returns template_dir, either as its passed in on CLI
58 or, during --self-test, the directory whos name matches
59 the current tests module name
61 if metafunc.config.getoption("template_dir") is None:
63 path.dirname(metafunc.module.__file__),
65 metafunc.function.__module__.split(".")[-1],
68 return metafunc.config.getoption("template_dir")[0]
71 def file_is_a_nested_template(file):
72 directory = path.dirname(file)
74 for filename in listdir(directory):
75 if filename.endswith(".yaml") or filename.endswith(".yml"):
76 filename = "{}/{}".format(directory, filename)
78 with open(filename) as fh:
80 if "resources" not in yml:
83 get_list_of_nested_files(yml["resources"], path.dirname(filename))
85 except yaml.YAMLError as e:
86 print(e) # pylint: disable=superfluous-parens
88 return file in nested_files
91 def get_nested_files(filenames):
93 returns all the nested files for a set of filenames
96 for filename in filenames:
97 if file_is_a_nested_template(filename):
98 nested_files.append(filename)
102 def list_filenames_in_template_dir(
103 metafunc, extensions, template_type="", sub_dirs=None
106 returns the filenames in a template_dir, either as its passed in
107 on CLI or, during --self-test, the directory whos name matches
108 the current tests module name
110 sub_dirs = [] if sub_dirs is None else sub_dirs
111 template_dir = get_template_dir(metafunc)
113 if metafunc.config.getoption("self_test"):
115 path.join(template_dir, s, f)
117 for f in listdir(path.join(template_dir, s))
118 if path.isfile(path.join(template_dir, s, f))
119 and path.splitext(f)[-1] in extensions
120 and check_basename_ending(template_type, path.splitext(f)[0])
124 path.join(template_dir, f)
125 for f in listdir(template_dir)
126 if path.isfile(path.join(template_dir, f))
127 and path.splitext(f)[-1] in extensions
128 and check_basename_ending(template_type, path.splitext(f)[0])
133 def list_template_dir(
134 metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
137 returns the filenames excluding the nested files for a template_dir,
138 either as its passed in on CLI or, during --self-test, the
139 directory whos name matches the current tests module name
141 sub_dirs = [] if sub_dirs is None else sub_dirs
144 filenames = list_filenames_in_template_dir(
145 metafunc, extensions, template_type, sub_dirs
148 nested_files = get_nested_files(filenames)
149 return list(set(filenames) - set(nested_files))
152 def get_filenames_list(
153 metafunc, extensions=None, exclude_nested=False, template_type=""
156 returns the filename fixtures for the template dir, either as by how its
157 passed in on CLI or, during --self-test, the directory whos name
158 matches the current tests module name
160 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
161 if metafunc.config.getoption("self_test"):
162 filenames_list = list_template_dir(
163 metafunc, extensions, exclude_nested, template_type, ["pass"]
166 pytest.mark.xfail(f, strict=True)
167 for f in list_template_dir(
168 metafunc, extensions, exclude_nested, template_type, ["fail"]
172 filenames_list = list_template_dir(
173 metafunc, extensions, exclude_nested, template_type
176 return filenames_list
179 def get_filenames_lists(
180 metafunc, extensions=None, exclude_nested=False, template_type=""
183 returns the list of files in the template dir, either as by how its
184 passed in on CLI or, during --self-test, the directory whos name
185 matches the current tests module name
187 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
189 if metafunc.config.getoption("self_test"):
190 filenames_lists.append(
192 metafunc, extensions, exclude_nested, template_type, ["pass"]
195 filenames_lists.append(
198 metafunc, extensions, exclude_nested, template_type, ["fail"]
204 filenames_lists.append(
205 list_template_dir(metafunc, extensions, exclude_nested, template_type)
207 return filenames_lists
210 def get_parsed_yaml_files(
211 metafunc, extensions, exclude_nested=True, template_type="", sections=None
214 returns the list of parsed yaml files in the specified template dir,
215 either as by how its passed in on CLI or, during --self-test, the
216 directory whos name matches the current tests module name
218 sections = [] if sections is None else sections
219 extensions = [".yaml", ".yml"]
221 if metafunc.config.getoption("self_test"):
222 yaml_files = list_template_dir(
223 metafunc, extensions, exclude_nested, template_type, ["pass"]
225 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
227 yaml_files = list_template_dir(
228 metafunc, extensions, exclude_nested, template_type, ["fail"]
230 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
232 pytest.mark.xfail(parsed_yml, strict=True) 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, sections)
237 return parsed_yml_list
240 def parametrize_filenames(metafunc):
242 This param runs tests all files in the template dir
244 filenames = get_filenames_lists(metafunc)
245 metafunc.parametrize("filenames", filenames)
248 def parametrize_filename(metafunc):
250 This param runs tests once for every file in the template dir
252 filenames = get_filenames_list(metafunc)
253 metafunc.parametrize("filename", filenames)
256 def parametrize_yaml_files(metafunc):
258 This param runs tests for the yaml files in the template dir
260 yaml_files = get_filenames_lists(metafunc, [".yaml", ".yml"], False)
261 metafunc.parametrize("yaml_files", yaml_files)
264 def parametrize_yaml_file(metafunc):
266 This param runs tests for every yaml file in the template dir
268 yaml_files = get_filenames_list(metafunc, [".yaml", ".yml"], False)
269 metafunc.parametrize("yaml_file", yaml_files)
272 def parametrize_templates(metafunc):
274 This param runs tests for the template in the template dir
276 templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True)
277 metafunc.parametrize("templates", templates)
280 def parametrize_template(metafunc):
282 This param runs tests for every template in the template dir
284 templates = get_filenames_list(metafunc, [".yaml", ".yml"], True)
285 metafunc.parametrize("template", templates)
288 def parametrize_parsed_yaml_file(metafunc):
290 This param runs tests for a parsed version of each yaml file
293 parsed_yaml_files = get_parsed_yaml_files(metafunc, [".yaml", ".yml"], False)
294 metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
297 def parametrize_heat_templates(metafunc):
299 This param runs tests for all heat templates in the template dir
301 heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
302 metafunc.parametrize("heat_templates", heat_templates)
305 def parametrize_heat_template(metafunc):
307 This param runs tests for every heat template in the template dir
309 heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
310 metafunc.parametrize("heat_template", heat_templates)
313 def parametrize_volume_templates(metafunc):
315 This param runs tests for all volume templates in the template dir
317 volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
318 metafunc.parametrize("volume_templates", volume_templates)
321 def parametrize_volume_template(metafunc):
324 This param runs tests for every volume template in the template dir
326 volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
327 metafunc.parametrize("volume_template", volume_templates)
330 def parametrize_environment_files(metafunc):
332 This param runs tests for all environment files in the template dir
334 env_files = get_filenames_lists(metafunc, [".env"])
335 metafunc.parametrize("env_files", env_files)
338 def parametrize_environment_file(metafunc):
340 This param runs tests for every environment file in the template dir
342 env_files = get_filenames_list(metafunc, [".env"])
343 metafunc.parametrize("env_file", env_files)
346 def parametrize_parsed_environment_file(metafunc):
348 This param runs tests for every parsed environment file
351 parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
352 metafunc.parametrize("parsed_env_file", parsed_env_files)
355 def parametrize_template_dir(metafunc):
357 This param passes a the template_dir as passed in on CLI
358 or, during --self-test, passes in the sub directories of
359 template_dir/pass/ and template_dir/fail
360 template_dir = get_template_dir(metafunc)
362 template_dir = get_template_dir(metafunc)
364 if metafunc.config.getoption("self_test"):
366 path.join(template_dir, s, t)
368 for t in listdir(path.join(template_dir, s))
369 if path.isdir(path.join(template_dir, s, t))
373 pytest.mark.xfail(path.join(template_dir, s, t))
375 for t in listdir(path.join(template_dir, s))
376 if path.isdir(path.join(template_dir, s, t))
379 dirs = [template_dir]
381 metafunc.parametrize("template_dir", dirs)
384 def parametrize_environment_pair(metafunc, template_type=""):
386 Define a list of pairs of parsed yaml from the heat templates and
390 if metafunc.config.getoption("self_test"):
391 sub_dirs = ["pass", "fail"]
392 env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
393 yaml_files = list_template_dir(
394 metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
397 env_files = list_template_dir(metafunc, [".env"], True, template_type)
398 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
400 for filename in env_files:
401 basename = path.splitext(filename)[0]
402 if basename + ".yml" in yaml_files:
403 yfilename = basename + ".yml"
405 yfilename = basename + ".yaml"
408 with open(filename) as fh:
410 with open(yfilename) as fh:
413 if "fail" in filename:
416 {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
420 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
422 except yaml.YAMLError as e:
423 print(e) # pylint: disable=superfluous-parens
425 metafunc.parametrize("environment_pair", pairs)
428 def parametrize_heat_volume_pair(metafunc):
430 Define a list of pairs of parsed yaml from the a heat and volume
434 if metafunc.config.getoption("self_test"):
435 sub_dirs = ["pass", "fail"]
436 volume_files = list_template_dir(
437 metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
439 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
441 volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
442 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
444 pattern = re.compile(r"\_volume$")
445 for vfilename in volume_files:
446 basename = pattern.sub("", path.splitext(vfilename)[0])
447 if basename + ".yml" in yaml_files:
448 yfilename = basename + ".yml"
450 yfilename = basename + ".yaml"
453 with open(vfilename) as fh:
455 with open(yfilename) as fh:
458 if "fail" in vfilename:
461 {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
465 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
467 except yaml.YAMLError as e:
468 print(e) # pylint: disable=superfluous-parens
470 metafunc.parametrize("heat_volume_pair", pairs)