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