[VVP] Support pluggable data sources for preload data
[vvp/validation-scripts.git] / ice_validator / app_tests / test_config.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 import uuid
39 from io import StringIO
40
41 import pytest
42 import yaml
43
44 from config import Config, to_uri
45 import vvp
46 from preload.engine import PLUGIN_MGR
47
48 DEFAULT_CONFIG = """
49 namespace: {namespace}
50 owner: onap-test
51 ui:
52   app-name: VNF Validation Tool
53   requirement-link-url: http://requirement.url.com
54 categories:
55   - name: Environment File Compliance. (Required to Onboard)
56     category: environment_file
57     description:
58       Checks certain parameters are excluded from the .env file, per HOT Requirements.
59       Required for ASDC onboarding, not needed for manual Openstack testing.
60 settings:
61   polling-freqency: 1000
62   env-specs:
63   - tests.test_environment_file_parameters.ENV_PARAMETER_SPEC
64 terms:
65     version: 1.0.0
66     path: path/to/terms.txt
67     popup-title: Terms and Conditions
68     popup-link-text: View Terms and Conditions
69     popup-msg-text: Review and Accept the Terms
70 """
71
72
73 # noinspection PyShadowingNames
74 @pytest.fixture()
75 def config():
76     unique = str(uuid.uuid4())
77     data = DEFAULT_CONFIG.format(namespace=unique)
78     return Config(yaml.safe_load(StringIO(data)))
79
80
81 def test_app_name(config):
82     assert "VNF Validation Tool" in config.app_name
83     assert vvp.VERSION in config.app_name
84
85
86 def test_categories_names_length(config):
87     names = config.category_names
88     assert len(names) == 1
89     assert names[0] == "Environment File Compliance. (Required to Onboard)"
90
91
92 def test_polling_frequency(config):
93     assert config.polling_frequency == 1000
94
95
96 def test_get_category_when_other(config):
97     assert (
98         config.get_category("Environment File Compliance. (Required to Onboard)")
99         == "environment_file"
100     )
101
102
103 def test_queues(config):
104     assert config.log_queue.empty(), "Log should start empty"
105     config.log_file.write("Test")
106     assert config.log_queue.get() == "Test"
107
108     assert config.status_queue.empty(), "status should start empty"
109     config.status_queue.put((True, None))
110     assert config.status_queue.get() == (True, None)
111
112
113 MISSING_CATEGORY_FIELD = """
114 namespace: org.onap.test
115 owner: onap-test
116 ui:
117   app-name: VNF Validation Tool
118 categories:
119   - description: |
120       Runs all default validations that apply to all VNF packages
121       regardless of deployment environment
122 settings:
123   polling-freqency: 1000
124 """
125
126
127 def test_missing_category_fields():
128     settings = yaml.safe_load(StringIO(MISSING_CATEGORY_FIELD))
129     with pytest.raises(RuntimeError) as e:
130         Config(settings)
131     assert "Missing: name" in str(e)
132
133
134 def test_default_output_format(config):
135     assert config.default_report_format == "HTML"
136
137
138 def test_output_formats(config):
139     for format in ["CSV", "HTML", "Excel"]:
140         assert format in config.report_formats
141
142
143 def test_category_names(config):
144     assert "Environment File Compliance. (Required to Onboard)" in config.category_names
145
146
147 def test_default_input_format(config):
148     assert "Directory (Uncompressed)" == config.default_input_format
149
150
151 def test_input_formats(config):
152     assert "Directory (Uncompressed)" in config.input_formats
153     assert "ZIP File" in config.input_formats
154
155
156 def test_env_specs(config):
157     specs = config.env_specs
158     assert len(specs) == 1
159     assert "ALL" in specs[0]
160
161
162 def test_get_generator_plugin_names(config):
163     names = [g.format_name() for g in PLUGIN_MGR.preload_generators]
164     assert "VNF-API" in names
165     assert "GR-API" in names
166
167
168 def test_preload_formats(config):
169     formats = config.preload_formats
170     assert all(format in formats for format in ("VNF-API", "GR-API"))
171
172
173 def test_requirement_link_http(config):
174     assert config.requirement_link_url == "http://requirement.url.com"
175
176
177 def test_to_uri_relative_path():
178     assert to_uri("path/").startswith("file://")
179     assert to_uri("path/").endswith("/path")
180
181
182 def test_to_uri_relative_http():
183     assert to_uri("http://url.com") == "http://url.com"
184
185
186 def test_to_uri_absolute_path():
187     assert to_uri("/path/one").startswith("file:///")
188     assert to_uri("/path/one").endswith("/path/one")
189
190
191 def test_requirement_link_path(config):
192     config._config["ui"]["requirement-link-url"] = "path/to/reqs.txt"
193     url = config.requirement_link_url
194     assert url.startswith("file://")
195     assert "path/to/reqs.txt" in url
196
197
198 def test_terms_version(config):
199     assert config.terms_version == "1.0.0"
200
201
202 def test_terms_popup_title(config):
203     assert config.terms_popup_title == "Terms and Conditions"
204
205
206 def test_terms_popup_message(config):
207     assert config.terms_popup_message == "Review and Accept the Terms"
208
209
210 def test_terms_link_url_default(config):
211     config._config["terms"]["path"] = None
212     assert config.terms_link_url is None
213
214
215 def test_terms_acceptance(config):
216     assert not config.are_terms_accepted
217     config.set_terms_accepted()
218     assert config.are_terms_accepted
219
220
221 def test_terms_link_url_path(config):
222     assert config.terms_link_url.startswith("file://")
223     assert config.terms_link_url.endswith("/path/to/terms.txt")
224
225
226 def test_terms_link_text(config):
227     assert config.terms_link_text == "View Terms and Conditions"
228
229
230 def test_default_halt_on_failure(config):
231     assert config.default_halt_on_failure
232
233
234 def test_get_subdir_for_preload(config):
235     assert config.get_subdir_for_preload("VNF-API") == "vnfapi"
236
237
238 def test_default_preload_format(config):
239     assert config.default_preload_format in ("VNF-API", "GR-API", "Excel")
240
241
242 def test_category_description(config):
243     assert "Checks certain parameters" in config.get_description(
244         "Environment File Compliance. (Required to Onboard)"
245     )
246
247
248 def test_get_category_by_name(config):
249     assert (
250         config.get_category("Environment File Compliance. (Required to Onboard)")
251         == "environment_file"
252     )
253
254
255 def test_cached_category_setting(config):
256     assert (
257         config.get_category_value("Environment File Compliance. (Required to Onboard)")
258         == 0
259     )
260
261
262 def test_disclaimer_text(config):
263     assert config.disclaimer_text == ""
264
265
266 def test_requirement_link_text(config):
267     url_text = "Requirement URL"
268     config._config["ui"]["requirement-link-text"] = url_text
269     assert config.requirement_link_text == url_text