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============================================
41 from yaml import YAMLError
42 from yaml.constructor import ConstructorError
44 from tests import cached_yaml as yaml
45 from tests.utils import yaml_custom_utils
47 from tests.helpers import validates, load_yaml
48 from tests.utils.nested_files import check_for_invalid_nesting
49 from tests.utils.nested_iterables import find_all_get_resource_in_yml
50 from tests.utils.nested_iterables import find_all_get_param_in_yml
55 def test_00_valid_yaml(filename):
56 if path.splitext(filename)[-1].lower() not in (".yml", ".yaml", ".env"):
57 pytest.skip("Not a YAML file")
60 except YAMLError as e:
62 "Invalid YAML detected: {} "
63 "NOTE: Online YAML checkers such as yamllint.com "
64 "can helpful in diagnosing errors in YAML"
65 ).format(str(e).replace("\n", " "))
68 def check_duplicate_keys(yaml_path):
69 import yaml as normal_yaml
72 with open(yaml_path) as fh:
73 normal_yaml.load(fh, yaml_custom_utils.UniqueKeyLoader) # nosec
74 except ConstructorError as e:
75 pytest.fail("{} {}".format(e.problem, e.problem_mark))
80 def test_02_no_duplicate_keys_in_file(yaml_file):
81 check_duplicate_keys(yaml_file)
86 def test_02a_no_duplicate_keys_in_env(env_file):
87 check_duplicate_keys(env_file)
92 def test_03_all_referenced_resources_exists(yaml_file):
94 Check that all resources referenced by get_resource
95 actually exists in all yaml files
97 with open(yaml_file) as fh:
98 yml = yaml.safe_load(fh)
100 # skip if resources are not defined
101 if "resources" not in yml:
102 pytest.skip("No resources specified in the yaml file")
104 resources = yml.get("resources")
106 resource_ids = resources.keys()
107 referenced_resource_ids = find_all_get_resource_in_yml(yml)
109 missing_referenced_resources = set()
110 for referenced_resource_id in referenced_resource_ids:
111 if referenced_resource_id not in resource_ids:
112 missing_referenced_resources.add(referenced_resource_id)
114 assert not missing_referenced_resources, (
115 "Unable to resolve get_resource for the following "
116 "resource IDS: {}. Please ensure the resource ID is defined and "
117 "nested under the resources section of the template".format(
118 ", ".join(missing_referenced_resources)
124 @validates("R-92635")
125 def test_04_valid_nesting(yaml_file):
127 Check that the nesting is following the proper format and
128 that all nested files exists and are parsable
132 with open(yaml_file) as fh:
134 if "resources" in yml:
136 invalid_nesting.extend(
137 check_for_invalid_nesting(
138 yml["resources"], yaml_file, path.dirname(yaml_file)
142 invalid_nesting.append(yaml_file)
144 assert not invalid_nesting, "invalid nested file detected in file {}\n\n".format(
150 @validates("R-92635")
151 def test_05_all_get_param_have_defined_parameter(yaml_file):
153 Check that all referenced parameters are actually defined
156 invalid_get_params = []
157 with open(yaml_file) as fh:
160 resource_params = find_all_get_param_in_yml(yml)
162 parameters = set(yml.get("parameters", {}).keys())
164 pytest.skip("no parameters detected")
166 for rp in resource_params:
167 if rp not in parameters:
168 invalid_get_params.append(rp)
171 not invalid_get_params
172 ), "get_param reference detected without corresponding parameter defined {}".format(
177 @validates("R-90152")
179 def test_06_heat_template_resource_section_has_resources(heat_template):
181 found_resource = False
183 with open(heat_template) as fh:
186 resources = yml.get("resources")
188 for k1, v1 in yml["resources"].items():
189 if not isinstance(v1, dict):
192 found_resource = True
195 assert found_resource, "Heat templates must contain at least one resource"