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