[VVP] updating validation scripts in dublin
[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 © 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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 """
42 resource property name
43 """
44
45 import pytest
46
47 from .structures import Heat
48 from .helpers import validates
49
50 VERSION = "1.1.0"
51
52
53 def get_non_servers(heat):
54     """
55     Return a dict of non servers.  key is rid, value is resource
56     """
57     non_servers = {
58         rid: resource
59         for rid, resource in heat.resources.items()
60         if heat.nested_get(resource, "type") != "OS::Nova::Server"
61     }
62     return non_servers
63
64
65 @validates("R-85734")
66 def test_non_server_name(heat_template):
67     """
68     If a VNF's Heat Orchestration Template contains the property ``name``
69     for a non ``OS::Nova::Server`` resource, the intrinsic function
70     ``str_replace`` **MUST** be used in conjunction with the ECOMP
71     supplied metadata parameter ``vnf_name`` to generate a unique value.
72
73     """
74     h = Heat(filepath=heat_template)
75     if not h.resources:
76         pytest.skip("No resources in this template")
77
78     non_servers = get_non_servers(h)
79     if not non_servers:
80         pytest.skip("No non-server resources in this template")
81
82     bad = []
83     for rid, resource in non_servers.items():
84         name = h.nested_get(resource, "properties", "name")
85         if not name:
86             continue
87
88         # Make sure it uses str_replace
89         str_replace = name.get("str_replace") if hasattr(name, "get") else None
90         if not str_replace:
91             bad.append("{}'s name property does not use str_replace".format(rid))
92             continue
93
94         # Make sure str_replace is properly formatted
95         if not all(key in str_replace for key in ("template", "params")):
96             bad.append(
97                 (
98                     "{}'s name property use of str_replace is "
99                     + "missing template, params, or both"
100                 ).format(rid)
101             )
102             continue
103         params = str_replace["params"]
104         if not isinstance(params, dict):
105             bad.append(
106                 (
107                     "{}'s name property's use of str_replace.params is "
108                     + "missing or invalid"
109                 ).format(rid)
110             )
111             continue
112
113         # Find the param that uses vnf_name
114         vnf_name_param = None
115         for key, value in params.items():
116             if not isinstance(value, dict):
117                 continue
118             if value.get("get_param", "") == "vnf_name":
119                 vnf_name_param = key
120                 break
121         if not vnf_name_param:
122             bad.append(
123                 (
124                     "{}'s name property's use str_replace does not "
125                     + "use have a params that maps to the parameter "
126                     "via {{get_param: vnf_name}}"
127                 ).format(rid)
128             )
129             continue
130
131         # make sure the VNF name is used in the template string
132         template = str_replace.get("template") or ""
133         if vnf_name_param not in template:
134             bad.append(
135                 (
136                     "{}'s name property's str_replace template does "
137                     + "not incorporate vnf_name; expected {} in "
138                     + "template ({})"
139                 ).format(rid, vnf_name_param, template)
140             )
141     msg = "Improper name property for non-OS::Nova::Server resources. " + ". ".join(bad)
142
143     assert not bad, msg