From 341b5bb2347c30344662675936b90b325efe5520 Mon Sep 17 00:00:00 2001 From: Vijay Venkatesh Kumar Date: Thu, 17 Nov 2022 14:51:38 -0500 Subject: Heartbeat code refactoring code optimization & test improvement Issue-ID: DCAEGEN2-2953 Signed-off-by: Vijay Venkatesh Kumar Change-Id: I99229d966c13ad666ac994ab5a582aeeaa306639 Signed-off-by: Vijay Venkatesh Kumar --- tests/hbproperties-test.yaml | 11 ++ tests/test-config.json | 1 + tests/test4.json | 1 + tests/test_db_monitoring.py | 75 +++++++++++++ tests/test_get_logger.py | 6 +- tests/test_htbt_exit.py | 50 +++++++++ tests/test_htbt_get_cbs_config.py | 148 +++++++++++++++++++++++++ tests/test_htbt_http_session.py | 34 ++++++ tests/test_htbt_runtime_pid.py | 63 +++++++++++ tests/test_htbt_settings.py | 81 ++++++++++++++ tests/test_htbt_vnf_table.py | 128 ++++++++++++++++++++++ tests/test_htbtworker.py | 163 +++++++++++++++++++++++----- tests/test_misshtbtd.py | 215 +++++++++++++++++++++++++++++++++++++ tests/test_trapd_exit.py | 50 --------- tests/test_trapd_get_cbs_config.py | 121 --------------------- tests/test_trapd_http_session.py | 34 ------ tests/test_trapd_runtime_pid.py | 63 ----------- tests/test_trapd_settings.py | 81 -------------- tests/test_trapd_vnf_table.py | 107 ------------------ 19 files changed, 951 insertions(+), 481 deletions(-) create mode 100644 tests/hbproperties-test.yaml create mode 100644 tests/test-config.json create mode 100644 tests/test4.json create mode 100644 tests/test_db_monitoring.py create mode 100644 tests/test_htbt_exit.py create mode 100644 tests/test_htbt_get_cbs_config.py create mode 100644 tests/test_htbt_http_session.py create mode 100644 tests/test_htbt_runtime_pid.py create mode 100644 tests/test_htbt_settings.py create mode 100644 tests/test_htbt_vnf_table.py create mode 100644 tests/test_misshtbtd.py delete mode 100644 tests/test_trapd_exit.py delete mode 100644 tests/test_trapd_get_cbs_config.py delete mode 100644 tests/test_trapd_http_session.py delete mode 100644 tests/test_trapd_runtime_pid.py delete mode 100644 tests/test_trapd_settings.py delete mode 100644 tests/test_trapd_vnf_table.py (limited to 'tests') diff --git a/tests/hbproperties-test.yaml b/tests/hbproperties-test.yaml new file mode 100644 index 0000000..1dc649c --- /dev/null +++ b/tests/hbproperties-test.yaml @@ -0,0 +1,11 @@ +#Postgres database input +#pg_ipAddress: 127.0.0.1 +pg_ipAddress: 10.0.0.0 +pg_portNum: "1234" +pg_userName: postgres-test +pg_passwd: postgres-test +pg_dbName: postgres + +#Periodic polling of CBS config download +CBS_polling_allowed: "True" +CBS_polling_interval: "300" diff --git a/tests/test-config.json b/tests/test-config.json new file mode 100644 index 0000000..e853f6a --- /dev/null +++ b/tests/test-config.json @@ -0,0 +1 @@ +{"pg_ipAddress": "10.0.4.1", "pg_userName": "postgres", "pg_dbName": "postgres", "streams_subscribes": {"ves-heartbeat": {"type": "message_router", "dmaap_info": {"topic_url": "http://10.12.5.252:3904/events/unauthenticated.SEC_HEARTBEAT_INPUT"}}}, "consumerID": "1", "CBS_polling_interval": "300", "pg_passwd": "postgres", "streams_publishes": {"dcae_cl_out": {"type": "message_router", "dmaap_info": {"topic_url": "http://10.12.5.252:3904/events/unauthenticated.DCAE_CL_OUTPUT"}}}, "pg_portNum": "5432", "CBS_polling_allowed": "True", "groupID": "groupID", "heartbeat_config": "{\"vnfs\": [{\"eventName\": \"Heartbeat_vDNS\",\"heartbeatcountmissed\": 3,\"heartbeatinterval\": 60,\"closedLoopControlName\": \"ControlLoopEvent1\",\t\"policyVersion\": \"1.0.0.5\",\t\"policyName\":\"vFireWall\",\"policyScope\": \"resource=sampleResource,type=sampletype,CLName=sampleCLName\",\"target_type\": \"VNF\",\t\"target\": \"genVnfName\",\t\"version\": \"1.0\"}, {\"eventName\": \"Heartbeat_vFW\",\"heartbeatcountmissed\": 3,\t\"heartbeatinterval\": 60,\"closedLoopControlName\": \"ControlLoopEvent1\",\"policyVersion\": \"1.0.0.5\",\"policyName\": \"vFireWall\",\"policyScope\": \"resource=sampleResource,type=sampletype,CLName=sampleCLName\",\t\"target_type\":\"VNF\",\t\"target\": \"genVnfName\",\t\"version\": \"1.0\"}, {\"eventName\": \"Heartbeat_xx\",\"heartbeatcountmissed\": 3,\t\"heartbeatinterval\": 60,\"closedLoopControlName\": \"ControlLoopEvent1\",\"policyVersion\": \"1.0.0.5\",\"policyName\": \"vFireWall\",\t\"policyScope\": \"resource=sampleResource,type=sampletype,CLName=sampleCLName\",\"target_type\": \"VNF\",\"target\": \"genVnfName\",\"version\": \"1.0\"}]}"} diff --git a/tests/test4.json b/tests/test4.json new file mode 100644 index 0000000..b223497 --- /dev/null +++ b/tests/test4.json @@ -0,0 +1 @@ +{"event":{"commonEventHeader":{"startEpochMicrosec":1621969018000,"sourceId":"79e90d76-513a-4f79-886d-470a0037c5cf","eventId":"Heartbeat_vDNS_10.0.0.1","nfcNamingCode":"DNS","reportingEntityId":"79e90d76-513a-4f79-886d-470a0037c5cf","internalHeaderFields":{"collectorTimeStamp":"Wed, 01 04 2023 01:36:35 GMT"},"eventType":"applicationVnf","priority":"Normal","version":3,"reportingEntityName":"VVVVVVcmd010","sequence":36312,"domain":"heartbeat","lastEpochMicrosec":1621969018000,"eventName":"Heartbeat_vDNS","sourceName":"zalp1bmdns01cmd010","nfNamingCode":"MDNS"}}} diff --git a/tests/test_db_monitoring.py b/tests/test_db_monitoring.py new file mode 100644 index 0000000..b9a644e --- /dev/null +++ b/tests/test_db_monitoring.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +# ============LICENSE_START======================================================= +# Copyright (c) 2023 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========================================================= + +import db_monitoring +import htbtworker +import logging +import requests +import tempfile +import os +import json +import unittest + +from unittest.mock import * +from _pytest.outcomes import skip + +_logger = logging.getLogger(__name__) +class Test_db_monitoring(unittest.TestCase): + + def setUp(self): + htbtworker.configjsonfile = (os.path.dirname(__file__))+"/test-config.json" + + @patch('requests.post') + def test_sendControlLoopEvent(self, mock1): + status = True + mock_resp = Mock() + mock_resp.configure_mock( + **{ + "status_code": 200 + } + ) + mock1.return_value = mock_resp + db_monitoring.sendControlLoopEvent("ONSET", "ABC","1.0","vFW","vFW","VNF","NODE","1234567890","VFW","1.0","DCAE") + self.assertEqual(status, True) + db_monitoring.sendControlLoopEvent("ONSET", "ABC","1.0","vFW","vFW","VM","NODE","1234567890","VFW","1.0","DCAE") + self.assertEqual(status, True) + db_monitoring.sendControlLoopEvent("ABATED", "ABC","1.0","vFW","vFW","VNF","NODE","1234567890","VFW","1.0","DCAE") + self.assertEqual(status, True) + db_monitoring.sendControlLoopEvent("ABATED", "ABC","1.0","vFW","vFW","VM","NODE","1234567890","VFW","1.0","DCAE") + self.assertEqual(status, True) + + @patch('misshtbtd.read_hb_common',return_value = ("1234","RUNNING", "XYZ", 1234)) + @patch('htbtworker.postgres_db_open') + def test_db_monitoring(self, mock1, mock2): + status = True + mock_cursor = Mock() + mock2.cursor.return_value = mock_cursor + db_monitoring.db_monitoring("111",htbtworker.configjsonfile ,"testuser","testpwd","10.0.0.0","1234","db_name") + self.assertEqual(status, True) + db_monitoring.db_monitoring("1234",htbtworker.configjsonfile ,"testuser","testpwd","10.0.0.0","1234","db_name") + self.assertEqual(status, True) + mock1.cursor.return_value = ("1234","RECONFIGURATION", "XYZ", 1234) + db_monitoring.db_monitoring("1234",htbtworker.configjsonfile ,"testuser","testpwd","10.0.0.0","1234","db_name") + self.assertEqual(status, True) + + def test_db_monitoring_wrapper(self): + status = True + db_monitoring.db_monitoring_wrapper("111", htbtworker.configjsonfile, number_of_iterations=0) + self.assertEqual(status, True) + +if __name__ == "__main__": # pragma: no cover + unittest.main() diff --git a/tests/test_get_logger.py b/tests/test_get_logger.py index a4ceea5..1096271 100644 --- a/tests/test_get_logger.py +++ b/tests/test_get_logger.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2020-2021 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2020-2023 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2021 Fujitsu Ltd. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,8 @@ log = logging.getLogger(__name__) def test_configure_logger(): + # logpath = (os.path.dirname(__file__))+"hb_logs.txt" + # expected_log_path = Path(logpath) expected_log_path = Path("./hb_logs.txt") if expected_log_path.exists(): os.remove(expected_log_path) @@ -34,6 +36,8 @@ def test_configure_logger(): def test_configure_logger_with_name(): + # logpath = (os.path.dirname(__file__))+"hb_htbtworker_logs.txt" + # expected_log_path = Path(logpath) expected_log_path = Path("./hb_htbtworker_logs.txt") if expected_log_path.exists(): os.remove(expected_log_path) diff --git a/tests/test_htbt_exit.py b/tests/test_htbt_exit.py new file mode 100644 index 0000000..f3698d5 --- /dev/null +++ b/tests/test_htbt_exit.py @@ -0,0 +1,50 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2017-2023 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2019 Pantheon.tech. All rights reserved. +# Copyright (c) 2021 Fujitsu Ltd. +# ================================================================================ +# 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========================================================= + +import pytest +import unittest +from miss_htbt_service.mod import htbt_exit + +pid_file = "/tmp/test_pid_file" +pid_file_dne = "/tmp/test_pid_file_NOT" + + +class test_cleanup_and_exit(unittest.TestCase): + """ + Test the cleanup_and_exit mod + """ + + def test_normal_exit(self): + """ + Test normal exit works as expected + """ + open(pid_file, "w") + + with pytest.raises(SystemExit) as pytest_wrapped_sys_exit: + result = htbt_exit.cleanup_and_exit(0, pid_file) + assert pytest_wrapped_sys_exit.type == SystemExit + assert pytest_wrapped_sys_exit.value.code == 0 + + def test_abnormal_exit(self): + """ + Test exit with missing PID file exits non-zero + """ + with pytest.raises(SystemExit) as pytest_wrapped_sys_exit: + result = htbt_exit.cleanup_and_exit(0, pid_file_dne) + assert pytest_wrapped_sys_exit.type == SystemExit + assert pytest_wrapped_sys_exit.value.code == 1 diff --git a/tests/test_htbt_get_cbs_config.py b/tests/test_htbt_get_cbs_config.py new file mode 100644 index 0000000..99e347c --- /dev/null +++ b/tests/test_htbt_get_cbs_config.py @@ -0,0 +1,148 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2017-2023 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2019 Pantheon.tech. All rights reserved. +# Copyright (c) 2021 Fujitsu Ltd. +# ================================================================================ +# 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========================================================= + +import pytest +import unittest +import os +import cbs_polling as cp +import time +import misshtbtd +from unittest.mock import * + +from miss_htbt_service.mod import htbt_get_cbs_config + + +class test_get_cbs_config(unittest.TestCase): + """ + Test the htbt_get_cbs_config mod + """ + + pytest_json_data = ( + "{" + ' "heartbeat_config": {' + ' "vnfs": [' + " {" + ' "eventName": "Heartbeat_vDNS",' + ' "heartbeatcountmissed": 3,' + ' "heartbeatinterval": 60,' + ' "closedLoopControlName": "ControlLoopEvent1",' + ' "policyVersion": "1.0.0.5",' + ' "policyName": "vFireWall",' + ' "policyScope": "resource=sampleResource,' + " type=sampletype," + ' CLName=sampleCLName",' + ' "target_type": "VNF",' + ' "target": "genVnfName",' + ' "version": "1.0"' + " }," + " {" + ' "eventName": "Heartbeat_vFW",' + ' "heartbeatcountmissed": 3,' + ' "heartbeatinterval": 60,' + ' "closedLoopControlName": "ControlLoopEvent1",' + ' "policyVersion": "1.0.0.5",' + ' "policyName": "vFireWall",' + ' "policyScope": "resource=sampleResource,' + " type=sampletype," + ' CLName=sampleCLName",' + ' "target_type": "VNF",' + ' "target": "genVnfName",' + ' "version": "1.0"' + " }," + " {" + ' "eventName": "Heartbeat_xx",' + ' "heartbeatcountmissed": 3,' + ' "heartbeatinterval": 60,' + ' "closedLoopControlName": "ControlLoopEvent1",' + ' "policyVersion": "1.0.0.5",' + ' "policyName": "vFireWall",' + ' "policyScope": "resource=sampleResource,' + " type=sampletype," + ' CLName=sampleCLName",' + ' "target_type": "VNF",' + ' "target": "genVnfName",' + ' "version": "1.0"' + " }" + " ]" + " }," + ' "streams_publishes": {' + ' "dcae_cl_out": {' + ' "dmaap_info": {' + ' "topic_url": "http://message-router:3904/events/unauthenticated.DCAE_CL_OUTPUT/"' + " }," + ' "type": "message_router"' + " }" + " }," + ' "streams_subscribes": {' + ' "ves-heartbeat": {' + ' "dmaap_info": {' + ' "topic_url": "http://message-router:3904/events/unauthenticated.SEC_HEARTBEAT_INPUT/"' + " }," + ' "type": "message_router"' + " }" + " }," + ' "pg_ipAddress": "10.0.4.1",' + ' "pg_userName": "postgres",' + ' "pg_dbName": "postgres",' + ' "pg_passwd": "postgres",' + ' "pg_portNum": "5432"' + "}" + ) + + # create copy of snmphtbt.json for pytest + #pytest_json_config = "/tmp/opt/app/miss_htbt_service/etc/config.json" + pytest_json_config = "test-config.json" + with open(pytest_json_config, "w") as outfile: + outfile.write(pytest_json_data) + + def test_cbs_env_present(self): + """ + Test that CONSUL_HOST env variable exists but fails to + respond + """ + + with pytest.raises(Exception) as pytest_wrapped_sys_exit: + result = htbt_get_cbs_config.get_cbs_config() + assert pytest_wrapped_sys_exit.type == SystemExit + + def test_cbs_fallback_env_present(self): + """ + Test that CBS fallback env variable exists and we can get config + from fallback env var + """ + os.environ.update(CBS_HTBT_JSON="/tmp/opt/app/miss_htbt_service/etc/config.json") + result = True + print("result: %s" % result) + self.assertEqual(result, True) + + @patch('misshtbtd.create_update_hb_common') + @patch('misshtbtd.read_hb_common') + def test_poll_cbs(self, mock1, mock2): + """ + TBD + """ + status = True + current_time = round(time.time()) + mock1.return_value = ('1', 'RUNNING', 'AA', current_time) + # configjsonfile = (os.path.dirname(__file__))+"/test-config.json" + configjsonfile = "test-config.json" + os.environ.update(CBS_HTBT_JSON=configjsonfile) + os.environ["pytest"] = "test" + cp.poll_cbs(1) + self.assertEqual(status, True) + diff --git a/tests/test_htbt_http_session.py b/tests/test_htbt_http_session.py new file mode 100644 index 0000000..ed3a33a --- /dev/null +++ b/tests/test_htbt_http_session.py @@ -0,0 +1,34 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2017-2023 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2019 Pantheon.tech. All rights reserved. +# Copyright (c) 2021 Fujitsu Ltd. +# ================================================================================ +# 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========================================================= + +import unittest +from miss_htbt_service.mod import htbt_http_session + + +class test_init_session_obj(unittest.TestCase): + """ + Test the init_session_obj mod + """ + + def test_correct_usage(self): + """ + Test that attempt to create http session object works + """ + result = htbt_http_session.init_session_obj() + compare = str(result).startswith("