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