2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
44 from .utils.vm_types import get_vm_type_for_nova_server
47 def test_volume_resource_ids(heat_template):
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
53 with open(heat_template) as fh:
56 # skip if resources are not defined
57 if "resources" not in yml:
58 pytest.skip("No resources specified in the heat template")
60 volume_pattern = re.compile(r'(.+?)_volume_id_\d+')
61 resources = yml['resources']
64 for k, v in resources.items():
65 if not isinstance(v, dict):
69 if v['type'] not in ['OS::Nova::Server',
70 'OS::Cinder::VolumeAttachment']:
73 if v['type'] == 'OS::Nova::Server':
74 # check block_device_mapping and make sure the right
76 if 'properties' not in v:
78 if 'block_device_mapping' not in v['properties']:
81 vm_type = get_vm_type_for_nova_server(v)
84 vm_type = vm_type.lower()
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():
92 if not isinstance(v3, dict):
96 v3.get('get_param') or
97 v3.get('get_resource'))
100 volume_id = volume_id.lower()
102 if vm_type+"_" not in volume_id:
103 invalid_volumes.append(volume_id)
105 elif v['type'] == 'OS::Cinder::VolumeAttachment':
106 # check the volume attachment and the {vm_type}
107 # of the corresponding nova server
108 if 'properties' not in v:
110 if 'volume_id' not in v['properties']:
112 if 'instance_uuid' not in v['properties']:
115 properties = v['properties']
117 # get the instance_uuid and when applicable
118 # the nova server instance
122 if 'get_param' in properties['instance_uuid']:
124 elif 'get_resource' in properties['instance_uuid']:
125 instance_uuid = properties['instance_uuid']['get_resource']
126 if not resources[instance_uuid]:
128 nova_server = resources[instance_uuid]
129 instance_uuid = instance_uuid.lower()
136 properties['volume_id'].get('get_param') or
137 properties['volume_id'].get('get_resource'))
140 volume_id = volume_id.lower()
142 # do not test the case when the instance_uuid and
143 # volume_id are not defined
144 if not instance_uuid and not volume_id:
148 vm_type = get_vm_type_for_nova_server(nova_server)
151 vm_type = vm_type.lower()
152 if vm_type+"_" not in volume_id:
153 invalid_volumes.append(volume_id)
155 # extract the assumed {vm_type} from volume_id
156 m = volume_pattern.match(volume_id)
158 vm_type = m.group(1).lower()
159 if vm_type+"_" not in instance_uuid:
160 invalid_volumes.append(volume_id)
164 assert not set(invalid_volumes)