[VVP] updating validation scripts in dublin
[vvp/validation-scripts.git] / ice_validator / tests / test_no_unused_parameters_between_env_and_templates.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 #
38 import os
39 import pytest
40
41 from .helpers import validates, get_environment_pair
42
43
44 def get_keys(template, key):
45     """
46     Gets the set of keys from an expected dict from the ``template`` mapped to
47     the ``key``.  If the key is not found, or is not a dict, then an empty
48     set is returned
49     """
50     value = template.get(key)
51     if not value or not hasattr(value, "keys"):
52         return set()
53     else:
54         return set(value.keys())
55
56
57 @pytest.mark.heat_only
58 @validates("R-01896", "R-26124")
59 def test_no_unused_parameters_between_env_and_templates(heat_template):
60     """
61     Check all defined parameters are used in the appropriate Heat template.
62     """
63     environment_pair = get_environment_pair(heat_template)
64     if not environment_pair:
65         pytest.skip("No heat/env pair could be identified")
66
67     env_parameters = get_keys(environment_pair["eyml"], "parameters")
68     template_parameters = get_keys(environment_pair["yyml"], "parameters")
69
70     extra_in_template = template_parameters.difference(env_parameters)
71     extra_in_env = env_parameters.difference(template_parameters)
72
73     msg = (
74         "Mismatched parameters detected for the template and environment pair "
75         "({basename}). Ensure the parameters exist in both "
76         "templates indented under their respective parameters sections. "
77     )
78     if extra_in_env:
79         msg += (
80             "The following parameters exist in the env file, but not the "
81             "template: {extra_in_env}. "
82         )
83     if extra_in_template:
84         msg += (
85             "The following parameters exist in the template file, but not the "
86             "environment file: {extra_in_template}"
87         )
88
89     assert not (extra_in_template or extra_in_env), msg.format(
90         basename=os.path.split(environment_pair["name"])[-1],
91         extra_in_env=", ".join(extra_in_env),
92         extra_in_template=", ".join(extra_in_template),
93     )