12b5768b5076af1d0e02c513a0cbb9f056f5ac8f
[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 from .utils.nested_iterables import is_pseudo_param
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     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 is_pseudo_param(parameter):
99                         continue
100
101                     if k1 != parameter:
102                         invalid_parameters.append(
103                             {
104                                 "resource": r.resource_id,
105                                 "nested parameter": k1,
106                                 "parameter": parameter,
107                                 "file": yaml_file,
108                             }
109                         )
110
111     assert (
112         not invalid_parameters
113     ), "Invalid parameter name change detected in nested template {}".format(
114         invalid_parameters
115     )
116
117
118 @validates("R-708564")
119 def test_parameter_name_doesnt_change_in_nested_template(yaml_file):
120     check_nested_parameter_doesnt_change(yaml_file)