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