Skip to content
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
33 changes: 33 additions & 0 deletions virttest/utils_logfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,36 @@ def hook(self):
close_log_file(log_file)

return hook


def clear_log_file(log_file_name, log_dir=None):
"""
Clears the (e.g. VM) log file before a test run.
Closes the file if in open files.

:param log_file_name: name of file to be cleared.
:param log_dir: directory with logfile; default is the _log_file_dir
:raise LogLockError: If the lock is unavailable.
"""

global _open_log_files, _log_file_dir, _log_lock

if log_file_name in _open_log_files:
close_log_file(log_file_name) # close_log_file will acquire lock for closure

if not _acquire_lock(_log_lock):
raise LogLockError(
"Could not acquire exclusive lock to access" " _open_log_files"
)

try:
if log_dir:
log_file = os.path.join(log_dir, log_file_name)
else:
log_file = os.path.join(_log_file_dir, log_file_name)

if os.path.exists(log_file):
# Clear the log file
open(log_file, "w").close()
finally:
_log_lock.release()
3 changes: 2 additions & 1 deletion virttest/utils_test/libvirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4115,7 +4115,7 @@ def check_logfile(

:param search_str: the string to be searched
:param log_file: the given file
:param str_in_log: True if the file should include the given string,
:param str_in_log: bool, True if the file should include the given string,
otherwise, False
:param cmd_parms: The parms for remote executing
:param runner_on_target: Remote runner
Expand All @@ -4132,6 +4132,7 @@ def check_logfile(
cmdRes = process.run(cmd, shell=True, ignore_status=True)
else:
cmdRes = remote_old.run_remote_cmd(cmd, cmd_parms, runner_on_target)

if str_in_log == bool(int(cmdRes.exit_status)):
error_msg = "The string '{}' {} included in {}".format(
search_str, "is not" if str_in_log else "is", log_file
Expand Down