c72080cd017d06adb2629e8b155b1bd9dea86212
[vvp/validation-scripts.git] / ice_validator / tests / test_server_parameters.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 #
39 """test
40 """
41 import pytest
42 from tests import cached_yaml as yaml
43
44 from .helpers import validates
45
46 VERSION = "1.1.0"
47
48
49 def check_parameter_type(heat_template, parameter, parameter_type):
50     """
51     Make sure these OS::Nova::Server parameters are defined w/
52     the correct type
53     """
54
55     with open(heat_template) as fh:
56         yml = yaml.load(fh)
57
58     # skip if resources are not defined
59     if "parameters" not in yml:
60         pytest.skip("No parameters specified in the heat template")
61
62     invalid_parameters = []
63
64     for k1, v1 in yml["parameters"].items():
65         if not isinstance(v1, dict):
66             continue
67         if "type" not in v1:
68             continue
69
70         if k1.find(parameter) == -1:
71             continue
72
73         param_type = v1.get("type")
74
75         if not param_type:
76             continue
77
78         if param_type != parameter_type:
79             invalid_parameters.append(k1)
80
81     assert (
82         not invalid_parameters
83     ), "{} parameters must be defined as type {}: {}".format(
84         parameter, parameter_type, invalid_parameters
85     )
86
87
88 def check_server_parameter_name(heat_template, parameter, parameter_name):
89     """
90     Check each OS::Nova::Server metadata property
91     uses the same parameter name w/ get_param
92     """
93
94     with open(heat_template) as fh:
95         yml = yaml.load(fh)
96
97     # skip if resources are not defined
98     if "resources" not in yml:
99         pytest.skip("No resources specified in the heat template")
100
101     invalid_parameters = []
102
103     for k1, v1 in yml["resources"].items():
104         if not isinstance(v1, dict):
105             continue
106         if "type" not in v1:
107             continue
108
109         if v1["type"] != "OS::Nova::Server":
110             continue
111
112         metadata = v1.get("properties", {}).get("metadata", {}).get(parameter)
113
114         if not metadata or not isinstance(metadata, dict):
115             continue
116
117         get_param = metadata.get("get_param")
118
119         if not get_param:
120             continue
121
122         if get_param != parameter_name:
123             invalid_parameters.append(
124                 {
125                     "resource": k1,
126                     "metadata property": parameter_name,
127                     "get_param": get_param,
128                 }
129             )
130
131     assert not invalid_parameters, (
132         "metadata property {} must use get_param and "
133         "the parameter name must be {}: {}".format(
134             parameter, parameter_name, invalid_parameters
135         )
136     )
137
138
139 @validates("R-23311")
140 def test_availability_zone_parameter_type(yaml_file):
141     check_parameter_type(yaml_file, "availability_zone_", "string")
142
143
144 @validates("R-07507")
145 def test_vnf_id_parameter_type_and_parameter_name(yaml_file):
146     check_parameter_type(yaml_file, "vnf_id", "string")
147     check_server_parameter_name(yaml_file, "vnf_id", "vnf_id")
148
149
150 @validates("R-82134")
151 def test_vf_module_id_parameter_type_and_parameter_name(yaml_file):
152     check_parameter_type(yaml_file, "vf_module_id", "string")
153     check_server_parameter_name(yaml_file, "vf_module_id", "vf_module_id")
154
155
156 @validates("R-62428")
157 def test_vnf_name_parameter_type_and_parameter_name(yaml_file):
158     check_parameter_type(yaml_file, "vnf_name", "string")
159     check_server_parameter_name(yaml_file, "vnf_name", "vnf_name")
160
161
162 @validates("R-39067")
163 def test_vf_module_name_parameter_type_and_parameter_name(yaml_file):
164     check_parameter_type(yaml_file, "vf_module_name", "string")
165     check_server_parameter_name(yaml_file, "vf_module_name", "vf_module_name")
166
167
168 @validates("R-54340")
169 def test_vf_module_index_parameter_type_and_parameter_name(yaml_file):
170     check_parameter_type(yaml_file, "vf_module_index", "number")
171     check_server_parameter_name(yaml_file, "vf_module_index", "vf_module_index")