Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame^] | 1 | # ============LICENSE_START======================================================= |
| 2 | # Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. |
| 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 | |
| 24 | import cherrypy |
| 25 | import pytest |
| 26 | from cherrypy.test.helper import CPWebCase |
| 27 | |
| 28 | from policyhandler.config import Config |
| 29 | from policyhandler.onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit |
| 30 | from policyhandler.pdp_api.pdp_consts import POLICY_NAME |
| 31 | from policyhandler.policy_consts import LATEST_POLICIES |
| 32 | from policyhandler.policy_receiver import PolicyReceiver |
| 33 | from policyhandler.utils import Utils |
| 34 | from policyhandler.web_server import _PolicyWeb |
| 35 | |
| 36 | from ..mock_tracker import Tracker |
| 37 | from .mock_policy_engine import MockPolicyEngine |
| 38 | |
| 39 | _LOGGER = Utils.get_logger(__file__) |
| 40 | |
| 41 | |
| 42 | @pytest.mark.usefixtures("fix_pdp_post", "fix_discovery") |
| 43 | class WebServerTest(CPWebCase): |
| 44 | """testing the web-server - runs tests in alphabetical order of method names""" |
| 45 | def setup_server(): |
| 46 | """setup the web-server""" |
| 47 | cherrypy.tree.mount(_PolicyWeb(), '/') |
| 48 | |
| 49 | setup_server = staticmethod(setup_server) |
| 50 | |
| 51 | def test_web_healthcheck(self): |
| 52 | """test /healthcheck""" |
| 53 | result = self.getPage("/healthcheck") |
| 54 | _LOGGER.info("healthcheck result: %s", result) |
| 55 | _LOGGER.info("got healthcheck: %s", self.body) |
| 56 | self.assertStatus('200 OK') |
| 57 | |
| 58 | Tracker.validate() |
| 59 | |
| 60 | def test_web_policy_latest(self): |
| 61 | """test /policy_latest/<policy-id>""" |
| 62 | policy_id, expected_policy = MockPolicyEngine.gen_policy_latest(3) |
| 63 | |
| 64 | self.getPage("/policy_latest/{0}".format(policy_id or "")) |
| 65 | self.assertStatus('200 OK') |
| 66 | |
| 67 | policy_latest = json.loads(self.body) |
| 68 | |
| 69 | _LOGGER.info("policy_latest: %s", self.body) |
| 70 | _LOGGER.info("expected_policy: %s", json.dumps(expected_policy)) |
| 71 | assert Utils.are_the_same(policy_latest, expected_policy) |
| 72 | |
| 73 | result = self.getPage("/healthcheck") |
| 74 | _LOGGER.info("healthcheck result: %s", result) |
| 75 | |
| 76 | Tracker.validate() |
| 77 | |
| 78 | @pytest.mark.usefixtures("fix_deploy_handler") |
| 79 | def test_web_all_policies_latest(self): |
| 80 | """test GET /policies_latest""" |
| 81 | |
| 82 | result = self.getPage("/policies_latest") |
| 83 | _LOGGER.info("result: %s", result) |
| 84 | _LOGGER.info("body: %s", self.body) |
| 85 | |
| 86 | self.assertStatus('404 Not Found') |
| 87 | |
| 88 | def test_web_policies_latest(self): |
| 89 | """test POST /policies_latest with policyName""" |
| 90 | body = json.dumps({"junk": "to-be-developed"}) |
| 91 | result = self.getPage("/policies_latest", method='POST', |
| 92 | body=body, |
| 93 | headers=[ |
| 94 | (REQUEST_X_ECOMP_REQUESTID, str(uuid.uuid4())), |
| 95 | ("Content-Type", "application/json"), |
| 96 | ('Content-Length', str(len(body))) |
| 97 | ]) |
| 98 | _LOGGER.info("result: %s", result) |
| 99 | _LOGGER.info("body: %s", self.body) |
| 100 | |
| 101 | self.assertStatus('404 Not Found') |
| 102 | |
| 103 | @pytest.mark.usefixtures( |
| 104 | "fix_deploy_handler", |
| 105 | "fix_cherrypy_engine_exit") |
| 106 | def test_zzzzz_shutdown(self): |
| 107 | """test shutdown""" |
| 108 | _LOGGER.info("start shutdown") |
| 109 | assert not PolicyReceiver.is_running() |
| 110 | audit = Audit(job_name="test_zzzzz_shutdown", req_message="start shutdown") |
| 111 | PolicyReceiver.run(audit) |
| 112 | |
| 113 | result = self.getPage("/healthcheck") |
| 114 | _LOGGER.info("healthcheck result: %s", result) |
| 115 | |
| 116 | WebServerTest.do_gc_test = False |
| 117 | _LOGGER.info("shutdown...") |
| 118 | audit.audit_done("shutdown") |
| 119 | result = self.getPage("/shutdown") |
| 120 | _LOGGER.info("shutdown result: %s", result) |
| 121 | self.assertStatus('200 OK') |
| 122 | _LOGGER.info("got shutdown: %s", self.body) |
| 123 | time.sleep(1) |
| 124 | assert not PolicyReceiver.is_running() |
| 125 | |
| 126 | if Config.is_pdp_api_default(): |
| 127 | _LOGGER.info("passive for new PDP API") |
| 128 | return |
| 129 | |
| 130 | Tracker.validate() |