2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
43 from tests import cached_yaml as yaml
45 from .helpers import validates
46 from .utils.network_roles import property_uses_get_resource
48 RE_INTERNAL_NETWORK_RID = re.compile( # match pattern
49 r"int_(?P<network_role>.+)_network$"
51 NETWORK_RESOURCE_TYPES = ["OS::Neutron::Net", "OS::ContrailV2::VirtualNetwork"]
54 @validates("R-16968", "R-35666")
55 def test_network_resource_id_format(yaml_file):
57 Make sure all network resource ids use the allowed naming
60 RE_INTERNAL_NETWORK_RID = re.compile( # match pattern
61 r"int_(?P<network_role>.+)_network$"
64 with open(yaml_file) as fh:
67 # skip if resources are not defined
68 if "resources" not in yml:
69 pytest.skip("No resources specified in the heat template")
72 for k, v in yml["resources"].items():
73 if not has_properties(v):
75 if property_uses_get_resource(v, "network"):
77 if v.get("type") not in NETWORK_RESOURCE_TYPES:
79 match = RE_INTERNAL_NETWORK_RID.match(k)
81 invalid_networks.append(k)
83 assert not set(invalid_networks), (
84 "Heat templates must only create internal networks "
85 "and follow format int_{{network-role}}_network"
86 "{}".format(", ".join(invalid_networks))
91 def test_network_has_subnet(yaml_file):
93 if creating internal network, make sure there is a
94 corresponding subnet that references it
97 with open(yaml_file) as fh:
100 # skip if resources are not defined
101 if "resources" not in yml:
102 pytest.skip("No resources specified in the heat template")
106 for k, v in yml["resources"].items():
107 if not has_properties(v) or v.get("type") not in ["OS::Neutron::Net"]:
109 # need to check if contrail networks also require subnet
110 # and it is defined the same as neutron networks
111 # if v.get("type") not in NETWORK_RESOURCE_TYPES:
114 for k, v in yml["resources"].items():
115 if not has_properties(v) and v.get("type") != "OS::Neutron::Subnet":
117 network_prop = v.get("properties", {}).get("network", {}).get("get_resource")
122 for network in networks:
123 if network == network_prop:
128 assert not networks, "Networks detected without subnet {}".format(networks)
131 def has_properties(resource):
133 checks resource is a Neutron Subnet
135 return isinstance(resource, dict) and "properties" in resource