[VVP] udpating scripts for casablanca 2
[vvp/validation-scripts.git] / ice_validator / tests / test_initial_configuration.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 from os import path
42
43 import pytest
44 from tests import cached_yaml as yaml
45
46 from .helpers import validates
47 from yamllint.config import YamlLintConfig
48 from yamllint import linter
49 from .utils.nested_files import check_for_invalid_nesting
50 from .utils.nested_iterables import find_all_get_resource_in_yml
51 from .utils.nested_iterables import find_all_get_param_in_yml
52
53 """
54 Order tests by number so they execute in order for base tests
55 """
56
57
58 @pytest.mark.base
59 @validates('R-95303')
60 def test_00_valid_yaml(filename):
61     '''
62     Read in each .yaml or .env file. If it is successfully parsed as yaml, save
63     contents, else add filename to list of bad yaml files. Log the result of
64     parse attempt.
65     '''
66     conf = YamlLintConfig('rules: {}')
67
68     if path.splitext(filename)[-1] in [".yml", ".yaml", ".env"]:
69         gen = linter.run(open(filename), conf)
70         errors = list(gen)
71
72         assert not errors, "Error parsing file {} with error {}".format(filename, errors)
73     else:
74         pytest.skip("The file does not have any of the extensions .yml,\
75             .yaml, or .env")
76
77
78 @pytest.mark.base
79 def test_02_all_referenced_resources_exists(yaml_file):
80     '''
81     Check that all resources referenced by get_resource
82     actually exists in all yaml files
83     '''
84     with open(yaml_file) as fh:
85         yml = yaml.load(fh)
86
87     # skip if resources are not defined
88     if "resources" not in yml:
89         pytest.skip("No resources specified in the yaml file")
90
91     resource_ids = yml['resources'].keys()
92     referenced_resource_ids = find_all_get_resource_in_yml(yml)
93
94     missing_referenced_resources = set()
95     for referenced_resource_id in referenced_resource_ids:
96         if referenced_resource_id not in resource_ids:
97             missing_referenced_resources.add(referenced_resource_id)
98
99     assert not missing_referenced_resources, (
100         'missing referenced resources %s' % list(
101             missing_referenced_resources))
102
103
104 @pytest.mark.base
105 def test_01_valid_nesting(yaml_file):
106     '''
107     Check that the nesting is following the proper format and
108     that all nested files exists and are parsable
109     '''
110     invalid_nesting = []
111
112     with open(yaml_file) as fh:
113         yml = yaml.load(fh)
114     if "resources" in yml:
115         try:
116             invalid_nesting.extend(check_for_invalid_nesting(
117                 yml["resources"],
118                 yaml_file,
119                 path.dirname(yaml_file)))
120         except Exception:
121             invalid_nesting.append(yaml_file)
122
123     assert not invalid_nesting, \
124         "invalid nested file detected in file {}\n\n".format(invalid_nesting)
125
126
127 @pytest.mark.base
128 def test_03_all_get_param_have_defined_parameter(yaml_file):
129     '''
130     Check that all referenced parameters are actually defined
131     as parameters
132     '''
133     invalid_get_params = []
134     with open(yaml_file) as fh:
135         yml = yaml.load(fh)
136
137     resource_params = find_all_get_param_in_yml(yml)
138
139     parameters = set(yml.get('parameters', {}).keys())
140     if not parameters:
141         pytest.skip("no parameters detected")
142
143     for rp in resource_params:
144         if rp not in parameters:
145             invalid_get_params.append(rp)
146
147     assert not invalid_get_params, (
148         "get_param reference detected without corresponding parameter defined {}"
149         .format(invalid_get_params))