Skip to content

Commit 7c565c9

Browse files
committed
add new clear_logfile method:
- this will clear defined log_file - so the test will consider only the messages added in the test run. - previous log can be backuped to the tmp dir if requrired (the restore not yet prepared) add new backup_logfile method: used in clear_logfile, but it can be used just as is + minor refactor to process not only bollean str_in_log variable (as it comes from test in my case as str)
1 parent f999069 commit 7c565c9

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

virttest/utils_test/libvirt.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4101,6 +4101,63 @@ def customize_libvirt_config(
41014101
return obj_conf
41024102

41034103

4104+
def backup_logfile(log_file, backup_file_name, backup_dir="/tmp"):
4105+
"""
4106+
Copies log_file into backup position with required backup_file_name+timestamp
4107+
using /tmp dir as destination
4108+
4109+
:param log_file: path to the log_file
4110+
:param backup_file_name: name of the backup_file
4111+
:param backup_dir: dir where the log will be placed; default /tmp
4112+
4113+
Returns:
4114+
str: The name of created backup file
4115+
"""
4116+
4117+
timestamp = time.strftime('%Y%m%d_%H%M%S')
4118+
backup_file_name = f"{backup_file_name}_{timestamp}.log"
4119+
backup_file = os.path.join(backup_dir, backup_file_name)
4120+
LOG.debug(f"Going to backup file {log_file} into {backup_file}")
4121+
shutil.copy2(log_file, backup_file) # Create a backup with a timestamp
4122+
return backup_file
4123+
4124+
4125+
def clear_logfile(log_file_name,
4126+
backup_file_name="",
4127+
log_dir='/var/log/libvirt/qemu/',
4128+
backup_dir='/tmp',
4129+
backup_old_log=False):
4130+
"""
4131+
Clears the (e.g. VM) log file before a test run.
4132+
4133+
If the log file exists, it will be backed up to /tmp with a timestamp (if backup_old_log is True) and then cleared.
4134+
4135+
:param log_file_name: name of file to be cleared
4136+
:param backup_file_name: name of file to be used if different than log_file_name; default ""
4137+
:param log_dir: directory with logfile; default '/var/log/libvirt/qemu/'
4138+
:param backup_old_log: if True, the actual content of log_file will be backuped; default False
4139+
4140+
Returns:
4141+
str or None: The name of the backup file if created, otherwise None.
4142+
"""
4143+
# in case user didn't specified the backup_file_name, log_file_name will be used
4144+
if not backup_file_name:
4145+
backup_file_name = log_file_name
4146+
log_file = os.path.join(log_dir, log_file_name)
4147+
4148+
if not os.path.exists(log_file):
4149+
return None # Log file does not exist, do nothing
4150+
4151+
if backup_old_log:
4152+
backup_file = backup_logfile(log_file, backup_file_name, backup_dir)
4153+
else:
4154+
backup_file = None
4155+
4156+
# Clear the log file
4157+
open(log_file, 'w').close()
4158+
return backup_file
4159+
4160+
41044161
def check_logfile(
41054162
search_str,
41064163
log_file,
@@ -4132,6 +4189,10 @@ def check_logfile(
41324189
cmdRes = process.run(cmd, shell=True, ignore_status=True)
41334190
else:
41344191
cmdRes = remote_old.run_remote_cmd(cmd, cmd_parms, runner_on_target)
4192+
4193+
if isinstance(str_in_log, str):
4194+
str_in_log = str_in_log.lower() in ['true', 'yes', '1']
4195+
41354196
if str_in_log == bool(int(cmdRes.exit_status)):
41364197
error_msg = "The string '{}' {} included in {}".format(
41374198
search_str, "is not" if str_in_log else "is", log_file

0 commit comments

Comments
 (0)