[VVP] revert nested resource section
[vvp/validation-scripts.git] / ice_validator / tests / test_initial_configuration.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 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 #
22 #
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
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
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.
35 #
36 # ============LICENSE_END============================================
37 #
38 from os import path
39
40 import pytest
41 from yaml.constructor import ConstructorError
42
43 from tests import cached_yaml as yaml
44 from tests.utils import yaml_custom_utils
45
46 from .helpers import validates
47 from yamllint.config import YamlLintConfig
48 from yamllint import linter
49 from .utils.nested_files import check_for_invalid_nesting
50 from .utils.nested_iterables import find_all_get_resource_in_yml
51 from .utils.nested_iterables import find_all_get_param_in_yml
52
53
54 @pytest.mark.base
55 @validates("R-95303")
56 def test_00_valid_yaml(filename):
57     """
58     Read in each .yaml or .env file. If it is successfully parsed as yaml, save
59     contents, else add filename to list of bad yaml files. Log the result of
60     parse attempt.
61     """
62     conf = YamlLintConfig("rules: {}")
63
64     if path.splitext(filename)[-1] in [".yml", ".yaml", ".env"]:
65         gen = linter.run(open(filename), conf)
66         errors = list(gen)
67
68         assert not errors, "Error parsing file {} with error {}".format(
69             filename, errors
70         )
71     else:
72         pytest.skip(
73             "The file does not have any of the extensions .yml,\
74             .yaml, or .env"
75         )
76
77
78 @pytest.mark.base
79 @validates("R-92635")
80 def test_02_no_duplicate_keys_in_file(yaml_file):
81     """
82     Checks that no duplicate keys exist in a given YAML file.
83     """
84     import yaml as normal_yaml  # we can't use the caching version in this test
85
86     normal_yaml.add_constructor(
87         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
88         yaml_custom_utils.raise_duplicates_keys,
89     )
90
91     try:
92         with open(yaml_file) as fh:
93             normal_yaml.load(fh)
94     except ConstructorError as e:
95         pytest.fail("{} {}".format(e.problem, e.problem_mark))
96
97
98 @pytest.mark.base
99 @validates("R-92635")
100 def test_03_all_referenced_resources_exists(yaml_file):
101     """
102     Check that all resources referenced by get_resource
103     actually exists in all yaml files
104     """
105     with open(yaml_file) as fh:
106         yml = yaml.load(fh)
107
108     # skip if resources are not defined
109     if "resources" not in yml:
110         pytest.skip("No resources specified in the yaml file")
111
112     resources = yml.get("resources")
113     if resources:
114         resource_ids = resources.keys()
115         referenced_resource_ids = find_all_get_resource_in_yml(yml)
116
117         missing_referenced_resources = set()
118         for referenced_resource_id in referenced_resource_ids:
119             if referenced_resource_id not in resource_ids:
120                 missing_referenced_resources.add(referenced_resource_id)
121
122         assert not missing_referenced_resources, (
123             "Unable to resolve get_resource for the following "
124             "resource IDS: {}. Please ensure the resource ID is defined and "
125             "nested under the resources section of the template".format(
126                 ", ".join(missing_referenced_resources)
127             )
128         )
129
130
131 @pytest.mark.base
132 @validates("R-92635")
133 def test_04_valid_nesting(yaml_file):
134     """
135     Check that the nesting is following the proper format and
136     that all nested files exists and are parsable
137     """
138     invalid_nesting = []
139
140     with open(yaml_file) as fh:
141         yml = yaml.load(fh)
142     if "resources" in yml:
143         try:
144             invalid_nesting.extend(
145                 check_for_invalid_nesting(
146                     yml["resources"], yaml_file, path.dirname(yaml_file)
147                 )
148             )
149         except Exception:
150             invalid_nesting.append(yaml_file)
151
152     assert not invalid_nesting, "invalid nested file detected in file {}\n\n".format(
153         invalid_nesting
154     )
155
156
157 @pytest.mark.base
158 @validates("R-92635")
159 def test_05_all_get_param_have_defined_parameter(yaml_file):
160     """
161     Check that all referenced parameters are actually defined
162     as parameters
163     """
164     invalid_get_params = []
165     with open(yaml_file) as fh:
166         yml = yaml.load(fh)
167
168     resource_params = find_all_get_param_in_yml(yml)
169
170     parameters = set(yml.get("parameters", {}).keys())
171     if not parameters:
172         pytest.skip("no parameters detected")
173
174     for rp in resource_params:
175         if rp not in parameters:
176             invalid_get_params.append(rp)
177
178     assert (
179         not invalid_get_params
180     ), "get_param reference detected without corresponding parameter defined {}".format(
181         invalid_get_params
182     )
183
184
185 @validates("R-90152")
186 @pytest.mark.base
187 def test_06_heat_template_resource_section_has_resources(heat_template):
188
189     found_resource = False
190
191     with open(heat_template) as fh:
192         yml = yaml.load(fh)
193
194     resources = yml.get("resources")
195     if resources:
196         for k1, v1 in yml["resources"].items():
197             if not isinstance(v1, dict):
198                 continue
199
200             found_resource = True
201             break
202
203     assert found_resource, "Heat templates must contain at least one resource"