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