Aligned test with updated R-610030
[vvp/validation-scripts.git] / ice_validator / tests / test_base_template_names.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2020 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
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
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 # Unless otherwise specified, all documentation contained herein is licensed
22 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 # you may not use this documentation except in compliance with the License.
24 # You may obtain a copy of the License at
25 #
26 #             https://creativecommons.org/licenses/by/4.0/
27 #
28 # Unless required by applicable law or agreed to in writing, documentation
29 # distributed under the License is distributed on an "AS IS" BASIS,
30 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 # See the License for the specific language governing permissions and
32 # limitations under the License.
33 #
34 # ============LICENSE_END============================================
35
36 from os import listdir
37 from os import path
38 import re
39 from pathlib import Path
40
41 import pytest
42
43 from .helpers import check_basename_ending
44 from .helpers import validates
45
46
47 RE_BASE = re.compile(r"(^base$)|(^base_)|(_base_)|(_base$)")
48
49
50 def list_filenames(template_dir):
51     return [
52         f
53         for f in listdir(template_dir)
54         if path.isfile(path.join(template_dir, f))
55         and path.splitext(f)[-1] in [".yaml", ".yml"]
56     ]
57
58
59 @validates("R-81339", "R-87247", "R-76057")
60 def test_template_names_valid_characters(template_dir):
61     filenames = list_filenames(template_dir)
62     errors = []
63     for f in filenames:
64         stem = Path(f).stem
65         if not stem.replace("_", "").isalnum():
66             errors.append(f)
67     assert not errors, (
68         "The following Heat template names include characters other than "
69         "alphanumerics and underscores: {}"
70     ).format(", ".join(errors))
71
72
73 @validates("R-37028", "R-87485", "R-81339", "R-87247", "R-76057")
74 def test_base_template_names(template_dir):
75     """
76     Check all base templates have a filename that includes "_base_".
77     """
78     filenames = list_filenames(template_dir)
79
80     if not filenames and listdir(template_dir):
81         pytest.skip("Nested directory detected.  Let that test fail instead.")
82
83     base_modules = []
84     for filename in filenames:
85         basename = path.splitext(filename)[0]
86
87         # volume templates are tied to their parent naming wise
88         if check_basename_ending("volume", basename):
89             continue
90
91         if RE_BASE.search(basename.lower()):
92             base_modules.append(filename)
93
94     if not base_modules:
95         msg = (
96             "No base module detected in the following files "
97             "from the template directory: {}"
98         ).format(", ".join(filenames))
99     elif len(base_modules) > 1:
100         msg = (
101             "Multiple base modules detected in the template "
102             "directory, but only one is allowed: {}"
103         ).format(", ".join(base_modules))
104     else:
105         msg = ""
106
107     assert len(base_modules) == 1, msg