575e5dcf4425a960f3ea87c55ac02e281a78a68b
[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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40 import glob
41 import os
42
43 import pytest
44 from tests import cached_yaml as yaml
45
46 from .helpers import validates
47
48
49 def expected_template_module_pair(volume_path):
50     """Returns the path to the expected base or incremental module for a given volume"""
51     base_dir, filename = os.path.split(volume_path)
52     return os.path.join(base_dir, filename.replace("_volume", ""))
53
54
55 @validates("R-82732")
56 def test_volume_module_name_matches_incremental_or_base_module(volume_template):
57     expected_template_name = expected_template_module_pair(volume_template)
58     assert os.path.exists(
59         expected_template_name
60     ), "Could not find corresponding module ({}) for volume module ({}".format(
61         expected_template_name, volume_template
62     )
63
64
65 @validates("R-11200", "R-07443")
66 def test_volume_outputs_consumed(template_dir, volume_template):
67     """
68     Check that all outputs in a volume template is consumed
69     by the corresponding heat template
70     """
71     pair_template = expected_template_module_pair(volume_template)
72
73     # Make sure all the output parameters in the volume module are
74     # consumed by the expected base or incremental module
75     if not os.path.exists(pair_template):
76         pytest.skip("Expected pair module not found")
77     with open(volume_template, "r") as f:
78         volume = yaml.load(f)
79     with open(pair_template, "r") as f:
80         pair = yaml.load(f)
81     outputs = set(volume.get("outputs", {}).keys())
82     parameters = set(pair.get("parameters", {}).keys())
83     missing_output_parameters = outputs.difference(parameters)
84     assert not missing_output_parameters, (
85         "The output parameters ({}) in {} were not all "
86         "used by the expected module {}".format(
87             ",".join(missing_output_parameters), volume_template, pair_template
88         )
89     )
90
91     # Now make sure that none of the output parameters appear in any other
92     # template
93     template_files = set(glob.glob("*.yaml"))
94     errors = {}
95     for template_path in template_files:
96         if template_path in (pair_template, volume_template):
97             continue  # Skip these files since we already checked this pair
98         with open(template_path, "r") as f:
99             template = yaml.load(f)
100         parameters = set(template.get("parameters", {}).keys())
101         misused_outputs = outputs.intersection(parameters)
102         if misused_outputs:
103             errors[template_path] = misused_outputs
104     message = ", ".join(
105         "{} ({})".format(path, ", ".join(params)) for path, params in errors.items()
106     )
107     assert not errors, "Volume output parameters detected in unexpected modules: " + \
108                        message