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 tests.helpers import get_parsed_yml_for_yaml_files, check_basename_ending
48 from tests.utils.nested_files import get_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", None) is None:
63 path.dirname(metafunc.module.__file__),
65 metafunc.function.__module__.split(".")[-1],
68 return metafunc.config.getoption("template_dir")[0]
71 def list_filenames_in_template_dir(
72 metafunc, extensions, template_type="", sub_dirs=None
75 returns the filenames in a template_dir, either as its passed in
76 on CLI or, during --self-test, the directory whos name matches
77 the current tests module name
79 sub_dirs = [] if sub_dirs is None else sub_dirs
80 template_dir = get_template_dir(metafunc)
82 if metafunc.config.getoption("self_test"):
84 path.join(template_dir, s, f)
86 for f in listdir(path.join(template_dir, s))
87 if path.isfile(path.join(template_dir, s, f))
88 and path.splitext(f)[-1] in extensions
89 and check_basename_ending(template_type, path.splitext(f)[0])
93 path.join(template_dir, f)
94 for f in listdir(template_dir)
95 if path.isfile(path.join(template_dir, f))
96 and path.splitext(f)[-1] in extensions
97 and check_basename_ending(template_type, path.splitext(f)[0])
102 def list_template_dir(
103 metafunc, extensions, exclude_nested=True, template_type="", sub_dirs=None
106 returns the filenames excluding the nested files for a template_dir,
107 either as its passed in on CLI or, during --self-test, the
108 directory whos name matches the current tests module name
110 sub_dirs = [] if sub_dirs is None else sub_dirs
113 filenames = list_filenames_in_template_dir(
114 metafunc, extensions, template_type, sub_dirs
117 nested_files = get_nested_files(filenames)
118 return list(set(filenames) - set(nested_files))
121 def get_filenames_list(
122 metafunc, extensions=None, exclude_nested=False, template_type=""
125 returns the filename fixtures for the template dir, either as by how its
126 passed in on CLI or, during --self-test, the directory whos name
127 matches the current tests module name
129 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
130 if metafunc.config.getoption("self_test"):
131 filenames_list = list_template_dir(
132 metafunc, extensions, exclude_nested, template_type, ["pass"]
135 pytest.mark.xfail(f, strict=True)
136 for f in list_template_dir(
137 metafunc, extensions, exclude_nested, template_type, ["fail"]
141 filenames_list = list_template_dir(
142 metafunc, extensions, exclude_nested, template_type
145 return filenames_list
148 def get_filenames_lists(
149 metafunc, extensions=None, exclude_nested=False, template_type=""
152 returns the list of files in the template dir, either as by how its
153 passed in on CLI or, during --self-test, the directory whos name
154 matches the current tests module name
156 extensions = [".yaml", ".yml", ".env"] if extensions is None else extensions
158 if metafunc.config.getoption("self_test", None):
159 filenames_lists.append(
161 metafunc, extensions, exclude_nested, template_type, ["pass"]
164 filenames_lists.append(
167 metafunc, extensions, exclude_nested, template_type, ["fail"]
173 filenames_lists.append(
174 list_template_dir(metafunc, extensions, exclude_nested, template_type)
176 return filenames_lists
179 def get_parsed_yaml_files(
180 metafunc, extensions, exclude_nested=True, template_type="", sections=None
183 returns the list of parsed yaml files in the specified template dir,
184 either as by how its passed in on CLI or, during --self-test, the
185 directory whos name matches the current tests module name
187 sections = [] if sections is None else sections
188 extensions = [".yaml", ".yml"]
190 if metafunc.config.getoption("self_test"):
191 yaml_files = list_template_dir(
192 metafunc, extensions, exclude_nested, template_type, ["pass"]
194 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
196 yaml_files = list_template_dir(
197 metafunc, extensions, exclude_nested, template_type, ["fail"]
199 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
201 pytest.mark.xfail(parsed_yml, strict=True) for parsed_yml in parsed_yml_list
204 yaml_files = list_template_dir(metafunc, extensions)
205 parsed_yml_list = get_parsed_yml_for_yaml_files(yaml_files, sections)
206 return parsed_yml_list
209 def parametrize_filenames(metafunc):
211 This param runs tests all files in the template dir
213 filenames = get_filenames_lists(metafunc)
214 metafunc.parametrize("filenames", filenames)
217 def parametrize_filename(metafunc):
219 This param runs tests once for every file in the template dir
221 filenames = get_filenames_list(metafunc)
222 metafunc.parametrize("filename", filenames)
225 def parametrize_yaml_files(metafunc):
227 This param runs tests for the yaml files in the template dir
229 yaml_files = get_filenames_lists(metafunc, [".yaml", ".yml"], False)
230 metafunc.parametrize("yaml_files", yaml_files)
233 def parametrize_yaml_file(metafunc):
235 This param runs tests for every yaml file in the template dir
237 yaml_files = get_filenames_list(metafunc, [".yaml", ".yml"], False)
238 metafunc.parametrize("yaml_file", yaml_files)
241 def parametrize_templates(metafunc):
243 This param runs tests for the template in the template dir
245 templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True)
246 metafunc.parametrize("templates", templates)
249 def parametrize_template(metafunc):
251 This param runs tests for every template in the template dir
253 templates = get_filenames_list(metafunc, [".yaml", ".yml"], True)
254 metafunc.parametrize("template", templates)
257 def parametrize_parsed_yaml_file(metafunc):
259 This param runs tests for a parsed version of each yaml file
262 parsed_yaml_files = get_parsed_yaml_files(metafunc, [".yaml", ".yml"], False)
263 metafunc.parametrize("parsed_yaml_file", parsed_yaml_files)
266 def parametrize_heat_templates(metafunc):
268 This param runs tests for all heat templates in the template dir
270 heat_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "heat")
271 metafunc.parametrize("heat_templates", heat_templates)
274 def parametrize_heat_template(metafunc):
276 This param runs tests for every heat template in the template dir
278 heat_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "heat")
279 metafunc.parametrize("heat_template", heat_templates)
282 def parametrize_volume_templates(metafunc):
284 This param runs tests for all volume templates in the template dir
286 volume_templates = get_filenames_lists(metafunc, [".yaml", ".yml"], True, "volume")
287 metafunc.parametrize("volume_templates", volume_templates)
290 def parametrize_volume_template(metafunc):
293 This param runs tests for every volume template in the template dir
295 volume_templates = get_filenames_list(metafunc, [".yaml", ".yml"], True, "volume")
296 metafunc.parametrize("volume_template", volume_templates)
299 def parametrize_environment_files(metafunc):
301 This param runs tests for all environment files in the template dir
303 env_files = get_filenames_lists(metafunc, [".env"])
304 metafunc.parametrize("env_files", env_files)
307 def parametrize_environment_file(metafunc):
309 This param runs tests for every environment file in the template dir
311 env_files = get_filenames_list(metafunc, [".env"])
312 metafunc.parametrize("env_file", env_files)
315 def parametrize_parsed_environment_file(metafunc):
317 This param runs tests for every parsed environment file
320 parsed_env_files = get_parsed_yaml_files(metafunc, [".env"])
321 metafunc.parametrize("parsed_env_file", parsed_env_files)
324 def parametrize_template_dir(metafunc):
326 This param passes a the template_dir as passed in on CLI
327 or, during --self-test, passes in the sub directories of
328 template_dir/pass/ and template_dir/fail
329 template_dir = get_template_dir(metafunc)
331 template_dir = get_template_dir(metafunc)
333 if metafunc.config.getoption("self_test"):
335 path.join(template_dir, s, t)
337 for t in listdir(path.join(template_dir, s))
338 if path.isdir(path.join(template_dir, s, t))
342 pytest.mark.xfail(path.join(template_dir, s, t))
344 for t in listdir(path.join(template_dir, s))
345 if path.isdir(path.join(template_dir, s, t))
348 dirs = [template_dir]
350 metafunc.parametrize("template_dir", dirs)
353 def parametrize_environment_pair(metafunc, template_type=""):
355 Define a list of pairs of parsed yaml from the heat templates and
359 if metafunc.config.getoption("self_test"):
360 sub_dirs = ["pass", "fail"]
361 env_files = list_template_dir(metafunc, [".env"], True, template_type, sub_dirs)
362 yaml_files = list_template_dir(
363 metafunc, [".yaml", ".yml"], True, template_type, sub_dirs
366 env_files = list_template_dir(metafunc, [".env"], True, template_type)
367 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, template_type)
369 for filename in env_files:
370 basename = path.splitext(filename)[0]
371 if basename + ".yml" in yaml_files:
372 yfilename = basename + ".yml"
374 yfilename = basename + ".yaml"
377 with open(filename) as fh:
379 with open(yfilename) as fh:
382 if "fail" in filename:
385 {"name": basename, "yyml": yyml, "eyml": eyml}, strict=True
389 pairs.append({"name": basename, "yyml": yyml, "eyml": eyml})
391 except yaml.YAMLError as e:
392 print(e) # pylint: disable=superfluous-parens
394 metafunc.parametrize("environment_pair", pairs)
397 def parametrize_heat_volume_pair(metafunc):
399 Define a list of pairs of parsed yaml from the a heat and volume
403 if metafunc.config.getoption("self_test"):
404 sub_dirs = ["pass", "fail"]
405 volume_files = list_template_dir(
406 metafunc, [".yaml", ".yml"], True, "volume", sub_dirs
408 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "", sub_dirs)
410 volume_files = list_template_dir(metafunc, [".yaml", ".yml"], True, "volume")
411 yaml_files = list_template_dir(metafunc, [".yaml", ".yml"], True)
413 pattern = re.compile(r"\_volume$")
414 for vfilename in volume_files:
415 basename = pattern.sub("", path.splitext(vfilename)[0])
416 if basename + ".yml" in yaml_files:
417 yfilename = basename + ".yml"
419 yfilename = basename + ".yaml"
422 with open(vfilename) as fh:
424 with open(yfilename) as fh:
427 if "fail" in vfilename:
430 {"name": basename, "yyml": yyml, "vyml": vyml}, strict=True
434 pairs.append({"name": basename, "yyml": yyml, "vyml": vyml})
436 except yaml.YAMLError as e:
437 print(e) # pylint: disable=superfluous-parens
439 metafunc.parametrize("heat_volume_pair", pairs)