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.
42 Ensure that if a resource_id has an {index}, then all resources of
43 the same vm-type have an index, the indices are consecutive and start
52 from .structures import Heat
53 from .helpers import validates
54 from .utils import vm_types
58 RE_INDEXED_RESOURCE_ID = re.compile(r"\w+_(?P<index>\d+)$")
62 def test_indices(heat_templates):
65 indexed_resource_ids = {}
67 for heat_template in heat_templates:
68 h = Heat(filepath=heat_template)
70 indexed_resource_ids.update(get_indexed_resource_ids(h.resources))
71 resources.update(h.resources)
73 pytest.skip("No resources found")
75 if not indexed_resource_ids:
76 pytest.skip("No resources with {index} found")
78 types = get_types(resources, indexed_resource_ids)
80 pytest.skip("No resources with {vm-type} found")
82 indices = collections.defaultdict(list)
83 for resource_id, vm_type in types.items():
84 indices[vm_type].append(indexed_resource_ids[resource_id])
86 for vm_type, index_list in indices.items():
87 for i in range(len(index_list)):
88 if i not in index_list:
89 bad[vm_type] = index_list
92 "vm-type indices must be consecutive, unique,"
93 " and start at 0.\n %s"
97 "Resource ID %s: VM Type: %s" % (x, y)
98 for x, y in types.items()
106 def get_indexed_resource_ids(resources):
107 """Return dict. keys are resource_ids which end in an index.
108 values are the integer index parsed from the resource_id.
110 indexed_resource_ids = {}
111 for resource in resources:
112 match = RE_INDEXED_RESOURCE_ID.match(resource)
114 indexed_resource_ids[resource] = int(match.groupdict()["index"])
115 return indexed_resource_ids
118 def get_types(resources, indexed_resource_ids):
119 """Return dict. keys are resource_ids from indexed_resource_ids.
120 values are the vm-type extracted from the resource.
123 for rid in indexed_resource_ids:
124 x = vm_types.get_vm_types_for_resource(resources[rid])
125 if x and len(x) == 1:
126 all_vm_types[rid] = list(x)[0] # x is a set.