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