[VVP] Adding bandit security scans and fixes
[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 import YAMLError
42 from yaml.constructor import ConstructorError
43
44 from tests import cached_yaml as yaml
45 from tests.utils import yaml_custom_utils
46
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
51
52
53 @pytest.mark.base
54 @validates("R-95303")
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")
58     try:
59         load_yaml(filename)
60     except YAMLError as e:
61         assert False, (
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", " "))
66
67
68 @pytest.mark.base
69 @validates("R-92635")
70 def test_02_no_duplicate_keys_in_file(yaml_file):
71     """
72     Checks that no duplicate keys exist in a given YAML file.
73     """
74     import yaml as normal_yaml  # we can't use the caching version in this test
75
76     normal_yaml.add_constructor(
77         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
78         yaml_custom_utils.raise_duplicates_keys,
79     )
80
81     try:
82         with open(yaml_file) as fh:
83             normal_yaml.safe_load(fh)
84     except ConstructorError as e:
85         pytest.fail("{} {}".format(e.problem, e.problem_mark))
86
87
88 @pytest.mark.base
89 @validates("R-92635")
90 def test_03_all_referenced_resources_exists(yaml_file):
91     """
92     Check that all resources referenced by get_resource
93     actually exists in all yaml files
94     """
95     with open(yaml_file) as fh:
96         yml = yaml.safe_load(fh)
97
98     # skip if resources are not defined
99     if "resources" not in yml:
100         pytest.skip("No resources specified in the yaml file")
101
102     resources = yml.get("resources")
103     if resources:
104         resource_ids = resources.keys()
105         referenced_resource_ids = find_all_get_resource_in_yml(yml)
106
107         missing_referenced_resources = set()
108         for referenced_resource_id in referenced_resource_ids:
109             if referenced_resource_id not in resource_ids:
110                 missing_referenced_resources.add(referenced_resource_id)
111
112         assert not missing_referenced_resources, (
113             "Unable to resolve get_resource for the following "
114             "resource IDS: {}. Please ensure the resource ID is defined and "
115             "nested under the resources section of the template".format(
116                 ", ".join(missing_referenced_resources)
117             )
118         )
119
120
121 @pytest.mark.base
122 @validates("R-92635")
123 def test_04_valid_nesting(yaml_file):
124     """
125     Check that the nesting is following the proper format and
126     that all nested files exists and are parsable
127     """
128     invalid_nesting = []
129
130     with open(yaml_file) as fh:
131         yml = yaml.load(fh)
132     if "resources" in yml:
133         try:
134             invalid_nesting.extend(
135                 check_for_invalid_nesting(
136                     yml["resources"], yaml_file, path.dirname(yaml_file)
137                 )
138             )
139         except Exception:
140             invalid_nesting.append(yaml_file)
141
142     assert not invalid_nesting, "invalid nested file detected in file {}\n\n".format(
143         invalid_nesting
144     )
145
146
147 @pytest.mark.base
148 @validates("R-92635")
149 def test_05_all_get_param_have_defined_parameter(yaml_file):
150     """
151     Check that all referenced parameters are actually defined
152     as parameters
153     """
154     invalid_get_params = []
155     with open(yaml_file) as fh:
156         yml = yaml.load(fh)
157
158     resource_params = find_all_get_param_in_yml(yml)
159
160     parameters = set(yml.get("parameters", {}).keys())
161     if not parameters:
162         pytest.skip("no parameters detected")
163
164     for rp in resource_params:
165         if rp not in parameters:
166             invalid_get_params.append(rp)
167
168     assert (
169         not invalid_get_params
170     ), "get_param reference detected without corresponding parameter defined {}".format(
171         invalid_get_params
172     )
173
174
175 @validates("R-90152")
176 @pytest.mark.base
177 def test_06_heat_template_resource_section_has_resources(heat_template):
178
179     found_resource = False
180
181     with open(heat_template) as fh:
182         yml = yaml.load(fh)
183
184     resources = yml.get("resources")
185     if resources:
186         for k1, v1 in yml["resources"].items():
187             if not isinstance(v1, dict):
188                 continue
189
190             found_resource = True
191             break
192
193     assert found_resource, "Heat templates must contain at least one resource"