[VVP] Add test for R-100260 and fix mapping
[vvp/validation-scripts.git] / ice_validator / tests / test_volume_module_naming.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 import os
38
39 from tests.helpers import validates
40 from tests.utils.nested_files import get_nested_files
41 from tests.structures import Heat, Resource
42
43
44 def non_nested_files(filenames):
45     nested_files = get_nested_files(filenames)
46     return set(filenames).difference(set(nested_files))
47
48
49 @validates("R-589037")
50 def test_detected_volume_module_follows_naming_convention(template_dir):
51     all_files = [os.path.join(template_dir, f) for f in os.listdir(template_dir)]
52     yaml_files = [f for f in all_files if f.endswith(".yaml") or f.endswith(".yml")]
53     errors = []
54     for yaml_file in non_nested_files(yaml_files):
55         heat = Heat(filepath=yaml_file)
56         if not heat.resources:
57             continue
58         base_dir, filename = os.path.split(yaml_file)
59         resources = heat.get_all_resources(base_dir)
60         non_nested_ids = {
61             r_id
62             for r_id, r_data in resources.items()
63             if not Resource(r_id, r_data).is_nested()
64         }
65         volume_ids = {
66             r_id
67             for r_id, r_data in resources.items()
68             if Resource(r_id, r_data).resource_type == "OS::Cinder::Volume"
69         }
70         non_volume_ids = non_nested_ids.difference(volume_ids)
71         if non_volume_ids:
72             continue  # Not a volume module
73         base_name, ext = os.path.splitext(filename)
74         if not base_name.endswith("_volume") or ext not in (".yaml", ".yml"):
75             errors.append(yaml_file)
76         msg = (
77             "Volume modules detected, but they do not follow the expected "
78             + " naming convention {{module_name}}_volume.[yaml|yml]: {}"
79         ).format(", ".join(errors))
80         assert not errors, msg