Skip to content

controller/vm: add unit tests for VM Controller state machine#2634

Draft
shreyanshjain7174 wants to merge 9 commits intomicrosoft:mainfrom
shreyanshjain7174:controller/vm-unit-tests
Draft

controller/vm: add unit tests for VM Controller state machine#2634
shreyanshjain7174 wants to merge 9 commits intomicrosoft:mainfrom
shreyanshjain7174:controller/vm-unit-tests

Conversation

@shreyanshjain7174
Copy link
Contributor

@shreyanshjain7174 shreyanshjain7174 commented Mar 18, 2026

Depends on #2627 — must merge first.

Adds unit tests for the VM Controller state machine. To make the Manager testable, two field types are changed to their existing interfaces (vmmanager.LifetimeManager, combined GuestManager). Mocks are generated with mockgen following the internal/windows/mock/ pattern.

42 test cases covering:

  • TerminateVM transitions (idempotency, Created/Running→Terminated, Close fail→Invalid, Invalid recovery)
  • Guard checks — all state-dependent methods reject wrong states with validated error messages
  • ExecIntoHost (terminal+stderr rejection, success, non-zero exit, exec error)
  • DumpStacks (supported, not supported, dump error)
  • StartTime / ExitStatus for relevant states
  • StartVM idempotency + error states
  • State.String() for all values

rawahars and others added 9 commits March 12, 2026 02:42
In this commit, we are adding the initial draft of `containerd-shim-lcow-v1`. This shim is used for running Linux Containers on Windows.

As part of this commit, we are adding the following-
- The main entrypoint and supporting logic to start + serve the shim server.
- The stubs for Task Service and Shimdiag service.
- The implementation for `Sandbox` service.
- The implementation for `VM Controller` which manages the VM lifecycle and operations.

Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
Change Manager.uvm from *vmmanager.UtilityVM to vmmanager.LifetimeManager and
Manager.guest from *guestmanager.Guest to a new combined GuestManager interface.
This enables dependency injection for unit testing with mocks.

No behavioral change to production code.

Signed-off-by: Shreyansh Sancheti <shreyanshjain7174@gmail.com>
Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
Add go:generate mockgen directives and generate mocks for:
- vmmanager.LifetimeManager
- vm.Controller and vm.GuestManager
- gcs.GuestDefinedCapabilities

Signed-off-by: Shreyansh Sancheti <shreyanshjain7174@gmail.com>
Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
Add 42 test cases covering:
- State.String() for all states including unknown/negative values
- TerminateVM state transitions (idempotency, success, error, recovery)
- Guard checks for all state-dependent methods with error content validation
- ExecIntoHost validation, success, non-zero exit code, and exec error paths
- DumpStacks with supported/unsupported capabilities and dump error path
- StartTime for all states
- ExitStatus success and error cases (table-driven)
- StartVM idempotency and error states (table-driven)

Follows Go test best practices:
- Table-driven tests with t.Run() and / separator for subtest names
- t.Parallel() on all subtests for concurrent execution
- defer ctrl.Finish() per repo convention (TestLookupVMMEM pattern)
- t.Context() instead of context.Background() (Go 1.24+)
- t.Errorf for independent assertions, t.Fatalf only for nil guards
- Error content validation (not just err != nil)

Signed-off-by: Shreyansh Sancheti <shreyanshjain7174@gmail.com>
Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
- Use 'go tool mockgen' instead of bare 'mockgen' in go:generate directives
  (matches go.mod tool directive, ensures CI uses pinned version)
- Add -build_constraint=windows to vmcontroller mock (imports Windows-only packages)
- Remove stale internal/controller/vm/mock/ directory (tests use internal/test/mock/vmcontroller/)

Signed-off-by: Shreyansh Sancheti <shreyanshjain7174@gmail.com>
Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
CI uses oldstable (Go 1.23). t.Context() requires Go 1.24.

Signed-off-by: Shreyansh Sancheti <shreyanshjain7174@gmail.com>
Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.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