7b67682f5043d6bd0a6e1722caf38742d3fce6d6
[vvp/validation-scripts.git] / ice_validator / tests / test_nova_servers_correct_parameter_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 yaml
43 import re
44
45
46 def test_nova_servers_correct_parameter_types(heat_template):
47     '''
48     Make sure all nova servers have properly assigned types for the parameters
49     used for their name, image and flavor
50     '''
51     key_values = ["name", "flavor", "image"]
52     key_value_formats = [
53                         ["name", "string",
54                             re.compile(r'(.+?)_name_\d+')],
55                         ["name", "comma_delimited_list",
56                             re.compile(r'(.+?)_names')],
57                         ["flavor", "string",
58                             re.compile(r'(.+?)_flavor_name')],
59                         ["image", "string",
60                             re.compile(r'(.+?)_image_name')],
61                         ]
62
63     with open(heat_template) as fh:
64         yml = yaml.load(fh)
65
66     # skip if parameters are not defined
67     if "parameters" not in yml:
68         pytest.skip("No parameters specified in the heat template")
69
70     # skip if resources are not defined
71     if "resources" not in yml:
72         pytest.skip("No resources specified in the heat template")
73
74     parameters = yml["parameters"]
75
76     invalid_nova_servers = []
77
78     for k1, v1 in yml["resources"].items():
79         if not isinstance(v1, dict):
80             continue
81         if "properties" not in v1:
82             continue
83         if v1.get("type") != "OS::Nova::Server":
84             continue
85
86         valid_nova_server = True
87         for k2, v2 in v1["properties"].items():
88             if k2 not in key_values:
89                 continue
90             formats = [v for v in key_value_formats if v[0] == k2]
91             for v3 in formats:
92                 if "get_param" not in v2:
93                     continue
94
95                 param = v2["get_param"]
96                 if isinstance(param, list):
97                     param = param[0]
98
99                 m = v3[2].match(param)
100                 if m and m.group(1):
101                     if parameters[param]:
102                         param_spec = parameters[param]
103                         if not param_spec["type"]:
104                             valid_nova_server = False
105                         elif param_spec["type"] != v3[1]:
106                             valid_nova_server = False
107
108         if not valid_nova_server:
109             invalid_nova_servers.append(k1)
110
111     assert not set(invalid_nova_servers)