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