Aligned test with updated R-610030
[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 © 2019 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 re
41
42 import pytest
43 from tests import cached_yaml as yaml
44
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,
50 )
51 from .utils.vm_types import get_vm_type_for_nova_server
52
53
54 @validates("R-20453", "R-26351", "R-26506", "R-681859")
55 def test_port_resource_ids(yaml_file):
56     """
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
61     """
62     with open(yaml_file) as fh:
63         yml = yaml.load(fh)
64
65     # skip if resources are not defined
66     if "resources" not in yml:
67         pytest.skip("No resources specified in the heat template")
68
69     resources = yml["resources"]
70
71     invalid_ports = []
72     for k, v in resources.items():
73         if not isinstance(v, dict):
74             continue
75         if "type" not in v:
76             continue
77         if v["type"] not in "OS::Nova::Server":
78             continue
79         if "properties" not in v:
80             continue
81         if "networks" not in v["properties"]:
82             continue
83
84         vm_type = get_vm_type_for_nova_server(v)
85         if not vm_type:
86             continue
87         vm_type = vm_type.lower()
88
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():
93                 if k3 != "port":
94                     continue
95                 if not isinstance(v3, dict):
96                     continue
97
98                 if "get_param" in v3:
99                     continue
100                 elif "get_resource" in v3:
101                     port_id = v3["get_resource"]
102                     if not resources[port_id]:
103                         continue
104                     port_resource = resources[port_id]
105                     port_id = port_id.lower()
106                 else:
107                     continue
108
109                 if property_uses_get_resource(v, "network"):
110                     continue
111
112                 if port_resource.get("type", "") != "OS::Neutron::Port":
113                     continue
114
115                 network_role = get_network_role_from_port(port_resource)
116                 if not network_role:
117                     invalid_ports.append(
118                         (port_id, "Unable to determine network role for port.")
119                     )
120                     continue
121                 network_role = network_role.lower()
122
123                 network_type = get_network_type_from_port(port_resource)
124                 if not network_type:
125                     invalid_ports.append(
126                         (
127                             port_id,
128                             "Unable to determine network type for port (internal or external).",
129                         )
130                     )
131                     continue
132
133                 if network_type == "external":
134                     expected_r_id = r"{}_\d+_{}_port_\d+".format(vm_type, network_role)
135                 else:
136                     expected_r_id = r"{}_\d+_int_{}_port_\d+".format(
137                         vm_type, network_role
138                     )
139                 if not re.match(expected_r_id, port_id):
140                     invalid_ports.append(
141                         (port_id, "Did not match {}".format(expected_r_id))
142                     )
143
144     port_errors = "; ".join(
145         "{} -> {}".format(port, error) for port, error in invalid_ports
146     )
147     msg = "The following ports have invalid resource IDs: {}".format(port_errors)
148     msg = msg.replace(r"\d+", "{index}")
149     assert not invalid_ports, msg