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