wincmd/svc_windows_test.go
starainrt 7e6cc73106
完善 Windows 运维封装与 NTFS 索引解析
- 新增自启动幂等配置、统一错误语义、进程等待和进程树终止能力
- 增强服务生命周期管理,支持等待状态、重启、幂等创建和配置更新
- 新增 NTFS 卷索引、文件 ID 解析、文件遍历、USN 变更监听和 bookmark 持久化
- 修复 NTFS boot sector、fragment、MFT、USN 解析边界和路径重建问题
- 补充权限、进程、服务、NTFS 解析和工作流回归测试
- 增加 Windows 测试脚本和管理员 NTFS smoke 验证脚本
- 升级 Go 兼容版本到 1.18,并更新 stario、win32api 及相关间接依赖
2026-06-09 15:59:31 +08:00

89 lines
2.6 KiB
Go

//go:build windows
// +build windows
package wincmd
import (
"errors"
"testing"
"time"
"golang.org/x/sys/windows/svc/mgr"
)
func TestNewWinSvcExecuteSetsName(t *testing.T) {
exec := NewWinSvcExecute("unit-svc", func() {}, func() {})
if exec.Name != "unit-svc" {
t.Fatalf("Name = %q, want %q", exec.Name, "unit-svc")
}
if exec.Run == nil || exec.Stop == nil {
t.Fatal("expected Run and Stop callbacks to be set")
}
if len(exec.Accepted) == 0 {
t.Fatal("expected default accepted command set to be initialized")
}
}
func TestBuildServiceBinaryPathIncludesArgs(t *testing.T) {
path, err := buildServiceBinaryPath(`C:\tools\svc.exe`, []string{"-a", "hello world"})
if err != nil {
t.Fatalf("buildServiceBinaryPath returned error: %v", err)
}
if path == "" {
t.Fatal("expected non-empty binary path")
}
}
func TestEqualRecoveryActions(t *testing.T) {
left := []mgr.RecoveryAction{
{Type: mgr.ServiceRestart, Delay: 5 * time.Second},
}
right := []mgr.RecoveryAction{
{Type: mgr.ServiceRestart, Delay: 5 * time.Second},
}
if !equalRecoveryActions(left, right) {
t.Fatal("expected recovery action slices to be equal")
}
right[0].Delay = 10 * time.Second
if equalRecoveryActions(left, right) {
t.Fatal("expected recovery action slices to differ")
}
}
func TestShouldUpdateRecoveryActionsTracksResetPeriod(t *testing.T) {
actions := []mgr.RecoveryAction{
{Type: mgr.ServiceRestart, Delay: 5 * time.Second},
}
if shouldUpdateRecoveryActions(actions, actions, 30, 30) {
t.Fatal("expected identical recovery actions and reset period to skip update")
}
if !shouldUpdateRecoveryActions(actions, actions, 30, 60) {
t.Fatal("expected reset period change to require update")
}
if !shouldUpdateRecoveryActions(actions, []mgr.RecoveryAction{}, 30, 0) {
t.Fatal("expected empty desired recovery actions to require reset")
}
}
func TestRecoveryCommandSpecified(t *testing.T) {
if recoveryCommandSpecified(WinSvcInput{}) {
t.Fatal("zero-value recovery command should be unspecified")
}
if !recoveryCommandSpecified(WinSvcInput{RecoveryCommand: "cmd.exe /c exit 0"}) {
t.Fatal("non-empty recovery command should stay specified")
}
if !recoveryCommandSpecified(WinSvcInput{RecoveryCommandSet: true}) {
t.Fatal("explicit empty recovery command should be treated as specified")
}
}
func TestCreateServiceRejectsEmptyExecPath(t *testing.T) {
_, err := CreateService(WinSvcInput{Name: "unit-svc"})
if err == nil {
t.Fatal("expected validation error for empty executable path")
}
if !errors.Is(err, ErrInvalidInput) {
t.Fatalf("expected ErrInvalidInput, got %v", err)
}
}