[VVP] add bug fixes and reserve port updates
[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 import pytest
43 import yaml
44 from .utils.vm_types import get_vm_type_for_nova_server
45 from .utils.network_roles import get_network_role_from_port,\
46                                  get_network_type_from_port,\
47                                  property_uses_get_resource
48
49
50 def test_port_resource_ids(heat_template):
51     '''
52     Check that all resource ids for ports follow the right
53     naming convention to include the {vm_type} of the
54     nova server it is associated to and also contains the
55     {network_role} of the network it is associated with
56     '''
57     with open(heat_template) as fh:
58         yml = yaml.load(fh)
59
60     # skip if resources are not defined
61     if "resources" not in yml:
62         pytest.skip("No resources specified in the heat template")
63
64     port_patterns = {
65                     'internal': re.compile(r'(.+?)_\d+_int_(.+?)_\d+_port'),
66                     'external': re.compile(r'(.+?)_\d+_(.+?)_\d+_port'),
67                     }
68     resources = yml['resources']
69
70     invalid_ports = []
71     for k, v in resources.items():
72         if not isinstance(v, dict):
73             continue
74         if 'type' not in v:
75             continue
76         if v['type'] not in 'OS::Nova::Server':
77             continue
78         if 'properties' not in v:
79             continue
80         if 'networks' not in v['properties']:
81             continue
82
83         has_vm_type = False
84         has_network_role = True
85         port_resource = None
86
87         vm_type = get_vm_type_for_nova_server(v)
88         if not vm_type:
89             continue
90         vm_type = vm_type.lower()
91
92         # get all ports associated with the nova server
93         properties = v['properties']
94         for v2 in properties['networks']:
95             for k3, v3 in v2.items():
96                 if k3 != 'port':
97                     continue
98                 if not isinstance(v3, dict):
99                     continue
100
101                 if 'get_param' in v3:
102                     continue
103                 elif 'get_resource' in v3:
104                     port_id = v3['get_resource']
105                     if not resources[port_id]:
106                         continue
107                     port_resource = resources[port_id]
108                     port_id = port_id.lower()
109                 else:
110                     continue
111
112                 has_vm_type = vm_type+"_" in port_id
113
114                 if port_resource:
115                     if property_uses_get_resource(v, "network"):
116                         continue
117                     network_role = get_network_role_from_port(port_resource)
118                     if not network_role:
119                         continue
120                     network_role = network_role.lower()
121
122                     network_type = get_network_type_from_port(port_resource)
123                     if not network_type:
124                         continue
125
126                     prepend = ""
127                     if network_type == 'internal':
128                         prepend = "int_"
129                     has_network_role = prepend+network_role+"_" in port_id
130                 else:
131                     # match the assumed naming convention for ports
132                     # if the specified port is provided via get_param
133                     network_type = 'external'
134                     if "int_" in port_id:
135                         network_type = 'internal'
136                     if port_patterns[network_type].match(port_id):
137                         has_network_role = True
138
139                 if has_vm_type and has_network_role:
140                     continue
141                 invalid_ports.append(port_id)
142
143     assert not set(invalid_ports)