From dd01b5600994b3e51ba46ab06c96d58ae4fb30dd Mon Sep 17 00:00:00 2001 From: Renuka Manavalan <47282725+renukamanavalan@users.noreply.github.com> Date: Mon, 2 Aug 2021 09:13:15 -0700 Subject: [PATCH] disk_check updates: (#1736) 1) Set default loglevel to SYSLOG_ERR 2) Make log level configurable via args --- scripts/disk_check.py | 21 +++++++++++++++------ tests/disk_check_test.py | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/scripts/disk_check.py b/scripts/disk_check.py index 94959bfa1a06..b13f1335f023 100644 --- a/scripts/disk_check.py +++ b/scripts/disk_check.py @@ -33,19 +33,23 @@ WORK_DIR = "/run/mount/work" MOUNTS_FILE = "/proc/mounts" +chk_log_level = syslog.LOG_ERR + +def _log_msg(lvl, pfx, msg): + if lvl <= chk_log_level: + print("{}: {}".format(pfx, msg)) + syslog.syslog(lvl, msg) + def log_err(m): - print("Err: {}".format(m), file=sys.stderr) - syslog.syslog(syslog.LOG_ERR, m) + _log_msg(syslog.LOG_ERR, "Err", m) def log_info(m): - print("Info: {}".format(m)) - syslog.syslog(syslog.LOG_INFO, m) + _log_msg(syslog.LOG_INFO, "Info", m) def log_debug(m): - print("debug: {}".format(m)) - syslog.syslog(syslog.LOG_DEBUG, m) + _log_msg(syslog.LOG_DEBUG, "Debug", m) def test_writable(dirs): @@ -135,14 +139,19 @@ def do_check(skip_mount, dirs): def main(): + global chk_log_level + parser=argparse.ArgumentParser( description="check disk for Read-Write and mount etc & home as Read-Write") parser.add_argument('-s', "--skip-mount", action='store_true', default=False, help="Skip mounting /etc & /home as Read-Write") parser.add_argument('-d', "--dirs", default="/etc,/home", help="dirs to mount") + parser.add_argument('-l', "--loglvl", default=syslog.LOG_ERR, type=int, + help="log level") args = parser.parse_args() + chk_log_level = args.loglvl ret = do_check(args.skip_mount, args.dirs.split(",")) return ret diff --git a/tests/disk_check_test.py b/tests/disk_check_test.py index 40bc55f0f5ad..b5ad7a489c3a 100644 --- a/tests/disk_check_test.py +++ b/tests/disk_check_test.py @@ -56,6 +56,7 @@ } err_data = "" +max_log_lvl = -1 cmds = [] current_tc = None @@ -66,6 +67,11 @@ def mount_file(d): def report_err_msg(lvl, m): global err_data + global max_log_lvl + + if lvl > max_log_lvl: + max_log_lvl = lvl + if lvl == syslog.LOG_ERR: if err_data: err_data += "|" @@ -123,11 +129,16 @@ def setup(self): @patch("disk_check.syslog.syslog") @patch("disk_check.subprocess.run") def test_readonly(self, mock_proc, mock_log): - global err_data, cmds + global err_data, cmds, max_log_lvl mock_proc.side_effect = mock_subproc_run mock_log.side_effect = report_err_msg + with patch('sys.argv', ["", "-l", "7", "-d", "/tmp"]): + disk_check.main() + assert max_log_lvl == syslog.LOG_DEBUG + max_log_lvl = -1 + for i, tc in test_data.items(): print("-----------Start tc {}---------".format(i)) init_tc(tc) @@ -159,3 +170,7 @@ def test_readonly(self, mock_proc, mock_log): assert err_data == tc["err"] assert cmds == tc.get("cmds", []) print("-----------End tc {}-----------".format(i)) + + + assert max_log_lvl == syslog.LOG_ERR +