Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 1 | # ============LICENSE_START======================================================= |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 2 | # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 3 | # ================================================================================ |
| 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 | |
| 20 | import json |
| 21 | import time |
| 22 | import uuid |
| 23 | |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 24 | import pytest |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 25 | import cherrypy |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 26 | |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 27 | from cherrypy.test.helper import CPWebCase |
| 28 | from policyhandler.onap.audit import (REQUEST_X_ECOMP_REQUESTID, |
| 29 | REQUEST_X_ONAP_REQUESTID, Audit) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 30 | from policyhandler.policy_receiver import PolicyReceiver |
| 31 | from policyhandler.utils import Utils |
| 32 | from policyhandler.web_server import _PolicyWeb |
| 33 | |
| 34 | from ..mock_tracker import Tracker |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 35 | |
| 36 | _LOGGER = Utils.get_logger(__file__) |
| 37 | |
| 38 | |
| 39 | @pytest.mark.usefixtures("fix_pdp_post", "fix_discovery") |
| 40 | class 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 Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 59 | 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 Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 80 | |
| 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 Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 103 | self.assertStatus('200 OK') |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 104 | |
| 105 | def test_web_policies_latest(self): |
| 106 | """test POST /policies_latest with policyName""" |
| 107 | body = json.dumps({"junk": "to-be-developed"}) |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 108 | request_id = str(uuid.uuid4()) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 109 | result = self.getPage("/policies_latest", method='POST', |
| 110 | body=body, |
| 111 | headers=[ |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 112 | (REQUEST_X_ECOMP_REQUESTID, request_id), |
| 113 | (REQUEST_X_ONAP_REQUESTID, request_id), |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 114 | ("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 Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 124 | "fix_dmaap_mr", |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 125 | "fix_cherrypy_engine_exit") |
| 126 | def test_zzzzz_shutdown(self): |
| 127 | """test shutdown""" |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 128 | _LOGGER.info("testing the shutdown") |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 129 | assert not PolicyReceiver.is_running() |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 130 | audit = Audit(job_name="test_zzzzz_shutdown", req_message="testing the shutdown") |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 131 | PolicyReceiver.run(audit) |
| 132 | |
| 133 | result = self.getPage("/healthcheck") |
| 134 | _LOGGER.info("healthcheck result: %s", result) |
| 135 | |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 136 | time.sleep(1) |
| 137 | |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 138 | 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 Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 145 | time.sleep(5) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 146 | assert not PolicyReceiver.is_running() |
| 147 | |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 148 | 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 Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 172 | |
| 173 | Tracker.validate() |