Skip to content

Qemu_utils Preperation/Tests Module#4325

Open
YongxueHong wants to merge 2 commits intoavocado-framework:masterfrom
YongxueHong:LNXVASTACA-301
Open

Qemu_utils Preperation/Tests Module#4325
YongxueHong wants to merge 2 commits intoavocado-framework:masterfrom
YongxueHong:LNXVASTACA-301

Conversation

@YongxueHong
Copy link
Copy Markdown

Improvement of the qemu_utils module and add the Functional/Unit tests for the migration of aautils.

ID: LNXVASTACA-301

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @YongxueHong, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness and maintainability of the qemu_utils module by introducing comprehensive test coverage. Both functional and unit tests have been added to validate the module's core functionalities, ensuring reliable interaction with QEMU binaries. This work is crucial for the ongoing migration of aautils, providing a solid testing foundation for the utility functions.

Highlights

  • New Functional Tests: Added a new functional test module for qemu_utils to verify has_option and get_support_machine_type using actual QEMU binaries available on the system.
  • New Unit Tests: Introduced a new unit test module for qemu_utils, employing unittest.mock to thoroughly test has_option and get_support_machine_type in isolation, covering various scenarios including edge cases and invalid paths.
  • Code Refactoring in qemu_utils.py: Updated the module-level docstring for qemu_utils.py to provide a clearer overview and improved the docstrings for has_option and get_support_machine_type. Additionally, string formatting within the module was modernized to use f-strings.
  • Pylint Configuration Update: A minor formatting adjustment was made to the .pylintrc_utils file, specifically re-wrapping a long line in the ignore list.
Changelog
  • .pylintrc_utils
    • Updated Pylint ignore list formatting.
  • selftests/functional/test_vt_utils/test_qemu_utils.py
    • Added functional tests for QEMU utility functions, including has_option and get_support_machine_type.
  • selftests/unit/test_vt_utils/test_qemu_utils.py
    • Added unit tests for QEMU utility functions, utilizing mocking for has_option and get_support_machine_type.
  • virttest/vt_utils/qemu_utils.py
    • Refactored module and function docstrings for clarity.
    • Updated string formatting to use f-strings for improved readability and modern Python practices.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the qemu_utils module by introducing comprehensive unit and functional tests, enhancing its robustness and reliability, and improving maintainability through updated docstrings and modern f-string formatting. However, it maintains and introduces security risks by using string interpolation for shell commands. Specifically, the has_option and get_support_machine_type functions are vulnerable to command injection if provided paths are maliciously crafted or contain shell metacharacters. These functions should be refactored to use list-based arguments for process execution to ensure safe handling of command-line parameters.

Comment on lines 41 to 43
hlp = process.run(
"%s -help" % qemu_path, shell=True, ignore_status=True, verbose=False
f"{qemu_path} -help", shell=True, ignore_status=True, verbose=False
).stdout_text
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-high high

The qemu_path parameter is interpolated directly into a command string executed with shell=True. This allows for arbitrary command injection if the path contains shell metacharacters or is maliciously crafted. For example, a path like /usr/bin/qemu; id would execute the id command. To remediate this, pass the command and its arguments as a list and set shell=False.

Suggested change
hlp = process.run(
"%s -help" % qemu_path, shell=True, ignore_status=True, verbose=False
f"{qemu_path} -help", shell=True, ignore_status=True, verbose=False
).stdout_text
hlp = process.run(
[qemu_path, "-help"], shell=False, ignore_status=True, verbose=False
).stdout_text

:rtype: Tuple[List, List, List]
"""
o = process.run("%s -M ?" % qemu_binary).stdout_text.splitlines()
o = process.run(f"{qemu_binary} -M ?").stdout_text.splitlines()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-high high

The qemu_binary parameter is interpolated into a command string. This is vulnerable to command injection if process.run uses a shell, or argument injection if the path contains spaces. It is safer to pass the command as a list of arguments and avoid string interpolation for command construction.

Suggested change
o = process.run(f"{qemu_binary} -M ?").stdout_text.splitlines()
o = process.run([qemu_binary, "-M", "?"]).stdout_text.splitlines()

Comment thread virttest/vt_utils/qemu_utils.py Outdated
@YongxueHong YongxueHong force-pushed the LNXVASTACA-301 branch 4 times, most recently from b844690 to ba462d8 Compare February 27, 2026 03:11
Copy link
Copy Markdown
Contributor

@richtja richtja left a comment

Choose a reason for hiding this comment

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

Hi @YongxueHong, thanks a lot for preparing qemu_utils for migration. Overall it LGTM I have just one question related to pylint check.

Comment thread .pylintrc_utils Outdated
useless-suppression,
use-symbolic-message-instead,
too-few-public-methods,
E0401,
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.

can you please tell me, because of what you got the import error? IIUIC this is valid check and shouldn't get it normally.

Copy link
Copy Markdown
Author

@YongxueHong YongxueHong Mar 27, 2026

Choose a reason for hiding this comment

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

Hi @richtja
Yeah, I thought it should not be here, but I met a CI failure about "Unable to import process from avocado.utils" for from avocado.utils import process before, I am not sure what happened with the environment of CI, so here I did this workaround to disable this pylint checker.
I could remove it right now and then have a look if it can be passed.
Thanks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi @richtja
I pushed the PR without the E0401 pylint checker, but it failed in CI checkers. Thanks.

pylint...................................................................Failed
- hook id: pylint
- exit code: 1

Found configuration file: static-checks/../avocado-static-checks.conf
** Running pylint on directory 'virttest' with config from '.pylintrc_utils'...
************* Module avocado-vt.virttest.vt_utils.qemu_utils
virttest/vt_utils/qemu_utils.py:27:0: E0401: Unable to import 'avocado.utils' (import-error)

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 see, let me look into that.

Yongxue Hong added 2 commits March 27, 2026 10:25
- Fix docstring formatting by removing empty first lines
- Add missing type documentation for qemu_path parameter
- Convert % string formatting to f-strings for better readability
- Improve module docstring to provide clearer functionality overview
- Remove from .pylintrc_utils ignore list to enable pylint checking

Signed-off-by: Yongxue Hong <yhong@redhat.com>
This commit introduces both unit and functional tests for the qemu_utils
module, providing thorough coverage of QEMU option detection and machine
type querying functionality.

Signed-off-by: Yongxue Hong <yhong@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants