Remove unnecessary check for pytest.skip
[vvp/validation-scripts.git] / ice_validator / tests / test_non_server_name.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 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 """
41 resource property name
42 """
43 import os
44 import collections
45
46 from .structures import Heat
47 from .structures import HeatProcessor
48 from .helpers import validates
49 from tests.utils import nested_files
50
51 VERSION = "1.2.0"
52
53
54 def get_non_servers(heat):
55     """
56     Return a dict of non servers.  key is rid, value is resource
57     """
58     non_servers = {
59         rid: resource
60         for rid, resource in heat.resources.items()
61         if heat.nested_get(resource, "type") != "OS::Nova::Server"
62     }
63     return non_servers
64
65
66 @validates("R-85734")
67 def test_non_server_name(yaml_file):
68     """
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.
73
74     """
75     h = Heat(filepath=yaml_file)
76     non_servers = get_non_servers(h)
77
78     bad = []
79     for rid, resource in non_servers.items():
80         name = h.nested_get(resource, "properties", "name")
81         if not name:
82             continue
83
84         # Make sure it uses str_replace
85         str_replace = name.get("str_replace") if hasattr(name, "get") else None
86         if not str_replace:
87             bad.append("{}'s name property does not use str_replace".format(rid))
88             continue
89
90         # Make sure str_replace is properly formatted
91         if not all(key in str_replace for key in ("template", "params")):
92             bad.append(
93                 (
94                     "{}'s name property use of str_replace is "
95                     + "missing template, params, or both"
96                 ).format(rid)
97             )
98             continue
99         params = str_replace["params"]
100         if not isinstance(params, dict):
101             bad.append(
102                 (
103                     "{}'s name property's use of str_replace.params is "
104                     + "missing or invalid"
105                 ).format(rid)
106             )
107             continue
108
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):
113                 continue
114             if value.get("get_param", "") == "vnf_name":
115                 vnf_name_param = key
116                 break
117         if not vnf_name_param:
118             bad.append(
119                 (
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}}"
123                 ).format(rid)
124             )
125             continue
126
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:
130             bad.append(
131                 (
132                     "{}'s name property's str_replace template does "
133                     + "not incorporate vnf_name; expected {} in "
134                     + "template ({})"
135                 ).format(rid, vnf_name_param, template)
136             )
137     msg = (
138         "Improper name property for" " non-OS::Nova::Server resources. "
139     ) + ". ".join(bad)
140
141     assert not bad, msg
142
143
144 @validates("R-85734")
145 def test_non_server_name_unique(heat_template):
146     """Test name has unique value
147     """
148     list_nest = nested_files.get_list_of_nested_files(
149         heat_template, os.path.dirname(heat_template)
150     )
151     list_nest.append(heat_template)
152     non_servers = {}
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)
159         if name:
160             names[name].add(rid)
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" % (
164         delim,
165         delim.join("%s: %s" % (name, list(value)) for name, value in bad.items()),
166     )