[VVP] create new validation scripts
[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 = ["parameters", "event_sinks", "encrypted_parameters",
59                   "parameter_merge_strategies"]
60
61     with open(env_file) as fh:
62         yml = yaml.load(fh)
63     assert [k for k in key_values if k in yml], (
64             '%s missing any of %s' % (
65                     env_file,
66                     key_values))
67
68
69 @validates('R-03324')
70 def test_environment_file_contains_required_sections(env_file):
71     '''
72     Check that all environments files only have the allowed sections
73     '''
74     required_keys = ["parameters"]
75
76     with open(env_file) as fh:
77         yml = yaml.load(fh)
78     missing_keys = [v for v in required_keys if v not in yml]
79     assert not missing_keys, '%s missing %s' % (env_file, missing_keys)
80
81
82 def test_environment_file_sections_have_the_right_format(env_file):
83     '''
84     Check that all environment files have sections of the right format.
85     Do note that it only tests for dicts or not dicts currently.
86     '''
87     dict_keys = [
88             "parameters",
89             "encrypted_parameters",
90             "parameter_merge_strategies"]
91     not_dict_keys = [
92             "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
101                      if k in yml
102                      and not isinstance(yml[k], dict)]
103     bad_not_dict_keys = [k for k in not_dict_keys
104                          if k in yml
105                          and isinstance(yml[k], dict)]
106     errors = []
107     if bad_dict_keys:
108         errors.append('must be dict %s' % bad_dict_keys)
109     if bad_not_dict_keys:
110         errors.append('must not be dict %s' % bad_not_dict_keys)
111     assert not errors, '%s errors:\n    %s' % (
112             env_file,
113             '\n    '.join(errors))