5c93e77380a07843227b29afe1aa7de0cdb3b5c6
[vvp/validation-scripts.git] / ice_validator / tests / test_nested_parameters.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 """heat parameters
42 """
43 import pytest
44 from tests import cached_yaml as yaml
45 from tests.structures import Resource
46 from .helpers import validates
47
48 VERSION = "1.0.0"
49
50
51 def check_nested_parameter_doesnt_change(yaml_file):
52
53     with open(yaml_file) as fh:
54         yml = yaml.load(fh)
55
56     # skip if resources are not defined
57     if "resources" not in yml:
58         pytest.skip("No resources specified in the heat template")
59
60     invalid_parameters = []
61
62     """
63     checking if property: { get_param: parameter }, then property == parameter
64
65     resource_id:
66         type: nested.yaml
67         properties:
68             property: { get_param: parameter }
69
70     resource_id:
71         type: OS::Heat::ResourceGroup
72         properties:
73             resource_def:
74                 properties:
75                     property: { get_param: parameter }
76     """
77     for resource_id, resource in yml.get("resources", {}).items():
78         resource_type = resource.get("type")
79         if resource_type and (
80             resource_type.endswith("yaml")
81             or resource_type.endswith("yml")
82             or resource_type == "OS::Heat::ResourceGroup"
83         ):
84             # workaround for subinterfaces
85             metadata = resource.get("metadata")
86             if metadata:
87                 subinterface_type = metadata.get("subinterface_type")
88                 if subinterface_type and subinterface_type == "network_collection":
89                     continue
90
91             r = Resource(resource_id=resource_id, resource=resource)
92             properties = r.get_nested_properties()
93             for k1, v1 in properties.items():
94                 if isinstance(v1, dict) and "get_param" in v1:
95                     parameter = v1.get("get_param")
96                     if isinstance(parameter, list):
97                         parameter = parameter[0]
98
99                     if k1 != parameter:
100                         invalid_parameters.append(
101                             {
102                                 "resource": r.resource_id,
103                                 "nested parameter": k1,
104                                 "parameter": parameter,
105                                 "file": yaml_file,
106                             }
107                         )
108
109     assert (
110         not invalid_parameters
111     ), "Invalid parameter name change detected in nested template {}".format(
112         invalid_parameters
113     )
114
115
116 @validates("R-708564")
117 def test_parameter_name_doesnt_change_in_nested_template(yaml_file):
118     check_nested_parameter_doesnt_change(yaml_file)