Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disk_check: Minor log related updates #1736

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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