blob: c474e2979f0960d097a623d92498ab45e06d3e54 [file] [log] [blame]
Alex Shatov9a4d3c52019-04-01 11:32:06 -04001# ============LICENSE_START=======================================================
Alex Shatov78ff88f2020-02-27 12:45:54 -05002# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
Alex Shatov9a4d3c52019-04-01 11:32:06 -04003# ================================================================================
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ============LICENSE_END=========================================================
16#
17
18"""test of the package for policy-handler of DCAE-Controller"""
19
20import json
21import time
22import uuid
23
Alex Shatov9a4d3c52019-04-01 11:32:06 -040024import pytest
Alex Shatov78ff88f2020-02-27 12:45:54 -050025import cherrypy
Alex Shatov9a4d3c52019-04-01 11:32:06 -040026
Alex Shatov78ff88f2020-02-27 12:45:54 -050027from cherrypy.test.helper import CPWebCase
28from policyhandler.onap.audit import (REQUEST_X_ECOMP_REQUESTID,
29 REQUEST_X_ONAP_REQUESTID, Audit)
Alex Shatov9a4d3c52019-04-01 11:32:06 -040030from policyhandler.policy_receiver import PolicyReceiver
31from policyhandler.utils import Utils
32from policyhandler.web_server import _PolicyWeb
33
34from ..mock_tracker import Tracker
Alex Shatov9a4d3c52019-04-01 11:32:06 -040035
36_LOGGER = Utils.get_logger(__file__)
37
38
39@pytest.mark.usefixtures("fix_pdp_post", "fix_discovery")
40class WebServerTest(CPWebCase):
41 """testing the web-server - runs tests in alphabetical order of method names"""
42 def setup_server():
43 """setup the web-server"""
44 cherrypy.tree.mount(_PolicyWeb(), '/')
45
46 setup_server = staticmethod(setup_server)
47
48 def test_web_healthcheck(self):
49 """test /healthcheck"""
50 result = self.getPage("/healthcheck")
51 _LOGGER.info("healthcheck result: %s", result)
52 _LOGGER.info("got healthcheck: %s", self.body)
53 self.assertStatus('200 OK')
54
55 Tracker.validate()
56
57 def test_web_policy_latest(self):
58 """test /policy_latest/<policy-id>"""
Alex Shatov78ff88f2020-02-27 12:45:54 -050059 policy_id = "test_scope_prefix.pdp_decision_sit"
60 expected_policy = {
61 "policy_id": "test_scope_prefix.pdp_decision_sit",
62 "policy_body": {
63 "type": "unit.test.type.policies",
64 "version": "1.0.0",
65 "metadata": {
66 "policy-id": "test_scope_prefix.pdp_decision_sit",
67 "policy-version": "4.4.4",
68 "description": "description for test_scope_prefix.pdp_decision_sit"
69 },
70 "policyName": "test_scope_prefix.pdp_decision_sit.4-4-4.xml",
71 "policyVersion": "4.4.4",
72 "config": {
73 "policy_updated_from_ver": 3,
74 "policy_updated_to_ver": 4,
75 "policy_hello": "world!",
76 "updated_policy_id": "test_scope_prefix.pdp_decision_sit"
77 }
78 }
79 }
Alex Shatov9a4d3c52019-04-01 11:32:06 -040080
81 self.getPage("/policy_latest/{0}".format(policy_id or ""))
82 self.assertStatus('200 OK')
83
84 policy_latest = json.loads(self.body)
85
86 _LOGGER.info("policy_latest: %s", self.body)
87 _LOGGER.info("expected_policy: %s", json.dumps(expected_policy))
88 assert Utils.are_the_same(policy_latest, expected_policy)
89
90 result = self.getPage("/healthcheck")
91 _LOGGER.info("healthcheck result: %s", result)
92
93 Tracker.validate()
94
95 @pytest.mark.usefixtures("fix_deploy_handler")
96 def test_web_all_policies_latest(self):
97 """test GET /policies_latest"""
98
99 result = self.getPage("/policies_latest")
100 _LOGGER.info("result: %s", result)
101 _LOGGER.info("body: %s", self.body)
102
Alex Shatov78ff88f2020-02-27 12:45:54 -0500103 self.assertStatus('200 OK')
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400104
105 def test_web_policies_latest(self):
106 """test POST /policies_latest with policyName"""
107 body = json.dumps({"junk": "to-be-developed"})
Alex Shatov78ff88f2020-02-27 12:45:54 -0500108 request_id = str(uuid.uuid4())
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400109 result = self.getPage("/policies_latest", method='POST',
110 body=body,
111 headers=[
Alex Shatov78ff88f2020-02-27 12:45:54 -0500112 (REQUEST_X_ECOMP_REQUESTID, request_id),
113 (REQUEST_X_ONAP_REQUESTID, request_id),
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400114 ("Content-Type", "application/json"),
115 ('Content-Length', str(len(body)))
116 ])
117 _LOGGER.info("result: %s", result)
118 _LOGGER.info("body: %s", self.body)
119
120 self.assertStatus('404 Not Found')
121
122 @pytest.mark.usefixtures(
123 "fix_deploy_handler",
Alex Shatov78ff88f2020-02-27 12:45:54 -0500124 "fix_dmaap_mr",
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400125 "fix_cherrypy_engine_exit")
126 def test_zzzzz_shutdown(self):
127 """test shutdown"""
Alex Shatov78ff88f2020-02-27 12:45:54 -0500128 _LOGGER.info("testing the shutdown")
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400129 assert not PolicyReceiver.is_running()
Alex Shatov78ff88f2020-02-27 12:45:54 -0500130 audit = Audit(job_name="test_zzzzz_shutdown", req_message="testing the shutdown")
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400131 PolicyReceiver.run(audit)
132
133 result = self.getPage("/healthcheck")
134 _LOGGER.info("healthcheck result: %s", result)
135
Alex Shatov78ff88f2020-02-27 12:45:54 -0500136 time.sleep(1)
137
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400138 WebServerTest.do_gc_test = False
139 _LOGGER.info("shutdown...")
140 audit.audit_done("shutdown")
141 result = self.getPage("/shutdown")
142 _LOGGER.info("shutdown result: %s", result)
143 self.assertStatus('200 OK')
144 _LOGGER.info("got shutdown: %s", self.body)
Alex Shatov78ff88f2020-02-27 12:45:54 -0500145 time.sleep(5)
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400146 assert not PolicyReceiver.is_running()
147
Alex Shatov78ff88f2020-02-27 12:45:54 -0500148 Tracker.validate()
149
150 @pytest.mark.usefixtures(
151 "fix_deploy_handler",
152 "fix_dmaap_mr",
153 "fix_cherrypy_engine_exit")
154 def test_zzz_policy_updates_and_catch_ups(self):
155 """test run policy handler with policy updates and catchups"""
156 _LOGGER.info("start policy_updates_and_catch_ups")
157 assert not PolicyReceiver.is_running()
158
159 audit = Audit(job_name="test_zzz_policy_updates_and_catch_ups",
160 req_message="start policy_updates_and_catch_ups")
161 PolicyReceiver.run(audit)
162
163 _LOGGER.info("sleep 20 before shutdown...")
164 time.sleep(20)
165
166 result = self.getPage("/healthcheck")
167 _LOGGER.info("healthcheck result: %s", result)
168
169 PolicyReceiver.shutdown(audit)
170 time.sleep(5)
171 assert not PolicyReceiver.is_running()
Alex Shatov9a4d3c52019-04-01 11:32:06 -0400172
173 Tracker.validate()