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