Merge "[VVP] Support pluggable data sources for preload data"
[vvp/validation-scripts.git] / ice_validator / tests / test_neutron_port_fixed_ips_subnet.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 import re
40
41
42 from tests.utils.network_roles import get_network_type_from_port
43
44 from tests.structures import Heat
45 from tests.helpers import validates, load_yaml, get_base_template_from_yaml_files, get_param
46 from tests.utils.nested_files import get_nested_files
47 from .utils.ports import check_parameter_format
48 from tests.structures import NeutronPortProcessor
49
50 RE_EXTERNAL_PARAM_SUBNET = re.compile(  # match pattern
51     r"(?P<network_role>.+?)(_v6)?_subnet_id$"
52 )
53
54 RE_INTERNAL_PARAM_SUBNET = re.compile(  # match pattern
55     r"int_(?P<network_role>.+?)(_v6)?_subnet_id$"
56 )
57
58 fip_regx_dict = {
59     "external": {
60         "string": {
61             "readable": "{network-role}_subnet_id or {network-role}_v6_subnet_id",
62             "machine": RE_EXTERNAL_PARAM_SUBNET,
63         }
64     },
65     "internal": {
66         "string": {
67             "readable": "int_{network-role}_subnet_id or int_{network-role}_v6_subnet_id",
68             "machine": RE_INTERNAL_PARAM_SUBNET,
69         }
70     },
71     "parameter_to_resource_comparisons": ["network_role"],
72 }
73
74
75 @validates("R-38236", "R-84123", "R-76160")
76 def test_internal_subnet_format(yaml_file):
77     check_parameter_format(
78         yaml_file,
79         fip_regx_dict,
80         "internal",
81         NeutronPortProcessor,
82         "fixed_ips",
83         "subnet",
84     )
85
86
87 @validates("R-38236", "R-62802", "R-15287")
88 def test_external_subnet_format(yaml_file):
89     check_parameter_format(
90         yaml_file,
91         fip_regx_dict,
92         "external",
93         NeutronPortProcessor,
94         "fixed_ips",
95         "subnet",
96     )
97
98
99 @validates("R-84123", "R-76160")
100 def test_neutron_port_internal_fixed_ips_subnet_in_base(yaml_files):
101     base_path = get_base_template_from_yaml_files(yaml_files)
102     base_heat = load_yaml(base_path)
103     base_outputs = base_heat.get("outputs") or {}
104     nested_template_paths = get_nested_files(yaml_files)
105     errors = []
106
107     for yaml_file in yaml_files:
108         if yaml_file == base_path or yaml_file in nested_template_paths:
109             continue  # Only applies to incremental modules
110         heat = Heat(filepath=yaml_file)
111         internal_ports = {
112             r_id: p
113             for r_id, p in heat.neutron_port_resources.items()
114             if get_network_type_from_port(p) == "internal"
115         }
116         for r_id, port in internal_ports.items():
117             props = port.get("properties") or {}
118             fip_list = props.get("fixed_ips") or []
119             if not isinstance(fip_list, list):
120                 continue
121             for ip in fip_list:
122                 subnet = ip.get("subnet")
123                 if not subnet:
124                     continue
125
126                 if "get_param" not in subnet:
127                     continue
128                 param = get_param(subnet)
129                 if param not in base_outputs:
130                     errors.append(
131                         (
132                             "Internal fixed_ips/subnet parameter {} is attached to "
133                             "port {}, but the subnet parameter "
134                             "is not defined as an output in the base module ({})."
135                         ).format(param, r_id, base_path)
136                     )
137
138     assert not errors, " ".join(errors)