Skip to content

test teview - #1

Open
Yingshun wants to merge 1 commit into
masterfrom
test_review
Open

test teview#1
Yingshun wants to merge 1 commit into
masterfrom
test_review

Conversation

@Yingshun

Copy link
Copy Markdown
Owner

No description provided.

Signed-off-by: Yingshun Cui <yicui@redhat.com>
@Yingshun

Copy link
Copy Markdown
Owner Author

/gemini review

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

The pull request adds a new test file for VM lifecycle testing with a VFIO variant driver. The review identifies several issues in the new test file, following Python's general best practices. These include a critical security vulnerability due to the use of eval(), a bug in the test cleanup logic, and several opportunities to improve code maintainability and robustness by removing code duplication, using the test framework's features for temporary files, and centralizing parameter access.

hotplug = params.get("hotplug", "no") == "yes"
err_msg = params.get("err_msg")
managed = params.get("managed")
iommu_dict = eval(params.get("iommu_dict", "{}"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Using eval() on input from parameters is a significant security risk as it can execute arbitrary code. For safely parsing string-formatted Python literals, ast.literal_eval() should be used instead.

Note: This change requires adding import ast at the top of the file.

Suggested change
iommu_dict = eval(params.get("iommu_dict", "{}"))
iommu_dict = ast.literal_eval(params.get("iommu_dict", "{}"))


finally:
orig_vm_xml.sync()
if managed == "no":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The condition managed == "no" is too strict. If the managed parameter is not provided in the test configuration, params.get("managed") will return None, and this condition will be false. However, when the managed attribute is omitted from the device XML, libvirt defaults to not managing the device, which means it should be reattached by the test script.

To correctly handle the default case (and any value other than 'yes'), the condition should be managed != "yes".

Suggested change
if managed == "no":
if managed != "yes":

vm_name = params.get("main_vm", "avocado-vt-vm1")
vm = env.get_vm(vm_name)
rand_id = utils_misc.generate_random_string(3)
save_path = f'/var/tmp/{vm_name}_{rand_id}.save'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The save path is hardcoded to /var/tmp. It's better practice to use the test's working directory, which is managed by the test framework (Avocado). This ensures files are created in a temporary, isolated location and are properly cleaned up. You can use test.workdir for this.

Note: This change requires adding import os at the top of the file to use os.path.join.

Suggested change
save_path = f'/var/tmp/{vm_name}_{rand_id}.save'
save_path = os.path.join(test.workdir, f'{vm_name}_{rand_id}.save')

Comment on lines +36 to +43
if hotplug:
vm.start()
vm.wait_for_serial_login().close()
dev_names = sriov_vfio.attach_dev(vm, params)
else:
dev_names = sriov_vfio.attach_dev(vm, params)
vm.start()
vm.wait_for_serial_login().close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The if/else block for handling hotplug contains duplicated code for attaching devices and starting the VM. This can be refactored to be more concise and less repetitive by removing the redundant calls, which improves maintainability.

Suggested change
if hotplug:
vm.start()
vm.wait_for_serial_login().close()
dev_names = sriov_vfio.attach_dev(vm, params)
else:
dev_names = sriov_vfio.attach_dev(vm, params)
vm.start()
vm.wait_for_serial_login().close()
if hotplug:
vm.start()
vm.wait_for_serial_login().close()
dev_names = sriov_vfio.attach_dev(vm, params)
if not hotplug:
vm.start()
vm.wait_for_serial_login().close()

Comment on lines +72 to +73
session = vm.wait_for_serial_login(
timeout=int(params.get('login_timeout')))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The login_timeout parameter is fetched from params and converted to an integer multiple times in this function (here, and on lines 81-82, 89-90). It would be more efficient and readable to do this once at the beginning of the run function and store the result in a variable.

For example:

# At the start of the run function
login_timeout = int(params.get('login_timeout', '240')) # Use a sensible default

# ... later in the code
session = vm.wait_for_serial_login(timeout=login_timeout)

This avoids redundant calls and conversions, and centralizes the logic for getting the timeout.

@Yingshun

Copy link
Copy Markdown
Owner Author

/gemini summary

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

This pull request introduces a new automated test case focused on validating the lifecycle management of virtual machines that utilize SR-IOV VFIO devices. The test aims to ensure the stability and correct behavior of VMs through various operational states, from initial startup to shutdown and reboot, while managing the attached SR-IOV devices.

Highlights

  • New Test Case Introduction: I've added a new Python test script, sriov_vm_lifecycle_vfio_variant_driver.py, designed to perform comprehensive lifecycle testing of virtual machines configured with SR-IOV VFIO devices.
  • VM Lifecycle Operations: The test covers a full range of VM lifecycle operations, including starting, saving and restoring, suspending and resuming, managed saving, graceful shutdown, hard reset, and internal guest reboots.
  • SR-IOV VFIO Device Management: The script includes logic for attaching SR-IOV VFIO devices, handling both initial attachment and hotplug scenarios. It also ensures proper cleanup by reattaching devices in the finally block based on the 'managed' parameter.
Changelog
  • libvirt/tests/src/sriov/vfio/sriov_vm_lifecycle_vfio_variant_driver.py
    • New file added, implementing a test for VM lifecycle with SR-IOV VFIO variant drivers.
    • Includes steps for VM save/restore, suspend/resume, managedsave, shutdown, reset, and reboot.
    • Manages SR-IOV VFIO device attachment, supporting hotplug and ensuring device reattachment on test completion.
    • Incorporates checks for VM state transitions and handles potential errors during virsh operations.
Activity
  • @Yingshun requested a review from Gemini.
  • @Yingshun requested a summary from Gemini.
  • @gemini-code-assist[bot] provided a critical review comment, recommending replacing eval() with ast.literal_eval() for parsing iommu_dict due to security concerns (line 19).
  • @gemini-code-assist[bot] provided a high-priority review comment, suggesting changing the managed parameter check from managed == "no" to managed != "yes" for more robust handling of default libvirt behavior (line 98).
  • @gemini-code-assist[bot] provided a medium-priority review comment, advising the use of test.workdir for save_path instead of a hardcoded /var/tmp to improve test framework integration and cleanup (line 25).
  • @gemini-code-assist[bot] provided a medium-priority review comment, suggesting refactoring the hotplug logic to reduce code duplication in device attachment and VM start sequences (lines 36-43).
  • @gemini-code-assist[bot] provided a medium-priority review comment, recommending fetching the login_timeout parameter once at the beginning of the function to avoid redundant calls and conversions (lines 73, 81, 89).

@dzhengfy

Copy link
Copy Markdown

/gemini summary

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

This pull request introduces a new automated test case designed to validate the comprehensive lifecycle management of virtual machines configured with SR-IOV VFIO devices. The test ensures the stability and correct behavior of VMs through various operational states, from initial startup to shutdown and reboot, while properly managing the attached SR-IOV devices.

Highlights

  • New Test Case: I've added libvirt/tests/src/sriov/vfio/sriov_vm_lifecycle_vfio_variant_driver.py, a new Python test script for SR-IOV VFIO VM lifecycle validation.
  • Comprehensive VM Lifecycle Testing: The test covers essential VM operations including save/restore, suspend/resume, managedsave, graceful shutdown, hard reset, and internal guest reboots.
  • SR-IOV VFIO Device Management: The script includes logic for attaching SR-IOV VFIO devices, supporting both initial attachment and hotplug scenarios, and ensures proper device reattachment during cleanup.
Changelog
  • libvirt/tests/src/sriov/vfio/sriov_vm_lifecycle_vfio_variant_driver.py
    • This new file implements a test for the lifecycle of VMs utilizing SR-IOV VFIO variant drivers.
    • It includes steps to perform VM save/restore, suspend/resume, managedsave, shutdown, reset, and reboot operations.
    • The test manages SR-IOV VFIO device attachment, including support for hotplug, and ensures devices are reattached in the finally block for proper cleanup.
    • It incorporates checks for VM state transitions and handles potential errors during virsh command executions.
Activity
  • @Yingshun initiated a review request from Gemini.
  • @Yingshun requested a summary from Gemini.
  • @gemini-code-assist[bot] provided a critical review comment, recommending the use of ast.literal_eval() instead of eval() for parsing iommu_dict due to security concerns (line 19).
  • @gemini-code-assist[bot] provided a high-priority review comment, suggesting a more robust check for the managed parameter (managed != "yes") to correctly handle default libvirt behavior (line 98).
  • @gemini-code-assist[bot] provided a medium-priority review comment, advising the use of test.workdir for the save_path to integrate better with the test framework and ensure proper cleanup (line 25).
  • @gemini-code-assist[bot] provided a medium-priority review comment, suggesting refactoring the hotplug logic to reduce code duplication in device attachment and VM start sequences (lines 36-43).
  • @gemini-code-assist[bot] provided a medium-priority review comment, recommending fetching the login_timeout parameter once at the beginning of the function to avoid redundant calls and conversions (lines 73, 81, 89).
  • @dzhengfy requested a summary from Gemini.

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.

2 participants