Skip to content

libvirt_mem:Add test for memory attach-detach with different option#6878

Open
sneh-3 wants to merge 1 commit into
autotest:masterfrom
sneh-3:attach_dev_with_diff_option
Open

libvirt_mem:Add test for memory attach-detach with different option#6878
sneh-3 wants to merge 1 commit into
autotest:masterfrom
sneh-3:attach_dev_with_diff_option

Conversation

@sneh-3

@sneh-3 sneh-3 commented Jun 3, 2026

Copy link
Copy Markdown

supported options are --config, --live, --current, --persistent, --live --config

Summary by CodeRabbit

  • New Features

    • Added comprehensive memory attach/detach test scenarios covering multiple operation modes.
    • Introduced enhanced verification mechanisms for memory configuration changes.
  • Chores

    • Updated memory test configurations to reflect new baseline scenarios.
    • Expanded test coverage for memory attach/detach operations across different configuration modes.

options are as --config, --live, --current, --persistent,
--live --config

Signed-off-by: Sneh Shikha Yadav <syadav@linux.ibm.com>
@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

This PR enhances the libvirt memory attach/detach test with comprehensive verification capabilities. It introduces four new utility functions to inspect guest memory and domain XML, then integrates them into the main test flow. The test configuration is updated with new baseline memory parameters, a new memory_attach_detach_comprehensive variant with multiple option-flag sub-variants, and aligned negative-test and error-scenario sizing. The main test logic now captures baseline state, verifies attach operations against expected XML and memory deltas, and significantly refactors the detach phase to include VM restart for config persistence checks, guest-side memory optimization, and post-detach state verification.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: adding a test for memory attach-detach with different options, which aligns with the PR objectives and file changes.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@libvirt/tests/cfg/libvirt_mem.cfg`:
- Around line 190-193: The test variant current_shutoff sets start_vm = "no" but
the test harness in libvirt/tests/src/libvirt_mem.py still forces a guest start
whenever attach_device == "yes", so the case exercises the running (--current)
path instead of the shutoff path. Update the start logic in libvirt_mem.py (the
code that checks attach_device and starts the guest) to respect the start_vm
flag for this variant: when start_vm == "no" (or when the variant name is
current_shutoff) do not start the domain before invoking the attach/detach flow
so --current runs against a shutoff domain; alternatively add an explicit branch
for current_shutoff to skip the guest startup. Ensure the check references the
existing attach_device and start_vm variables (or the variant identifier) so
behavior matches the libvirt_mem.cfg setting.

In `@libvirt/tests/src/libvirt_mem.py`:
- Line 665: The branch uses detach_option without initializing it and reuses
attach_option in the detach path; initialize detach_option from params the same
way attach_option is loaded (e.g., detach_option = params.get("detach_option")
or equivalent), update the conditional to check if either attach_option or
detach_option is set, and replace uses of attach_option in the detach flow and
verification with detach_option so virsh.detach_device() and subsequent checks
exercise the configured detach-option variants.
- Around line 717-724: The verify_immediate_changes call is getting dicts from
get_domain_memory_xml (baseline_xml_active, after_attach_xml_active,
baseline_xml_inactive, after_attach_xml_inactive) but the helper expects scalar
counts and guest-memory values; update the call in the attach verification to
pass .get("memory_device_count") for each XML dict and the actual guest-memory
before/after attach (or alternatively change verify_immediate_changes to accept
the three models: active_xml, inactive_xml, guest_memory), and also stop
silently swallowing failures in the except block—replace the bare
logging.warning("Attach verification failed: %s", e) with either
logging.exception(...) followed by re-raising the exception or let the exception
propagate so the test fails on verification errors.
- Around line 892-897: The detach-only path can hit an UnboundLocalError because
dev_xml may never be defined before the detach block; ensure dev_xml is
initialized to None before the attach/ detach branches (or always create it when
detach_device is true). Specifically, add an initialization like dev_xml = None
at the start of the function handling attach/detach, or guard the detach branch
to call utils_hotplug.create_mem_xml (using tg_size, pg_size, mem_addr,
tg_sizeunit, pg_unit, tg_node, node_mask, mem_model, mem_discard)
unconditionally when detach_device is enabled so the code that checks "if
dev_xml is None:" no longer references an unbound variable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c244e0fa-e418-4c89-8195-817019e42314

📥 Commits

Reviewing files that changed from the base of the PR and between c96ab65 and 86f03c0.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/libvirt_mem.cfg
  • libvirt/tests/src/libvirt_mem.py

Comment on lines +190 to +193
- current_shutoff:
attach_option = "--current"
detach_option = "--current"
start_vm = "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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

current_shutoff does not currently test a shutoff-domain --current flow.

This variant sets start_vm = "no", but libvirt/tests/src/libvirt_mem.py still starts the guest up front whenever attach_device = "yes". As written, this case will execute --current against a running VM and duplicate current_running semantics instead of covering the shutoff path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libvirt/tests/cfg/libvirt_mem.cfg` around lines 190 - 193, The test variant
current_shutoff sets start_vm = "no" but the test harness in
libvirt/tests/src/libvirt_mem.py still forces a guest start whenever
attach_device == "yes", so the case exercises the running (--current) path
instead of the shutoff path. Update the start logic in libvirt_mem.py (the code
that checks attach_device and starts the guest) to respect the start_vm flag for
this variant: when start_vm == "no" (or when the variant name is
current_shutoff) do not start the domain before invoking the attach/detach flow
so --current runs against a shutoff domain; alternatively add an explicit branch
for current_shutoff to skip the guest startup. Ensure the check references the
existing attach_device and start_vm variables (or the variant identifier) so
behavior matches the libvirt_mem.cfg setting.

session.close()

# Capture baseline for verification
if attach_option or detach_option:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Initialize and use detach_option explicitly.

detach_option is never loaded from params, so this branch raises NameError whenever attach_option is empty. The same root cause also means the detach path still uses attach_option for virsh.detach_device() and verification, so the new detach-option variants are not actually exercised as configured.

🧰 Tools
🪛 Ruff (0.15.15)

[error] 665-665: Undefined name detach_option

(F821)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libvirt/tests/src/libvirt_mem.py` at line 665, The branch uses detach_option
without initializing it and reuses attach_option in the detach path; initialize
detach_option from params the same way attach_option is loaded (e.g.,
detach_option = params.get("detach_option") or equivalent), update the
conditional to check if either attach_option or detach_option is set, and
replace uses of attach_option in the detach flow and verification with
detach_option so virsh.detach_device() and subsequent checks exercise the
configured detach-option variants.

Comment on lines +717 to +724
verify_immediate_changes(attach_option, baseline_xml_active, after_attach_xml_active,
baseline_xml_inactive, after_attach_xml_inactive,
operation="attach", test=test)
logging.info("After attach: active_xml=%s devices, inactive_xml=%s devices (will be reused as before-detach baseline)",
after_attach_xml_active.get('memory_device_count') if after_attach_xml_active else 'N/A',
after_attach_xml_inactive.get('memory_device_count') if after_attach_xml_inactive else 'N/A')
except Exception as e:
logging.warning("Attach verification failed: %s", e)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

The new verification call is passing the wrong data types.

get_domain_memory_xml() returns dicts, but verify_immediate_changes() compares scalar XML counts and scalar guest-memory values. Passing baseline_xml_active/after_attach_xml_active plus the inactive-XML dicts here guarantees broken comparisons, and the warning-only except at Line 723 lets the test pass without validating the attach behavior. Please pass .get("memory_device_count") and real guest-memory before/after values, or widen the helper signature to model active XML, inactive XML, and guest memory separately.

🧰 Tools
🪛 Ruff (0.15.15)

[warning] 723-723: Do not catch blind exception: Exception

(BLE001)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libvirt/tests/src/libvirt_mem.py` around lines 717 - 724, The
verify_immediate_changes call is getting dicts from get_domain_memory_xml
(baseline_xml_active, after_attach_xml_active, baseline_xml_inactive,
after_attach_xml_inactive) but the helper expects scalar counts and guest-memory
values; update the call in the attach verification to pass
.get("memory_device_count") for each XML dict and the actual guest-memory
before/after attach (or alternatively change verify_immediate_changes to accept
the three models: active_xml, inactive_xml, guest_memory), and also stop
silently swallowing failures in the except block—replace the bare
logging.warning("Attach verification failed: %s", e) with either
logging.exception(...) followed by re-raising the exception or let the exception
propagate so the test fails on verification errors.

Comment on lines +892 to +897
# 1. Reuse dev_xml from attach operation instead of creating a new one
if dev_xml is None:
logging.warning("No dev_xml from attach, creating new one for detach")
dev_xml = utils_hotplug.create_mem_xml(tg_size, pg_size, mem_addr, tg_sizeunit,
pg_unit, tg_node, node_mask, mem_model,
mem_discard)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

dev_xml can be unbound in detach-only variants.

The new detach block assumes dev_xml already exists, but detach-only cases do not go through any earlier assignment. That makes if dev_xml is None: raise UnboundLocalError before the detach command is even attempted. Initialize dev_xml = None before the attach branches, or create it unconditionally when detach_device is enabled.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libvirt/tests/src/libvirt_mem.py` around lines 892 - 897, The detach-only
path can hit an UnboundLocalError because dev_xml may never be defined before
the detach block; ensure dev_xml is initialized to None before the attach/
detach branches (or always create it when detach_device is true). Specifically,
add an initialization like dev_xml = None at the start of the function handling
attach/detach, or guard the detach branch to call utils_hotplug.create_mem_xml
(using tg_size, pg_size, mem_addr, tg_sizeunit, pg_unit, tg_node, node_mask,
mem_model, mem_discard) unconditionally when detach_device is enabled so the
code that checks "if dev_xml is None:" no longer references an unbound variable.

@hholoubk

Copy link
Copy Markdown
Contributor

First of all please extend the PR description with ... what this do... why it is necessary...
(even it looks like obvious, e.g. there is inside the change of memory size (numbers) hardcoded ... and not clear why those numbers are changed.

Second .. please check existing code .. mainly avocado-vt functions and existing memory providers in tp-libvirt.

... I will add detailed info directly

@hholoubk

Copy link
Copy Markdown
Contributor

There are some additional comments from AI that I believe are correct but will require much more detailed check and I am not so involved into the memory tests.

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