diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 189 | ||||
-rw-r--r-- | tests/etc_config.json | 47 | ||||
-rw-r--r-- | tests/main/__init__.py | 20 | ||||
-rw-r--r-- | tests/main/conftest.py | 56 | ||||
-rw-r--r-- | tests/main/mock_expected.py | 526 | ||||
-rw-r--r-- | tests/main/mock_policy_engine.py | 96 | ||||
-rw-r--r-- | tests/main/pdp_policies.json | 25 | ||||
-rw-r--r-- | tests/main/test_policy_rest.py (renamed from tests/test_policy_rest.py) | 18 | ||||
-rw-r--r-- | tests/main/test_policyhandler.py | 130 | ||||
-rw-r--r-- | tests/mock_config.json | 1 | ||||
-rw-r--r-- | tests/mock_deploy_handler.py | 15 | ||||
-rw-r--r-- | tests/mock_settings.py | 56 | ||||
-rw-r--r-- | tests/mock_tracker.py | 72 | ||||
-rw-r--r-- | tests/pdp_api_v0/__init__.py | 20 | ||||
-rw-r--r-- | tests/pdp_api_v0/conftest.py | 133 | ||||
-rw-r--r-- | tests/pdp_api_v0/mock_expected.py (renamed from tests/mock_expected.py) | 49 | ||||
-rw-r--r-- | tests/pdp_api_v0/mock_policy_engine.py (renamed from tests/mock_policy_engine.py) | 117 | ||||
-rw-r--r-- | tests/pdp_api_v0/mock_websocket.py (renamed from tests/mock_websocket.py) | 31 | ||||
-rw-r--r-- | tests/pdp_api_v0/test_policy_rest.py | 47 | ||||
-rw-r--r-- | tests/pdp_api_v0/test_policyhandler.py (renamed from tests/test_policyhandler.py) | 142 | ||||
-rw-r--r-- | tests/pdp_api_v0/test_pz_catch_up.py (renamed from tests/test_pz_catch_up.py) | 35 | ||||
-rw-r--r-- | tests/pdp_api_v0/test_pz_pdp_boom.py (renamed from tests/test_pz_pdp_boom.py) | 119 | ||||
-rw-r--r-- | tests/pdp_api_v0/test_pz_ph_boom.py (renamed from tests/test_pz_ph_boom.py) | 120 | ||||
-rw-r--r-- | tests/utils/__init__.py | 20 | ||||
-rw-r--r-- | tests/utils/test_step_timer.py (renamed from tests/test_step_timer.py) | 42 | ||||
-rw-r--r-- | tests/utils/test_utils.py (renamed from tests/test_policy_utils.py) | 27 | ||||
-rw-r--r-- | tests/utils/test_zzz_memory.py (renamed from tests/test_zzz_memory.py) | 23 |
27 files changed, 1679 insertions, 497 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 0dcb2bb..c3f4100 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """ startdard pytest file that contains the shared fixtures https://docs.pytest.org/en/latest/fixture.html @@ -29,61 +28,61 @@ from policyhandler.config import Config from policyhandler.deploy_handler import DeployHandler from policyhandler.discovery import DiscoveryClient from policyhandler.onap.audit import Audit -from policyhandler.policy_consts import CATCH_UP, POLICY_NAME, TARGET_ENTITY -from policyhandler.policy_receiver import PolicyReceiver -from policyhandler.policy_rest import PolicyRest +from policyhandler.policy_consts import CATCH_UP, TARGET_ENTITY +from policyhandler.utils import Utils from .mock_deploy_handler import MockDeploymentHandler -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings +from .mock_settings import MockSettings from .mock_tracker import MockHttpResponse, Tracker -from .mock_websocket import MockWebSocket + +_LOGGER = Utils.get_logger(__file__) + +_LOGGER.info("init MockSettings") +MockSettings.init() + +@pytest.fixture(scope="session", autouse=True) +def _auto_setup__global(): + """initialize the _auto_setup__global per the whole test session""" + _LOGGER.info("_auto_setup__global") + + yield _auto_setup__global + Tracker.log_all_tests() + _LOGGER.info("teardown _auto_setup__global") @pytest.fixture(autouse=True) def _auto_test_cycle(request): """log all the test starts and ends""" + module_name = request.module.__name__.replace(".", "/") if request.cls: - test_name = "%s::%s::%s" % (request.module.__name__, - request.cls.__name__, - request.function.__name__) + test_name = "%s.py::%s::%s" % (module_name, request.cls.__name__, + request.function.__name__) else: - test_name = "%s::%s" % (request.module.__name__, request.function.__name__) + test_name = "%s.py::%s" % (module_name, request.function.__name__) Tracker.reset(test_name) - if Settings.logger: - Settings.logger.info(">>>>>>> start %s", test_name) + _LOGGER.info("-"*75) + _LOGGER.info(">>>>>>> start [%s]: %s", len(Tracker.test_names), test_name) yield _auto_test_cycle - if Settings.logger: - Settings.logger.info(">>>>>>> tracked messages: %s", Tracker.to_string()) - Settings.logger.info(">>>>>>> ended %s", test_name) + _LOGGER.info(">>>>>>> tracked messages: %s", Tracker.to_string()) + _LOGGER.info(">>>>>>> %s[%s]: %s", Tracker.get_status(test_name), + len(Tracker.test_names), test_name) -@pytest.fixture(scope="session", autouse=True) -def _auto_setup_policy_engine(): - """initialize the mock-policy-engine per the whole test session""" - Settings.init() +@pytest.fixture() +def fix_cherrypy_engine_exit(monkeypatch): + """monkeyed cherrypy.engine.exit()""" + _LOGGER.info("setup fix_cherrypy_engine_exit") - Settings.logger.info("create _auto_setup_policy_engine") - MockPolicyEngine.init() - yield _auto_setup_policy_engine - Settings.logger.info("teardown _auto_setup_policy_engine") + def monkeyed_cherrypy_engine_exit(): + """monkeypatch for deploy_handler""" + _LOGGER.info("cherrypy_engine_exit()") + monkeypatch.setattr('policyhandler.web_server.cherrypy.engine.exit', + monkeyed_cherrypy_engine_exit) + yield fix_cherrypy_engine_exit + _LOGGER.info("teardown fix_cherrypy_engine_exit") -@pytest.fixture() -def fix_pdp_post(monkeypatch): - """monkeyed request /getConfig to PDP""" - def monkeyed_policy_rest_post(uri, json=None, **kwargs): - """monkeypatch for the POST to policy-engine""" - res_json = MockPolicyEngine.get_config(json.get(POLICY_NAME)) - return MockHttpResponse("post", uri, res_json, json=json, **kwargs) - - Settings.logger.info("setup fix_pdp_post") - PolicyRest._lazy_init() - monkeypatch.setattr('policyhandler.policy_rest.PolicyRest._requests_session.post', - monkeyed_policy_rest_post) - yield fix_pdp_post - Settings.logger.info("teardown fix_pdp_post") @pytest.fixture() def fix_deploy_handler(monkeypatch): @@ -98,7 +97,7 @@ def fix_deploy_handler(monkeypatch): return MockHttpResponse("get", uri, MockDeploymentHandler.get_deployed_policies(), **kwargs) - Settings.logger.info("setup fix_deploy_handler") + _LOGGER.info("setup fix_deploy_handler") audit = None if DeployHandler._lazy_inited is False: audit = Audit(req_message="fix_deploy_handler") @@ -112,79 +111,10 @@ def fix_deploy_handler(monkeypatch): yield fix_deploy_handler if audit: audit.audit_done("teardown") - Settings.logger.info("teardown fix_deploy_handler") - - -@pytest.fixture() -def fix_cherrypy_engine_exit(monkeypatch): - """monkeyed cherrypy.engine.exit()""" - Settings.logger.info("setup fix_cherrypy_engine_exit") - - def monkeyed_cherrypy_engine_exit(): - """monkeypatch for deploy_handler""" - Settings.logger.info("cherrypy_engine_exit()") - - monkeypatch.setattr('policyhandler.web_server.cherrypy.engine.exit', - monkeyed_cherrypy_engine_exit) - yield fix_cherrypy_engine_exit - Settings.logger.info("teardown fix_cherrypy_engine_exit") + _LOGGER.info("teardown fix_deploy_handler") @pytest.fixture() -def fix_pdp_post_big(monkeypatch): - """monkeyed request /getConfig to PDP""" - def monkeyed_policy_rest_post(uri, **kwargs): - """monkeypatch for the POST to policy-engine""" - res_json = MockPolicyEngine.get_configs_all() - return MockHttpResponse("post", uri, res_json, **kwargs) - - Settings.logger.info("setup fix_pdp_post_big") - PolicyRest._lazy_init() - monkeypatch.setattr('policyhandler.policy_rest.PolicyRest._requests_session.post', - monkeyed_policy_rest_post) - yield fix_pdp_post_big - Settings.logger.info("teardown fix_pdp_post_big") - - -class MockException(Exception): - """mock exception""" - pass - - -@pytest.fixture() -def fix_pdp_post_boom(monkeypatch): - """monkeyed request /getConfig to PDP - exception""" - def monkeyed_policy_rest_post_boom(uri, **_): - """monkeypatch for the POST to policy-engine""" - raise MockException("fix_pdp_post_boom {}".format(uri)) - - Settings.logger.info("setup fix_pdp_post_boom") - PolicyRest._lazy_init() - monkeypatch.setattr('policyhandler.policy_rest.PolicyRest._requests_session.post', - monkeyed_policy_rest_post_boom) - yield fix_pdp_post_boom - Settings.logger.info("teardown fix_pdp_post_boom") - - -@pytest.fixture() -def fix_select_latest_policies_boom(monkeypatch): - """monkeyed exception""" - def monkeyed_boom(*args, **kwargs): - """monkeypatch for the select_latest_policies""" - raise MockException("monkeyed_boom") - - Settings.logger.info("setup fix_select_latest_policies_boom") - monkeypatch.setattr('policyhandler.policy_utils.PolicyUtils.select_latest_policies', - monkeyed_boom) - monkeypatch.setattr('policyhandler.policy_utils.PolicyUtils.select_latest_policy', - monkeyed_boom) - monkeypatch.setattr('policyhandler.policy_utils.PolicyUtils.extract_policy_id', - monkeyed_boom) - - yield fix_select_latest_policies_boom - Settings.logger.info("teardown fix_select_latest_policies_boom") - -@pytest.fixture() def fix_discovery(monkeypatch): """monkeyed discovery request.get""" def monkeyed_discovery(uri): @@ -203,29 +133,28 @@ def fix_discovery(monkeypatch): elif uri == DiscoveryClient.CONSUL_KV_MASK.format( Config.consul_url, Config.system_name): res_json = [{"Value": base64.b64encode( - json.dumps(Settings.mock_config).encode()).decode("utf-8")}] + json.dumps(MockSettings.mock_config).encode()).decode("utf-8")}] return MockHttpResponse("get", uri, res_json) - Settings.logger.info("setup fix_discovery") + _LOGGER.info("setup fix_discovery") monkeypatch.setattr('policyhandler.discovery.requests.get', monkeyed_discovery) yield fix_discovery - Settings.logger.info("teardown fix_discovery") - + _LOGGER.info("teardown fix_discovery") @pytest.fixture(scope="module") def fix_auto_catch_up(): """increase the frequency of auto catch_up""" - Settings.logger.info("setup fix_auto_catch_up %s", json.dumps(Settings.mock_config)) - prev_config = copy.deepcopy(Settings.mock_config) - Settings.mock_config.get(Config.SERVICE_NAME_POLICY_HANDLER, {}) \ + _LOGGER.info("setup fix_auto_catch_up %s", json.dumps(MockSettings.mock_config)) + prev_config = copy.deepcopy(MockSettings.mock_config) + MockSettings.mock_config.get(Config.SERVICE_NAME_POLICY_HANDLER, {}) \ .get(CATCH_UP, {})[Config.TIMER_INTERVAL] = 5 - Settings.logger.info("fix_auto_catch_up %s", json.dumps(Settings.mock_config)) - Settings.rediscover_config() + _LOGGER.info("fix_auto_catch_up %s", json.dumps(MockSettings.mock_config)) + MockSettings.rediscover_config() yield fix_auto_catch_up - Settings.rediscover_config(prev_config) - Settings.logger.info("teardown fix_auto_catch_up") + MockSettings.rediscover_config(prev_config) + _LOGGER.info("teardown fix_auto_catch_up") @pytest.fixture() @@ -235,7 +164,7 @@ def fix_deploy_handler_413(monkeypatch): """monkeypatch for deploy_handler""" return MockHttpResponse( "put", uri, - {"server_instance_uuid": Settings.deploy_handler_instance_uuid}, + {"server_instance_uuid": MockSettings.deploy_handler_instance_uuid}, status_code=413, **kwargs ) @@ -244,7 +173,7 @@ def fix_deploy_handler_413(monkeypatch): return MockHttpResponse("get", uri, MockDeploymentHandler.get_deployed_policies(), **kwargs) - Settings.logger.info("setup fix_deploy_handler_413") + _LOGGER.info("setup fix_deploy_handler_413") audit = None if DeployHandler._lazy_inited is False: audit = Audit(req_message="fix_deploy_handler_413") @@ -258,7 +187,7 @@ def fix_deploy_handler_413(monkeypatch): yield fix_deploy_handler_413 if audit: audit.audit_done("teardown") - Settings.logger.info("teardown fix_deploy_handler_413") + _LOGGER.info("teardown fix_deploy_handler_413") @pytest.fixture() @@ -274,7 +203,7 @@ def fix_deploy_handler_404(monkeypatch): return MockHttpResponse("get", uri, MockDeploymentHandler.default_response(), **kwargs) - Settings.logger.info("setup fix_deploy_handler_404") + _LOGGER.info("setup fix_deploy_handler_404") audit = None if DeployHandler._lazy_inited is False: audit = Audit(req_message="fix_deploy_handler_404") @@ -288,12 +217,4 @@ def fix_deploy_handler_404(monkeypatch): yield fix_deploy_handler_404 if audit: audit.audit_done("teardown") - Settings.logger.info("teardown fix_deploy_handler_404") - -@pytest.fixture() -def fix_policy_receiver_websocket(monkeypatch): - """monkeyed websocket for policy_receiver""" - Settings.logger.info("setup fix_policy_receiver_websocket") - monkeypatch.setattr('policyhandler.policy_receiver.websocket', MockWebSocket) - yield fix_policy_receiver_websocket - Settings.logger.info("teardown fix_policy_receiver_websocket") + _LOGGER.info("teardown fix_deploy_handler_404") diff --git a/tests/etc_config.json b/tests/etc_config.json new file mode 100644 index 0000000..d7715b2 --- /dev/null +++ b/tests/etc_config.json @@ -0,0 +1,47 @@ +{ + "wservice_port" : 25577, + "consul_url" : "http://unit-test-consul:850000", + "consul_timeout_in_secs" : 60, + "pdp_api_version" : null, + "policy_handler" : { + "system" : "policy_handler", + "tls" : { + "cert_directory" : "etc/tls/certs/", + "cacert" : "att_internal_root.crt", + "private_key" : "key.pem", + "server_cert" : "cert.pem", + "server_ca_chain" : "ca_chain.pem" + } + }, + "logging" : { + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "local": { + "format": "%(asctime)s.%(msecs)03d %(levelname)+8s %(threadName)s %(name)s.%(funcName)s: %(message)s", + "datefmt": "%Y%m%d_%H%M%S" + } + }, + "handlers": { + "file": { + "class": "logging.handlers.TimedRotatingFileHandler", + "formatter": "local", + "filename" : "logs/policy_handler.log", + "level": "DEBUG", + "when": "midnight", + "interval": 1, + "backupCount": 10, + "delay": true + } + }, + "loggers": { + "policy_handler" : { + "handlers": ["file"], + "propagate":false + } + }, + "root": { + "handlers": ["file"] + } + } +}
\ No newline at end of file diff --git a/tests/main/__init__.py b/tests/main/__init__.py new file mode 100644 index 0000000..b8e4605 --- /dev/null +++ b/tests/main/__init__.py @@ -0,0 +1,20 @@ +# ================================================================================ +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# + + +# empty __init__.py so that pytest can add correct path to coverage report, -- per pytest +# best practice guideline diff --git a/tests/main/conftest.py b/tests/main/conftest.py new file mode 100644 index 0000000..c985380 --- /dev/null +++ b/tests/main/conftest.py @@ -0,0 +1,56 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +""" +startdard pytest file that contains the shared fixtures +https://docs.pytest.org/en/latest/fixture.html +""" + +import pytest + +from policyhandler import pdp_client +from policyhandler.pdp_api.pdp_consts import PDP_POLICY_ID, PDP_REQ_RESOURCE +from policyhandler.utils import Utils + +from ..mock_tracker import MockHttpResponse +from .mock_policy_engine import MockPolicyEngine + +_LOGGER = Utils.get_logger(__file__) + +@pytest.fixture(scope="session", autouse=True) +def _auto_setup_policy_engine(): + """initialize the mock-policy-engine per the whole test session""" + _LOGGER.info("create _auto_setup_policy_engine") + MockPolicyEngine.init() + yield _auto_setup_policy_engine + _LOGGER.info("teardown _auto_setup_policy_engine") + +@pytest.fixture() +def fix_pdp_post(monkeypatch): + """monkeyed request /decision/v1 to PDP""" + def monkeyed_policy_rest_post(uri, json=None, **kwargs): + """monkeypatch for the POST to policy-engine""" + policy_ids = json.get(PDP_REQ_RESOURCE, {}).get(PDP_POLICY_ID) + policy_id = policy_ids and policy_ids[0] + res_json = MockPolicyEngine.get_policy(policy_id) + return MockHttpResponse("post", uri, res_json, json=json, **kwargs) + + _LOGGER.info("setup fix_pdp_post") + pdp_client.PolicyRest._lazy_init() + monkeypatch.setattr('policyhandler.pdp_client.PolicyRest._requests_session.post', + monkeyed_policy_rest_post) + yield fix_pdp_post + _LOGGER.info("teardown fix_pdp_post") diff --git a/tests/main/mock_expected.py b/tests/main/mock_expected.py new file mode 100644 index 0000000..450394c --- /dev/null +++ b/tests/main/mock_expected.py @@ -0,0 +1,526 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +"""expected message history per test for pdp API 2019 and after""" + + +HISTORY_EXPECTED = { + "tests/main/test_policy_rest.py::test_get_policy_latest" : [ + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "ONAPComponent": "policy_handler", + "ONAPInstance": "*", + "ONAPName": "DCAE", + "action": "configure", + "resource": { + "policy-id": [ + "test_scope_prefix.pdp_desition_sit" + ] + } + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/decision/v1/" + }, + "res": "*", + "status_code": 200 + } + ], + "tests/main/test_policyhandler.py::WebServerTest::test_web_all_policies_latest": [ + { + "request": { + "headers": { + "X-ECOMP-RequestID": "*" + }, + "json": None, + "method": "get", + "params": { + "cfy_tenant_name": "default_tenant" + }, + "uri": "http://unit-test-deployment_handler:8188000/policy" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_.*" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + } + ], + "tests/main/test_policyhandler.py::WebServerTest::test_web_policies_latest": [ + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_amet.*" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + } + ], + "tests/main/test_policyhandler.py::WebServerTest::test_web_policy_latest": [ + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "ONAPComponent": "policy_handler", + "ONAPInstance": "*", + "ONAPName": "DCAE", + "action": "configure", + "resource": { + "policy-id": [ + "test_scope_prefix.pdp_desition_sit" + ] + } + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/decision/v1/" + }, + "res": "*", + "status_code": 200 + } + ], + "tests/main/test_policyhandler.py::WebServerTest::test_zzzzz_shutdown": [ + { + "request": { + "headers": { + "X-ECOMP-RequestID": "*" + }, + "json": None, + "method": "get", + "params": { + "cfy_tenant_name": "default_tenant" + }, + "uri": "http://unit-test-deployment_handler:8188000/policy" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_.*" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "X-ECOMP-RequestID": "*" + }, + "json": { + "catch_up": True, + "latest_policies": { + "test_scope_prefix.Config_Lorem": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "0", + "policy_updated_to_ver": "1", + "updated_policy_id": "test_scope_prefix.Config_Lorem" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_Lorem.1.xml", + "policyVersion": "1", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_Lorem" + }, + "test_scope_prefix.Config_amet": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "4", + "policy_updated_to_ver": "5", + "updated_policy_id": "test_scope_prefix.Config_amet" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_amet.5.xml", + "policyVersion": "5", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_amet" + }, + "test_scope_prefix.Config_ametist": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "6", + "policy_updated_to_ver": "7", + "updated_policy_id": "test_scope_prefix.Config_ametist" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_ametist.7.xml", + "policyVersion": "7", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_ametist" + }, + "test_scope_prefix.Config_consectetur": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "5", + "policy_updated_to_ver": "6", + "updated_policy_id": "test_scope_prefix.Config_consectetur" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_consectetur.6.xml", + "policyVersion": "6", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_consectetur" + }, + "test_scope_prefix.Config_dolor": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "2", + "policy_updated_to_ver": "3", + "updated_policy_id": "test_scope_prefix.Config_dolor" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_dolor.3.xml", + "policyVersion": "3", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_dolor" + }, + "test_scope_prefix.Config_ipsum": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "1", + "policy_updated_to_ver": "2", + "updated_policy_id": "test_scope_prefix.Config_ipsum" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_ipsum.2.xml", + "policyVersion": "2", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_ipsum" + }, + "test_scope_prefix.Config_sit": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "3", + "policy_updated_to_ver": "4", + "updated_policy_id": "test_scope_prefix.Config_sit" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_sit.4.xml", + "policyVersion": "4", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_sit" + } + }, + "policy_filter_matches": { + "test_scope_prefix.Config_Lorem": {}, + "test_scope_prefix.Config_amet": {}, + "test_scope_prefix.Config_ametist": {}, + "test_scope_prefix.Config_consectetur": {}, + "test_scope_prefix.Config_dolor": {}, + "test_scope_prefix.Config_ipsum": {}, + "test_scope_prefix.Config_sit": {} + }, + "removed_policies": {} + }, + "method": "put", + "params": { + "cfy_tenant_name": "default_tenant" + }, + "uri": "http://unit-test-deployment_handler:8188000/policy" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "X-ECOMP-RequestID": "*" + }, + "json": None, + "method": "get", + "params": { + "cfy_tenant_name": "default_tenant" + }, + "uri": "http://unit-test-deployment_handler:8188000/policy" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_ipsum" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_sit" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "Accept": "application/json", + "Authorization": "Basic auth", + "ClientAuth": "Basic user", + "Content-Type": "application/json", + "Environment": "TEST", + "X-ECOMP-RequestID": "*" + }, + "json": { + "policyName": "test_scope_prefix.Config_consectetur" + }, + "method": "post", + "params": None, + "uri": "https://unit-test-pdp-server:8081000/pdp/api/getConfig" + }, + "res": "*", + "status_code": 200 + }, + { + "request": { + "headers": { + "X-ECOMP-RequestID": "*" + }, + "json": { + "catch_up": False, + "latest_policies": { + "test_scope_prefix.Config_consectetur": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "5", + "policy_updated_to_ver": "6", + "updated_policy_id": "test_scope_prefix.Config_consectetur" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_consectetur.6.xml", + "policyVersion": "6", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_consectetur" + }, + "test_scope_prefix.Config_ipsum": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "1", + "policy_updated_to_ver": "2", + "updated_policy_id": "test_scope_prefix.Config_ipsum" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_ipsum.2.xml", + "policyVersion": "2", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_ipsum" + }, + "test_scope_prefix.Config_sit": { + "policy_body": { + "config": { + "policy_hello": "world!", + "policy_updated_from_ver": "3", + "policy_updated_to_ver": "4", + "updated_policy_id": "test_scope_prefix.Config_sit" + }, + "matchingConditions": { + "ConfigName": "alex_config_name", + "ONAPName": "DCAE" + }, + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "policyName": "test_scope_prefix.Config_sit.4.xml", + "policyVersion": "4", + "property": None, + "responseAttributes": {}, + "type": "JSON" + }, + "policy_id": "test_scope_prefix.Config_sit" + } + }, + "policy_filter_matches": { + "test_scope_prefix.Config_consectetur": {}, + "test_scope_prefix.Config_ipsum": {}, + "test_scope_prefix.Config_sit": {} + }, + "removed_policies": {} + }, + "method": "put", + "params": { + "cfy_tenant_name": "default_tenant" + }, + "uri": "http://unit-test-deployment_handler:8188000/policy" + }, + "res": "*", + "status_code": 200 + } + ] +} diff --git a/tests/main/mock_policy_engine.py b/tests/main/mock_policy_engine.py new file mode 100644 index 0000000..1bac045 --- /dev/null +++ b/tests/main/mock_policy_engine.py @@ -0,0 +1,96 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +"""mocking for the policy-engine - shared by many tests""" + +import copy +import json + +from policyhandler.pdp_api.pdp_consts import (PDP_METADATA, + PDP_POLICY_ID, + PDP_POLICY_VERSION, + PDP_POLICIES, PDP_PROPERTIES) +from policyhandler.pdp_api.policy_utils import PolicyUtils +from policyhandler.utils import Utils + +_LOGGER = Utils.get_logger(__file__) + + +class MockPolicyEngine(object): + """pretend this is the policy-engine""" + scope_prefix = "test_scope_prefix.pdp_desition_" + LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split() + LONG_TEXT = "0123456789" * 100 + _policies = {} + + _inited = False + + @staticmethod + def init(): + """init collection of policies: policy_version = policy_index + 1""" + if MockPolicyEngine._inited: + return + MockPolicyEngine._inited = True + + MockPolicyEngine._policies = dict( + (policy_id, MockPolicyEngine._create_policy_body(policy_id, policy_version)) + for policy_id, policy_version in + [(MockPolicyEngine.get_policy_id(policy_index), policy_index + 1) + for policy_index in range(1 + len(MockPolicyEngine.LOREM_IPSUM))] + ) + _LOGGER.info("_policies: %s", json.dumps(MockPolicyEngine._policies)) + + @staticmethod + def get_policy_id(policy_index): + """get the policy_id by policy_index""" + return (MockPolicyEngine.scope_prefix + + MockPolicyEngine.LOREM_IPSUM[ + policy_index % len(MockPolicyEngine.LOREM_IPSUM)]) + + @staticmethod + def get_policy(policy_id): + """find policy the way the policy-engine finds""" + if policy_id not in MockPolicyEngine._policies: + return {} + return {PDP_POLICIES: {policy_id: copy.deepcopy(MockPolicyEngine._policies[policy_id])}} + + @staticmethod + def gen_policy_latest(policy_index, version_offset=0): + """generate the policy response from policy-handler by policy_index = version - 1""" + policy_id = MockPolicyEngine.get_policy_id(policy_index) + policy = PolicyUtils.convert_to_policy( + MockPolicyEngine._create_policy_body(policy_id, policy_index + 1 - version_offset) + ) + return policy_id, policy + + @staticmethod + def _create_policy_body(policy_id, policy_version=1): + """returns a fake policy-body""" + return { + "type": "unit.test.type.policies", + "version": "1.0.0", + PDP_METADATA: { + PDP_POLICY_ID: policy_id, + PDP_POLICY_VERSION: policy_version, + "description": "description for {}".format(policy_id) + }, + PDP_PROPERTIES: { + "policy_updated_from_ver": (policy_version - 1), + "policy_updated_to_ver": policy_version, + "policy_hello": "world!", + "updated_policy_id": policy_id + } + } diff --git a/tests/main/pdp_policies.json b/tests/main/pdp_policies.json new file mode 100644 index 0000000..b7d0d9e --- /dev/null +++ b/tests/main/pdp_policies.json @@ -0,0 +1,25 @@ +{ + "policies": { + "onap.scaleout.tca": { + "type": "onap.policies.monitoring.cdap.tca.hi.lo.app", + "version": "1.0.0", + "metadata": { + "policy-id": "onap.scaleout.tca", + "policy-version": 1, + "description": "The scaleout policy for vDNS" + }, + "properties": { + "tca_policy": { + "domain": "measurementsForVfScaling", + "metricsPerEventName": [ + { + "eventName": "vLoadBalancer", + "controlLoopSchemaType": "VNF", + "policyScope": "type=configuration" + } + ] + } + } + } + } +}
\ No newline at end of file diff --git a/tests/test_policy_rest.py b/tests/main/test_policy_rest.py index ac2529a..964a21d 100644 --- a/tests/test_policy_rest.py +++ b/tests/main/test_policy_rest.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,21 +14,20 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test policy_rest methods directly""" import json import pytest +from policyhandler import pdp_client from policyhandler.onap.audit import Audit -from policyhandler.policy_rest import PolicyRest -from policyhandler.policy_utils import Utils +from policyhandler.utils import Utils +from ..mock_tracker import Tracker from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings -from .mock_tracker import Tracker +_LOGGER = Utils.get_logger(__file__) @pytest.mark.usefixtures("fix_pdp_post") def test_get_policy_latest(): @@ -37,11 +36,12 @@ def test_get_policy_latest(): audit = Audit(job_name="test_get_policy_latest", req_message="get /policy_latest/{}".format(policy_id or "")) - policy_latest = PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} + + policy_latest = pdp_client.PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} audit.audit_done(result=json.dumps(policy_latest)) - Settings.logger.info("expected_policy: %s", json.dumps(expected_policy)) - Settings.logger.info("policy_latest: %s", json.dumps(policy_latest)) + _LOGGER.info("expected_policy: %s", json.dumps(expected_policy)) + _LOGGER.info("policy_latest: %s", json.dumps(policy_latest)) assert Utils.are_the_same(policy_latest, expected_policy) Tracker.validate() diff --git a/tests/main/test_policyhandler.py b/tests/main/test_policyhandler.py new file mode 100644 index 0000000..73ab9ca --- /dev/null +++ b/tests/main/test_policyhandler.py @@ -0,0 +1,130 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# + +"""test of the package for policy-handler of DCAE-Controller""" + +import json +import time +import uuid + +import cherrypy +import pytest +from cherrypy.test.helper import CPWebCase + +from policyhandler.config import Config +from policyhandler.onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit +from policyhandler.pdp_api.pdp_consts import POLICY_NAME +from policyhandler.policy_consts import LATEST_POLICIES +from policyhandler.policy_receiver import PolicyReceiver +from policyhandler.utils import Utils +from policyhandler.web_server import _PolicyWeb + +from ..mock_tracker import Tracker +from .mock_policy_engine import MockPolicyEngine + +_LOGGER = Utils.get_logger(__file__) + + +@pytest.mark.usefixtures("fix_pdp_post", "fix_discovery") +class WebServerTest(CPWebCase): + """testing the web-server - runs tests in alphabetical order of method names""" + def setup_server(): + """setup the web-server""" + cherrypy.tree.mount(_PolicyWeb(), '/') + + setup_server = staticmethod(setup_server) + + def test_web_healthcheck(self): + """test /healthcheck""" + result = self.getPage("/healthcheck") + _LOGGER.info("healthcheck result: %s", result) + _LOGGER.info("got healthcheck: %s", self.body) + self.assertStatus('200 OK') + + Tracker.validate() + + def test_web_policy_latest(self): + """test /policy_latest/<policy-id>""" + policy_id, expected_policy = MockPolicyEngine.gen_policy_latest(3) + + self.getPage("/policy_latest/{0}".format(policy_id or "")) + self.assertStatus('200 OK') + + policy_latest = json.loads(self.body) + + _LOGGER.info("policy_latest: %s", self.body) + _LOGGER.info("expected_policy: %s", json.dumps(expected_policy)) + assert Utils.are_the_same(policy_latest, expected_policy) + + result = self.getPage("/healthcheck") + _LOGGER.info("healthcheck result: %s", result) + + Tracker.validate() + + @pytest.mark.usefixtures("fix_deploy_handler") + def test_web_all_policies_latest(self): + """test GET /policies_latest""" + + result = self.getPage("/policies_latest") + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + self.assertStatus('404 Not Found') + + def test_web_policies_latest(self): + """test POST /policies_latest with policyName""" + body = json.dumps({"junk": "to-be-developed"}) + result = self.getPage("/policies_latest", method='POST', + body=body, + headers=[ + (REQUEST_X_ECOMP_REQUESTID, str(uuid.uuid4())), + ("Content-Type", "application/json"), + ('Content-Length', str(len(body))) + ]) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + self.assertStatus('404 Not Found') + + @pytest.mark.usefixtures( + "fix_deploy_handler", + "fix_cherrypy_engine_exit") + def test_zzzzz_shutdown(self): + """test shutdown""" + _LOGGER.info("start shutdown") + assert not PolicyReceiver.is_running() + audit = Audit(job_name="test_zzzzz_shutdown", req_message="start shutdown") + PolicyReceiver.run(audit) + + result = self.getPage("/healthcheck") + _LOGGER.info("healthcheck result: %s", result) + + WebServerTest.do_gc_test = False + _LOGGER.info("shutdown...") + audit.audit_done("shutdown") + result = self.getPage("/shutdown") + _LOGGER.info("shutdown result: %s", result) + self.assertStatus('200 OK') + _LOGGER.info("got shutdown: %s", self.body) + time.sleep(1) + assert not PolicyReceiver.is_running() + + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + Tracker.validate() diff --git a/tests/mock_config.json b/tests/mock_config.json index ef02a15..30e3251 100644 --- a/tests/mock_config.json +++ b/tests/mock_config.json @@ -13,6 +13,7 @@ }, "policy_engine" : { "url" : "https://unit-test-pdp-server:8081000", + "path_decision" : "/decision/v1/", "path_notifications" : "/pdp/notifications", "path_api" : "/pdp/api/", "headers" : { diff --git a/tests/mock_deploy_handler.py b/tests/mock_deploy_handler.py index ebbbfc7..cd8e791 100644 --- a/tests/mock_deploy_handler.py +++ b/tests/mock_deploy_handler.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,14 +14,13 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """mocking for the deployment-handler - shared by many tests""" -from policyhandler.policy_consts import (POLICY_BODY, POLICY_ID, - POLICY_VERSION, POLICY_VERSIONS) +from policyhandler.pdp_api_v0.pdp_consts import POLICY_VERSION +from policyhandler.policy_consts import POLICY_BODY, POLICY_ID, POLICY_VERSIONS -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings +from .mock_settings import MockSettings +from .pdp_api_v0.mock_policy_engine import MockPolicyEngine2018 class MockDeploymentHandler(object): @@ -30,7 +29,7 @@ class MockDeploymentHandler(object): @staticmethod def default_response(): """generate the deployed policies message""" - return {"server_instance_uuid": Settings.deploy_handler_instance_uuid} + return {"server_instance_uuid": MockSettings.deploy_handler_instance_uuid} @staticmethod def get_deployed_policies(): @@ -42,7 +41,7 @@ class MockDeploymentHandler(object): POLICY_VERSIONS: {policy.get(POLICY_BODY, {}).get(POLICY_VERSION, "999"): True}, "pending_update": False}) for policy_id, policy in ( - MockPolicyEngine.gen_all_policies_latest(version_offset=1).items())) + MockPolicyEngine2018.gen_all_policies_latest(version_offset=1).items())) response["policies"] = policies return response diff --git a/tests/mock_settings.py b/tests/mock_settings.py index 8dec8e5..9e99561 100644 --- a/tests/mock_settings.py +++ b/tests/mock_settings.py @@ -14,12 +14,12 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """settings that are general to all tests""" import copy +import importlib import json -import logging +import os import sys import uuid from functools import wraps @@ -29,7 +29,9 @@ from policyhandler.config import Config from policyhandler.discovery import DiscoveryClient from policyhandler.onap.audit import Audit from policyhandler.service_activator import ServiceActivator +from policyhandler.utils import Utils +_LOGGER = Utils.get_logger(__file__) def _fix_discover_config(func): """the decorator""" @@ -38,7 +40,7 @@ def _fix_discover_config(func): def mocked_discover_get_value(*_): """monkeypatch for get from consul""" - return copy.deepcopy(Settings.mock_config) + return copy.deepcopy(MockSettings.mock_config) @wraps(func) def wrapper(*args, **kwargs): @@ -54,49 +56,65 @@ def _fix_discover_config(func): return func_result return wrapper -class Settings(object): +class MockSettings(object): """init all locals""" + PDP_API_VERSION = "PDP_API_VERSION" + OLD_PDP_API_VERSION = "pdp_api_v0" _loaded = False - logger = None mock_config = None deploy_handler_instance_uuid = str(uuid.uuid4()) @staticmethod def init(): """init configs""" - if Settings._loaded: - Settings.logger.info("testing policy_handler with config: %s", Config.discovered_config) + if MockSettings._loaded: + _LOGGER.info("testing policy_handler with config: %s", Config.discovered_config) return - Settings._loaded = True + MockSettings._loaded = True - Config.init_config() - - Config.consul_url = "http://unit-test-consul:850000" + Config.init_config("tests/etc_config.json") with open("tests/mock_config.json", 'r') as config_json: - Settings.mock_config = json.load(config_json) + MockSettings.mock_config = json.load(config_json) - Settings.logger = logging.getLogger("policy_handler.unit_test") - sys.stdout = LogWriter(Settings.logger.info) - sys.stderr = LogWriter(Settings.logger.error) + sys.stdout = LogWriter(_LOGGER.info) + sys.stderr = LogWriter(_LOGGER.error) print("print is expected to be in the log") - Settings.logger.info("========== run_policy_handler ==========") + _LOGGER.info("========== run_policy_handler ==========") Audit.init(Config.system_name, Config.LOGGER_CONFIG_FILE_PATH) - Settings.rediscover_config() + MockSettings.rediscover_config() @staticmethod @_fix_discover_config def rediscover_config(updated_config=None): """rediscover the config""" if updated_config is not None: - Settings.mock_config = copy.deepcopy(updated_config) + MockSettings.mock_config = copy.deepcopy(updated_config) audit = Audit(req_message="rediscover_config") Config.discover(audit) ServiceActivator.determine_mode_of_operation(audit) - Settings.logger.info("testing policy_handler with config: %s", Config.discovered_config) + _LOGGER.info("testing policy_handler with config: %s", Config.discovered_config) audit.audit_done(" -- started") + + @staticmethod + def setup_pdp_api(pdp_api_version=None): + """set the environment var for pdp_api""" + if Config._pdp_api_version == pdp_api_version: + _LOGGER.info("unchanged setup_pdp_api %s", pdp_api_version) + return + + _LOGGER.info("setup_pdp_api %s -> %s", Config._pdp_api_version, pdp_api_version) + + if pdp_api_version: + os.environ[MockSettings.PDP_API_VERSION] = pdp_api_version + elif MockSettings.PDP_API_VERSION in os.environ: + del os.environ[MockSettings.PDP_API_VERSION] + Config._pdp_api_version = pdp_api_version + + importlib.reload(importlib.import_module("policyhandler.pdp_client")) + _LOGGER.info("done setup_pdp_api %s", Config._pdp_api_version) diff --git a/tests/mock_tracker.py b/tests/mock_tracker.py index 4e69afd..a5fffd5 100644 --- a/tests/mock_tracker.py +++ b/tests/mock_tracker.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,19 +14,18 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """record all the messages going outside policy-handler during testing""" import copy import json +from policyhandler.config import Config from policyhandler.onap.audit import REQUEST_X_ECOMP_REQUESTID -from policyhandler.policy_utils import Utils - -from .mock_expected import HISTORY_EXPECTED -from .mock_settings import Settings +from policyhandler.utils import Utils RESPONSE = "res" +PEP_INSTANCE = "ONAPInstance" +_LOGGER = Utils.get_logger(__file__) class _MockHttpRequestInResponse(object): """Mock http request in reponse object""" @@ -89,20 +88,24 @@ class Tracker(object): """record all the messages going outside policy-handler during testing""" test_name = None messages = [] + test_names = [] + validated_tests = {} + valid_tests = {} @staticmethod - def reset(test_name): + def reset(test_name=None): """remove all the messages from history""" Tracker.test_name = test_name Tracker.messages.clear() + Tracker.test_names.append(test_name) @staticmethod def track(message): """append the tracked message to the history""" message = copy.deepcopy(message) Tracker.messages.append(message) - if Settings.logger: - Settings.logger.info("tracked_message: %s", json.dumps(message, sort_keys=True)) + if _LOGGER: + _LOGGER.info("tracked_message: %s", json.dumps(message, sort_keys=True)) @staticmethod def to_string(): @@ -110,13 +113,44 @@ class Tracker(object): return json.dumps(Tracker.messages, sort_keys=True) @staticmethod + def get_status(test_name=None): + """get the status of validation""" + if Tracker.valid_tests.get(test_name): + return "success" + if Tracker.validated_tests.get(test_name): + return "failed" + if test_name in Tracker.test_names: + return "covered" + return "unknown" + + @staticmethod + def log_all_tests(): + """log the covered and not covered test names""" + _LOGGER.info("-"*75) + _LOGGER.info("tracked test_names[%s]", len(Tracker.test_names)) + for idx, test_name in enumerate(Tracker.test_names): + _LOGGER.info("%s[%s]: %s", Tracker.get_status(test_name), (idx + 1), test_name) + + _LOGGER.info("not tracked test_names listed in main.mock_expected") + from .main.mock_expected import HISTORY_EXPECTED as main_history + for test_name in main_history: + if test_name not in Tracker.test_names: + _LOGGER.info("untracked: %s", test_name) + + _LOGGER.info("not tracked test_names listed in pdp_api_v0.mock_expected") + from .pdp_api_v0.mock_expected import HISTORY_EXPECTED as pdp_api_v0_history + for test_name in pdp_api_v0_history: + if test_name not in Tracker.test_names: + _LOGGER.info("untracked: %s", test_name) + + @staticmethod def _hide_volatiles(obj): """hides the volatile field values""" if not isinstance(obj, dict): return obj for key, value in obj.items(): - if key in [REQUEST_X_ECOMP_REQUESTID, RESPONSE]: + if key in [REQUEST_X_ECOMP_REQUESTID, RESPONSE, PEP_INSTANCE]: obj[key] = "*" elif isinstance(value, dict): obj[key] = Tracker._hide_volatiles(value) @@ -126,13 +160,21 @@ class Tracker(object): @staticmethod def validate(): """validate that the message history is as expected""" - Settings.logger.info("Tracker.validate(%s)", Tracker.test_name) + _LOGGER.info("Tracker.validate(%s)", Tracker.test_name) messages = [Tracker._hide_volatiles(copy.deepcopy(message)) for message in Tracker.messages] - expected = HISTORY_EXPECTED.get(Tracker.test_name, []) + Tracker.validated_tests[Tracker.test_name] = True + + if Config.is_pdp_api_default(): + from .main.mock_expected import HISTORY_EXPECTED as main_history + expected = main_history.get(Tracker.test_name, []) + else: + from .pdp_api_v0.mock_expected import HISTORY_EXPECTED as pdp_api_v0_history + expected = pdp_api_v0_history.get(Tracker.test_name, []) - Settings.logger.info("messages: %s", json.dumps(messages, sort_keys=True)) - Settings.logger.info("expected: %s", json.dumps(expected, sort_keys=True)) + _LOGGER.info("messages: %s", json.dumps(messages, sort_keys=True)) + _LOGGER.info("expected: %s", json.dumps(expected, sort_keys=True)) assert Utils.are_the_same(messages, expected) - Settings.logger.info("history valid for Tracker.validate(%s)", Tracker.test_name) + _LOGGER.info("history valid for Tracker.validate(%s)", Tracker.test_name) + Tracker.valid_tests[Tracker.test_name] = True diff --git a/tests/pdp_api_v0/__init__.py b/tests/pdp_api_v0/__init__.py new file mode 100644 index 0000000..b8e4605 --- /dev/null +++ b/tests/pdp_api_v0/__init__.py @@ -0,0 +1,20 @@ +# ================================================================================ +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# + + +# empty __init__.py so that pytest can add correct path to coverage report, -- per pytest +# best practice guideline diff --git a/tests/pdp_api_v0/conftest.py b/tests/pdp_api_v0/conftest.py new file mode 100644 index 0000000..07e566f --- /dev/null +++ b/tests/pdp_api_v0/conftest.py @@ -0,0 +1,133 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +""" +startdard pytest file that contains the shared fixtures +https://docs.pytest.org/en/latest/fixture.html +""" + +import pytest + +from policyhandler import pdp_client +from policyhandler.pdp_api_v0.pdp_consts import POLICY_NAME +from policyhandler.utils import Utils + +from ..mock_settings import MockSettings +from ..mock_tracker import MockHttpResponse +from .mock_policy_engine import MockPolicyEngine2018 +from .mock_websocket import MockWebSocket + +_LOGGER = Utils.get_logger(__file__) + +@pytest.fixture(scope="session", autouse=True) +def _auto_setup_policy_engine_pdp_api_v0(): + """initialize the mock-policy-engine_pdp_api_v0 per the whole test session""" + _LOGGER.info("create _auto_setup_policy_engine_pdp_api_v0") + MockPolicyEngine2018.init() + yield _auto_setup_policy_engine_pdp_api_v0 + _LOGGER.info("teardown _auto_setup_policy_engine_pdp_api_v0") + + +@pytest.fixture(scope="module") +def fix_pdp_api_v0(): + """test on the old (pdp_api_v0) pdp API""" + _LOGGER.info("setup fix_pdp_api_v0 %s", MockSettings.OLD_PDP_API_VERSION) + MockSettings.setup_pdp_api(MockSettings.OLD_PDP_API_VERSION) + + yield fix_pdp_api_v0 + MockSettings.setup_pdp_api() + _LOGGER.info("teardown fix_pdp_api_v0 %s", MockSettings.OLD_PDP_API_VERSION) + +@pytest.fixture() +def fix_pdp_post(monkeypatch): + """monkeyed request /getConfig to PDP""" + def monkeyed_policy_rest_post(uri, json=None, **kwargs): + """monkeypatch for the POST to policy-engine""" + res_json = MockPolicyEngine2018.get_config(json.get(POLICY_NAME)) + return MockHttpResponse("post", uri, res_json, json=json, **kwargs) + + _LOGGER.info("setup fix_pdp_post") + pdp_client.PolicyRest._lazy_init() + monkeypatch.setattr('policyhandler.pdp_client.PolicyRest._requests_session.post', + monkeyed_policy_rest_post) + yield fix_pdp_post + _LOGGER.info("teardown fix_pdp_post") + +@pytest.fixture() +def fix_pdp_post_big(monkeypatch): + """monkeyed request /getConfig to PDP""" + def monkeyed_policy_rest_post(uri, **kwargs): + """monkeypatch for the POST to policy-engine""" + res_json = MockPolicyEngine2018.get_configs_all() + return MockHttpResponse("post", uri, res_json, **kwargs) + + _LOGGER.info("setup fix_pdp_post_big") + pdp_client.PolicyRest._lazy_init() + monkeypatch.setattr('policyhandler.pdp_client.PolicyRest._requests_session.post', + monkeyed_policy_rest_post) + yield fix_pdp_post_big + _LOGGER.info("teardown fix_pdp_post_big") + + +class MockException(Exception): + """mock exception""" + pass + +@pytest.fixture() +def fix_pdp_post_boom(monkeypatch): + """monkeyed request /getConfig to PDP - exception""" + def monkeyed_policy_rest_post_boom(uri, **_): + """monkeypatch for the POST to policy-engine""" + raise MockException("fix_pdp_post_boom {}".format(uri)) + + _LOGGER.info("setup fix_pdp_post_boom") + pdp_client.PolicyRest._lazy_init() + monkeypatch.setattr('policyhandler.pdp_client.PolicyRest._requests_session.post', + monkeyed_policy_rest_post_boom) + yield fix_pdp_post_boom + _LOGGER.info("teardown fix_pdp_post_boom") + + +@pytest.fixture() +def fix_policy_receiver_websocket(monkeypatch): + """monkeyed websocket for policy_receiver""" + _LOGGER.info("setup fix_policy_receiver_websocket") + monkeypatch.setattr('policyhandler.pdp_api_v0.policy_listener.websocket', MockWebSocket) + + yield fix_policy_receiver_websocket + _LOGGER.info("teardown fix_policy_receiver_websocket") + +class MockBoom(Exception): + """mock exception""" + pass + +@pytest.fixture() +def fix_select_latest_policies_boom(monkeypatch): + """monkeyed exception""" + def monkeyed_boom(*_, **__): + """monkeypatch for the select_latest_policies""" + raise MockBoom("fix_select_latest_policies_boom") + + policy_utils_path = 'policyhandler.pdp_api_v0.policy_utils.PolicyUtils' + + _LOGGER.info("setup fix_select_latest_policies_boom at %s", policy_utils_path) + + monkeypatch.setattr('{}.select_latest_policies'.format(policy_utils_path), monkeyed_boom) + monkeypatch.setattr('{}.select_latest_policy'.format(policy_utils_path), monkeyed_boom) + monkeypatch.setattr('{}.extract_policy_id'.format(policy_utils_path), monkeyed_boom) + + yield fix_select_latest_policies_boom + _LOGGER.info("teardown fix_select_latest_policies_boom at %s", policy_utils_path) diff --git a/tests/mock_expected.py b/tests/pdp_api_v0/mock_expected.py index c10215d..6210e10 100644 --- a/tests/mock_expected.py +++ b/tests/pdp_api_v0/mock_expected.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,12 +14,11 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -"""expected message history per test""" +"""expected message history per test for pdp API 2018 and before""" HISTORY_EXPECTED = { - "tests.test_policy_rest::test_get_policy_latest" : [ + "tests/pdp_api_v0/test_policy_rest.py::test_get_policy_latest" : [ { "request": { "headers": { @@ -41,7 +40,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_web_all_policies_latest": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_web_all_policies_latest": [ { "request": { "headers": { @@ -78,7 +77,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_web_policies_latest": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_web_policies_latest": [ { "request": { "headers": { @@ -100,7 +99,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_web_policy_latest": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_web_policy_latest": [ { "request": { "headers": { @@ -122,7 +121,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_zzz_get_catch_up": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_zzz_get_catch_up": [ { "request": { "headers": { @@ -560,7 +559,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_zzz_policy_updates_and_catch_ups": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_zzz_policy_updates_and_catch_ups": [ { "request": { "headers": { @@ -946,7 +945,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_zzzzz_shutdown": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_zzzzz_shutdown": [ { "request": { "headers": { @@ -1332,7 +1331,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_policyhandler::WebServerTest::test_zzz_catch_up_on_deploy_handler_changed": [ + "tests/pdp_api_v0/test_policyhandler.py::WebServer2018Test::test_zzz_catch_up_on_deploy_handler_changed": [ { "request": { "headers": { @@ -1973,7 +1972,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_catch_up::test_catch_up_failed_dh": [ + "tests/pdp_api_v0/test_pz_catch_up.py::test_catch_up_failed_dh": [ { "request": { "headers": { @@ -2524,7 +2523,7 @@ HISTORY_EXPECTED = { "status_code": 413 } ], - "tests.test_pz_catch_up::test_catch_up_dh_404": [ + "tests/pdp_api_v0/test_pz_catch_up.py::test_catch_up_dh_404": [ { "request": { "headers": { @@ -2571,7 +2570,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_pdp_boom::WebServerPDPBoomTest::test_web_all_policies_latest": [ + "tests/pdp_api_v0/test_pz_pdp_boom.py::WebServerPDPBoom2018Test::test_web_all_policies_latest": [ { "request": { "headers": { @@ -2588,7 +2587,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_pdp_boom::WebServerPDPBoomTest::test_zzz_catch_up_on_deploy_handler_changed": [ + "tests/pdp_api_v0/test_pz_pdp_boom.py::WebServerPDPBoom2018Test::test_zzz_catch_up_on_deploy_handler_changed": [ { "request": { "headers": { @@ -2650,7 +2649,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_pdp_boom::WebServerPDPBoomTest::test_zzz_get_catch_up": [ + "tests/pdp_api_v0/test_pz_pdp_boom.py::WebServerPDPBoom2018Test::test_zzz_get_catch_up": [ { "request": { "headers": { @@ -2682,7 +2681,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_pdp_boom::WebServerPDPBoomTest::test_zzz_policy_updates_and_catch_ups": [ + "tests/pdp_api_v0/test_pz_pdp_boom.py::WebServerPDPBoom2018Test::test_zzz_policy_updates_and_catch_ups": [ { "request": { "headers": { @@ -2714,7 +2713,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_pdp_boom::WebServerPDPBoomTest::test_zzzzz_shutdown": [ + "tests/pdp_api_v0/test_pz_pdp_boom.py::WebServerPDPBoom2018Test::test_zzzzz_shutdown": [ { "request": { "headers": { @@ -2746,7 +2745,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_web_all_policies_latest": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_web_all_policies_latest": [ { "request": { "headers": { @@ -2783,7 +2782,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_web_policies_latest": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_web_policies_latest": [ { "request": { "headers": { @@ -2805,7 +2804,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_web_policy_latest": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_web_policy_latest": [ { "request": { "headers": { @@ -2827,7 +2826,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_zzz_catch_up_on_deploy_handler_changed": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_zzz_catch_up_on_deploy_handler_changed": [ { "request": { "headers": { @@ -2864,7 +2863,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_zzz_get_catch_up": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_zzz_get_catch_up": [ { "request": { "headers": { @@ -2936,7 +2935,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_zzz_policy_updates_and_catch_ups": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_zzz_policy_updates_and_catch_ups": [ { "request": { "headers": { @@ -2973,7 +2972,7 @@ HISTORY_EXPECTED = { "status_code": 200 } ], - "tests.test_pz_ph_boom::WebServerInternalBoomTest::test_zzzzz_shutdown": [ + "tests/pdp_api_v0/test_pz_ph_boom.py::WebServerInternalBoom2018Test::test_zzzzz_shutdown": [ { "request": { "headers": { diff --git a/tests/mock_policy_engine.py b/tests/pdp_api_v0/mock_policy_engine.py index d57d613..cdded14 100644 --- a/tests/mock_policy_engine.py +++ b/tests/pdp_api_v0/mock_policy_engine.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,50 +14,22 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """mocking for the policy-engine - shared by many tests""" import copy import json import re -from policyhandler.policy_consts import (POLICY_BODY, POLICY_CONFIG, POLICY_ID, - POLICY_NAME, POLICY_VERSION) -from policyhandler.policy_utils import PolicyUtils +from policyhandler.pdp_api_v0.pdp_consts import (POLICY_CONFIG, POLICY_NAME, + POLICY_VERSION) +from policyhandler.pdp_api_v0.policy_utils import PolicyUtils +from policyhandler.policy_consts import POLICY_BODY, POLICY_ID +from policyhandler.utils import Utils -from .mock_settings import Settings +_LOGGER = Utils.get_logger(__file__) -class MonkeyPolicyBody(object): - """policy body that policy-engine returns""" - @staticmethod - def create_policy_body(policy_id, policy_version=1): - """returns a fake policy-body""" - prev_ver = str(policy_version - 1) - this_ver = str(policy_version) - config = { - "policy_updated_from_ver": prev_ver, - "policy_updated_to_ver": this_ver, - "policy_hello": "world!", - "updated_policy_id": policy_id - } - return { - "policyConfigMessage": "Config Retrieved! ", - "policyConfigStatus": "CONFIG_RETRIEVED", - "type": "JSON", - POLICY_NAME: "{0}.{1}.xml".format(policy_id, this_ver), - POLICY_VERSION: this_ver, - POLICY_CONFIG: json.dumps(config, sort_keys=True), - "matchingConditions": { - "ONAPName": "DCAE", - "ConfigName": "alex_config_name" - }, - "responseAttributes": {}, - "property": None - } - - -class MockPolicyEngine(object): +class MockPolicyEngine2018(object): """pretend this is the policy-engine""" scope_prefix = "test_scope_prefix.Config_" LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split() @@ -68,18 +40,17 @@ class MockPolicyEngine(object): @staticmethod def init(): - """init static vars""" - if MockPolicyEngine._inited: + """init collection of policies: policy_version = policy_index + 1""" + if MockPolicyEngine2018._inited: return - MockPolicyEngine._inited = True + MockPolicyEngine2018._inited = True - MockPolicyEngine._policies = [ - MonkeyPolicyBody.create_policy_body( - MockPolicyEngine.scope_prefix + policy_id, policy_index + 1) - for policy_id in MockPolicyEngine.LOREM_IPSUM - for policy_index in range(1 + MockPolicyEngine.LOREM_IPSUM.index(policy_id))] - Settings.logger.info("MockPolicyEngine._policies: %s", - json.dumps(MockPolicyEngine._policies)) + MockPolicyEngine2018._policies = [ + MockPolicyEngine2018._create_policy_body( + MockPolicyEngine2018.scope_prefix + policy_id, policy_index + 1) + for policy_id in MockPolicyEngine2018.LOREM_IPSUM + for policy_index in range(1 + MockPolicyEngine2018.LOREM_IPSUM.index(policy_id))] + _LOGGER.info("_policies: %s", json.dumps(MockPolicyEngine2018._policies)) @staticmethod def get_config(policy_name): @@ -87,32 +58,32 @@ class MockPolicyEngine(object): if not policy_name: return [] return [copy.deepcopy(policy) - for policy in MockPolicyEngine._policies + for policy in MockPolicyEngine2018._policies if re.match(policy_name, policy[POLICY_NAME])] @staticmethod def get_configs_all(): """get all policies the way the policy-engine finds""" policies = [copy.deepcopy(policy) - for policy in MockPolicyEngine._policies] + for policy in MockPolicyEngine2018._policies] for policy in policies: - policy["config"] = MockPolicyEngine.LONG_TEXT + policy["config"] = MockPolicyEngine2018.LONG_TEXT return policies @staticmethod def get_policy_id(policy_index): - """get the policy_id by index""" - return (MockPolicyEngine.scope_prefix - + MockPolicyEngine.LOREM_IPSUM[ - policy_index % len(MockPolicyEngine.LOREM_IPSUM)]) + """get the policy_id by policy_index""" + return (MockPolicyEngine2018.scope_prefix + + MockPolicyEngine2018.LOREM_IPSUM[ + policy_index % len(MockPolicyEngine2018.LOREM_IPSUM)]) @staticmethod def gen_policy_latest(policy_index, version_offset=0): - """generate the policy response by policy_index = version - 1""" - policy_id = MockPolicyEngine.get_policy_id(policy_index) + """generate the policy response from policy-handler by policy_index = version - 1""" + policy_id = MockPolicyEngine2018.get_policy_id(policy_index) policy = { POLICY_ID: policy_id, - POLICY_BODY: MonkeyPolicyBody.create_policy_body( + POLICY_BODY: MockPolicyEngine2018._create_policy_body( policy_id, policy_index + 1 - version_offset) } return policy_id, PolicyUtils.parse_policy_config(policy) @@ -120,12 +91,40 @@ class MockPolicyEngine(object): @staticmethod def gen_all_policies_latest(version_offset=0): """generate all latest policies""" - return dict(MockPolicyEngine.gen_policy_latest(policy_index, version_offset=version_offset) - for policy_index in range(len(MockPolicyEngine.LOREM_IPSUM))) + return dict( + MockPolicyEngine2018.gen_policy_latest(policy_index, version_offset=version_offset) + for policy_index in range(len(MockPolicyEngine2018.LOREM_IPSUM)) + ) @staticmethod def gen_policies_latest(match_to_policy_name): """generate all latest policies""" return dict((k, v) - for k, v in MockPolicyEngine.gen_all_policies_latest().items() + for k, v in MockPolicyEngine2018.gen_all_policies_latest().items() if re.match(match_to_policy_name, k)) + + @staticmethod + def _create_policy_body(policy_id, policy_version=1): + """returns a fake policy-body""" + prev_ver = str(policy_version - 1) + this_ver = str(policy_version) + config = { + "policy_updated_from_ver": prev_ver, + "policy_updated_to_ver": this_ver, + "policy_hello": "world!", + "updated_policy_id": policy_id + } + return { + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "type": "JSON", + POLICY_NAME: "{0}.{1}.xml".format(policy_id, this_ver), + POLICY_VERSION: this_ver, + POLICY_CONFIG: json.dumps(config, sort_keys=True), + "matchingConditions": { + "ONAPName": "DCAE", + "ConfigName": "alex_config_name" + }, + "responseAttributes": {}, + "property": None + } diff --git a/tests/mock_websocket.py b/tests/pdp_api_v0/mock_websocket.py index ac64ed8..17f3bbe 100644 --- a/tests/mock_websocket.py +++ b/tests/pdp_api_v0/mock_websocket.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,19 +14,20 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """mocking for the websocket - for testing of policy-engine""" import json import time -from policyhandler.policy_consts import POLICY_NAME -from policyhandler.policy_receiver import (LOADED_POLICIES, POLICY_VER, - REMOVED_POLICIES) +from policyhandler.pdp_api_v0.pdp_consts import POLICY_NAME +from policyhandler.pdp_api_v0.policy_listener import (LOADED_POLICIES, + POLICY_VER, + REMOVED_POLICIES) +from policyhandler.utils import Utils -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings +from .mock_policy_engine import MockPolicyEngine2018 +_LOGGER = Utils.get_logger(__file__) class MockWebSocket(object): """Mock websocket""" @@ -40,14 +41,14 @@ class MockWebSocket(object): message = { LOADED_POLICIES: [ {POLICY_NAME: "{0}.{1}.xml".format( - MockPolicyEngine.get_policy_id(policy_index), policy_index + 1), + MockPolicyEngine2018.get_policy_id(policy_index), policy_index + 1), POLICY_VER: str(policy_index + 1)} for policy_index in updated_indexes or [] ], REMOVED_POLICIES : [] } message = json.dumps(message) - Settings.logger.info("send_notification: %s", message) + _LOGGER.info("send_notification: %s", message) MockWebSocket.on_message(None, message) @staticmethod @@ -71,18 +72,18 @@ class MockWebSocket(object): self.on_error = on_error self.on_pong = on_pong self.sock = MockWebSocket.MockSocket() - Settings.logger.info("MockWebSocket for: %s", self.web_socket_url) + _LOGGER.info("MockWebSocket for: %s", self.web_socket_url) - def run_forever(self, sslopt=None): + def run_forever(self, sslopt=None, ping_interval=None): """forever in the loop""" - Settings.logger.info("MockWebSocket run_forever with sslopt=%s...", - json.dumps(sslopt)) + _LOGGER.info("MockWebSocket run_forever with sslopt=%s, ping_interval=%s...", + json.dumps(sslopt), ping_interval) counter = 0 while self.sock.connected: counter += 1 - Settings.logger.info("MockWebSocket sleep %s...", counter) + _LOGGER.info("MockWebSocket sleep %s...", counter) time.sleep(5) - Settings.logger.info("MockWebSocket exit %s", counter) + _LOGGER.info("MockWebSocket exit %s", counter) def close(self): """close socket""" diff --git a/tests/pdp_api_v0/test_policy_rest.py b/tests/pdp_api_v0/test_policy_rest.py new file mode 100644 index 0000000..67b06ae --- /dev/null +++ b/tests/pdp_api_v0/test_policy_rest.py @@ -0,0 +1,47 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +"""test policy_rest methods directly""" + +import json + +import pytest + +from policyhandler import pdp_client +from policyhandler.onap.audit import Audit +from policyhandler.utils import Utils + +from ..mock_tracker import Tracker +from .mock_policy_engine import MockPolicyEngine2018 + +_LOGGER = Utils.get_logger(__file__) + +@pytest.mark.usefixtures("fix_pdp_api_v0", "fix_pdp_post") +def test_get_policy_latest(): + """test /policy_latest/<policy-id>""" + policy_id, expected_policy = MockPolicyEngine2018.gen_policy_latest(3) + + audit = Audit(job_name="test_get_policy_latest", + req_message="get /policy_latest/{}".format(policy_id or "")) + + policy_latest = pdp_client.PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} + audit.audit_done(result=json.dumps(policy_latest)) + + _LOGGER.info("expected_policy: %s", json.dumps(expected_policy)) + _LOGGER.info("policy_latest: %s", json.dumps(policy_latest)) + assert Utils.are_the_same(policy_latest, expected_policy) + + Tracker.validate() diff --git a/tests/test_policyhandler.py b/tests/pdp_api_v0/test_policyhandler.py index 4743a6e..2b2629b 100644 --- a/tests/test_policyhandler.py +++ b/tests/pdp_api_v0/test_policyhandler.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test of the package for policy-handler of DCAE-Controller""" @@ -22,24 +21,31 @@ import json import time import uuid -import pytest import cherrypy +import pytest from cherrypy.test.helper import CPWebCase +from policyhandler.config import Config from policyhandler.onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit -from policyhandler.policy_consts import LATEST_POLICIES, POLICY_NAME +from policyhandler.pdp_api_v0.pdp_consts import POLICY_NAME +from policyhandler.policy_consts import LATEST_POLICIES from policyhandler.policy_receiver import PolicyReceiver -from policyhandler.policy_utils import Utils +from policyhandler.utils import Utils from policyhandler.web_server import _PolicyWeb -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings -from .mock_tracker import Tracker +from ..mock_settings import MockSettings +from ..mock_tracker import Tracker +from .mock_policy_engine import MockPolicyEngine2018 from .mock_websocket import MockWebSocket +_LOGGER = Utils.get_logger(__file__) -@pytest.mark.usefixtures("fix_pdp_post", "fix_discovery") -class WebServerTest(CPWebCase): +@pytest.mark.usefixtures( + "fix_pdp_api_v0", + "fix_pdp_post", + "fix_discovery" +) +class WebServer2018Test(CPWebCase): """testing the web-server - runs tests in alphabetical order of method names""" def setup_server(): """setup the web-server""" @@ -50,57 +56,62 @@ class WebServerTest(CPWebCase): def test_web_healthcheck(self): """test /healthcheck""" result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) - Settings.logger.info("got healthcheck: %s", self.body) + _LOGGER.info("healthcheck result: %s", result) + _LOGGER.info("got healthcheck: %s", self.body) self.assertStatus('200 OK') Tracker.validate() def test_web_policy_latest(self): """test /policy_latest/<policy-id>""" - policy_id, expected_policy = MockPolicyEngine.gen_policy_latest(3) + policy_id, expected_policy = MockPolicyEngine2018.gen_policy_latest(3) self.getPage("/policy_latest/{0}".format(policy_id or "")) self.assertStatus('200 OK') policy_latest = json.loads(self.body) - Settings.logger.info("policy_latest: %s", self.body) - Settings.logger.info("expected_policy: %s", json.dumps(expected_policy)) + _LOGGER.info("policy_latest: %s", self.body) + _LOGGER.info("expected_policy: %s", json.dumps(expected_policy)) assert Utils.are_the_same(policy_latest, expected_policy) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @pytest.mark.usefixtures("fix_deploy_handler") def test_web_all_policies_latest(self): """test GET /policies_latest""" - expected_policies = MockPolicyEngine.gen_all_policies_latest() + expected_policies = MockPolicyEngine2018.gen_all_policies_latest() result = self.getPage("/policies_latest") - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus('200 OK') policies_latest = json.loads(self.body) self.assertIn(LATEST_POLICIES, policies_latest) policies_latest = policies_latest[LATEST_POLICIES] - Settings.logger.info("policies_latest: %s", json.dumps(policies_latest)) - Settings.logger.info("expected_policies: %s", json.dumps(expected_policies)) + _LOGGER.info("policies_latest: %s", json.dumps(policies_latest)) + _LOGGER.info("expected_policies: %s", json.dumps(expected_policies)) assert Utils.are_the_same(policies_latest, expected_policies) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() def test_web_policies_latest(self): """test POST /policies_latest with policyName""" - match_to_policy_name = MockPolicyEngine.scope_prefix + "amet.*" - expected_policies = MockPolicyEngine.gen_policies_latest(match_to_policy_name) + match_to_policy_name = MockPolicyEngine2018.scope_prefix + "amet.*" + expected_policies = MockPolicyEngine2018.gen_policies_latest(match_to_policy_name) body = json.dumps({POLICY_NAME: match_to_policy_name}) result = self.getPage("/policies_latest", method='POST', @@ -110,43 +121,52 @@ class WebServerTest(CPWebCase): ("Content-Type", "application/json"), ('Content-Length', str(len(body))) ]) - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus('200 OK') policies_latest = json.loads(self.body)[LATEST_POLICIES] - Settings.logger.info("policies_latest: %s", json.dumps(policies_latest)) - Settings.logger.info("expected_policies: %s", json.dumps(expected_policies)) + _LOGGER.info("policies_latest: %s", json.dumps(policies_latest)) + _LOGGER.info("expected_policies: %s", json.dumps(expected_policies)) assert Utils.are_the_same(policies_latest, expected_policies) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_policy_updates_and_catch_ups(self): """test run policy handler with policy updates and catchups""" - Settings.logger.info("start policy_updates_and_catch_ups") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start policy_updates_and_catch_ups") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_policy_updates_and_catch_ups", req_message="start policy_updates_and_catch_ups") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 10 before shutdown...") + _LOGGER.info("sleep 10 before shutdown...") time.sleep(10) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -157,31 +177,35 @@ class WebServerTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_catch_up_on_deploy_handler_changed(self): """test run policy handler with deployment-handler changed underneath""" - Settings.logger.info("start zzz_catch_up_on_deploy_handler_changed") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start zzz_catch_up_on_deploy_handler_changed") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_catch_up_on_deploy_handler_changed", req_message="start zzz_catch_up_on_deploy_handler_changed") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.deploy_handler_instance_uuid = str(uuid.uuid4()) - Settings.logger.info("new deploy-handler uuid=%s", Settings.deploy_handler_instance_uuid) + MockSettings.deploy_handler_instance_uuid = str(uuid.uuid4()) + _LOGGER.info("new deploy-handler uuid=%s", MockSettings.deploy_handler_instance_uuid) MockWebSocket.send_notification([2, 4]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -192,21 +216,25 @@ class WebServerTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_get_catch_up(self): """test /catch_up""" - Settings.logger.info("start /catch_up") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start /catch_up") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_get_catch_up", req_message="start /catch_up") PolicyReceiver.run(audit) time.sleep(5) result = self.getPage("/catch_up") - Settings.logger.info("catch_up result: %s", result) + _LOGGER.info("catch_up result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got catch_up: %s", self.body) + _LOGGER.info("got catch_up: %s", self.body) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -220,29 +248,33 @@ class WebServerTest(CPWebCase): "fix_cherrypy_engine_exit") def test_zzzzz_shutdown(self): """test shutdown""" - Settings.logger.info("start shutdown") + _LOGGER.info("start shutdown") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzzzz_shutdown", req_message="start shutdown") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) - WebServerTest.do_gc_test = False - Settings.logger.info("shutdown...") + WebServer2018Test.do_gc_test = False + _LOGGER.info("shutdown...") audit.audit_done("shutdown") result = self.getPage("/shutdown") - Settings.logger.info("shutdown result: %s", result) + _LOGGER.info("shutdown result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got shutdown: %s", self.body) + _LOGGER.info("got shutdown: %s", self.body) time.sleep(1) assert not PolicyReceiver.is_running() + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + Tracker.validate() diff --git a/tests/test_pz_catch_up.py b/tests/pdp_api_v0/test_pz_catch_up.py index 89be9bb..3b37af5 100644 --- a/tests/test_pz_catch_up.py +++ b/tests/pdp_api_v0/test_pz_catch_up.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test policy catch_up methods directly""" import json @@ -22,14 +21,17 @@ import time import pytest +from policyhandler.config import Config from policyhandler.onap.audit import Audit from policyhandler.policy_receiver import PolicyReceiver +from policyhandler.utils import Utils -from .mock_settings import Settings -from .mock_tracker import Tracker +from ..mock_tracker import Tracker +_LOGGER = Utils.get_logger(__file__) @pytest.mark.usefixtures( + "fix_pdp_api_v0", "fix_auto_catch_up", "fix_discovery", "fix_pdp_post_big", @@ -38,19 +40,23 @@ from .mock_tracker import Tracker ) def test_catch_up_failed_dh(): """test run policy handler with catchups and failed deployment-handler""" - Settings.logger.info("start test_catch_up_failed_dh") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start test_catch_up_failed_dh") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_catch_up_failed_dh", req_message="start test_catch_up_failed_dh") PolicyReceiver.run(audit) - Settings.logger.info("sleep 12 before shutdown...") + _LOGGER.info("sleep 12 before shutdown...") time.sleep(12) health = audit.health(full=True) audit.audit_done(result=json.dumps(health)) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) assert bool(health) PolicyReceiver.shutdown(audit) @@ -58,11 +64,12 @@ def test_catch_up_failed_dh(): assert not PolicyReceiver.is_running() health = audit.health(full=True) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) Tracker.validate() @pytest.mark.usefixtures( + "fix_pdp_api_v0", "fix_auto_catch_up", "fix_discovery", "fix_pdp_post", @@ -71,19 +78,23 @@ def test_catch_up_failed_dh(): ) def test_catch_up_dh_404(): """test run policy handler with catchups and failed deployment-handler""" - Settings.logger.info("start test_catch_up_dh_404") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start test_catch_up_dh_404") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_catch_up_dh_404", req_message="start test_catch_up_dh_404") PolicyReceiver.run(audit) - Settings.logger.info("sleep 12 before shutdown...") + _LOGGER.info("sleep 12 before shutdown...") time.sleep(12) health = audit.health(full=True) audit.audit_done(result=json.dumps(health)) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) assert bool(health) PolicyReceiver.shutdown(audit) @@ -91,6 +102,6 @@ def test_catch_up_dh_404(): assert not PolicyReceiver.is_running() health = audit.health(full=True) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) Tracker.validate() diff --git a/tests/test_pz_pdp_boom.py b/tests/pdp_api_v0/test_pz_pdp_boom.py index 1b6f150..effadc2 100644 --- a/tests/test_pz_pdp_boom.py +++ b/tests/pdp_api_v0/test_pz_pdp_boom.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,34 +14,37 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test policyhandler web-server when pdp booms = fails""" import json import time import uuid -import pytest import cherrypy +import pytest from cherrypy.test.helper import CPWebCase +from policyhandler.config import Config from policyhandler.onap.audit import (REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode) -from policyhandler.policy_consts import POLICY_NAME +from policyhandler.pdp_api_v0.pdp_consts import POLICY_NAME from policyhandler.policy_receiver import PolicyReceiver +from policyhandler.utils import Utils from policyhandler.web_server import _PolicyWeb -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings -from .mock_tracker import Tracker +from ..mock_settings import MockSettings +from ..mock_tracker import Tracker +from .mock_policy_engine import MockPolicyEngine2018 from .mock_websocket import MockWebSocket +_LOGGER = Utils.get_logger(__file__) @pytest.mark.usefixtures( + "fix_pdp_api_v0", "fix_discovery", "fix_pdp_post_boom" ) -class WebServerPDPBoomTest(CPWebCase): +class WebServerPDPBoom2018Test(CPWebCase): """testing the web-server - runs tests in alphabetical order of method names""" def setup_server(): """setup the web-server""" @@ -52,21 +55,21 @@ class WebServerPDPBoomTest(CPWebCase): def test_web_healthcheck(self): """test /healthcheck""" result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) - Settings.logger.info("got healthcheck: %s", self.body) + _LOGGER.info("healthcheck result: %s", result) + _LOGGER.info("got healthcheck: %s", self.body) self.assertStatus('200 OK') Tracker.validate() def test_web_policy_latest(self): """test /policy_latest/<policy-id>""" - policy_id, _ = MockPolicyEngine.gen_policy_latest(3) + policy_id, _ = MockPolicyEngine2018.gen_policy_latest(3) self.getPage("/policy_latest/{0}".format(policy_id or "")) self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @@ -74,18 +77,23 @@ class WebServerPDPBoomTest(CPWebCase): def test_web_all_policies_latest(self): """test GET /policies_latest""" result = self.getPage("/policies_latest") - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() def test_web_policies_latest(self): """test POST /policies_latest with policyName""" - match_to_policy_name = MockPolicyEngine.scope_prefix + "amet.*" + match_to_policy_name = MockPolicyEngine2018.scope_prefix + "amet.*" body = json.dumps({POLICY_NAME: match_to_policy_name}) result = self.getPage("/policies_latest", method='POST', @@ -95,36 +103,45 @@ class WebServerPDPBoomTest(CPWebCase): ("Content-Type", "application/json"), ('Content-Length', str(len(body))) ]) - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_policy_updates_and_catch_ups(self): """test run policy handler with policy updates and catchups""" - Settings.logger.info("start policy_updates_and_catch_ups") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start policy_updates_and_catch_ups") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_policy_updates_and_catch_ups", req_message="start policy_updates_and_catch_ups") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 10 before shutdown...") + _LOGGER.info("sleep 10 before shutdown...") time.sleep(10) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -135,31 +152,35 @@ class WebServerPDPBoomTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_catch_up_on_deploy_handler_changed(self): """test run policy handler with deployment-handler changed underneath""" - Settings.logger.info("start zzz_catch_up_on_deploy_handler_changed") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start zzz_catch_up_on_deploy_handler_changed") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_catch_up_on_deploy_handler_changed", req_message="start zzz_catch_up_on_deploy_handler_changed") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.deploy_handler_instance_uuid = str(uuid.uuid4()) - Settings.logger.info("new deploy-handler uuid=%s", Settings.deploy_handler_instance_uuid) + MockSettings.deploy_handler_instance_uuid = str(uuid.uuid4()) + _LOGGER.info("new deploy-handler uuid=%s", MockSettings.deploy_handler_instance_uuid) MockWebSocket.send_notification([2, 4]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -170,21 +191,25 @@ class WebServerPDPBoomTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_get_catch_up(self): """test /catch_up""" - Settings.logger.info("start /catch_up") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start /catch_up") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_get_catch_up", req_message="start /catch_up") PolicyReceiver.run(audit) time.sleep(5) result = self.getPage("/catch_up") - Settings.logger.info("catch_up result: %s", result) + _LOGGER.info("catch_up result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got catch_up: %s", self.body) + _LOGGER.info("got catch_up: %s", self.body) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -198,29 +223,33 @@ class WebServerPDPBoomTest(CPWebCase): "fix_cherrypy_engine_exit") def test_zzzzz_shutdown(self): """test shutdown""" - Settings.logger.info("start shutdown") + _LOGGER.info("start shutdown") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzzzz_shutdown", req_message="start shutdown") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) - WebServerPDPBoomTest.do_gc_test = False - Settings.logger.info("shutdown...") + WebServerPDPBoom2018Test.do_gc_test = False + _LOGGER.info("shutdown...") audit.audit_done("shutdown") result = self.getPage("/shutdown") - Settings.logger.info("shutdown result: %s", result) + _LOGGER.info("shutdown result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got shutdown: %s", self.body) + _LOGGER.info("got shutdown: %s", self.body) time.sleep(1) assert not PolicyReceiver.is_running() + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + Tracker.validate() diff --git a/tests/test_pz_ph_boom.py b/tests/pdp_api_v0/test_pz_ph_boom.py index c392b3a..4203110 100644 --- a/tests/test_pz_ph_boom.py +++ b/tests/pdp_api_v0/test_pz_ph_boom.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,36 +14,38 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test policyhandler web-server when the policy-handler booms = fails""" import json import time import uuid - -import pytest import cherrypy +import pytest from cherrypy.test.helper import CPWebCase +from policyhandler.config import Config from policyhandler.onap.audit import (REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode) -from policyhandler.policy_consts import POLICY_NAME +from policyhandler.pdp_api_v0.pdp_consts import POLICY_NAME from policyhandler.policy_receiver import PolicyReceiver +from policyhandler.utils import Utils from policyhandler.web_server import _PolicyWeb -from .mock_policy_engine import MockPolicyEngine -from .mock_settings import Settings -from .mock_tracker import Tracker +from ..mock_settings import MockSettings +from ..mock_tracker import Tracker +from .mock_policy_engine import MockPolicyEngine2018 from .mock_websocket import MockWebSocket +_LOGGER = Utils.get_logger(__file__) @pytest.mark.usefixtures( + "fix_pdp_api_v0", "fix_discovery", "fix_pdp_post", "fix_select_latest_policies_boom" ) -class WebServerInternalBoomTest(CPWebCase): +class WebServerInternalBoom2018Test(CPWebCase): """testing the web-server - runs tests in alphabetical order of method names""" def setup_server(): """setup the web-server""" @@ -54,21 +56,21 @@ class WebServerInternalBoomTest(CPWebCase): def test_web_healthcheck(self): """test /healthcheck""" result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) - Settings.logger.info("got healthcheck: %s", self.body) + _LOGGER.info("healthcheck result: %s", result) + _LOGGER.info("got healthcheck: %s", self.body) self.assertStatus('200 OK') Tracker.validate() def test_web_policy_latest(self): """test /policy_latest/<policy-id>""" - policy_id, _ = MockPolicyEngine.gen_policy_latest(3) + policy_id, _ = MockPolicyEngine2018.gen_policy_latest(3) self.getPage("/policy_latest/{0}".format(policy_id or "")) self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @@ -76,18 +78,23 @@ class WebServerInternalBoomTest(CPWebCase): def test_web_all_policies_latest(self): """test GET /policies_latest""" result = self.getPage("/policies_latest") - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() def test_web_policies_latest(self): """test POST /policies_latest with policyName""" - match_to_policy_name = MockPolicyEngine.scope_prefix + "amet.*" + match_to_policy_name = MockPolicyEngine2018.scope_prefix + "amet.*" body = json.dumps({POLICY_NAME: match_to_policy_name}) result = self.getPage("/policies_latest", method='POST', @@ -97,36 +104,45 @@ class WebServerInternalBoomTest(CPWebCase): ("Content-Type", "application/json"), ('Content-Length', str(len(body))) ]) - Settings.logger.info("result: %s", result) - Settings.logger.info("body: %s", self.body) + _LOGGER.info("result: %s", result) + _LOGGER.info("body: %s", self.body) + + if Config.is_pdp_api_default(): + self.assertStatus('404 Not Found') + return + self.assertStatus(AuditHttpCode.SERVER_INTERNAL_ERROR.value) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) Tracker.validate() @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_policy_updates_and_catch_ups(self): """test run policy handler with policy updates and catchups""" - Settings.logger.info("start policy_updates_and_catch_ups") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start policy_updates_and_catch_ups") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_policy_updates_and_catch_ups", req_message="start policy_updates_and_catch_ups") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 10 before shutdown...") + _LOGGER.info("sleep 10 before shutdown...") time.sleep(10) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -137,31 +153,35 @@ class WebServerInternalBoomTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_catch_up_on_deploy_handler_changed(self): """test run policy handler with deployment-handler changed underneath""" - Settings.logger.info("start zzz_catch_up_on_deploy_handler_changed") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start zzz_catch_up_on_deploy_handler_changed") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_catch_up_on_deploy_handler_changed", req_message="start zzz_catch_up_on_deploy_handler_changed") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.deploy_handler_instance_uuid = str(uuid.uuid4()) - Settings.logger.info("new deploy-handler uuid=%s", Settings.deploy_handler_instance_uuid) + MockSettings.deploy_handler_instance_uuid = str(uuid.uuid4()) + _LOGGER.info("new deploy-handler uuid=%s", MockSettings.deploy_handler_instance_uuid) MockWebSocket.send_notification([2, 4]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -172,21 +192,25 @@ class WebServerInternalBoomTest(CPWebCase): @pytest.mark.usefixtures("fix_deploy_handler", "fix_policy_receiver_websocket") def test_zzz_get_catch_up(self): """test /catch_up""" - Settings.logger.info("start /catch_up") + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + + _LOGGER.info("start /catch_up") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzz_get_catch_up", req_message="start /catch_up") PolicyReceiver.run(audit) time.sleep(5) result = self.getPage("/catch_up") - Settings.logger.info("catch_up result: %s", result) + _LOGGER.info("catch_up result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got catch_up: %s", self.body) + _LOGGER.info("got catch_up: %s", self.body) - Settings.logger.info("sleep 5 before shutdown...") + _LOGGER.info("sleep 5 before shutdown...") time.sleep(5) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) PolicyReceiver.shutdown(audit) time.sleep(1) @@ -200,29 +224,33 @@ class WebServerInternalBoomTest(CPWebCase): "fix_cherrypy_engine_exit") def test_zzzzz_shutdown(self): """test shutdown""" - Settings.logger.info("start shutdown") + _LOGGER.info("start shutdown") assert not PolicyReceiver.is_running() audit = Audit(job_name="test_zzzzz_shutdown", req_message="start shutdown") PolicyReceiver.run(audit) - Settings.logger.info("sleep before send_notification...") + _LOGGER.info("sleep before send_notification...") time.sleep(2) MockWebSocket.send_notification([1, 3, 5]) - Settings.logger.info("sleep after send_notification...") + _LOGGER.info("sleep after send_notification...") time.sleep(3) result = self.getPage("/healthcheck") - Settings.logger.info("healthcheck result: %s", result) + _LOGGER.info("healthcheck result: %s", result) - WebServerInternalBoomTest.do_gc_test = False - Settings.logger.info("shutdown...") + WebServerInternalBoom2018Test.do_gc_test = False + _LOGGER.info("shutdown...") audit.audit_done("shutdown") result = self.getPage("/shutdown") - Settings.logger.info("shutdown result: %s", result) + _LOGGER.info("shutdown result: %s", result) self.assertStatus('200 OK') - Settings.logger.info("got shutdown: %s", self.body) + _LOGGER.info("got shutdown: %s", self.body) time.sleep(1) assert not PolicyReceiver.is_running() + if Config.is_pdp_api_default(): + _LOGGER.info("passive for new PDP API") + return + Tracker.validate() diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 0000000..5e2fe60 --- /dev/null +++ b/tests/utils/__init__.py @@ -0,0 +1,20 @@ +# ================================================================================ +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# + + +# empty __init__.py so that pytest can add correct path to coverage report, -- per pytest +# best practice guideline diff --git a/tests/test_step_timer.py b/tests/utils/test_step_timer.py index d4a0df1..152836d 100644 --- a/tests/test_step_timer.py +++ b/tests/utils/test_step_timer.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,25 +14,20 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test of the step_timer""" import json -import logging import time from datetime import datetime -from policyhandler.config import Config from policyhandler.step_timer import StepTimer +from policyhandler.utils import Utils -Config.init_config() - +_LOGGER = Utils.get_logger(__file__) class MockTimerController(object): """testing step_timer""" - logger = logging.getLogger("policy_handler.unit_test.step_timer") - INIT = "init" NEXT = "next" STARTED = "started" @@ -64,25 +59,24 @@ class MockTimerController(object): """timer event""" self.exe_ts = datetime.utcnow() self.exe_interval = (self.exe_ts - self.status_ts).total_seconds() - MockTimerController.logger.info("run on_time[%s] (%s, %s) in %s for %s", - self.run_counter, json.dumps(args), json.dumps(kwargs), - self.exe_interval, self.get_status()) + _LOGGER.info("run on_time[%s] (%s, %s) in %s for %s", + self.run_counter, json.dumps(args), json.dumps(kwargs), + self.exe_interval, self.get_status()) time.sleep(3) - MockTimerController.logger.info("done on_time[%s] (%s, %s) in %s for %s", - self.run_counter, json.dumps(args), json.dumps(kwargs), - self.exe_interval, self.get_status()) + _LOGGER.info("done on_time[%s] (%s, %s) in %s for %s", + self.run_counter, json.dumps(args), json.dumps(kwargs), + self.exe_interval, self.get_status()) def verify_last_event(self): """assertions needs to be in the main thread""" if self.exe_interval is None: - MockTimerController.logger.info("not executed: %s", self.get_status()) + _LOGGER.info("not executed: %s", self.get_status()) return - MockTimerController.logger.info("verify exe %s for %s", - self.exe_interval, self.get_status()) + _LOGGER.info("verify exe %s for %s", self.exe_interval, self.get_status()) assert self.exe_interval >= (self.interval - 0.01) assert self.exe_interval < 2 * self.interval - MockTimerController.logger.info("success %s", self.get_status()) + _LOGGER.info("success %s", self.get_status()) def run_timer(self): """create and start the step_timer""" @@ -91,11 +85,7 @@ class MockTimerController(object): self.set_status(MockTimerController.NEXT) return - self.step_timer = StepTimer( - self.name, self.interval, MockTimerController.on_time, - MockTimerController.logger, - self - ) + self.step_timer = StepTimer(self.name, self.interval, MockTimerController.on_time, self) self.step_timer.start() self.set_status(MockTimerController.STARTED) @@ -123,7 +113,7 @@ class MockTimerController(object): utcnow = datetime.utcnow() time_step = (utcnow - self.status_ts).total_seconds() self.status_ts = utcnow - MockTimerController.logger.info("[%s]: %s", time_step, self.get_status()) + _LOGGER.info("[%s]: %s", time_step, self.get_status()) def get_status(self): """string representation""" @@ -138,7 +128,7 @@ class MockTimerController(object): def test_step_timer(): """test step_timer""" - MockTimerController.logger.info("============ test_step_timer =========") + _LOGGER.info("============ test_step_timer =========") with MockTimerController("step_timer", 5) as step_timer: step_timer.run_timer() time.sleep(1) @@ -166,7 +156,7 @@ def test_step_timer(): def test_interrupt_step_timer(): """test step_timer""" - MockTimerController.logger.info("============ test_interrupt_step_timer =========") + _LOGGER.info("============ test_interrupt_step_timer =========") with MockTimerController("step_timer", 5) as step_timer: step_timer.run_timer() time.sleep(1) diff --git a/tests/test_policy_utils.py b/tests/utils/test_utils.py index dcf6ccb..18026ff 100644 --- a/tests/test_policy_utils.py +++ b/tests/utils/test_utils.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,56 +14,51 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test of the policy_utils""" import json -import logging import re -from policyhandler.config import Config -from policyhandler.policy_utils import RegexCoarser - -Config.init_config() -LOGGER = logging.getLogger("policy_handler.unit_test_policy_utils") +from policyhandler.utils import RegexCoarser, Utils +_LOGGER = Utils.get_logger(__file__) def check_coarse_regex(test_name, patterns, matching_strings=None, expected_subpatterns=None): """generic test""" regex_coarser = RegexCoarser(patterns) coarse_patterns = regex_coarser.get_coarse_regex_patterns(max_length=20) - LOGGER.info("check_coarse_regex %s (%s) for [%s]", - test_name, coarse_patterns, json.dumps(regex_coarser.patterns)) + _LOGGER.info("check_coarse_regex %s (%s) for [%s]", + test_name, coarse_patterns, json.dumps(regex_coarser.patterns)) coarse_regexes = [re.compile(coarse_pattern) for coarse_pattern in coarse_patterns] coarse_patterns_str = json.dumps(coarse_patterns) if matching_strings: for test_str in matching_strings: - LOGGER.info(" match '%s' to %s (%s)", test_str, test_name, coarse_patterns_str) + _LOGGER.info(" match '%s' to %s (%s)", test_str, test_name, coarse_patterns_str) assert bool(list(filter(None, [ coarse_regex.match(test_str) for coarse_regex in coarse_regexes ]))) if expected_subpatterns: for subpattern in expected_subpatterns: - LOGGER.info(" subpattern '%s' in %s", subpattern, coarse_patterns_str) + _LOGGER.info(" subpattern '%s' in %s", subpattern, coarse_patterns_str) assert subpattern in coarse_patterns_str def check_combined_regex(test_name, patterns, matching_strings=None, unmatching_strings=None): """generic test""" regex_coarser = RegexCoarser(patterns) combined_pattern = regex_coarser.get_combined_regex_pattern() - LOGGER.info("check_combined_regex %s (%s) for [%s]", - test_name, combined_pattern, json.dumps(regex_coarser.patterns)) + _LOGGER.info("check_combined_regex %s (%s) for [%s]", + test_name, combined_pattern, json.dumps(regex_coarser.patterns)) coarse_regex = re.compile(combined_pattern) if matching_strings: for test_str in matching_strings: - LOGGER.info(" match '%s' to %s (%s)", test_str, test_name, combined_pattern) + _LOGGER.info(" match '%s' to %s (%s)", test_str, test_name, combined_pattern) assert coarse_regex.match(test_str) if unmatching_strings: for test_str in unmatching_strings: - LOGGER.info(" not match '%s' to %s (%s)", test_str, test_name, combined_pattern) + _LOGGER.info(" not match '%s' to %s (%s)", test_str, test_name, combined_pattern) assert not coarse_regex.match(test_str) def test_regex_coarser(): diff --git a/tests/test_zzz_memory.py b/tests/utils/test_zzz_memory.py index e1b5af3..4b934cb 100644 --- a/tests/test_zzz_memory.py +++ b/tests/utils/test_zzz_memory.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. """test of the package for policy-handler of DCAE-Controller""" @@ -22,12 +21,10 @@ import gc import json import time -import pytest - from policyhandler.onap.audit import Audit, AuditHttpCode, Metrics +from policyhandler.utils import Utils -from .mock_settings import Settings - +_LOGGER = Utils.get_logger(__file__) class Node(object): """making the cycled objects""" @@ -50,7 +47,7 @@ def test_healthcheck(): health = audit.health(full=True) audit.audit_done(result=json.dumps(health)) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) assert bool(health) @@ -74,7 +71,7 @@ def test_healthcheck_with_error(): health = audit.health(full=True) audit.audit_done(result=json.dumps(health)) - Settings.logger.info("healthcheck: %s", json.dumps(health)) + _LOGGER.info("healthcheck: %s", json.dumps(health)) assert bool(health) @@ -97,22 +94,22 @@ def test_healthcheck_with_garbage(): health = audit.health(full=True) audit.audit_done(result=json.dumps(health)) - Settings.logger.info("test_healthcheck_with_garbage[%s]: %s", gc_found, json.dumps(health)) + _LOGGER.info("test_healthcheck_with_garbage[%s]: %s", gc_found, json.dumps(health)) assert bool(health) assert bool(health.get("runtime", {}).get("gc", {}).get("gc_garbage")) - Settings.logger.info("clearing up garbage...") + _LOGGER.info("clearing up garbage...") for obj in gc.garbage: if isinstance(obj, Node): - Settings.logger.info("in garbage: %s 0x%x", obj, id(obj)) + _LOGGER.info("in garbage: %s 0x%x", obj, id(obj)) obj.next = None gc_found = gc.collect() - Settings.logger.info("after clear test_healthcheck_with_garbage[%s]: %s", + _LOGGER.info("after clear test_healthcheck_with_garbage[%s]: %s", gc_found, json.dumps(audit.health(full=True))) gc.set_debug(False) gc_found = gc.collect() - Settings.logger.info("after turned off gc debug test_healthcheck_with_garbage[%s]: %s", + _LOGGER.info("after turned off gc debug test_healthcheck_with_garbage[%s]: %s", gc_found, json.dumps(audit.health(full=True))) |