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