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