34d3b4884319cb34f617fc133ee99263a5a2fd21
[vvp/validation-scripts.git] / ice_validator / tests / test_environment_file_structure.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
41 """ environment file structure
42 """
43
44 import yaml
45 import pytest
46
47 from .helpers import validates
48
49 VERSION = "1.0.0"
50
51 # pylint: disable=invalid-name
52
53
54 def test_environment_structure(env_file):
55     """
56     Check that all environments files only have the allowed sections
57     """
58     key_values = [
59         "parameters",
60         "event_sinks",
61         "encrypted_parameters",
62         "parameter_merge_strategies",
63     ]
64
65     with open(env_file) as fh:
66         yml = yaml.load(fh)
67     assert [k for k in key_values if k in yml], "%s missing any of %s" % (
68         env_file,
69         key_values,
70     )
71
72
73 @validates("R-03324")
74 def test_environment_file_contains_required_sections(env_file):
75     """
76     Check that all environments files only have the allowed sections
77     """
78     required_keys = ["parameters"]
79
80     with open(env_file) as fh:
81         yml = yaml.load(fh)
82     missing_keys = [v for v in required_keys if v not in yml]
83     assert not missing_keys, "%s missing %s" % (env_file, missing_keys)
84
85
86 def test_environment_file_sections_have_the_right_format(env_file):
87     """
88     Check that all environment files have sections of the right format.
89     Do note that it only tests for dicts or not dicts currently.
90     """
91     dict_keys = ["parameters", "encrypted_parameters", "parameter_merge_strategies"]
92     not_dict_keys = ["event_sinks"]
93
94     with open(env_file) as fh:
95         yml = yaml.load(fh)
96
97     if not [k for k in dict_keys + not_dict_keys if k in yml]:
98         pytest.skip("The fixture is not applicable for this test")
99
100     bad_dict_keys = [k for k in dict_keys if k in yml and not isinstance(yml[k], dict)]
101     bad_not_dict_keys = [
102         k for k in not_dict_keys if k in yml and isinstance(yml[k], dict)
103     ]
104     errors = []
105     if bad_dict_keys:
106         errors.append("must be dict %s" % bad_dict_keys)
107     if bad_not_dict_keys:
108         errors.append("must not be dict %s" % bad_not_dict_keys)
109     assert not errors, "%s errors:\n    %s" % (env_file, "\n    ".join(errors))