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