98fcd3785b1cb2aa3f244270df00ffde14b63824
[vvp/validation-scripts.git] / ice_validator / tests / test_network_format.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 from itertools import chain
40
41 import pytest
42 import re
43
44 from tests import cached_yaml as yaml
45 from tests.structures import Heat
46
47 from tests.helpers import validates
48
49 RE_INTERNAL_NETWORK_RID = re.compile(r"int_(?P<network_role>.+)_network$")
50 NETWORK_RESOURCE_TYPES = ["OS::Neutron::Net", "OS::ContrailV2::VirtualNetwork"]
51
52
53 @validates("R-16968")
54 def test_network_resource_id_format(yaml_file):
55     heat = Heat(yaml_file)
56     network_ids = chain.from_iterable(
57         heat.get_resource_by_type(t) for t in NETWORK_RESOURCE_TYPES
58     )
59     invalid_networks = {
60         r_id for r_id in network_ids if not RE_INTERNAL_NETWORK_RID.match(r_id)
61     }
62     assert not invalid_networks, (
63         "Heat templates must only create internal networks "
64         "and their resource IDs must follow the format "
65         "int_{{network-role}}_network. The following network's resource IDs "
66         "have invalid resource ID formats: "
67         "{}".format(", ".join(invalid_networks))
68     )
69
70
71 @validates("R-16241")
72 def test_network_has_subnet(yaml_file):
73     """
74     if creating internal network, make sure there is a
75     corresponding subnet that references it
76     """
77
78     with open(yaml_file) as fh:
79         yml = yaml.load(fh)
80
81     # skip if resources are not defined
82     if "resources" not in yml:
83         pytest.skip("No resources specified in the heat template")
84
85     networks = []
86
87     for k, v in yml["resources"].items():
88         if not has_properties(v) or v.get("type") not in ["OS::Neutron::Net"]:
89             continue
90         # need to check if contrail networks also require subnet
91         # and it is defined the same as neutron networks
92         # if v.get("type") not in NETWORK_RESOURCE_TYPES:
93         networks.append(k)
94
95     for k, v in yml["resources"].items():
96         network_prop = v.get("properties", {}).get("network", {}).get("get_resource")
97         if (
98             not has_properties(v)
99             and v.get("type") != "OS::Neutron::Subnet"
100             and not network_prop
101         ):
102             continue
103         x = 0
104         for network in networks:
105             if network == network_prop:
106                 networks.pop(x)
107                 break
108             x += 1
109
110     assert not networks, "Networks detected without subnet {}".format(networks)
111
112
113 def has_properties(resource):
114     """
115     checks resource is a Neutron Subnet
116     """
117     return isinstance(resource, dict) and "properties" in resource