[VVP] updating validation scripts in dublin
[vvp/validation-scripts.git] / ice_validator / tests / test_nested_template_parameters.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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 """nested template parameters
42 """
43 import os.path
44
45 import pytest
46
47 from .structures import Heat
48 from .helpers import validates
49 from .utils import nested_files
50
51 VERSION = "1.2.0"
52
53
54 def validate_parms(dirname, basename, nested, nested_props, prop_type):
55     """ensure all parms are props
56     return list of errors.
57     """
58     bad = []
59     for rid, nested_filename in nested.items():
60         nested_filepath = os.path.join(dirname, nested_filename)
61         nested_heat = Heat(filepath=nested_filepath)
62         parms = set(nested_heat.parameters.keys())
63         props = nested_props.get(rid, set())
64         missing = parms - props
65         if missing:
66             bad.append(
67                 "%s parameters %s missing as %s"
68                 " of %s resource %s"
69                 % (nested_filename, list(missing), prop_type, basename, rid)
70             )
71     return bad
72
73
74 @validates("R-11041")
75 def test_nested_template_parameters(heat_template):
76     """
77     All parameters defined in a VNFs Nested YAML file
78     **MUST** be passed in as properties of the resource calling
79     the nested yaml file.
80     """
81     dirname, basename = os.path.split(heat_template)
82     heat = Heat(filepath=heat_template)
83     if not heat.resources:
84         pytest.skip("No resources found")
85     nested_type = nested_files.get_type_nested_files(heat.yml, dirname)
86     nested_resourcegroup = nested_files.get_resourcegroup_nested_files(
87         heat.yml, dirname
88     )
89     if not nested_type and not nested_resourcegroup:
90         pytest.skip("No nested files")
91     bad = []
92     nested_type_props = {
93         rid: set(heat.resources[rid].get("properties", {}).keys())
94         for rid in nested_type
95     }
96     nested_resourcegroup_props = {
97         rid: set(
98             heat.nested_get(
99                 heat.resources[rid],
100                 "properties",
101                 "resource_def",
102                 "properties",
103                 default={},
104             ).keys()
105         )
106         for rid in nested_resourcegroup
107     }
108     bad.extend(
109         validate_parms(dirname, basename, nested_type, nested_type_props, "properties")
110     )
111     bad.extend(
112         validate_parms(
113             dirname,
114             basename,
115             nested_resourcegroup,
116             nested_resourcegroup_props,
117             "resource_def.properties",
118         )
119     )
120     assert not bad, "; ".join(bad)