Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/controller/vm/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build windows

package vm

import (
"github.com/Microsoft/hcsshim/internal/vm/vmmanager"
)

// SetManagerForTest configures a Manager with injected dependencies for testing.
// This is only available in test builds.
//
// MUST be called during test setup before any concurrent operations on the Manager.
//
// Note: logOutputDone is initialized by NewController() and is NOT reset here.
// Tests that exercise Wait() in Running/Terminated state must close the channel
// manually or the Wait call will block on the logOutputDone select.
func (c *Manager) SetManagerForTest(uvm vmmanager.LifetimeManager, guest GuestManager, state State) {
c.uvm = uvm
c.guest = guest
c.vmState = state
}
13 changes: 12 additions & 1 deletion internal/controller/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package vm

//go:generate go tool mockgen -source=interface.go -build_constraint=windows -package=mockvmcontroller -destination=../../test/mock/vmcontroller/mock_interface.go

import (
"context"
"time"
Expand All @@ -15,9 +17,18 @@ import (
"github.com/Microsoft/go-winio/pkg/guid"
)

// GuestManager defines the guest operations required by the VM Controller.
// It combines the core guest manager, security policy, and HvSocket interfaces
// from the guestmanager package into a single interface for dependency injection.
type GuestManager interface {
guestmanager.Manager
guestmanager.SecurityPolicyManager
guestmanager.HVSocketManager
}

type Controller interface {
// Guest returns the guest manager instance for this VM.
Guest() *guestmanager.Guest
Guest() GuestManager

// State returns the current VM state.
State() State
Expand Down
33 changes: 33 additions & 0 deletions internal/controller/vm/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build windows

package vm_test

import (
"testing"

vm "github.com/Microsoft/hcsshim/internal/controller/vm"
)

func TestStateString(t *testing.T) {
tests := []struct {
state vm.State
want string
}{
{vm.StateNotCreated, "NotCreated"},
{vm.StateCreated, "Created"},
{vm.StateRunning, "Running"},
{vm.StateTerminated, "Terminated"},
{vm.StateInvalid, "Invalid"},
{vm.State(99), "Unknown"},
{vm.State(-1), "Unknown"},
}

for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
t.Parallel()
if got := tt.state.String(); got != tt.want {
t.Errorf("State(%d).String() = %q, want %q", tt.state, got, tt.want)
}
})
}
}
6 changes: 3 additions & 3 deletions internal/controller/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
// and its associated resources.
type Manager struct {
vmID string
uvm *vmmanager.UtilityVM
guest *guestmanager.Guest
uvm vmmanager.LifetimeManager
guest GuestManager

// vmState tracks the current state of the VM lifecycle.
// Access must be guarded by mu.
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewController() *Manager {

// Guest returns the guest manager instance for this VM.
// The guest manager provides access to guest-host communication.
func (c *Manager) Guest() *guestmanager.Guest {
func (c *Manager) Guest() GuestManager {
return c.guest
}

Expand Down
Loading
Loading