[VVP] Additional check for RG params
[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"
76                     % (rid, list(additional), nested_filepath)
77                 )
78     return bad
79
80
81 @validates("R-11041")
82 def test_nested_template_parameters(yaml_file):
83     """
84     All parameters defined in a VNFs Nested YAML file
85     **MUST** be passed in as properties of the resource calling
86     the nested yaml file.
87     """
88     dirname, basename = os.path.split(yaml_file)
89     heat = Heat(filepath=yaml_file)
90     if not heat.resources:
91         pytest.skip("No resources found")
92     nested_type = nested_files.get_type_nested_files(heat.yml, dirname)
93     nested_resourcegroup = nested_files.get_resourcegroup_nested_files(
94         heat.yml, dirname
95     )
96     if not nested_type and not nested_resourcegroup:
97         pytest.skip("No nested files")
98     bad = []
99     nested_type_props = {
100         rid: set(heat.resources[rid].get("properties", {}).keys())
101         for rid in nested_type
102     }
103     nested_resourcegroup_props = {
104         rid: set(
105             heat.nested_get(
106                 heat.resources[rid],
107                 "properties",
108                 "resource_def",
109                 "properties",
110                 default={},
111             ).keys()
112         )
113         for rid in nested_resourcegroup
114     }
115     bad.extend(
116         validate_parms(dirname, basename, nested_type, nested_type_props, "properties")
117     )
118     bad.extend(
119         validate_parms(
120             dirname,
121             basename,
122             nested_resourcegroup,
123             nested_resourcegroup_props,
124             "resource_def.properties",
125         )
126     )
127     assert not bad, "; ".join(bad)