From db08ae5dac684a1155dedfd199ae4e372c82e9df Mon Sep 17 00:00:00 2001 From: "Hansen, Tony (th1395)" Date: Mon, 27 Jan 2020 22:28:34 +0000 Subject: increase code coverage Change-Id: I30632d2e34401f8eff6e67b9b3d8abba32e80050 Signed-off-by: Hansen, Tony (th1395) Issue-ID: DCAEGEN2-1902 Signed-off-by: Hansen, Tony (th1395) --- miss_htbt_service/__init__.py | 5 +-- miss_htbt_service/cbs_polling.py | 2 +- miss_htbt_service/check_health.py | 10 +++--- miss_htbt_service/config_notif.py | 47 +++++++++++++++++---------- miss_htbt_service/db_monitoring.py | 2 +- miss_htbt_service/get_logger.py | 4 +-- miss_htbt_service/htbtworker.py | 12 +++---- miss_htbt_service/makefile | 2 ++ miss_htbt_service/misshtbt.sh | 3 -- miss_htbt_service/misshtbtd.py | 13 ++++---- miss_htbt_service/mod/__init__.py | 4 +-- miss_htbt_service/mod/trapd_exit.py | 6 ++-- miss_htbt_service/mod/trapd_get_cbs_config.py | 6 ++-- miss_htbt_service/mod/trapd_http_session.py | 6 ++-- miss_htbt_service/mod/trapd_io.py | 6 ++-- miss_htbt_service/mod/trapd_runtime_pid.py | 6 ++-- miss_htbt_service/mod/trapd_settings.py | 6 ++-- miss_htbt_service/mod/trapd_vnf_table.py | 9 ++--- 18 files changed, 69 insertions(+), 80 deletions(-) create mode 100644 miss_htbt_service/makefile (limited to 'miss_htbt_service') diff --git a/miss_htbt_service/__init__.py b/miss_htbt_service/__init__.py index 8da7cd3..1ae08ca 100644 --- a/miss_htbt_service/__init__.py +++ b/miss_htbt_service/__init__.py @@ -1,5 +1,5 @@ # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2020 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. @@ -13,9 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. - # empty __init__.py so that pytest can add correct path to coverage report, -- per pytest # best practice guideline diff --git a/miss_htbt_service/cbs_polling.py b/miss_htbt_service/cbs_polling.py index 01f30bb..7d19610 100644 --- a/miss_htbt_service/cbs_polling.py +++ b/miss_htbt_service/cbs_polling.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright 2018 AT&T Intellectual Property, Inc. All rights reserved. +# Copyright 2018-2020 AT&T Intellectual Property, Inc. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/miss_htbt_service/check_health.py b/miss_htbt_service/check_health.py index 5732749..03c390b 100644 --- a/miss_htbt_service/check_health.py +++ b/miss_htbt_service/check_health.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 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,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. from http.server import HTTPServer, BaseHTTPRequestHandler from urllib import parse @@ -45,18 +43,18 @@ class GetHandler(BaseHTTPRequestHandler): ]) self.send_response(200) self.end_headers() - self.wfile.write(message) + self.wfile.write(bytes(message, 'utf-8')) return def do_POST(self): - content_len = int(self.headers.getheader('content-length')) + content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) self.send_response(200) self.end_headers() data = json.loads(post_body) - self.wfile.write(data['health']) + self.wfile.write(bytes(data['health'], 'utf-8')) return if __name__ == '__main__': diff --git a/miss_htbt_service/config_notif.py b/miss_htbt_service/config_notif.py index a1d2d8b..6b54bad 100644 --- a/miss_htbt_service/config_notif.py +++ b/miss_htbt_service/config_notif.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright 2018 AT&T Intellectual Property, Inc. All rights reserved. +# Copyright 2018-2020 AT&T Intellectual Property, Inc. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,7 +29,9 @@ import psycopg2 from pathlib import Path import os.path as path from .mod import trapd_settings as tds -from .mod.trapd_get_cbs_config import get_cbs_config +# use the fully qualified name here to let monkeypatching work +# from .mod.trapd_get_cbs_config import get_cbs_config +import miss_htbt_service.mod.trapd_get_cbs_config hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.yaml")) @@ -37,39 +39,42 @@ def postgres_db_open(username,password,host,port,database_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True - try: #pragma: no cover - connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port) + try: + connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port) except Exception as e: - print("HB_Notif::postgress connect error: %s" % e) - connection = True + print("HB_Notif::postgress connect error: %s" % e) + connection = True return connection def db_table_creation_check(connection_db,table_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True - try: #pragma: no cover + cur = None + try: cur = connection_db.cursor() query_db = "select * from information_schema.tables where table_name='%s'" %(table_name) cur.execute(query_db) database_names = cur.fetchone() - if(database_names is not None): - if(table_name in database_names): - print("HB_Notif::Postgres already has table - %s" % table_name) - return True + if (database_names is not None) and (table_name in database_names): + print(f"FOUND the table {table_name}") + print("HB_Notif::Postgres already has table - %s" % table_name) + return True else: + print(f"did NOT find the table {table_name}") print("HB_Notif::Postgres does not have table - %s" % table_name) return False except psycopg2.DatabaseError as e: print('COMMON:Error %s' % e) finally: - cur.close() + if cur: + cur.close() def commit_and_close_db(connection_db): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True - try: #pragma: no cover + try: connection_db.commit() # <--- makes sure the change is shown in the database connection_db.close() return True @@ -96,6 +101,8 @@ def read_hb_properties_default(): cbs_polling_required = a['CBS_polling_allowed'] cbs_polling_interval = a['CBS_polling_interval'] s.close() + # TODO: there is a mismatch here between read_hb_properties_default and read_hb_properties. + # read_hb_properties() forces all of the variables returned here to be strings, while the code here does not. return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval def read_hb_properties(jsfile): @@ -134,6 +141,7 @@ def read_hb_common(user_name,password,ip_address,port_num,db_name): query_value = "SELECT process_id,source_name,last_accessed_time,current_state FROM hb_common;" cur.execute(query_value) rows = cur.fetchall() + # TODO: What if rows returned None or empty? print("HB_Notif::hb_common contents - %s" % rows) hbc_pid = rows[0][0] hbc_srcName = rows[0][1] @@ -158,9 +166,11 @@ def update_hb_common(update_flg, process_id, state, user_name,password,ip_addres cur.close() return True -def fetch_json_file(): - if get_cbs_config(): - current_runtime_config_file_name = "../etc/download1.json" +def fetch_json_file(download_json = "../etc/download1.json", config_json = "../etc/config.json"): + # use the fully qualified name here to let monkeypatching work + # if get_cbs_config(): + if miss_htbt_service.mod.trapd_get_cbs_config.get_cbs_config(): + current_runtime_config_file_name = download_json envPytest = os.getenv('pytest', "") if (envPytest == 'test'): jsfile = "../etc/config.json" @@ -171,7 +181,7 @@ def fetch_json_file(): jsfile = current_runtime_config_file_name else: print("MSHBD:CBS Config not available, using local config") - jsfile = "../etc/config.json" + jsfile = config_json print("Config_N: The json file is - %s" % jsfile) return jsfile @@ -187,11 +197,12 @@ def config_notif_run(): if(db_table_creation_check(connection_db,"hb_common") == False): print("HB_Notif::ERROR::hb_common table not exists - No config download") connection_db.close() - else: #pragma: no cover + else: hbc_pid, hbc_state, hbc_srcName, hbc_time = read_hb_common(user_name,password,ip_address,port_num,db_name) state = "RECONFIGURATION" update_flg = 1 ret = update_hb_common(update_flg, hbc_pid, state, user_name,password,ip_address,port_num,db_name) + # TODO: There is no way for update_hb_common() to return false if (ret == True): print("HB_Notif::hb_common table updated with RECONFIGURATION state") commit_and_close_db(connection_db) diff --git a/miss_htbt_service/db_monitoring.py b/miss_htbt_service/db_monitoring.py index 95b2dbe..6ab7732 100644 --- a/miss_htbt_service/db_monitoring.py +++ b/miss_htbt_service/db_monitoring.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright 2018 AT&T Intellectual Property, Inc. All rights reserved. +# Copyright 2018-2020 AT&T Intellectual Property, Inc. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/miss_htbt_service/get_logger.py b/miss_htbt_service/get_logger.py index 7afbc92..7d9d8d6 100644 --- a/miss_htbt_service/get_logger.py +++ b/miss_htbt_service/get_logger.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 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. @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. import os import logging diff --git a/miss_htbt_service/htbtworker.py b/miss_htbt_service/htbtworker.py index c02f817..db0561f 100644 --- a/miss_htbt_service/htbtworker.py +++ b/miss_htbt_service/htbtworker.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright 2018 AT&T Intellectual Property, Inc. All rights reserved. +# Copyright 2018-2020 AT&T Intellectual Property, Inc. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -30,19 +30,19 @@ from . import get_logger _logger = get_logger.get_logger(__name__) -def read_json_file(i): +def read_json_file(i, prefix="../../tests"): if (i==0): - with open (path.abspath(path.join(__file__, "../../tests/test1.json")), "r") as outfile: + with open (path.abspath(path.join(__file__, f"{prefix}/test1.json")), "r") as outfile: cfg = json.load(outfile) elif (i == 1): - with open (path.abspath(path.join(__file__, "../../tests/test2.json")), "r") as outfile: + with open (path.abspath(path.join(__file__, f"{prefix}/test2.json")), "r") as outfile: cfg = json.load(outfile) elif (i ==2): - with open( path.abspath(path.join(__file__, "../../tests/test3.json")), 'r') as outfile: + with open( path.abspath(path.join(__file__, f"{prefix}/test3.json")), 'r') as outfile: cfg = json.load(outfile) return cfg -def process_msg(jsfile,user_name, password, ip_address, port_num, db_name): +def process_msg(jsfile, user_name, password, ip_address, port_num, db_name): global mr_url i=0 sleep_duration = 20 diff --git a/miss_htbt_service/makefile b/miss_htbt_service/makefile new file mode 100644 index 0000000..9901727 --- /dev/null +++ b/miss_htbt_service/makefile @@ -0,0 +1,2 @@ +runtox: + cd .. && $(MAKE) runtox diff --git a/miss_htbt_service/misshtbt.sh b/miss_htbt_service/misshtbt.sh index 5b598b1..df453b4 100644 --- a/miss_htbt_service/misshtbt.sh +++ b/miss_htbt_service/misshtbt.sh @@ -17,9 +17,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# # get to where we are supposed to be for startup cd /opt/app/misshtbt/bin diff --git a/miss_htbt_service/misshtbtd.py b/miss_htbt_service/misshtbtd.py index 790ab6a..c56ad99 100644 --- a/miss_htbt_service/misshtbtd.py +++ b/miss_htbt_service/misshtbtd.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # ============LICENSE_START======================================================= -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +16,6 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. # This is a main process that does the following # - Creates the CBS polling process that indicates the periodic download of # configuration file from CBS @@ -109,11 +108,11 @@ def create_database(update_db, jsfile, ip_address, port_num, user_name, password def read_hb_common(user_name,password,ip_address,port_num,db_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): - hbc_pid = 10 - hbc_srcName = "srvc_name" - hbc_time = 1584595881 - hbc_state = "RUNNING" - return hbc_pid, hbc_state, hbc_srcName, hbc_time + hbc_pid = 10 + hbc_srcName = "srvc_name" + hbc_time = 1584595881 + hbc_state = "RUNNING" + return hbc_pid, hbc_state, hbc_srcName, hbc_time connection_db = heartbeat.postgres_db_open(user_name,password,ip_address,port_num,db_name) cur = connection_db.cursor() query_value = "SELECT process_id,source_name,last_accessed_time,current_state FROM hb_common;" diff --git a/miss_htbt_service/mod/__init__.py b/miss_htbt_service/mod/__init__.py index 1875bf6..8e53efc 100644 --- a/miss_htbt_service/mod/__init__.py +++ b/miss_htbt_service/mod/__init__.py @@ -1,5 +1,5 @@ # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2020 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. @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. # empty __init__.py so that pytest can add correct path to coverage report, -- per pytest diff --git a/miss_htbt_service/mod/trapd_exit.py b/miss_htbt_service/mod/trapd_exit.py index 80ed1b0..b1bd68f 100644 --- a/miss_htbt_service/mod/trapd_exit.py +++ b/miss_htbt_service/mod/trapd_exit.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ trapc_exit_snmptrapd is responsible for removing any existing runtime PID file, and exiting with the provided (param 1) exit code diff --git a/miss_htbt_service/mod/trapd_get_cbs_config.py b/miss_htbt_service/mod/trapd_get_cbs_config.py index 86e621f..d0b8110 100644 --- a/miss_htbt_service/mod/trapd_get_cbs_config.py +++ b/miss_htbt_service/mod/trapd_get_cbs_config.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ Look for CBS broker and return application config; if not present, look for env variable that specifies JSON equiv of CBS config (typically used for diff --git a/miss_htbt_service/mod/trapd_http_session.py b/miss_htbt_service/mod/trapd_http_session.py index b34c19d..0d7f220 100644 --- a/miss_htbt_service/mod/trapd_http_session.py +++ b/miss_htbt_service/mod/trapd_http_session.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 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. @@ -15,9 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ trapd_http_session establishes an http session for future use in publishing messages to the dmaap cluster. diff --git a/miss_htbt_service/mod/trapd_io.py b/miss_htbt_service/mod/trapd_io.py index 063f3ee..4f0904e 100644 --- a/miss_htbt_service/mod/trapd_io.py +++ b/miss_htbt_service/mod/trapd_io.py @@ -1,7 +1,7 @@ # ============LICENSE_START=======================================================) # org.onap.dcae # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ """ diff --git a/miss_htbt_service/mod/trapd_runtime_pid.py b/miss_htbt_service/mod/trapd_runtime_pid.py index c6ef76e..7194b8f 100644 --- a/miss_htbt_service/mod/trapd_runtime_pid.py +++ b/miss_htbt_service/mod/trapd_runtime_pid.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 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. @@ -15,9 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ trapd_runtime_pid maintains a 'PID file' (file that contains the PID of currently running trap receiver) diff --git a/miss_htbt_service/mod/trapd_settings.py b/miss_htbt_service/mod/trapd_settings.py index be87e26..0c8457f 100644 --- a/miss_htbt_service/mod/trapd_settings.py +++ b/miss_htbt_service/mod/trapd_settings.py @@ -1,7 +1,7 @@ # ============LICENSE_START=======================================================) # org.onap.dcae # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018-2020 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. @@ -15,9 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# + """ """ diff --git a/miss_htbt_service/mod/trapd_vnf_table.py b/miss_htbt_service/mod/trapd_vnf_table.py index 56aedbb..eeb4dc6 100644 --- a/miss_htbt_service/mod/trapd_vnf_table.py +++ b/miss_htbt_service/mod/trapd_vnf_table.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,9 +17,8 @@ # limitations under the License. # ============LICENSE_END========================================================= # -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# ## Author Kiran Mandal (km386e) + """ trapd_vnf_table verifies the successful creation of DB Tables. """ @@ -120,7 +119,9 @@ def verify_cbspolling(): try: _cbspolling=cbs.pollCBS(10) except Exception as e: - #print("CBSP error - %s" % e) + # print("CBSP error - %s" % e) + # print("CBSP error - %s" % e, file=sys.stderr) + # print("Stack: {0}".format(traceback.format_exc()), file=sys.stderr) return None os.unsetenv('pytest') -- cgit 1.2.3-korg