Skip to content

Commit

Permalink
disk_check updates: (sonic-net#1736)
Browse files Browse the repository at this point in the history
1) Set default loglevel to SYSLOG_ERR
2) Make log level configurable via args
  • Loading branch information
renukamanavalan authored and qiluo-msft committed Aug 3, 2021
1 parent d68ac1c commit 85f14e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
21 changes: 15 additions & 6 deletions scripts/disk_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion tests/disk_check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
}

err_data = ""
max_log_lvl = -1
cmds = []
current_tc = None

Expand All @@ -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 += "|"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 85f14e1

Please sign in to comment.