From f759989a43ddbc8991e45697f0682808716f8d16 Mon Sep 17 00:00:00 2001 From: Vijay Venkatesh Kumar Date: Fri, 24 Mar 2023 22:39:50 +0000 Subject: Fix build job error black check failure https://jenkins.onap.org/view/dcaegen2/job/dcaegen2-services-heartbeat-master-release-version-java-daily/lastBuild/console onap-gerrit-review: -changelog-or-release-file-missing Change-Id: I0e57025285ea90c6e649feae0d2e955064b76083 Signed-off-by: Vijay Venkatesh Kumar Issue-ID: DCAEGEN2-3366 --- miss_htbt_service/db_monitoring.py | 27 +++---- tests/test_db_monitoring.py | 140 +++++++++++++++++++------------------ 2 files changed, 82 insertions(+), 85 deletions(-) diff --git a/miss_htbt_service/db_monitoring.py b/miss_htbt_service/db_monitoring.py index 472c356..2b7c056 100644 --- a/miss_htbt_service/db_monitoring.py +++ b/miss_htbt_service/db_monitoring.py @@ -156,7 +156,6 @@ def sendControlLoopEvent( return True - def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_num, db_name, sleeptime=20): while True: time.sleep(sleeptime) @@ -169,21 +168,18 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_ msg = "Json file process error : ", err _logger.error(msg) continue - db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_address, port_num, db_name) + db_monitoring_singlepass(current_pid, pol_url, user_name, password, ip_address, port_num, db_name) break - -def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_address, port_num, db_name) : - hbc_pid, hbc_state, hbc_src_name, hbc_time = db.read_hb_common( - user_name, password, ip_address, port_num, db_name - ) + + +def db_monitoring_singlepass(current_pid, pol_url, user_name, password, ip_address, port_num, db_name): + hbc_pid, hbc_state, hbc_src_name, hbc_time = db.read_hb_common(user_name, password, ip_address, port_num, db_name) source_name = socket.gethostname() source_name = source_name + "-" + str(os.getenv("SERVICE_NAME", "")) - + connection_db = pm.postgres_db_open() cur = connection_db.cursor() - if ( - int(current_pid) == int(hbc_pid) and source_name == hbc_src_name and hbc_state == "RUNNING" - ): + if int(current_pid) == int(hbc_pid) and source_name == hbc_src_name and hbc_state == "RUNNING": _logger.info("DBM: Active DB Monitoring Instance") cur.execute("SELECT event_name FROM vnf_table_1") vnf_list = [item[0] for item in cur.fetchall()] @@ -228,7 +224,7 @@ def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_addr epoc_time_sec = row[0][0] src_name = row[0][1] cl_flag = row[0][2] - if (epoc_time - epoc_time_sec) > comparision_time and cl_flag == 0: + if (epoc_time - epoc_time_sec) > comparision_time and cl_flag == 0: sendControlLoopEvent( "ONSET", pol_url, @@ -248,7 +244,7 @@ def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_addr (cl_flag, event_name, (source_name_key + 1)), ) connection_db.commit() - elif (epoc_time - epoc_time_sec) < comparision_time and cl_flag == 1: + elif (epoc_time - epoc_time_sec) < comparision_time and cl_flag == 1: sendControlLoopEvent( "ABATED", pol_url, @@ -269,7 +265,7 @@ def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_addr ) connection_db.commit() - else: + else: msg = "DBM:DB Monitoring is ignored for %s since validity flag is 0" % event_name _logger.info(msg) @@ -279,7 +275,7 @@ def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_addr """ Delete the VNF entry in table1 and delete all the source ids related to vnfs in table2 """ - else: + else: msg = "DBM:Inactive instance or hb_common state is not RUNNING" _logger.info(msg) try: @@ -293,7 +289,6 @@ def db_monitoring_singlepass (current_pid, pol_url, user_name, password, ip_addr cur.close() - def db_monitoring_wrapper(current_pid, jsfile, number_of_iterations=-1): get_logger.configure_logger("db_monitoring") _logger.info("DBM: DBM Process started") diff --git a/tests/test_db_monitoring.py b/tests/test_db_monitoring.py index 9d5c3c3..fccbae0 100644 --- a/tests/test_db_monitoring.py +++ b/tests/test_db_monitoring.py @@ -34,119 +34,130 @@ _logger = logging.getLogger(__name__) class Test_db_monitoring(unittest.TestCase): - - class PseudoCursorCase1(): + class PseudoCursorCase1: # Test setup for RECONFIGURATION state fetchall_1 = "" + @classmethod - def execute(ctype ,command,arg=None): + def execute(ctype, command, arg=None): global fetchall_1 if command.startswith("SELECT validity_flag, source_name_count, heartbeat_interval,"): - fetchall_1 = [[1,1,300,1,"TEMP-CL", "1.0","TEMPPOLICY","TEMP","VM","TEMP","1.0"]] - elif command.startswith ("SELECT last_epo_time, source_name, cl_flag FROM "): + fetchall_1 = [[1, 1, 300, 1, "TEMP-CL", "1.0", "TEMPPOLICY", "TEMP", "VM", "TEMP", "1.0"]] + elif command.startswith("SELECT last_epo_time, source_name, cl_flag FROM "): millisec = time.time() * 1000 - fetchall_1 = [[millisec -300 ,"testnodeA",0]] - elif command.startswith ("SELECT event_name FROM vnf_table_1"): - fetchall_1 = [["Heartbeat_vDNS","Heartbeat_vFw"]] - elif command.startswith ("SELECT current_state"): + fetchall_1 = [[millisec - 300, "testnodeA", 0]] + elif command.startswith("SELECT event_name FROM vnf_table_1"): + fetchall_1 = [["Heartbeat_vDNS", "Heartbeat_vFw"]] + elif command.startswith("SELECT current_state"): fetchall_1 = [["RECONFIGURATION"]] - elif command.startswith ("DELETE "): + elif command.startswith("DELETE "): fetchall_1 = None - elif command.startswith ("UPDATE"): + elif command.startswith("UPDATE"): fetchall_1 = None else: raise RuntimeError("Unknown db execution") + @classmethod - def fetchall (ctype): + def fetchall(ctype): global fetchall_1 return fetchall_1 + @classmethod def close(ctype): pass - class PseudoCursorCase2(): + class PseudoCursorCase2: # Test setup for RUNNING state and CL Onset condition fetchall_2 = "" + @classmethod - def execute(ctype ,command,arg=None): + def execute(ctype, command, arg=None): global fetchall_2 if command.startswith("SELECT validity_flag, source_name_count, heartbeat_interval,"): - fetchall_2 = [[1,1,300,1,"TEMP-CL", "1.0","TEMPPOLICY","TEMP","VM","TEMP","1.0"]] - elif command.startswith ("SELECT last_epo_time, source_name, cl_flag FROM "): + fetchall_2 = [[1, 1, 300, 1, "TEMP-CL", "1.0", "TEMPPOLICY", "TEMP", "VM", "TEMP", "1.0"]] + elif command.startswith("SELECT last_epo_time, source_name, cl_flag FROM "): millisec = time.time() * 1000 - fetchall_2 = [[millisec -500 ,"testnodeA",0]] - elif command.startswith ("SELECT event_name FROM vnf_table_1"): - fetchall_2 = [["Heartbeat_vDNS","Heartbeat_vFw"]] - elif command.startswith ("SELECT current_state"): + fetchall_2 = [[millisec - 500, "testnodeA", 0]] + elif command.startswith("SELECT event_name FROM vnf_table_1"): + fetchall_2 = [["Heartbeat_vDNS", "Heartbeat_vFw"]] + elif command.startswith("SELECT current_state"): fetchall_2 = [["RUNNING"]] - elif command.startswith ("DELETE "): + elif command.startswith("DELETE "): fetchall_2 = None - elif command.startswith ("UPDATE"): + elif command.startswith("UPDATE"): fetchall_2 = None else: raise RuntimeError("Unknown db execution") + @classmethod - def fetchall (ctype): + def fetchall(ctype): global fetchall_2 return fetchall_2 + @classmethod def close(ctype): pass - class PseudoCursorCase3(): + class PseudoCursorCase3: # Test setup for RUNNING state and CL Abatement condition fetchall_3 = "" + @classmethod - def execute(ctype ,command,arg=None): + def execute(ctype, command, arg=None): global fetchall_3 if command.startswith("SELECT validity_flag, source_name_count, heartbeat_interval,"): - fetchall_3 = [[1,1,300,1,"TEMP-CL", "1.0","TEMPPOLICY","TEMP","VM","TEMP","1.0"]] - elif command.startswith ("SELECT last_epo_time, source_name, cl_flag FROM "): + fetchall_3 = [[1, 1, 300, 1, "TEMP-CL", "1.0", "TEMPPOLICY", "TEMP", "VM", "TEMP", "1.0"]] + elif command.startswith("SELECT last_epo_time, source_name, cl_flag FROM "): millisec = time.time() * 1000 - fetchall_3 = [[millisec -20 ,"testnodeA",1]] - elif command.startswith ("SELECT event_name FROM vnf_table_1"): - fetchall_3 = [["Heartbeat_vDNS","Heartbeat_vFw"]] - elif command.startswith ("SELECT current_state"): + fetchall_3 = [[millisec - 20, "testnodeA", 1]] + elif command.startswith("SELECT event_name FROM vnf_table_1"): + fetchall_3 = [["Heartbeat_vDNS", "Heartbeat_vFw"]] + elif command.startswith("SELECT current_state"): fetchall_3 = [["RUNNING"]] - elif command.startswith ("DELETE "): + elif command.startswith("DELETE "): fetchall_3 = None - elif command.startswith ("UPDATE"): + elif command.startswith("UPDATE"): fetchall_3 = None else: raise RuntimeError("Unknown db execution") + @classmethod - def fetchall (ctype): + def fetchall(ctype): global fetchall_3 return fetchall_3 + @classmethod def close(ctype): pass - class PseudoCursorCase4(): + class PseudoCursorCase4: # Test setup for SourceNode not actively tracked for CL (validity_flag=0) fetchall_4 = "" + @classmethod - def execute(ctype ,command,arg=None): + def execute(ctype, command, arg=None): global fetchall_4 if command.startswith("SELECT validity_flag, source_name_count, heartbeat_interval,"): - fetchall_4 = [[0,1,300,1,"TEMP-CL", "1.0","TEMPPOLICY","TEMP","VM","TEMP","1.0"]] - elif command.startswith ("SELECT last_epo_time, source_name, cl_flag FROM "): + fetchall_4 = [[0, 1, 300, 1, "TEMP-CL", "1.0", "TEMPPOLICY", "TEMP", "VM", "TEMP", "1.0"]] + elif command.startswith("SELECT last_epo_time, source_name, cl_flag FROM "): millisec = time.time() * 1000 - fetchall_4 = [[millisec -500 ,"testnodeA",0]] - elif command.startswith ("SELECT event_name FROM vnf_table_1"): - fetchall_4 = [["Heartbeat_vDNS","Heartbeat_vFw"]] - elif command.startswith ("SELECT current_state"): + fetchall_4 = [[millisec - 500, "testnodeA", 0]] + elif command.startswith("SELECT event_name FROM vnf_table_1"): + fetchall_4 = [["Heartbeat_vDNS", "Heartbeat_vFw"]] + elif command.startswith("SELECT current_state"): fetchall_4 = [["RUNNING"]] - elif command.startswith ("DELETE "): + elif command.startswith("DELETE "): fetchall_4 = None - elif command.startswith ("UPDATE"): + elif command.startswith("UPDATE"): fetchall_4 = None else: raise RuntimeError("Unknown db execution") + @classmethod - def fetchall (ctype): + def fetchall(ctype): global fetchall_4 return fetchall_4 + @classmethod def close(ctype): pass @@ -179,17 +190,15 @@ class Test_db_monitoring(unittest.TestCase): @patch("misshtbtd.read_hb_common", return_value=("1234", "RUNNING", "XYZ-", 1234)) @patch("htbtworker.postgres_db_open") - @patch("socket.gethostname", return_value = "XYZ") - #@patch("db_monitoring.sendControlLoopEvent", return_value = True) + @patch("socket.gethostname", return_value="XYZ") + # @patch("db_monitoring.sendControlLoopEvent", return_value = True) def test_db_monitoring(self, mock1, mock2, mock3): status = True mock_cursor = Mock() - + ## Test setup for RECONFIGURATION state - mock_cursor.configure_mock( - **{"cursor.return_value":Test_db_monitoring.PseudoCursorCase1} - ) + mock_cursor.configure_mock(**{"cursor.return_value": Test_db_monitoring.PseudoCursorCase1}) mock2.return_value = mock_cursor # Test for outer else when PID doesn't match db_monitoring.db_monitoring( @@ -199,37 +208,31 @@ class Test_db_monitoring(unittest.TestCase): # Test for RECONFIGURATION state mock1.cursor.return_value = ("1234", "RECONFIGURATION", "XYZ-", 1234) db_monitoring.db_monitoring( - "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 + "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 ) self.assertEqual(status, True) ## Test for RUNNING state and CL ONSET - mock_cursor.configure_mock( - **{"cursor.return_value":Test_db_monitoring.PseudoCursorCase2} - ) + mock_cursor.configure_mock(**{"cursor.return_value": Test_db_monitoring.PseudoCursorCase2}) mock2.return_value = mock_cursor db_monitoring.db_monitoring( - "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 + "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 ) self.assertEqual(status, True) ## Test for RUNNING state and CL Abatement condition - mock_cursor.configure_mock( - **{"cursor.return_value":Test_db_monitoring.PseudoCursorCase3} - ) - mock2.return_value = mock_cursor + mock_cursor.configure_mock(**{"cursor.return_value": Test_db_monitoring.PseudoCursorCase3}) + mock2.return_value = mock_cursor db_monitoring.db_monitoring( - "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name" , 1 + "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 ) self.assertEqual(status, True) ## # Test setup for SourceNode not actively tracked for CL (validity_flag=0) - mock_cursor.configure_mock( - **{"cursor.return_value":Test_db_monitoring.PseudoCursorCase4} - ) + mock_cursor.configure_mock(**{"cursor.return_value": Test_db_monitoring.PseudoCursorCase4}) mock2.return_value = mock_cursor db_monitoring.db_monitoring( - "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 + "1234", htbtworker.configjsonfile, "testuser", "testpwd", "10.0.0.0", "1234", "db_name", 1 ) self.assertEqual(status, True) @@ -246,15 +249,14 @@ class Test_db_monitoring(unittest.TestCase): @classmethod def switch_filepath(ctype, jsonfile): - #print (f"ctype: {ctype} type : {type(ctype)}") + # print (f"ctype: {ctype} type : {type(ctype)}") time.sleep(5) - os.rename(htbtworker.configjsonfile+"_1", htbtworker.configjsonfile) - + os.rename(htbtworker.configjsonfile + "_1", htbtworker.configjsonfile) def test_db_monitoring_wrapper(self): status = True db_monitoring.db_monitoring_wrapper("111", htbtworker.configjsonfile, number_of_iterations=0) - self.assertEqual(status, True) + self.assertEqual(status, True) if __name__ == "__main__": # pragma: no cover -- cgit 1.2.3-korg