[VVP] updating validation scripts in dublin
[vvp/validation-scripts.git] / ice_validator / tests / test_port_resource_ids.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 re
42
43 import pytest
44 from tests import cached_yaml as yaml
45
46 from .helpers import validates
47 from .utils.network_roles import (
48     get_network_role_from_port,
49     get_network_type_from_port,
50     property_uses_get_resource,
51 )
52 from .utils.vm_types import get_vm_type_for_nova_server
53
54
55 @validates("R-20453", "R-26351", "R-26506" "R-681859")
56 def test_port_resource_ids(heat_template):
57     """
58     Check that all resource ids for ports follow the right
59     naming convention to include the {vm_type} of the
60     nova server it is associated to and also contains the
61     {network_role} of the network it is associated with
62     """
63     with open(heat_template) as fh:
64         yml = yaml.load(fh)
65
66     # skip if resources are not defined
67     if "resources" not in yml:
68         pytest.skip("No resources specified in the heat template")
69
70     resources = yml["resources"]
71
72     invalid_ports = []
73     for k, v in resources.items():
74         if not isinstance(v, dict):
75             continue
76         if "type" not in v:
77             continue
78         if v["type"] not in "OS::Nova::Server":
79             continue
80         if "properties" not in v:
81             continue
82         if "networks" not in v["properties"]:
83             continue
84
85         vm_type = get_vm_type_for_nova_server(v)
86         if not vm_type:
87             continue
88         vm_type = vm_type.lower()
89
90         # get all ports associated with the nova server
91         properties = v["properties"]
92         for v2 in properties["networks"]:
93             for k3, v3 in v2.items():
94                 if k3 != "port":
95                     continue
96                 if not isinstance(v3, dict):
97                     continue
98
99                 if "get_param" in v3:
100                     continue
101                 elif "get_resource" in v3:
102                     port_id = v3["get_resource"]
103                     if not resources[port_id]:
104                         continue
105                     port_resource = resources[port_id]
106                     port_id = port_id.lower()
107                 else:
108                     continue
109
110                 if property_uses_get_resource(v, "network"):
111                     continue
112                 network_role = get_network_role_from_port(port_resource)
113                 if not network_role:
114                     continue
115                 network_role = network_role.lower()
116
117                 network_type = get_network_type_from_port(port_resource)
118                 if not network_type:
119                     continue
120                 if network_type == "external":
121                     expected_r_id = r"{}_\d+_{}_port_\d+".format(vm_type, network_role)
122                 else:
123                     expected_r_id = r"{}_\d+_int_{}_port_\d+".format(
124                         vm_type, network_role
125                     )
126                 if not re.match(expected_r_id, port_id):
127                     invalid_ports.append(
128                         (port_id, "Did not match {}".format(expected_r_id))
129                     )
130
131     port_errors = "; ".join(
132         "{} -> {}".format(port, error) for port, error in invalid_ports
133     )
134     msg = "The following ports have invalid resource IDs: {}".format(port_errors)
135     msg = msg.replace(r"\d+", "{index}")
136     assert not invalid_ports, msg