2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 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 (
47 get_network_role_from_port,
48 get_network_type_from_port,
49 property_uses_get_resource,
51 from .utils.vm_types import get_vm_type_for_nova_server
54 @validates("R-20453", "R-26351", "R-26506", "R-681859")
55 def test_port_resource_ids(yaml_file):
57 Check that all resource ids for ports follow the right
58 naming convention to include the {vm_type} of the
59 nova server it is associated to and also contains the
60 {network_role} of the network it is associated with
62 with open(yaml_file) as fh:
65 # skip if resources are not defined
66 if "resources" not in yml:
67 pytest.skip("No resources specified in the heat template")
69 resources = yml["resources"]
72 for k, v in resources.items():
73 if not isinstance(v, dict):
77 if v["type"] not in "OS::Nova::Server":
79 if "properties" not in v:
81 if "networks" not in v["properties"]:
84 vm_type = get_vm_type_for_nova_server(v)
87 vm_type = vm_type.lower()
89 # get all ports associated with the nova server
90 properties = v["properties"]
91 for v2 in properties["networks"]:
92 for k3, v3 in v2.items():
95 if not isinstance(v3, dict):
100 elif "get_resource" in v3:
101 port_id = v3["get_resource"]
102 if not resources[port_id]:
104 port_resource = resources[port_id]
105 port_id = port_id.lower()
109 if property_uses_get_resource(v, "network"):
111 network_role = get_network_role_from_port(port_resource)
114 network_role = network_role.lower()
116 network_type = get_network_type_from_port(port_resource)
119 if network_type == "external":
120 expected_r_id = r"{}_\d+_{}_port_\d+".format(vm_type, network_role)
122 expected_r_id = r"{}_\d+_int_{}_port_\d+".format(
123 vm_type, network_role
125 if not re.match(expected_r_id, port_id):
126 invalid_ports.append(
127 (port_id, "Did not match {}".format(expected_r_id))
130 port_errors = "; ".join(
131 "{} -> {}".format(port, error) for port, error in invalid_ports
133 msg = "The following ports have invalid resource IDs: {}".format(port_errors)
134 msg = msg.replace(r"\d+", "{index}")
135 assert not invalid_ports, msg