Skip to content

Conversation

@cliping
Copy link
Contributor

@cliping cliping commented Sep 7, 2025

To test that memory compression cache size can be set/got by migrate-compcache.

VIRT-298168 - [migrate-compcache] set/get cache size for migration memory compression

Summary by CodeRabbit

  • Tests
    • Added migration tests and accompanying configuration to validate memory compression-cache handling across running and shutoff VMs.
    • Covers multiple size variants (valid and invalid), verifies get/set operations, correct size/unit parsing and conversion, and ensures failures do not alter cache state.
    • Includes negative cases with expected error reporting for invalid or out-of-range values.

@cliping
Copy link
Contributor Author

cliping commented Sep 7, 2025

Test result:
(1/1) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_shutoff: STARTED
(1/1) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_shutoff: PASS (5.74 s)

(1/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_512M: STARTED
(1/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_512M: PASS (36.12 s)
(2/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_256M: STARTED
(2/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_256M: PASS (37.58 s)
(3/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_1K: STARTED
(3/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_1K: PASS (42.94 s)
(4/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_0: STARTED
(4/5) type_specific.io-github-autotest-libvirt.migration.migration_cmd.migrate_compcache.vm_running.compcache_0: PASS (44.84 s)

Copy link
Contributor

@smitterl smitterl left a comment

Choose a reason for hiding this comment

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

Thanks for this. Minor suggestions in error messages, please check.
Rest LGTM.

elif unit == "GiB":
size = int(int(compression_cache) / 1073741824)
if value != size:
test.fail("For successful setting, compression cache is not match with set.")
Copy link
Contributor

Choose a reason for hiding this comment

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

This message is confusing, do you mean to say something like f"Failed to set the compcache size: expected {size}, actual {value}"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

if status_error:
libvirt.check_result(ret, err_msg)
else:
test.fail("Set compression cache fail.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean to say something like f"Failed to set compression cache: {ret.stdout_text}"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

if value != size:
test.fail("For successful setting, compression cache is not match with set.")
else:
test.log.info("For successful setting, check compression cache PASS.")
Copy link
Contributor

@smitterl smitterl Oct 27, 2025

Choose a reason for hiding this comment

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

Do you mean to say something like "Compression cache size was set successfully"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

@coderabbitai
Copy link

coderabbitai bot commented Oct 28, 2025

Walkthrough

Adds a Libvirt migration compression-cache test: a YAML config defining multiple compression_cache variants with expected errors and a Python test that gets/sets compression_cache via virsh and validates success or failure for running and shutoff VMs.

Changes

Cohort / File(s) Change Summary
Libvirt migration compcache test config
libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg
New migration_cmd definition migrate_compcache with migration options, server credentials, status controls, and vm_status variants covering running/shutoff VMs and multiple compression_cache values with expected status_error and err_msg.
Libvirt migration compcache test implementation
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py
New test script adding run(test, params, env) that reads params, queries current compcache via virsh.migrate_compcache, sets a new value, handles expected failures, and verifies resulting size (including unit parsing/conversion) for running VMs; handles shutoff/error variants.

Sequence Diagram

sequenceDiagram
    participant Test
    participant Virsh
    participant VM

    Test->>Virsh: migrate_compcache(vm_name) — get current cache
    Virsh->>VM: Query compression cache
    VM-->>Virsh: Current cache size
    Virsh-->>Test: Return cache size

    alt status_error = false
        Test->>Virsh: migrate_compcache(vm_name, size) — set cache
        Virsh->>VM: Set compression cache
        VM-->>Virsh: Success
        Virsh-->>Test: Result OK
        opt vm_running
            Test->>Virsh: migrate_compcache(vm_name) — verify cache
            Virsh->>VM: Query compression cache
            VM-->>Virsh: New cache size
            Virsh-->>Test: Return cache size
            Test->>Test: Parse & compare (unit conversion)
        end
    else status_error = true
        Test->>Virsh: migrate_compcache(vm_name, size) — set cache
        Virsh->>VM: Attempt set -> Error
        VM-->>Virsh: Error message
        Virsh-->>Test: Error response
        Test->>Test: Validate error matches expected `err_msg`
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review unit parsing and conversion in migrate_compcache.py
  • Verify error-string matching and negative-test logic
  • Confirm handling of vm_shutoff branch and early-failure conditions

Suggested reviewers

  • chloerh
  • nanli1
  • smitterl

Poem

🐇 I nudged the cache, parsed bytes with cheer,

Sizes and errors hopped in lines so clear,
Virsh answered back with numbers and sighs,
I checked each unit, and watched tests rise,
A small rabbit claps — migrations near.

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 "migration: Add case about migrate-compcache" directly describes the main change in the changeset. The PR adds a new test case comprising both a configuration file and a test script for the migrate-compcache feature. The title uses appropriate conventions with the "migration:" prefix, clearly states the action ("Add case"), and specifically references the feature being tested ("migrate-compcache"). This aligns with the PR objectives to test memory compression cache size management. A teammate reviewing the commit history would understand that a test case for migrate-compcache is being added.
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

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: 4

🧹 Nitpick comments (1)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1)

32-32: Fix redundant text in log message.

The log message contains "migrate-compcache/migrate-compcache" which appears redundant.

Apply this diff:

-    test.log.info("Run test for migrate-compcache/migrate-compcache.")
+    test.log.info("Run test for migrate-compcache.")
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between acbf065 and b1f1a5b.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg (1 hunks)
  • libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.1)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py

57-57: f-string: unterminated string

(invalid-syntax)


57-58: Expected FStringEnd, found newline

(invalid-syntax)


58-58: Expected ',', found indent

(invalid-syntax)


58-58: Expected ',', found name

(invalid-syntax)


58-58: Expected ',', found name

(invalid-syntax)


58-58: Expected ',', found 'as'

(invalid-syntax)


58-58: missing closing quote in string literal

(invalid-syntax)


58-59: Expected ',', found newline

(invalid-syntax)


59-59: Expected ')', found dedent

(invalid-syntax)


70-70: f-string: unterminated string

(invalid-syntax)


70-71: Expected FStringEnd, found newline

(invalid-syntax)


71-71: Expected ',', found indent

(invalid-syntax)


71-71: Expected ',', found name

(invalid-syntax)


71-71: Expected ',', found ':'

(invalid-syntax)


71-71: Expected ',', found '{'

(invalid-syntax)


71-71: Expected ',', found '{'

(invalid-syntax)


71-71: missing closing quote in string literal

(invalid-syntax)


71-72: Expected ',', found newline

(invalid-syntax)


72-72: Expected ')', found dedent

(invalid-syntax)


72-72: Expected a statement

(invalid-syntax)


72-73: Expected a statement

(invalid-syntax)


73-73: Unexpected indentation

(invalid-syntax)


73-73: f-string: unterminated string

(invalid-syntax)


73-74: Expected FStringEnd, found newline

(invalid-syntax)


74-74: Expected ',', found indent

(invalid-syntax)


74-74: Expected ',', found name

(invalid-syntax)


74-74: Expected ',', found name

(invalid-syntax)


74-74: Expected ',', found name

(invalid-syntax)


74-74: missing closing quote in string literal

(invalid-syntax)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Python 3.8
  • GitHub Check: Python 3.9
  • GitHub Check: Python 3.11
🔇 Additional comments (6)
libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg (3)

1-16: LGTM!

The base configuration is well-structured and includes all necessary settings for migration testing.


18-36: LGTM!

The test variants provide excellent coverage including:

  • Valid compression cache sizes (512M, 256M)
  • Invalid sizes violating power-of-two constraints (1K, 0)
  • Out-of-range values testing overflow handling

37-41: LGTM!

The vm_shutoff variant correctly validates that migration compression cache operations require a running domain.

libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (3)

1-24: LGTM!

The imports and function structure are appropriate for a libvirt test module.


25-30: LGTM!

Parameter retrieval follows standard libvirt test patterns.


44-49: LGTM!

The set operation and error handling logic are correct.

@cliping cliping force-pushed the cache branch 2 times, most recently from f2bff01 to 992420f Compare October 28, 2025 07:47
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 (3)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (3)

30-30: Consider removing unused variable.

The vm object is retrieved but never used in the test. Since only vm_name is needed for the virsh.migrate_compcache() calls, this assignment can be removed.

Apply this diff:

-    vm = env.get_vm(vm_name)

32-32: Clarify redundant text in log message.

The log message contains "migrate-compcache/migrate-compcache" which appears redundant. Consider simplifying to clearly indicate what is being tested.

Apply this diff:

-    test.log.info("Run test for migrate-compcache/migrate-compcache.")
+    test.log.info("Run test for migrate-compcache.")

54-54: Remove redundant strip() operations.

The .strip() calls after .split()[index] are redundant because split() already removes leading and trailing whitespace from the resulting elements.

Apply this diff to simplify:

-            if orig_size.split()[0].strip() != new_size.split()[0].strip():
+            if orig_size.split()[0] != new_size.split()[0]:
-            value = new_size.split()[0].strip()
-            unit = new_size.split()[-1].strip()
+            value = new_size.split()[0]
+            unit = new_size.split()[-1]

Also applies to: 60-61

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b1f1a5b and 992420f.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg (1 hunks)
  • libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg
🧰 Additional context used
🪛 Ruff (0.14.1)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py

30-30: Local variable vm is assigned to but never used

Remove assignment to unused variable vm

(F841)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Python 3.12
  • GitHub Check: Python 3.11
  • GitHub Check: Python 3.8
  • GitHub Check: Python 3.9
🔇 Additional comments (2)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (2)

44-49: LGTM!

The SET operation correctly handles both expected errors (negative tests) and unexpected failures with appropriate error checking.


59-76: LGTM!

The positive test verification logic correctly:

  • Parses the compression cache size and unit from virsh output
  • Converts between units using correct conversion factors (1024 for KiB, 1048576 for MiB, 1073741824 for GiB)
  • Handles unexpected units with a clear error message
  • Provides detailed failure messages when verification fails

The issues flagged in previous reviews (f-string syntax, undefined size variable) have been properly addressed.

test.fail(f"Failed to set compression cache size: {ret.stdout_text}")

if vm_status != "vm_shutoff":
new_size = virsh.migrate_compcache(vm_name, debug=True).stdout_text.strip().split(":")[-1]
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Add error handling for the verification GET operation.

The third migrate_compcache call (line 52) doesn't check exit_status before accessing stdout_text. If this call fails, it could cause unexpected behavior.

Apply this diff to add error handling:

-    if vm_status != "vm_shutoff":
-        new_size = virsh.migrate_compcache(vm_name, debug=True).stdout_text.strip().split(":")[-1]
+    if vm_status != "vm_shutoff":
+        ret = virsh.migrate_compcache(vm_name, debug=True)
+        if ret.exit_status:
+            test.fail(f"Failed to get compression cache size for verification: {ret.stdout_text}")
+        new_size = ret.stdout_text.strip().split(":")[-1]
🤖 Prompt for AI Agents
In libvirt/tests/src/migration/migration_cmd/migrate_compcache.py around line
52, the third call to virsh.migrate_compcache uses .stdout_text without
verifying exit_status; update this to first check the result (e.g., capture the
call into a variable, test result.exit_status == 0) before accessing
stdout_text, and if the exit_status is non-zero raise an exception or log an
error including result.stderr_text/result.stdout_text for debugging so the test
fails cleanly instead of crashing.

@cliping cliping requested a review from smitterl October 28, 2025 08:00
Copy link
Contributor

@smitterl smitterl left a comment

Choose a reason for hiding this comment

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

I'm a bit confused by your update, please help me understand thanks.

test.fail(f"For negative testing, compression cache size is modified: {new_size}")
else:
test.log.info("For negative testing, compression cache size "
"remains the same as before.")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in the above messages for negative test cases, it is important that you use a message that describes your expectation rather than your observation. Something like "Negative test case: the compression cache size shouldn't have been modified but found {new_size}"'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

test.fail(f"For positive testing, failed to set the compression "
f"cache size: expected {size}, actual {value}")
else:
test.log.info("For positive testing, the compression cache "
Copy link
Contributor

@smitterl smitterl Oct 28, 2025

Choose a reason for hiding this comment

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

I'm not a native English speaker but "For positive testing," doesn't sound right. Why is there a need to prefix your expectations with 'negative testing' or 'positive testing'? You either have a certain expectation or not IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

To test that memory compression cache size can be set/got by
migrate-compcache.

VIRT-298168 - [migrate-compcache] set/get cache size for migration
memory compression

Signed-off-by: lcheng <[email protected]>
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: 0

♻️ Duplicate comments (1)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1)

52-52: Add error handling for the verification GET operation.

The verification GET at line 52 doesn't check exit_status before accessing stdout_text. If this call fails, it will cause a test crash or incorrect behavior.

Apply this diff to add error handling:

     if vm_status != "vm_shutoff":
-        new_size = virsh.migrate_compcache(vm_name, debug=True).stdout_text.strip().split(":")[-1]
+        ret = virsh.migrate_compcache(vm_name, debug=True)
+        if ret.exit_status:
+            test.fail(f"Failed to get compression cache size for verification: {ret.stdout_text}")
+        new_size = ret.stdout_text.strip().split(":")[-1]
🧹 Nitpick comments (3)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (3)

30-30: Remove unused variable assignment.

The vm variable is assigned but never used in the test. Since the test only interacts with the VM through virsh.migrate_compcache(vm_name, ...), the VM object itself is not needed.

Apply this diff to remove the unused variable:

-    vm = env.get_vm(vm_name)

54-55: Consider comparing the full size string for robustness.

Comparing only the numeric part (split()[0]) could cause false negatives if libvirt changes the unit representation (e.g., "1024 KiB" → "1 MiB" represents the same size but would fail the comparison).

For a more robust negative test comparison, consider:

  1. Comparing the entire orig_size == new_size string (if libvirt guarantees consistent formatting), or
  2. Normalizing both sizes to bytes before comparing (similar to lines 62-69)

Example normalization approach:

def normalize_to_bytes(size_str):
    """Convert size string like '1 MiB' to bytes."""
    value, unit = size_str.split()
    value = int(float(value))
    if unit == "KiB":
        return value * 1024
    elif unit == "MiB":
        return value * 1048576
    elif unit == "GiB":
        return value * 1073741824
    else:
        test.fail(f"Unexpected unit '{unit}'")

if normalize_to_bytes(orig_size) != normalize_to_bytes(new_size):
    test.fail(f"The compression cache size shouldn't have been modified but found {new_size}")

57-57: Consider simplifying log messages.

The "For positive testing," and "For negative testing," prefixes were noted in past reviews as potentially unnecessary. The context is clear from the conditional logic itself.

Consider these alternatives:

  • Line 57: "The compression cache size remains unchanged." or "Compression cache size correctly remained unchanged (negative test)."
  • Line 73: "The compression cache size was set successfully."

Also applies to: 73-73

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 992420f and 6ed1afb.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg (1 hunks)
  • libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • libvirt/tests/cfg/migration/migration_cmd/migrate_compcache.cfg
🧰 Additional context used
🧬 Code graph analysis (1)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (1)
libvirt/tests/src/virsh_cmd/domain/virsh_attach_device.py (1)
  • status_error (81-83)
🪛 Ruff (0.14.2)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py

30-30: Local variable vm is assigned to but never used

Remove assignment to unused variable vm

(F841)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Python 3.12
  • GitHub Check: Python 3.11
  • GitHub Check: Python 3.9
  • GitHub Check: Python 3.8
🔇 Additional comments (4)
libvirt/tests/src/migration/migration_cmd/migrate_compcache.py (4)

1-13: LGTM!

The file header and imports are appropriate for this test module.


34-42: LGTM!

The initial GET operation includes proper error handling for both shutoff and running VM states.


44-49: LGTM!

The SET operation includes proper error handling for both positive and negative test scenarios.


62-69: LGTM!

The unit conversion logic correctly handles KiB, MiB, and GiB, with proper error handling for unexpected units. The conversion factors (1024, 1048576, 1073741824) are correct.

Copy link
Contributor

@smitterl smitterl left a comment

Choose a reason for hiding this comment

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

Thank you

@cliping cliping requested a review from chloerh October 30, 2025 08:08
@chloerh chloerh merged commit 900a554 into autotest:master Nov 7, 2025
6 checks passed
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.

3 participants