Skip to content

Conversation

@yiqianwei
Copy link

@yiqianwei yiqianwei commented Oct 22, 2025

…l and inactive xml have different pci address

xxx-299225: Detach-interface with --persistent when live xml and inactive xml have difference pci address for interface

Summary by CodeRabbit

  • Tests
    • Added comprehensive new test suite for libvirt virtual network hotplug interface detach operations. Validates persistent interface behavior including PCI address consistency checks across active and inactive VM configurations, proper attachment and detachment of network interfaces, interface verification in configuration files, and robust cleanup procedures to ensure original VM state restoration.

…l and inactive xml have different pci address

xxx-299225: Detach-interface with --persistent when live xml and inactive xml
have difference pci address for interface

Signed-off-by: Yiqian Wei <[email protected]>
@coderabbitai
Copy link

coderabbitai bot commented Oct 22, 2025

Walkthrough

Adds a new test configuration and implementation script for testing libvirt virtual network hotplug detach interface behavior with persistent interfaces. The test verifies that detached persistent interfaces are properly removed from both active and inactive VM configurations.

Changes

Cohort / File(s) Summary
Test Configuration
libvirt/tests/cfg/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.cfg
Defines a new test scenario for detach_interface_persistent_pci_diff with VM reference, interface type (network), source (default), and model (virtio) parameters, with initial interface count set to 3.
Test Implementation
libvirt/tests/src/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.py
Implements test script with helper functions for interface attachment/detachment options, XML interface verification, and PCI address retrieval; main test flow attaches persistent interface, verifies PCI address changes between active/inactive states, detaches interface, and validates removal.

Sequence Diagram

sequenceDiagram
    participant Test
    participant VM
    participant libvirt
    participant XML

    Test->>VM: Start VM
    Test->>libvirt: Attach initial interfaces (default 3)
    libvirt->>VM: Configure interfaces
    Test->>libvirt: Attach persistent interface with MAC
    libvirt->>VM: Configure persistent interface
    Test->>XML: Get PCI address from active XML
    Test->>XML: Get PCI address from inactive XML
    rect rgb(200, 220, 255)
        Note over Test: Verify PCI addresses differ
    end
    Test->>libvirt: Detach persistent interface (--persistent)
    libvirt->>VM: Remove persistent interface
    Test->>XML: Verify interface removed from active XML
    Test->>XML: Verify interface removed from inactive XML
    rect rgb(220, 255, 220)
        Note over Test: Cleanup and restore VM state
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

The additions include a straightforward configuration file (trivial) and a new test script with helper functions for interface operations, XML parsing, and verification logic. The script demonstrates moderate complexity through multi-step test orchestration and state validation across active and inactive configurations.

Suggested reviewers

  • cliping
  • yanqzhan

Poem

🐰 Hoppity hops, a test takes flight,
Interfaces attach with persistent might,
PCI addresses dance and sway,
Then hop away when we detach today!
In active and inactive lands, they part,
A hotplug test, a fuzzy work of art! 🌐✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Network: Add case for detach-interface with --persistent when live xm…" is fully related to the main change in the changeset. The changeset adds a new test configuration file and test script that together implement a test case for verifying the behavior of detach-interface with the --persistent flag, specifically when live XML and inactive XML have different PCI addresses for an interface. The title clearly identifies the purpose (adding a test case), the feature being tested (detach-interface with --persistent), and the specific condition being tested (the state difference between live and inactive XML). While the title appears truncated with "xm…", the visible portion accurately and specifically conveys the main intent of the PR in a concise manner.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
libvirt/tests/src/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.py (4)

208-215: Reduce flakiness: replace fixed sleep with a short poll for XML readiness.

Polling avoids race with libvirt XML updates.

Apply this diff:

-        # Wait a moment for XML to stabilize
-        time.sleep(2)
-
-        active_pci = get_interface_pci_address(
-            vm_name, persistent_mac, True)
-        inactive_pci = get_interface_pci_address(
-            vm_name, persistent_mac, False)
+        # Wait for both active/inactive XML to expose addresses
+        active_pci = inactive_pci = None
+        for _ in range(10):  # ~10s max
+            active_pci = get_interface_pci_address(vm_name, persistent_mac, True)
+            inactive_pci = get_interface_pci_address(vm_name, persistent_mac, False)
+            if active_pci and inactive_pci:
+                break
+            time.sleep(1)

261-271: Reduce flakiness: poll for interface removal instead of fixed sleep.

Wait until the interface disappears from both XMLs, then assert.

Apply this diff:

-        # Wait a moment for XML to update
-        time.sleep(2)
-
-        if check_interface_in_xml(vm_name, persistent_mac, True):
-            test.fail(
-                "Interface %s still exists in active XML after detach" % persistent_mac)
-
-        if check_interface_in_xml(vm_name, persistent_mac, False):
-            test.fail(
-                "Interface %s still exists in inactive XML after detach" % persistent_mac)
+        # Wait for XMLs to reflect the detach
+        active_present = inactive_present = True
+        for _ in range(10):  # ~10s max
+            active_present = check_interface_in_xml(vm_name, persistent_mac, True)
+            inactive_present = check_interface_in_xml(vm_name, persistent_mac, False)
+            if not active_present and not inactive_present:
+                break
+            time.sleep(1)
+
+        if active_present:
+            test.fail("Interface %s still exists in active XML after detach" % persistent_mac)
+        if inactive_present:
+            test.fail("Interface %s still exists in inactive XML after detach" % persistent_mac)

285-293: Tighten cleanup error handling; avoid broad except Exception (Ruff BLE001).

With ignore_status=True, virsh returns a result; check exit_status and log details.

Apply this diff:

-        for mac in attached_macs:
-            try:
-                cleanup_options = set_interface_options(
-                    iface_type, None, mac, "", "detach", None)
-                virsh.detach_interface(
-                    vm_ref, cleanup_options, **virsh_dargs)
-                logging.info("Cleaned up interface with MAC %s", mac)
-            except Exception as e:
-                logging.warning(
-                    "Failed to cleanup interface with MAC %s: %s", mac, e)
+        for mac in attached_macs:
+            cleanup_options = set_interface_options(
+                iface_type, None, mac, "", "detach", None)
+            res = virsh.detach_interface(vm_ref, cleanup_options, **virsh_dargs)
+            if res.exit_status != 0:
+                logging.warning("Cleanup detach failed for %s (rc=%s): %s",
+                                mac, res.exit_status, res.stderr)
+            else:
+                logging.info("Cleaned up interface with MAC %s", mac)

11-14: Logger naming: avoid shadowing the logging module.

Rename the logger variable (e.g., LOGGER = log.getLogger(...)) and use LOGGER.info/debug(...) throughout for clarity.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9aa7dea and a49ae02.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.cfg (1 hunks)
  • libvirt/tests/src/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.1)
libvirt/tests/src/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.py

291-291: Do not catch blind exception: Exception

(BLE001)

🔇 Additional comments (1)
libvirt/tests/cfg/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.cfg (1)

1-10: Config looks consistent with the new test.

Parameters match the script defaults and flow; no blockers from config side.

Comment on lines +227 to +230
if active_pci == inactive_pci:
test.fail(
"PCI addresses are the same in active and inactive XML: %s", active_pci)
else:
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Fix: incorrect use of test.fail with multiple args (TypeError).

test.fail() accepts a single string. Current code passes a format string plus a second arg, causing a runtime error.

Apply this diff:

-        if active_pci == inactive_pci:
-            test.fail(
-                "PCI addresses are the same in active and inactive XML: %s", active_pci)
+        if active_pci == inactive_pci:
+            test.fail(
+                f"PCI addresses are the same in active and inactive XML: {active_pci}")
🤖 Prompt for AI Agents
In
libvirt/tests/src/virtual_network/hotplug/attach_detach_interface/detach_interface_persistent_pci_diff.py
around lines 227-230, test.fail is called with multiple arguments which raises a
TypeError because test.fail accepts a single string; change the call to pass a
single formatted string (for example use an f-string, .format(), or %
interpolation) so the PCI value is embedded into the message, e.g.
test.fail(f"PCI addresses are the same in active and inactive XML:
{active_pci}").

@yiqianwei
Copy link
Author

Test results:
(1/1) type_specific.io-github-autotest-libvirt.virtual_network.hotplug.detach_interface_persistent_pci_diff: PASS (36.94 s)
RESULTS : PASS 1 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
JOB HTML : /var/log/avocado/job-results/job-2025-10-21T23.38-bd3a190/results.html
JOB TIME : 38.66 s

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.

1 participant