[VVP] Updating vm_type class test to proceed if no cinder
[vvp/validation-scripts.git] / ice_validator / tests / test_volume_outputs_consumed.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 #
39 import glob
40 import os
41
42 import pytest
43 from tests import cached_yaml as yaml
44
45 from .helpers import validates
46
47
48 class VolumePairModule:
49     def __init__(self, volume_path):
50         self.volume_path = volume_path
51
52     @property
53     def path_options(self):
54         expected_path, _ = self.volume_path.rsplit("_volume", maxsplit=1)
55         return (expected_path + ".yaml", expected_path + ".yml")
56
57     @property
58     def exists(self):
59         return any(os.path.exists(option) for option in self.path_options)
60
61     def get_module_path(self):
62         """
63         Return the path of the volume module's pair if it exists,
64         otherwise None
65         """
66         for option in self.path_options:
67             if os.path.exists(option):
68                 return option
69         return None
70
71
72 @validates("R-82732")
73 def test_volume_module_name_matches_incremental_or_base_module(volume_template):
74     pair_module = VolumePairModule(volume_template)
75     assert pair_module.exists, (
76         "Could not find a corresponding module ({}) for " + "volume module ({})"
77     ).format(" or ".join(pair_module.path_options), volume_template)
78
79
80 @validates("R-11200", "R-07443")
81 def test_volume_outputs_consumed(template_dir, volume_template):
82     """
83     Check that all outputs in a volume template is consumed
84     by the corresponding heat template
85     """
86     pair_module = VolumePairModule(volume_template)
87     if not pair_module.exists:
88         pytest.skip("No pair module found for volume template")
89     with open(volume_template, "r") as f:
90         volume = yaml.load(f)
91     with open(pair_module.get_module_path(), "r") as f:
92         pair = yaml.load(f)
93     outputs = set(volume.get("outputs", {}).keys())
94     parameters = set(pair.get("parameters", {}).keys())
95     missing_output_parameters = outputs.difference(parameters)
96     assert not missing_output_parameters, (
97         "The output parameters ({}) in {} were not all "
98         "used by the expected module {}".format(
99             ",".join(missing_output_parameters), volume_template, pair_module
100         )
101     )
102
103     # Now make sure that none of the output parameters appear in any other
104     # template
105     template_files = set(glob.glob("*.yaml")).union(glob.glob(".yml"))
106     errors = {}
107     for template_path in template_files:
108         if template_path in (pair_module, volume_template):
109             continue  # Skip these files since we already checked this pair
110         with open(template_path, "r") as f:
111             template = yaml.load(f)
112         parameters = set(template.get("parameters", {}).keys())
113         misused_outputs = outputs.intersection(parameters)
114         if misused_outputs:
115             errors[template_path] = misused_outputs
116     message = ", ".join(
117         "{} ({})".format(path, ", ".join(params)) for path, params in errors.items()
118     )
119     assert not errors, (
120         "Volume output parameters detected in unexpected modules: " + message
121     )