Skip to content

Conversation

@elibosley
Copy link
Member

@elibosley elibosley commented Mar 30, 2025

Summary by CodeRabbit

  • New Features

    • Added the ability to suspend and resume virtual machines, providing enhanced lifecycle management.
  • Tests

    • Expanded testing to verify that virtual machines properly transition between running, paused, and shutdown states.
  • Chores

    • Streamlined the Docker testing workflow by simplifying build and run commands for improved efficiency.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 30, 2025

Walkthrough

This pull request implements VM suspend and resume functionality by introducing new asynchronous methods in both the Domain and Hypervisor classes. Test cases have been added to verify the domain lifecycle transitions from RUNNING to PAUSED and back to RUNNING, followed by shutdown. At the C++ layer, worker classes are introduced to handle suspend/resume operations via libvirt. Additionally, the Docker testing script is modified to remove caching and simplify its build/run process. No public API changes are made.

Changes

Files Change Summary
__tests__/domain-lifecycle.test.ts
lib/domain.spec.ts
Added test cases for VM suspend and resume operations, verifying domain state transitions and correct hypervisor method invocations.
lib/domain.ts
lib/hypervisor.ts
Introduced asynchronous suspend and resume methods in the Domain and Hypervisor classes with error handling via LibvirtError.
src/domain.h
src/hypervisor-domain.cc
src/hypervisor.cc
src/hypervisor.h
Added C++ worker classes (DomainSuspendWorker, DomainResumeWorker) and corresponding methods (DomainSuspend and DomainResume) to extend libvirt support for suspend/resume functionality.
scripts/test-docker.sh Removed Docker build caching and simplified the build/run commands, adding specific capabilities, device mappings, and updated network configurations.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant Domain
    participant Hypervisor
    participant NativeHypervisor
    Test->>Domain: call suspend()
    Domain->>Hypervisor: domainSuspend(domain)
    Hypervisor->>NativeHypervisor: invoke virDomainSuspend(domain)
    NativeHypervisor-->>Hypervisor: return result
    Hypervisor-->>Domain: promise returned (state: PAUSED)
    Test->>Domain: call resume()
    Domain->>Hypervisor: domainResume(domain)
    Hypervisor->>NativeHypervisor: invoke virDomainResume(domain)
    NativeHypervisor-->>Hypervisor: return result
    Hypervisor-->>Domain: promise returned (state: RUNNING)
Loading

Possibly related PRs

Poem

I’m a rabbit with code so light,
Hopping through VM states day and night.
Suspend and resume, a magical toggle,
Domains in limbo, then back to the throttle.
With each little change, I’m ever so bright! 🐰✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 87fb160 and 71af7b6.

📒 Files selected for processing (1)
  • src/hypervisor.h (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Docker Integration Tests (amd64)
🔇 Additional comments (1)
src/hypervisor.h (1)

76-77: LGTM! Good implementation of domain suspend/resume functionality.

The method declarations for DomainSuspend and DomainResume follow the consistent pattern of other domain-related operations in the class. These additions align well with the PR objective of implementing VM suspend and resume functionality.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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 (2)
scripts/test-docker.sh (1)

20-25: Simplifying build command removes error checking.

The Docker build command has been simplified by removing the caching mechanism, which is good for reliability. However, make sure the build succeeds before proceeding to run tests.

Consider adding error checking:

    docker buildx build \
        --platform linux/"$arch" \
        -t "$tag" \
        --load \
        .
+
+   # Check if build was successful
+   if [ $? -ne 0 ]; then
+       echo -e "${RED}Build failed for architecture $arch${NC}"
+       return 1
+   fi
__tests__/domain-lifecycle.test.ts (1)

176-289: LGTM! Comprehensive test for the new suspend/resume functionality.

The test thoroughly validates the lifecycle of a domain through:

  1. Creation and startup (verified RUNNING state)
  2. Suspension (verified PAUSED state)
  3. Resumption (verified RUNNING state)
  4. Shutdown and cleanup (verified SHUTOFF state)

The test properly exercises the new functionality and ensures the domain transitions through the expected states.

There's significant duplication between this test and the previous one in setting up the domain. Consider extracting the domain setup into a helper function to reduce duplication.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c995368 and cb57977.

📒 Files selected for processing (9)
  • __tests__/domain-lifecycle.test.ts (1 hunks)
  • lib/domain.spec.ts (5 hunks)
  • lib/domain.ts (1 hunks)
  • lib/hypervisor.ts (1 hunks)
  • scripts/test-docker.sh (1 hunks)
  • src/domain.h (1 hunks)
  • src/hypervisor-domain.cc (2 hunks)
  • src/hypervisor.cc (2 hunks)
  • src/hypervisor.h (2 hunks)
🧰 Additional context used
🧬 Code Definitions (4)
lib/domain.spec.ts (1)
lib/hypervisor.ts (2)
  • domainSuspend (188-193)
  • domainResume (200-205)
lib/hypervisor.ts (3)
lib/domain.ts (1)
  • Domain (8-139)
lib/types.ts (1)
  • Domain (130-130)
lib/error.ts (1)
  • wrapMethod (55-61)
__tests__/domain-lifecycle.test.ts (3)
lib/domain-desc.ts (1)
  • DomainDesc (103-150)
__tests__/helpers.ts (2)
  • TEST_VM_NAME (6-6)
  • DISK_IMAGE (7-7)
lib/domain-xml.ts (1)
  • domainDescToXml (300-412)
src/hypervisor-domain.cc (2)
src/worker.cc (2)
  • SetVirError (12-17)
  • SetVirError (12-12)
src/hypervisor-connect.cc (2)
  • dummyCallback (15-19)
  • dummyCallback (15-15)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Docker Integration Tests (amd64)
🔇 Additional comments (17)
src/domain.h (1)

48-49: LGTM: Friend class additions follow existing pattern

These new friend class declarations allow the suspend and resume workers to access the private members of the Domain class, following the same pattern used for other domain operations.

src/hypervisor.cc (2)

9-12: Explicit header includes improve maintainability

Adding these direct dependencies that were likely previously included indirectly helps make the code more maintainable by clearly documenting the module's dependencies.


46-47: LGTM: New method exposures follow existing pattern

The new instance methods for domain suspend and resume operations follow the established pattern for exposing libvirt functionality to JavaScript.

lib/domain.ts (2)

50-56: LGTM: Suspend implementation is well-documented

The suspend method is properly implemented with comprehensive JSDoc comments describing its purpose and potential error conditions.


58-65: LGTM: Resume implementation is well-documented

The resume method is properly implemented with comprehensive JSDoc comments describing its purpose and potential error conditions.

lib/hypervisor.ts (2)

183-193: LGTM: Domain suspend implementation follows existing pattern

The domainSuspend method correctly follows the established pattern for wrapping native hypervisor methods with proper error handling via the wrapMethod function.


195-205: LGTM: Domain resume implementation follows existing pattern

The domainResume method correctly follows the established pattern for wrapping native hypervisor methods with proper error handling via the wrapMethod function.

src/hypervisor.h (1)

76-77: LGTM! Method signatures correctly follow existing pattern.

The new methods DomainSuspend and DomainResume follow the same signature pattern as other domain methods, maintaining consistency in the codebase.

scripts/test-docker.sh (1)

30-38: LGTM! Added necessary capabilities for VM testing.

The additional capabilities (SYS_ADMIN, NET_ADMIN), device mapping (/dev/kvm), network configuration (--network host), and volume mount (/sys/fs/cgroup) are appropriate for testing virtualization functionality, especially for the new suspend/resume operations.

lib/domain.spec.ts (5)

4-4: LGTM! Using DomainState enum improves code readability.

Using the DomainState enum instead of numeric values makes the code more readable and less error-prone.


19-19: LGTM! Using enum value improves clarity.

Replacing the hardcoded value 1 with DomainState.RUNNING makes the code more maintainable and self-documenting.


28-29: LGTM! New mock functions support suspend/resume testing.

The new mock functions and their addition to the hypervisor object properly support testing the suspend and resume functionality.

Also applies to: 43-44


91-96: LGTM! Test verifies Domain.suspend calls hypervisor.domainSuspend.

The test correctly verifies that domain.suspend() calls hypervisor.domainSuspend() with the domain as an argument.


98-103: LGTM! Test verifies Domain.resume calls hypervisor.domainResume.

The test correctly verifies that domain.resume() calls hypervisor.domainResume() with the domain as an argument.

src/hypervisor-domain.cc (3)

658-658: Good job on fixing the comment header casing.

The casing correction in the comment header for the DomainShutdown function improves consistency.


856-898: Well-implemented domain suspend functionality.

The DomainSuspend functionality is well-implemented following the established pattern in the codebase. The worker class correctly calls virDomainSuspend and properly handles errors via SetVirError(). The Hypervisor::DomainSuspend method includes appropriate input validation before unwrapping the domain object and queueing the worker.


900-942: Well-implemented domain resume functionality.

The DomainResume implementation follows the same consistent pattern as other domain operations. The worker class correctly calls virDomainResume and handles errors appropriately. Input validation is properly done before unwrapping the domain object and queueing the worker.

@codecov
Copy link

codecov bot commented Mar 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (c27deb6) to head (71af7b6).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #47   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          767       785   +18     
  Branches       154       154           
=========================================
+ Hits           767       785   +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@elibosley elibosley merged commit 2b96ed3 into main Mar 30, 2025
14 checks passed
@elibosley elibosley deleted the feat/domain-suspension-resume branch March 30, 2025 18:23
elibosley pushed a commit that referenced this pull request Mar 30, 2025
🤖 I have created a release *beep* *boop*
---


## [2.1.0](v2.0.4...v2.1.0)
(2025-03-30)


### Features

* domain resume and suspend
([#47](#47))
([2b96ed3](2b96ed3))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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