Skip to content

Commit 1a0e42a

Browse files
author
Shreyansh Sancheti
committed
controller/vm: add comprehensive unit tests for VM Controller
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>
1 parent d4cbf06 commit 1a0e42a

4 files changed

Lines changed: 881 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build windows
2+
3+
package vm
4+
5+
import (
6+
"github.com/Microsoft/hcsshim/internal/vm/vmmanager"
7+
)
8+
9+
// SetManagerForTest configures a Manager with injected dependencies for testing.
10+
// This is only available in test builds.
11+
//
12+
// MUST be called during test setup before any concurrent operations on the Manager.
13+
func (c *Manager) SetManagerForTest(uvm vmmanager.LifetimeManager, guest GuestManager, state State) {
14+
c.uvm = uvm
15+
c.guest = guest
16+
c.vmState = state
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build windows
2+
3+
package vm_test
4+
5+
import (
6+
"testing"
7+
8+
vm "github.com/Microsoft/hcsshim/internal/controller/vm"
9+
)
10+
11+
func TestStateString(t *testing.T) {
12+
tests := []struct {
13+
state vm.State
14+
want string
15+
}{
16+
{vm.StateNotCreated, "NotCreated"},
17+
{vm.StateCreated, "Created"},
18+
{vm.StateRunning, "Running"},
19+
{vm.StateTerminated, "Terminated"},
20+
{vm.StateInvalid, "Invalid"},
21+
{vm.State(99), "Unknown"},
22+
{vm.State(-1), "Unknown"},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.want, func(t *testing.T) {
27+
t.Parallel()
28+
if got := tt.state.String(); got != tt.want {
29+
t.Errorf("State(%d).String() = %q, want %q", tt.state, got, tt.want)
30+
}
31+
})
32+
}
33+
}

0 commit comments

Comments
 (0)