feat(disk,host,ui): add qcow2 extended_l2 optimization, snapshot rewrite guard, and Defender exclusion guidance
72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle"
|
|
)
|
|
|
|
type stubStatsDriver struct {
|
|
diskPath string
|
|
}
|
|
|
|
func (d stubStatsDriver) ID() string { return driver.BuiltinReimsMacOSDriverID }
|
|
func (d stubStatsDriver) Capabilities() []string { return nil }
|
|
func (d stubStatsDriver) List(context.Context) ([]driver.VMInfo, error) { return []driver.VMInfo{{Name: "test", DriverID: d.ID(), State: "stopped", DiskPath: d.diskPath}}, nil }
|
|
func (d stubStatsDriver) Inspect(context.Context, string) (driver.VMInfo, error) {
|
|
return driver.VMInfo{Name: "test", DriverID: d.ID(), State: "stopped", DiskPath: d.diskPath}, nil
|
|
}
|
|
func (d stubStatsDriver) Start(context.Context, driver.StartRequest) (driver.StartResult, error) {
|
|
return driver.StartResult{}, nil
|
|
}
|
|
func (d stubStatsDriver) Stop(context.Context, string, bool, interface{ Write([]byte) (int, error) }, interface{ Write([]byte) (int, error) }) error {
|
|
return nil
|
|
}
|
|
|
|
func TestGetDefenderExclusionCommand(t *testing.T) {
|
|
a := &App{cfg: &config.Config{DataRoot: `D:\Rei's VMs`}}
|
|
const want = `Add-MpPreference -ExclusionPath 'D:\Rei''s VMs' -ExclusionProcess 'qemu-system-x86_64.exe'`
|
|
if got := a.GetDefenderExclusionCommand(); got != want {
|
|
t.Fatalf("GetDefenderExclusionCommand = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestVMStatsDiskHeaderMeta(t *testing.T) {
|
|
cfg := &config.Config{DataRoot: t.TempDir()}
|
|
runtimeDir := filepath.Join(cfg.RuntimesDir(), "test")
|
|
if err := os.MkdirAll(runtimeDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(runtimeDir, "manifest.json"), []byte(`{"commit":"test"}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
disk := filepath.Join(cfg.DataRoot, "disk.qcow2")
|
|
hdr := make([]byte, 104)
|
|
copy(hdr, "QFI\xfb")
|
|
binary.BigEndian.PutUint32(hdr[4:8], 3)
|
|
binary.BigEndian.PutUint32(hdr[60:64], 2)
|
|
binary.BigEndian.PutUint64(hdr[72:80], 0x10)
|
|
if err := os.WriteFile(disk, hdr, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
router := driver.NewRouter()
|
|
router.Register(stubStatsDriver{diskPath: disk})
|
|
a := &App{
|
|
cfg: cfg,
|
|
lastVMsAt: time.Now(),
|
|
lifecycleSvc: vmlifecycle.New(router, nil),
|
|
}
|
|
stats, err := a.GetVMStats("test")
|
|
if err != nil || !stats.DiskExtendedL2 || stats.DiskSnapshotCount != 2 {
|
|
t.Fatalf("GetVMStats = %+v, %v", stats, err)
|
|
}
|
|
}
|