[VVP] Fixing internal network check
[vvp/validation-scripts.git] / ice_validator / tests / test_neutron_port_internal_network.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 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 os.path
40 import re
41
42 from tests.parametrizers import get_nested_files
43 from tests.utils.network_roles import get_network_type_from_port
44 from .structures import Heat
45 from .helpers import validates, load_yaml
46
47
48 RE_BASE = re.compile(r"(^base$)|(^base_)|(_base_)|(_base$)")
49
50
51 def get_base_template_filepath(yaml_files):
52     """Return first filepath to match RE_BASE
53     """
54     for filepath in yaml_files:
55         basename, __ = os.path.splitext(os.path.basename(filepath))
56         if RE_BASE.search(basename) and basename.find("volume") == -1:
57             return filepath
58     return None
59
60
61 @validates("R-22688")
62 def test_neutron_port_internal_network_v2(yaml_files):
63     base_path = get_base_template_filepath(yaml_files)
64     nested_template_paths = get_nested_files(yaml_files)
65     errors = []
66     for yaml_file in yaml_files:
67         if yaml_file == base_path or yaml_file in nested_template_paths:
68             continue  # Only applies to incremental modules
69         heat = Heat(filepath=yaml_file)
70         internal_ports = {r_id: p for r_id, p in heat.neutron_port_resources.items()
71                           if get_network_type_from_port(p) == "internal"}
72         for r_id, port in internal_ports.items():
73             props = port.get("properties") or {}
74             network_value = props.get("network") or {}
75             if not isinstance(network_value, dict):
76                 continue
77             if "get_param" not in network_value:
78                 continue  # Not connecting to network outside the template
79             param = network_value.get("get_param")
80             base_heat = load_yaml(base_path)
81             base_outputs = base_heat.get("outputs") or {}
82             if not param.endswith("_net_id"):
83                 errors.append((
84                     "Internal network {} is attached to port {}, but the "
85                     "network must be attached via UUID of the network not "
86                     "the name (ex: int_{{network-role}}_net_id)."
87                 ).format(param, r_id))
88             if param not in base_outputs:
89                 errors.append((
90                     "Internal network {} is attached to port {}, but network "
91                     "is not defined as an output in the base module ({})."
92                 ).format(param, r_id, base_path))
93
94     assert not errors, " ".join(errors)