Skip to content

controller/vm: add comprehensive unit tests for VM Controller#2738

Open
shreyanshjain7174 wants to merge 2 commits into
microsoft:mainfrom
shreyanshjain7174:controller/vm-unit-tests-v2
Open

controller/vm: add comprehensive unit tests for VM Controller#2738
shreyanshjain7174 wants to merge 2 commits into
microsoft:mainfrom
shreyanshjain7174:controller/vm-unit-tests-v2

Conversation

@shreyanshjain7174
Copy link
Copy Markdown
Contributor

@shreyanshjain7174 shreyanshjain7174 commented May 14, 2026

Supersedes #2634.

Tracks: #2668

Summary

Adds comprehensive unit tests for the VM Controller state machine (internal/controller/vm/). Tests cover all public methods on Controller and are biased toward production failure paths — resource cleanup chains, state corruption, and half-started scenarios — rather than guard-check permutations.

Changes

Source changes

  • types.go: Slim utilityVM and guestManager interfaces with platform-specific embedding (platformUVM, platformGuestManager); package-level listenHVSock, createVM, newGuestManager vars for test injection
  • vm_lcow.go: Package-level buildSandboxConfig, parseUVMReferenceInfo vars for test injection
  • vm_wcow.go: WCOW listenHVSock implementation (returns no-op listener)

Test files

  • vm_test.go (build tag: lcow || wcow) — 73 cross-platform tests
  • vm_createvm_lcow_test.go (build tag: lcow) — 4 LCOW-only tests (requires lcow.SandboxOptions import)
  • platform_lcow_test.go / platform_wcow_test.goisLCOW() helper via build tags
  • mocks/mock_types.go — gomock mocks for utilityVM and guestManager

Test coverage (83 LCOW / 73 WCOW)

Category Tests What it covers
State guards 6 One per method for the most likely real-world wrong-state call
TerminateVM 6 Idempotency, Close fail→Invalid, Terminate/CloseConnection errors swallowed
StartVM error cascade 10 Each setup step fails in order: listeners, PrepareConnection, Start, errgroup, CreateConnection, finalizeGCS, confidential options
StartVM success 2 Full success path (LCOW only), idempotency when already Running
CreateVM 4 buildHCSConfig fails, createVM fails, buildSandboxConfig fails, addSecurityPolicy fails
Listener error paths 6 Entropy/logging bind and accept failures (LCOW only)
Wait 3 Error propagation, context cancellation, wrong-state guard
Stats 5 lookupVMMEM/PropertiesV2/getProcessMemoryInfo failures, physically backed path, success
Update* 4 CPU, Memory, CPUGroup, PolicyFragment — forwarding error propagation
ExecIntoHost 3 Terminal+stderr precondition, success, exec error
DumpStacks 3 Capabilities supported/not supported, dump error
Accessors 5 RuntimeID, StartTime, ExitStatus for relevant states
waitForVMExit race 1 Background goroutine vs explicit TerminateVM
Concurrent access 1 Parallel reads on running controller

All tests run without admin privileges or HCS.

…faces

Deferred SCSI controller setup from VM creation to first use, making SCSIController context-aware and fallible. Introduced internal utilityVM and guestManager interfaces on vm.Controller to decouple from concrete vmmanager/guestmanager types and enable mocking. Propagated the new signature through the LCOW pod controller and updated/regenerated the affected mocks and tests.

Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
Extends rawahars's interface refactor (utilityVM, guestManager, listenHVSock,
createVM) with five additional package-level function variables for test
injection: newGuestManager, lookupVMMEM, getProcessMemoryInfo (shared),
buildSandboxConfig and parseUVMReferenceInfo (LCOW-only). Also fixes the
WCOW setupLoggingListener to use the injected listenHVSock var instead of
calling winio.ListenHvsock directly.

Adds 62 unit tests (LCOW) / 61 (WCOW) covering: state machine guards,
TerminateVM cleanup chain (Close failure to Invalid, CloseConnection error
logged not returned, double-terminate idempotency), StartVM error cascade
(PrepareConnection, Start, CreateConnection failures each transition to
Invalid), waitForVMExit background goroutine race with TerminateVM,
ExecIntoHost precondition checks and active-count tracking, DumpStacks
capability branching, Wait dual-wait semantics, Stats vmmem lookup and
memory reporting (VA-backed vs physically-backed), and concurrent access
under the race detector. All tests run without admin or HCS.

Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
@shreyanshjain7174 shreyanshjain7174 force-pushed the controller/vm-unit-tests-v2 branch from 0481c1a to 8b78ebf Compare May 14, 2026 06:33
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