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