[VVP] udpating scripts for casablanca 2
[vvp/validation-scripts.git] / ice_validator / tests / test_volume_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 .utils.vm_types import get_vm_type_for_nova_server
47
48
49 def test_volume_resource_ids(heat_template):
50     '''
51     Check that all resource ids for cinder volumes follow the right
52     naming convention to include the {vm_type} of the
53     nova server it is associated to
54     '''
55     with open(heat_template) as fh:
56         yml = yaml.load(fh)
57
58     # skip if resources are not defined
59     if "resources" not in yml:
60         pytest.skip("No resources specified in the heat template")
61
62     volume_pattern = re.compile(r'(.+?)_volume_id_\d+')
63     resources = yml['resources']
64
65     invalid_volumes = []
66     for k, v in resources.items():
67         if not isinstance(v, dict):
68             continue
69         if 'type' not in v:
70             continue
71         if v['type'] not in ['OS::Nova::Server',
72                              'OS::Cinder::VolumeAttachment']:
73             continue
74
75         if v['type'] == 'OS::Nova::Server':
76             # check block_device_mapping and make sure the right
77             # {vm_type} is used
78             if 'properties' not in v:
79                 continue
80             if 'block_device_mapping' not in v['properties']:
81                 continue
82
83             vm_type = get_vm_type_for_nova_server(v)
84             if not vm_type:
85                 continue
86             vm_type = vm_type.lower()
87
88             # get the volume_id from the block_device_mapping
89             properties = v['properties']
90             for v2 in properties['block_device_mapping']:
91                 for k3, v3 in v2.items():
92                     if k3 != 'volume_id':
93                         continue
94                     if not isinstance(v3, dict):
95                         continue
96
97                     volume_id = (
98                         v3.get('get_param') or
99                         v3.get('get_resource'))
100                     if not volume_id:
101                         continue
102                     if isinstance(volume_id, list):
103                         volume_id = volume_id[0].lower()
104                     else:
105                         volume_id = volume_id.lower()
106
107                     if vm_type + "_" not in volume_id:
108                         invalid_volumes.append(volume_id)
109
110         elif v['type'] == 'OS::Cinder::VolumeAttachment':
111             # check the volume attachment and the {vm_type}
112             # of the corresponding nova server
113             if 'properties' not in v:
114                 continue
115             if 'volume_id' not in v['properties']:
116                 continue
117             if 'instance_uuid' not in v['properties']:
118                 continue
119
120             properties = v['properties']
121
122             # get the instance_uuid and when applicable
123             # the nova server instance
124             instance_uuid = None
125             nova_server = None
126
127             if 'get_param' in properties['instance_uuid']:
128                 continue
129             elif 'get_resource' in properties['instance_uuid']:
130                 instance_uuid = properties['instance_uuid']['get_resource']
131                 if not resources[instance_uuid]:
132                     continue
133                 nova_server = resources[instance_uuid]
134                 instance_uuid = instance_uuid.lower()
135             else:
136                 continue
137
138             # get the volume_id
139             volume_id = None
140             volume_id = (
141                 properties['volume_id'].get('get_param') or
142                 properties['volume_id'].get('get_resource'))
143             if not volume_id:
144                 continue
145             if isinstance(volume_id, list):
146                 volume_id = volume_id[0].lower()
147             else:
148                 volume_id = volume_id.lower()
149
150             # do not test the case when the instance_uuid and
151             # volume_id are not defined
152             if not instance_uuid and not volume_id:
153                 continue
154
155             if nova_server:
156                 vm_type = get_vm_type_for_nova_server(nova_server)
157                 if not vm_type:
158                     continue
159                 vm_type = vm_type.lower()
160                 if vm_type + "_" not in volume_id:
161                     invalid_volumes.append(volume_id)
162             else:
163                 # extract the assumed {vm_type} from volume_id
164                 m = volume_pattern.match(volume_id)
165                 if m:
166                     vm_type = m.group(1).lower()
167                     if vm_type + "_" not in instance_uuid:
168                         invalid_volumes.append(volume_id)
169                 else:
170                     continue
171
172     assert not set(invalid_volumes)