[VVP] updating validation scripts in dublin
[vvp/validation-scripts.git] / ice_validator / tests / test_nova_servers_vm_types.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 import pytest
42 import re
43
44 from tests import cached_yaml as yaml
45
46 from .helpers import validates
47
48 from .utils.vm_types import get_vm_types_for_resource, get_vm_types
49
50 from .utils.network_roles import get_network_roles
51
52
53 @validates("R-57282")
54 def test_vm_type_consistent_on_nova_servers(heat_template):
55     """
56     Make sure all nova servers have properly formatted properties
57     for their name, image and flavor
58     """
59     with open(heat_template) as fh:
60         yml = yaml.load(fh)
61
62     # skip if resources are not defined
63     if "resources" not in yml:
64         pytest.skip("No resources specified in the heat template")
65
66     invalid_nova_servers = []
67     for k, v in yml["resources"].items():
68         if not isinstance(v, dict):
69             continue
70         if v.get("type") != "OS::Nova::Server":
71             continue
72         if "properties" not in v:
73             continue
74
75         vm_types = get_vm_types_for_resource(v)
76         if len(vm_types) != 1:
77             invalid_nova_servers.append(k)
78
79     assert not set(
80         invalid_nova_servers
81     ), "vm_types not consistant on the following resources: {}".format(
82         ",".join(invalid_nova_servers)
83     )
84
85
86 @validates("R-48067", "R-00977")
87 def test_vm_type_network_role_collision(yaml_file):
88     with open(yaml_file) as fh:
89         yml = yaml.load(fh)
90
91     # skip if resources are not defined
92     if "resources" not in yml:
93         pytest.skip("No resources specified in the heat template")
94
95     resources = yml["resources"]
96
97     vm_types = get_vm_types(resources)
98     network_roles = get_network_roles(resources)
99
100     collisions = []
101     for nr in network_roles:
102         for vt in vm_types:
103             if vt in nr:
104                 collisions.append(
105                     (
106                         "vm_type ({}) cannot be a substring " "of network_role ({})"
107                     ).format(vt, nr)
108                 )
109             elif nr in vt:
110                 collisions.append(
111                     (
112                         "network_role ({}) cannot be a substring " "of vm_type ({})"
113                     ).format(nr, vt)
114                 )
115
116     assert not collisions, ", ".join(collisions)
117
118
119 @validates("R-50436", "R-45188", "R-40499")
120 def test_nova_server_flavor_parameter(yaml_file):
121
122     prop = "flavor"
123     check_nova_parameter_format(prop, yaml_file)
124
125
126 @validates("R-51430", "R-54171", "R-87817")
127 def test_nova_server_name_parameter(yaml_file):
128
129     prop = "name"
130     check_nova_parameter_format(prop, yaml_file)
131
132
133 @validates("R-71152", "R-57282", "R-58670")
134 def test_nova_server_image_parameter(yaml_file):
135
136     prop = "image"
137     check_nova_parameter_format(prop, yaml_file)
138
139
140 def check_nova_parameter_format(prop, yaml_file):
141
142     formats = {
143         "string": {
144             "name": re.compile(r"(.+?)_name_\d+$"),
145             "flavor": re.compile(r"(.+?)_flavor_name$"),
146             "image": re.compile(r"(.+?)_image_name$"),
147         },
148         "comma_delimited_list": {"name": re.compile(r"(.+?)_names$")},
149     }
150
151     with open(yaml_file) as fh:
152         yml = yaml.load(fh)
153
154     # skip if resources are not defined
155     if "resources" not in yml:
156         pytest.skip("No resources specified in the heat template")
157
158     # skip if resources are not defined
159     if "parameters" not in yml:
160         pytest.skip("No parameters specified in the heat template")
161
162     invalid_parameters = []
163
164     for k, v in yml["resources"].items():
165         if not isinstance(v, dict):
166             continue
167         if v.get("type") != "OS::Nova::Server":
168             continue
169
170         prop_val = v.get("properties", {}).get(prop, {})
171         prop_param = prop_val.get("get_param", "") if isinstance(prop_val, dict) else ""
172
173         if not prop_param:
174             pytest.skip("{} doesn't have property {}".format(k, prop))
175         elif isinstance(prop_param, list):
176             prop_param = prop_param[0]
177
178         template_param_type = yml.get("parameters", {}).get(prop_param, {}).get("type")
179
180         if not template_param_type:
181             pytest.skip("could not determine param type for {}".format(prop_param))
182
183         format_match = formats.get(template_param_type, {}).get(prop)
184
185         if not format_match or not format_match.match(prop_param):
186             msg = (
187                 "Invalid parameter format ({}) on Resource ID ({}) property" " ({})"
188             ).format(prop_param, k, prop)
189             invalid_parameters.append(msg)
190
191     assert not set(invalid_parameters), ", ".join(invalid_parameters)