[VVP] udpating scripts for casablanca 2
[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 © 2018 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
49 from .utils.vm_types import get_vm_types
50
51 from .utils.network_roles import get_network_roles
52
53
54 @validates('R-57282')
55 def test_vm_type_consistent_on_nova_servers(heat_template):
56     '''
57     Make sure all nova servers have properly formatted properties
58     for their name, image and flavor
59     '''
60     with open(heat_template) as fh:
61         yml = yaml.load(fh)
62
63     # skip if resources are not defined
64     if "resources" not in yml:
65         pytest.skip("No resources specified in the heat template")
66
67     invalid_nova_servers = []
68     for k, v in yml["resources"].items():
69         if not isinstance(v, dict):
70             continue
71         if v.get('type') != 'OS::Nova::Server':
72             continue
73         if 'properties' not in v:
74             continue
75
76         vm_types = get_vm_types_for_resource(v)
77         if len(vm_types) != 1:
78             invalid_nova_servers.append(k)
79
80     assert not set(invalid_nova_servers), \
81         "vm_types not consistant on the following resources {}" \
82         .format(invalid_nova_servers)
83
84
85 @validates('R-48067',
86            '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 or nr in vt:
104                 collisions.append({"vm_type": vt, "network_role": nr})
105
106     assert not collisions, \
107         "vm_type and network_role should not be substrings {}" .format(collisions)
108
109
110 @validates('R-50436',
111            'R-45188',
112            'R-40499')
113 def test_nova_server_flavor_parameter(yaml_file):
114
115     prop = "flavor"
116     check_nova_parameter_format(prop, yaml_file)
117
118
119 @validates('R-51430',
120            'R-54171',
121            'R-87817')
122 def test_nova_server_name_parameter(yaml_file):
123
124     prop = "name"
125     check_nova_parameter_format(prop, yaml_file)
126
127
128 @validates('R-71152',
129            'R-45188',
130            'R-57282')
131 def test_nova_server_image_parameter(yaml_file):
132
133     prop = "image"
134     check_nova_parameter_format(prop, yaml_file)
135
136
137 def check_nova_parameter_format(prop, yaml_file):
138
139     formats = {
140         "string": {
141             "name": re.compile(r'(.+?)_name_\d+$'),
142             "flavor": re.compile(r'(.+?)_flavor_name$'),
143             "image": re.compile(r'(.+?)_image_name$')
144         },
145         "comma_delimited_list": {
146             "name": re.compile(r'(.+?)_names$')
147         }
148     }
149
150     with open(yaml_file) as fh:
151         yml = yaml.load(fh)
152
153     # skip if resources are not defined
154     if "resources" not in yml:
155         pytest.skip("No resources specified in the heat template")
156
157     # skip if resources are not defined
158     if "parameters" not in yml:
159         pytest.skip("No parameters specified in the heat template")
160
161     invalid_parameters = []
162
163     for k, v in yml["resources"].items():
164         if not isinstance(v, dict):
165             continue
166         if v.get('type') != 'OS::Nova::Server':
167             continue
168
169         prop_param = v.get("properties", {}) \
170                       .get(prop, {}) \
171                       .get("get_param")
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", {}) \
179                                  .get(prop_param, {}) \
180                                  .get("type")
181
182         if not template_param_type:
183             pytest.skip("could not determine param type for {}".format(prop_param))
184
185         format_match = formats.get(template_param_type, {}) \
186                               .get(prop)
187
188         if not format_match or not format_match.match(prop_param):
189             invalid_parameters.append(prop_param)
190
191     assert not set(invalid_parameters), \
192         "invalid {} parameters detected {}" \
193         .format(prop, invalid_parameters)