Skip to content

add new clear_logfile method:#4095

Merged
Yingshun merged 1 commit intoavocado-framework:masterfrom
hholoubk:XXX-303225-add-clear-log-func
May 14, 2025
Merged

add new clear_logfile method:#4095
Yingshun merged 1 commit intoavocado-framework:masterfrom
hholoubk:XXX-303225-add-clear-log-func

Conversation

@hholoubk
Copy link
Copy Markdown
Contributor

  • 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)

@dzhengfy
Copy link
Copy Markdown
Contributor

@hholoubk please make sure the CI static check is passed.

Comment thread virttest/utils_test/libvirt.py Outdated
else:
cmdRes = remote_old.run_remote_cmd(cmd, cmd_parms, runner_on_target)

if isinstance(str_in_log, str):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think we need to add this support. This function is an utility and docstring already has clear description that str_in_log variable is a boolean. So the user needs to give correct type for this parameter by themselves.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(did you mean ...that we DON"T need this support ... .. according rest of the comment it looks like this )
issue is .. that if it is named "str_in_log" .. than it is misleading name and the we should update the docstring ...
this way the utility will be more robust ... (as I spent one hour debugging why it doesn't work with "False" ...
and if the content of str_in_log is passed from parameter .. it was interpretted as string ...

what do you think?
I can do some performance testing how this will impact the test runs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I missed "not".
As you can see

def check_logfile(
    search_str,
    log_file,
    str_in_log=True,  ===> this indicates the boolean, I think. 

And there is also indicator in below codes:
if str_in_log == bool(int(cmdRes.exit_status)):

But anyway, an easy way to make better readability is we add type for parameter in docstring.
So could you help update the docstring?

For example:

:param str_in_log:  bool, True if the file should include the given string,
                                            otherwise, False

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread virttest/utils_test/libvirt.py Outdated
return backup_file


def clear_logfile(log_file_name,
Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we make the function simple that it only does one thing to clear a specified log file. We do not need to embed the backup operation. Leave the backup operation to the end users if they need.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ... make sense ... the function is now in utils_logfile.py ... and doesn't contain backup of file (I've fully removed the function, as it is not necessary for this case ... and ti will need agreement with team to properly design it as part of the utils_logfile.py

Comment thread virttest/utils_test/libvirt.py Outdated
:param backup_old_log: if True, the actual content of log_file will be backuped; default False

Returns:
str or None: The name of the backup file if created, otherwise None.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just return True for clearing the log file successfully or False for failing to make it empty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread virttest/utils_test/libvirt.py Outdated
return obj_conf


def backup_logfile(log_file, backup_file_name, backup_dir="/tmp"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest backup_dir=None

Copy link
Copy Markdown
Contributor Author

@hholoubk hholoubk Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed whole function - not needed for the test

Comment thread virttest/utils_test/libvirt.py Outdated
Returns:
str: The name of created backup file
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd better provide an opportunity for the end users to define the final backup_file_name without changes (like + timestamp). So I would suggest below

if not backup_file_name:
    timestamp = time.strftime('%Y%m%d_%H%M%S')
    backup_file_name = f"{backup_file_name}_{timestamp}.log"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed whole function - not needed for the test

Comment thread virttest/utils_test/libvirt.py Outdated

timestamp = time.strftime('%Y%m%d_%H%M%S')
backup_file_name = f"{backup_file_name}_{timestamp}.log"
backup_file = os.path.join(backup_dir, backup_file_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backup_dir, its default is /tmp. Then the end user needs to remove the backup file by themselves.
I would suggest we use data_dir.get_tmp_dir() if the end user does not provide any value because the avocado-vt will help clear the files in data_dir.get_tmp_dir() at the end of the test for us.

So is it better if we use below:


if not backup_dir:
    backup_file = os.path.join(data_dir.get_tmp_dir(), backup_file_name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed whole function - not needed for the test

Comment thread virttest/utils_test/libvirt.py Outdated
return obj_conf


def backup_logfile(log_file, backup_file_name, backup_dir="/tmp"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do suggest not add any new function to libvirt.py any more because it is too long and not categorized.
Could you please move it to utils_logfile.py?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. moved (just the clear_log_file ... not the backup, as it is not necessary ... and it would need properly design more things to reflect file structure

@hholoubk hholoubk force-pushed the XXX-303225-add-clear-log-func branch from 7c565c9 to 998f80a Compare April 14, 2025 07:08
@hholoubk
Copy link
Copy Markdown
Contributor Author

@dzhengfy statick check should be ok

@hholoubk hholoubk force-pushed the XXX-303225-add-clear-log-func branch 3 times, most recently from 75fb96f to 71c0a88 Compare April 16, 2025 13:56
Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Yingshun Yingshun closed this Apr 25, 2025
@Yingshun Yingshun reopened this Apr 25, 2025
Copy link
Copy Markdown
Contributor

@Yingshun Yingshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the Static checks failures, thx!

Comment thread virttest/utils_logfile.py Outdated

def clear_log_file(log_file_name, log_dir=None):
"""
Clears the (e.g. VM) log file before a test run. closes the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's complete the sentence, thx!

@dzhengfy dzhengfy requested a review from Yingshun May 9, 2025 02:53
@Yingshun Yingshun closed this May 9, 2025
@Yingshun Yingshun reopened this May 9, 2025
@Yingshun
Copy link
Copy Markdown
Contributor

Yingshun commented May 9, 2025

Still got errors in checks, plz fix them, thanks!

@dzhengfy dzhengfy closed this May 9, 2025
@dzhengfy dzhengfy reopened this May 9, 2025
@hholoubk hholoubk force-pushed the XXX-303225-add-clear-log-func branch from 4a20953 to 5cf3ca3 Compare May 9, 2025 10:03
@hholoubk
Copy link
Copy Markdown
Contributor Author

hholoubk commented May 9, 2025

Still got errors in checks, plz fix them, thanks!

The previous errors were caused by some check limit ... it looks good now

@Yingshun
Copy link
Copy Markdown
Contributor

@hholoubk Please also fix the problem in Static checks.
image

@hholoubk hholoubk force-pushed the XXX-303225-add-clear-log-func branch 2 times, most recently from fea4d4f to 95760e2 Compare May 14, 2025 11:31
@hholoubk
Copy link
Copy Markdown
Contributor Author

I hope I have finaly realized .. what should be in commit message.

- 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)

Signed-off-by: hholoubk <hholoubk@redhat.com>
@hholoubk hholoubk force-pushed the XXX-303225-add-clear-log-func branch from 95760e2 to df0ba53 Compare May 14, 2025 11:42
@Yingshun Yingshun merged commit 58b79fa into avocado-framework:master May 14, 2025
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants