Skip to content
Merged
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
5 changes: 4 additions & 1 deletion libvirt/tests/src/migration/migration_xml_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ def prepare_vm(vm, vm_name, vmxml, params):
if vm.is_alive():
vm.destroy()
dev_type = params.get("remove_all")
dev_dir = eval(params.get("add_device"))
if dev_type == "interface":
current_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
dev_dir['mac_address'] = current_mac
Comment on lines +29 to +32
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Good fix for MAC preservation, but verify None handling.

The change correctly addresses the connectivity issue by preserving the MAC address. Moving dev_dir creation before device removal and conditionally injecting the current MAC for interface devices ensures the framework's IP cache remains valid.

However, verify that get_first_mac_by_name cannot return None in scenarios where the VM has no interfaces or the call fails. If None is possible, downstream device creation might fail or behave unexpectedly.

Run the following script to check how get_first_mac_by_name handles edge cases:


🏁 Script executed:

#!/bin/bash
# Verify get_first_mac_by_name implementation and None handling

# Search for the implementation of get_first_mac_by_name
ast-grep --pattern $'def get_first_mac_by_name($$$) {
  $$$
}'

# Search for existing None checks on get_first_mac_by_name results
rg -nP -C3 'get_first_mac_by_name.*\)' | rg -A5 -B5 'if.*None|is None'

Length of output: 886



🏁 Script executed:

#!/bin/bash
# Find the implementation of get_first_mac_by_name
rg -n "def get_first_mac_by_name" --type py

# Check how dev_dir is used after line 32 in the reviewed file
rg -n -A 15 "def prepare_vm" libvirt/tests/src/migration/migration_xml_update.py

# Also search for other None checks on mac assignments to establish pattern
rg -n -B2 -A2 "mac.*is None|is None.*mac" --type py | head -30

Length of output: 1382


🏁 Script executed:

#!/bin/bash
# Get info about the VMXML class and get_first_mac_by_name method
# Search for documentation or type hints
rg -n "get_first_mac_by_name" --type py -B2 -A5 | grep -A10 "def get_first_mac_by_name"

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Find the implementation of get_first_mac_by_name and check its return type
rg -n "def get_first_mac_by_name" --type py -A 20

# Check the complete prepare_vm function to see how dev_dir is used after line 32
rg -n "def prepare_vm" libvirt/tests/src/migration/migration_xml_update.py -A 50

# Check if there are any other places where mac_address is checked for None
rg -n "mac_address.*None|None.*mac_address" --type py -B2 -A2

Length of output: 2000


🏁 Script executed:

#!/bin/bash
# Search for create_vm_device_by_type to see if it validates mac_address parameter
rg -n "def create_vm_device_by_type" --type py -A 10

# Check if there are test cases that show what happens when mac_address is None
rg -n "mac_address.*=.*None\|mac_address.*=\s*None" --type py -B 2 -A 2

Length of output: 0


Add None validation before assigning MAC address.

The code fails to validate that get_first_mac_by_name() returned a MAC address. Elsewhere in the codebase (e.g., libguestfs/tests/guestfs_operated_disk.py:50 and libvirt/tests/src/passthrough/pci/vfio.py:135), the method's return value is explicitly checked for None before use and the test fails if the MAC cannot be retrieved.

At line 31-32 of libvirt/tests/src/migration/migration_xml_update.py, add a None check:

current_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
if current_mac is None:
    test.fail("Failed to get MAC address for VM %s" % vm_name)
dev_dir['mac_address'] = current_mac

Without this check, dev_dir['mac_address'] will be None, causing device creation at line 35 to fail or behave unexpectedly.

🧰 Tools
🪛 Ruff (0.14.5)

29-29: Use of possibly insecure function; consider using ast.literal_eval

(S307)

🤖 Prompt for AI Agents
In libvirt/tests/src/migration/migration_xml_update.py around lines 29 to 32,
the code assigns current_mac directly from
vm_xml.VMXML.get_first_mac_by_name(vm_name) without validating it; add a None
check immediately after retrieving current_mac and call test.fail with a clear
message if it is None, otherwise proceed to set dev_dir['mac_address'] =
current_mac so you avoid assigning None and causing downstream device-creation
failures.

vmxml.remove_all_device_by_type(dev_type)
vmxml.sync()
dev_dir = eval(params.get("add_device"))
dev = libvirt_vmxml.create_vm_device_by_type(dev_type, dev_dir)
virsh.attach_device(vm_name, dev.xml, flagstr="--config", debug="True")

Expand Down