9d3062adab6879bc4680c06545c7bbaa2777e67c
[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
40 import pytest
41 import re
42
43 from tests import cached_yaml as yaml
44
45 from .helpers import validates
46 from .utils.network_roles import get_network_role_from_port, property_uses_get_resource
47
48 RE_INTERNAL_NETWORK_RID = re.compile(  # match pattern
49     r"int_(?P<network_role>.+)_network$"
50 )
51 NETWORK_RESOURCE_TYPES = ["OS::Neutron::Net", "OS::ContrailV2::VirtualNetwork"]
52
53
54 @validates("R-62983", "R-86182")
55 def test_network_format(yaml_file):
56     """
57     Make sure all network properties use the allowed naming
58     conventions
59     """
60     with open(yaml_file) as fh:
61         yml = yaml.load(fh)
62
63     # skip if resources are not defined
64     if "resources" not in yml:
65         pytest.skip("No resources specified in the heat template")
66
67     invalid_ports = []
68     for k, v in yml["resources"].items():
69         if not isinstance(v, dict):
70             continue
71         if "properties" not in v:
72             continue
73         if property_uses_get_resource(v, "network"):
74             continue
75         if v.get("type") != "OS::Neutron::Port":
76             continue
77         if not get_network_role_from_port(v):
78             invalid_ports.append(k)
79
80     assert not set(invalid_ports), (
81         "Missing 'network' property or improperly "
82         "formatted network parameter name on the "
83         "following OS::Neutron::Ports: "
84         "{}".format(", ".join(invalid_ports))
85     )
86
87
88 @validates("R-16968", "R-35666")
89 def test_network_resource_id_format(yaml_file):
90     """
91     Make sure all network resource ids use the allowed naming
92     convention
93     """
94     RE_INTERNAL_NETWORK_RID = re.compile(  # match pattern
95         r"int_(?P<network_role>.+)_network$"
96     )
97
98     with open(yaml_file) as fh:
99         yml = yaml.load(fh)
100
101     # skip if resources are not defined
102     if "resources" not in yml:
103         pytest.skip("No resources specified in the heat template")
104
105     invalid_networks = []
106     for k, v in yml["resources"].items():
107         if not isinstance(v, dict):
108             continue
109         if "properties" not in v:
110             continue
111         if property_uses_get_resource(v, "network"):
112             continue
113         if v.get("type") not in NETWORK_RESOURCE_TYPES:
114             continue
115         match = RE_INTERNAL_NETWORK_RID.match(k)
116         if not match:
117             invalid_networks.append(k)
118
119     assert not set(invalid_networks), (
120         "Heat templates must only create internal networks "
121         "and follow format int_{{network-role}}_network"
122         "{}".format(", ".join(invalid_networks))
123     )
124
125
126 @validates("R-16241")
127 def test_network_has_subnet(yaml_file):
128     """
129     if creating internal network, make sure there is a
130     corresponding subnet that references it
131     """
132
133     with open(yaml_file) as fh:
134         yml = yaml.load(fh)
135
136     # skip if resources are not defined
137     if "resources" not in yml:
138         pytest.skip("No resources specified in the heat template")
139
140     networks = []
141
142     for k, v in yml["resources"].items():
143         if not isinstance(v, dict):
144             continue
145         if "properties" not in v:
146             continue
147         # need to check if contrail networks also require subnet
148         # and it is defined the same as neutron networks
149         # if v.get("type") not in NETWORK_RESOURCE_TYPES:
150         if v.get("type") not in ["OS::Neutron::Net"]:
151             continue
152         networks.append(k)
153
154     for k, v in yml["resources"].items():
155         if not isinstance(v, dict):
156             continue
157         if "properties" not in v:
158             continue
159         if v.get("type") != "OS::Neutron::Subnet":
160             continue
161         network_prop = v.get("properties", {}).get("network", {}).get("get_resource")
162
163         if not network_prop:
164             continue
165         x = 0
166         for network in networks:
167             if network == network_prop:
168                 networks.pop(x)
169                 break
170             x += 1
171
172     assert not networks, "Networks detected without subnet {}".format(networks)