2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
41 resource property name
46 from .structures import Heat
47 from .structures import HeatProcessor
48 from .helpers import validates
49 from tests.utils import nested_files
54 def get_non_servers(heat):
56 Return a dict of non servers. key is rid, value is resource
60 for rid, resource in heat.resources.items()
61 if heat.nested_get(resource, "type") != "OS::Nova::Server"
67 def test_non_server_name(yaml_file):
69 If a VNF's Heat Orchestration Template contains the property ``name``
70 for a non ``OS::Nova::Server`` resource, the intrinsic function
71 ``str_replace`` **MUST** be used in conjunction with the ONAP
72 supplied metadata parameter ``vnf_name`` to generate a unique value.
75 h = Heat(filepath=yaml_file)
76 non_servers = get_non_servers(h)
79 for rid, resource in non_servers.items():
80 name = h.nested_get(resource, "properties", "name")
84 # Make sure it uses str_replace
85 str_replace = name.get("str_replace") if hasattr(name, "get") else None
87 bad.append("{}'s name property does not use str_replace".format(rid))
90 # Make sure str_replace is properly formatted
91 if not all(key in str_replace for key in ("template", "params")):
94 "{}'s name property use of str_replace is "
95 + "missing template, params, or both"
99 params = str_replace["params"]
100 if not isinstance(params, dict):
103 "{}'s name property's use of str_replace.params is "
104 + "missing or invalid"
109 # Find the param that uses vnf_name
110 vnf_name_param = None
111 for key, value in params.items():
112 if not isinstance(value, dict):
114 if value.get("get_param", "") == "vnf_name":
117 if not vnf_name_param:
120 "{}'s name property's use str_replace does not "
121 + "use have a params that maps to the parameter "
122 "via {{get_param: vnf_name}}"
127 # make sure the VNF name is used in the template string
128 template = str_replace.get("template") or ""
129 if vnf_name_param not in template:
132 "{}'s name property's str_replace template does "
133 + "not incorporate vnf_name; expected {} in "
135 ).format(rid, vnf_name_param, template)
138 "Improper name property for" " non-OS::Nova::Server resources. "
144 @validates("R-85734")
145 def test_non_server_name_unique(heat_template):
146 """Test name has unique value
148 list_nest = nested_files.get_list_of_nested_files(
149 heat_template, os.path.dirname(heat_template)
151 list_nest.append(heat_template)
153 for yaml_file in list_nest:
154 h = Heat(filepath=yaml_file)
155 non_servers.update(get_non_servers(h))
156 names = collections.defaultdict(set)
157 for rid, resource in non_servers.items():
158 name = HeatProcessor.get_str_replace_name(resource)
161 bad = {key: value for key, value in names.items() if len(value) > 1}
162 delim = "\n" + 4 * " "
163 assert not bad, "Names must be unique," " not shared across resource ids.%s%s" % (
165 delim.join("%s: %s" % (name, list(value)) for name, value in bad.items()),