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============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
42 resource property name
49 from .structures import Heat
50 from .structures import HeatProcessor
51 from .helpers import validates
56 def get_non_servers(heat):
58 Return a dict of non servers. key is rid, value is resource
62 for rid, resource in heat.resources.items()
63 if heat.nested_get(resource, "type") != "OS::Nova::Server"
69 def test_non_server_name(heat_template):
71 If a VNF's Heat Orchestration Template contains the property ``name``
72 for a non ``OS::Nova::Server`` resource, the intrinsic function
73 ``str_replace`` **MUST** be used in conjunction with the ONAP
74 supplied metadata parameter ``vnf_name`` to generate a unique value.
77 h = Heat(filepath=heat_template)
79 pytest.skip("No resources in this template")
81 non_servers = get_non_servers(h)
83 pytest.skip("No non-server resources in this template")
86 for rid, resource in non_servers.items():
87 name = h.nested_get(resource, "properties", "name")
91 # Make sure it uses str_replace
92 str_replace = name.get("str_replace") if hasattr(name, "get") else None
94 bad.append("{}'s name property does not use str_replace".format(rid))
97 # Make sure str_replace is properly formatted
98 if not all(key in str_replace for key in ("template", "params")):
101 "{}'s name property use of str_replace is "
102 + "missing template, params, or both"
106 params = str_replace["params"]
107 if not isinstance(params, dict):
110 "{}'s name property's use of str_replace.params is "
111 + "missing or invalid"
116 # Find the param that uses vnf_name
117 vnf_name_param = None
118 for key, value in params.items():
119 if not isinstance(value, dict):
121 if value.get("get_param", "") == "vnf_name":
124 if not vnf_name_param:
127 "{}'s name property's use str_replace does not "
128 + "use have a params that maps to the parameter "
129 "via {{get_param: vnf_name}}"
134 # make sure the VNF name is used in the template string
135 template = str_replace.get("template") or ""
136 if vnf_name_param not in template:
139 "{}'s name property's str_replace template does "
140 + "not incorporate vnf_name; expected {} in "
142 ).format(rid, vnf_name_param, template)
145 "Improper name property for" " non-OS::Nova::Server resources. "
151 @validates("R-85734")
152 def test_non_server_name_unique(yaml_files):
153 """Test name has unique value
156 for yaml_file in yaml_files:
157 h = Heat(filepath=yaml_file)
158 non_servers.update(get_non_servers(h))
159 names = collections.defaultdict(set)
160 for rid, resource in non_servers.items():
161 name = HeatProcessor.get_str_replace_name(resource)
164 bad = {key: value for key, value in names.items() if len(value) > 1}
165 delim = "\n" + 4 * " "
166 assert not bad, "Names must be unique," " not shared across resource ids.%s%s" % (
168 delim.join("%s: %s" % (name, list(value)) for name, value in bad.items()),