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
97 changes: 97 additions & 0 deletions internal/hcs/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//go:build windows

package hcs

import (
"context"

"github.com/Microsoft/hcsshim/internal/vmcompute"
)

// SetDriverForTest replaces the SystemDriver on a System instance.
// This is intended for use in tests to inject a mock driver.
func SetDriverForTest(s *System, d hcsDriver) {
s.driver = d
}

// FireNotificationForTest simulates an HCS notification arriving on the
// notification channel for the given callback number. This allows tests
// to exercise the async completion paths without a real HCS system.
func FireNotificationForTest(callbackNumber uintptr, notification hcsNotification, result error) {
callbackMapLock.RLock()
ctx := callbackMap[callbackNumber]
callbackMapLock.RUnlock()
if ctx == nil {
return
}
if ch, ok := ctx.channels[notification]; ok {
ch <- result
}
}

// GetCallbackNumberForTest returns the callback number of a System.
func GetCallbackNumberForTest(s *System) uintptr {
return s.callbackNumber
}

// NewTestSystem creates a System with the given ID for testing.
// The handle is zero (closed state), useful for testing error guards.
func NewTestSystem(id string) *System {
return newSystem(id)
}

// NewTestSystemWithDriver creates a System with a custom driver and a
// non-zero handle for testing operations that require an open handle.
func NewTestSystemWithDriver(id string, driver hcsDriver, handle uintptr) *System {
s := newSystem(id)
s.driver = driver
s.handle = vmcompute.HcsSystem(handle)
return s
}

// RegisterCallbackForTest calls registerCallback on a test system so the
// notification channels are set up. This is needed for testing async paths
// (operations that return ErrVmcomputeOperationPending and wait on channels).
func RegisterCallbackForTest(s *System) error {
return s.registerCallback(context.Background())
}

// StartWaitBackgroundForTest launches the waitBackground goroutine for a test
// system. This is normally done by CreateComputeSystem/OpenComputeSystem.
func StartWaitBackgroundForTest(s *System) {
go s.waitBackground()
}

// WaitErrorForTest returns the waitError field of a System.
func WaitErrorForTest(s *System) error {
return s.waitError
}

// ExitErrorForTest returns the exitError field of a System.
func ExitErrorForTest(s *System) error {
return s.exitError
}

// UnregisterCallbackForTest calls unregisterCallback on a test system.
func UnregisterCallbackForTest(s *System) error {
return s.unregisterCallback(context.Background())
}

// CallbackExistsForTest checks if a callbackNumber is still in the global callbackMap.
func CallbackExistsForTest(callbackNumber uintptr) bool {
callbackMapLock.RLock()
defer callbackMapLock.RUnlock()
_, exists := callbackMap[callbackNumber]
return exists
}

// Expose notification values for test assertions.
var (
HcsNotificationSystemExited = hcsNotificationSystemExited
HcsNotificationSystemCreateCompleted = hcsNotificationSystemCreateCompleted
HcsNotificationSystemStartCompleted = hcsNotificationSystemStartCompleted
HcsNotificationSystemPauseCompleted = hcsNotificationSystemPauseCompleted
HcsNotificationSystemResumeCompleted = hcsNotificationSystemResumeCompleted
HcsNotificationSystemSaveCompleted = hcsNotificationSystemSaveCompleted
HcsNotificationServiceDisconnect = hcsNotificationServiceDisconnect
)
Loading
Loading