[VVP] udpating scripts for casablanca 2
[vvp/validation-scripts.git] / ice_validator / tests / test_nova_servers_index.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 '''
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
44 with 0.
45 '''
46
47 import collections
48 import re
49
50 import pytest
51
52 from .structures import Heat
53 from .helpers import validates
54 from .utils import vm_types
55
56 VERSION = '1.1.0'
57
58 RE_INDEXED_RESOURCE_ID = re.compile(r'\w+_(?P<index>\d+)$')
59
60
61 @validates('R-11690')
62 def test_indices(heat_templates):
63     '''validate indices
64     '''
65     indexed_resource_ids = {}
66     resources = {}
67     for heat_template in heat_templates:
68         h = Heat(filepath=heat_template)
69         if h.resources:
70             indexed_resource_ids.update(get_indexed_resource_ids(h.resources))
71             resources.update(h.resources)
72     if not resources:
73         pytest.skip('No resources found')
74
75     if not indexed_resource_ids:
76         pytest.skip('No resources with {index} found')
77
78     types = get_types(resources, indexed_resource_ids)
79     if not types:
80         pytest.skip('No resources with {vm-type} found')
81
82     indices = collections.defaultdict(list)
83     for resource_id, vm_type in types.items():
84         indices[vm_type].append(indexed_resource_ids[resource_id])
85     bad = {}
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
90                 break
91     assert not bad, (
92         'vm-type indices must be consecutive, unique,'
93         ' and start at 0.\n    %s' % (
94             '\n    '.join(['Resource ID %s: VM Type: %s' % (x, y)
95                            for x, y in types.items() if y in bad])))
96
97
98 def get_indexed_resource_ids(resources):
99     """Return dict. keys are resource_ids which end in an index.
100     values are the integer index parsed from the resource_id.
101     """
102     indexed_resource_ids = {}
103     for resource in resources:
104         match = RE_INDEXED_RESOURCE_ID.match(resource)
105         if match:
106             indexed_resource_ids[resource] = int(match.groupdict()['index'])
107     return indexed_resource_ids
108
109
110 def get_types(resources, indexed_resource_ids):
111     """Return dict. keys are resource_ids from indexed_resource_ids.
112     values are the vm-type extracted from the resource.
113     """
114     all_vm_types = {}
115     for rid in indexed_resource_ids:
116         x = vm_types.get_vm_types_for_resource(resources[rid])
117         if x and len(x) == 1:
118             all_vm_types[rid] = list(x)[0]  # x is a set.
119     return all_vm_types
120