aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/tests/test_neutron_port_internal_network.py
blob: 00a3a93154ec3b6b67f19a83fdc34d7178e7c128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#

import os.path
import re

from tests.parametrizers import get_nested_files
from tests.utils.network_roles import get_network_type_from_port
from .structures import Heat
from .helpers import validates, load_yaml


RE_BASE = re.compile(r"(^base$)|(^base_)|(_base_)|(_base$)")


def get_base_template_filepath(yaml_files):
    """Return first filepath to match RE_BASE
    """
    for filepath in yaml_files:
        basename, __ = os.path.splitext(os.path.basename(filepath))
        if RE_BASE.search(basename) and basename.find("volume") == -1:
            return filepath
    return None


@validates("R-22688")
def test_neutron_port_internal_network_v2(yaml_files):
    base_path = get_base_template_filepath(yaml_files)
    nested_template_paths = get_nested_files(yaml_files)
    errors = []
    for yaml_file in yaml_files:
        if yaml_file == base_path or yaml_file in nested_template_paths:
            continue  # Only applies to incremental modules
        heat = Heat(filepath=yaml_file)
        internal_ports = {r_id: p for r_id, p in heat.neutron_port_resources.items()
                          if get_network_type_from_port(p) == "internal"}
        for r_id, port in internal_ports.items():
            props = port.get("properties") or {}
            network_value = props.get("network") or {}
            if not isinstance(network_value, dict):
                continue
            if "get_param" not in network_value:
                continue  # Not connecting to network outside the template
            param = network_value.get("get_param")
            base_heat = load_yaml(base_path)
            base_outputs = base_heat.get("outputs") or {}
            if not param.endswith("_net_id"):
                errors.append((
                    "Internal network {} is attached to port {}, but the "
                    "network must be attached via UUID of the network not "
                    "the name (ex: int_{{network-role}}_net_id)."
                ).format(param, r_id))
            if param not in base_outputs:
                errors.append((
                    "Internal network {} is attached to port {}, but network "
                    "is not defined as an output in the base module ({})."
                ).format(param, r_id, base_path))

    assert not errors, " ".join(errors)