package reims import ( "context" "os" "path/filepath" "slices" "testing" "github.com/Nwflower/reims-go/internal/driver" "github.com/Nwflower/reims-go/internal/host" "github.com/Nwflower/reims-go/internal/i18n" ) func TestRawDevicePathAndDiskPath(t *testing.T) { vol := `\\?\Volume{11111111-2222-3333-4444-555555555555}` d := VMDetail{Directory: `/tmp/vm`, Disk: vol + ` (128G)`} if got := d.DiskPath(); got != vol { t.Fatalf("DiskPath() = %q, want %q", got, vol) } if !IsWindowsRawDevicePath(vol) { t.Fatalf("expected IsWindowsRawDevicePath(%q) = true", vol) } if IsWindowsRawDevicePath(`\\?\C:\Users\test\disk.qcow2`) { t.Fatalf("expected regular extended Win32 path not to match raw device") } if NeedsDiskConversion(vol) { t.Fatalf("expected NeedsDiskConversion(%q) = false", vol) } } func TestDiskCacheValidationAndArgs(t *testing.T) { vol := `\\?\Volume{11111111-2222-3333-4444-555555555555}` rep := host.Report{LogicalCPUs: 16} issues := ValidateCreate(CreateOpts{ Name: "apfs-vm", CPUs: 8, Memory: "8G", Disk: vol, DiskCache: "unsafe", }, rep, i18n.EnUS) if len(issues) != 0 { t.Fatalf("expected no validation issues for valid raw partition + unsafe cache, got %+v", issues) } copyIssues := ValidateCreate(CreateOpts{ Name: "apfs-vm", CPUs: 8, Memory: "8G", Disk: vol, DiskMode: "copy", }, rep, i18n.EnUS) if !slices.ContainsFunc(copyIssues, func(is ValidationIssue) bool { return is.Code == "raw_disk_in_place_only" }) { t.Fatalf("expected raw_disk_in_place_only issue, got %+v", copyIssues) } badCache := ValidateSet(SetOpts{Name: "apfs-vm", DiskCache: "invalid"}, VMDetail{Name: "apfs-vm"}, rep, i18n.EnUS) if !slices.ContainsFunc(badCache, func(is ValidationIssue) bool { return is.Code == "invalid_disk_cache" }) { t.Fatalf("expected invalid_disk_cache issue, got %+v", badCache) } createArgs, err := BuildCreateArgs(CreateOpts{ Name: "apfs-vm", Disk: vol, DiskCache: "none", }) if err != nil { t.Fatalf("BuildCreateArgs: %v", err) } wantCreate := []string{"create", "apfs-vm", "--disk", vol, "--disk-mode", "in-place", "--disk-cache", "none"} if !slices.Equal(createArgs, wantCreate) { t.Fatalf("BuildCreateArgs = %v, want %v", createArgs, wantCreate) } setArgs, err := BuildSetArgs(SetOpts{Name: "apfs-vm", DiskCache: "unsafe"}) if err != nil { t.Fatalf("BuildSetArgs: %v", err) } wantSet := []string{"set", "apfs-vm", "--disk-cache", "unsafe"} if !slices.Equal(setArgs, wantSet) { t.Fatalf("BuildSetArgs = %v, want %v", setArgs, wantSet) } } func TestParseShowDiskCache(t *testing.T) { show := `apfs-vm state stopped disk \\?\Volume{11111111-2222-3333-4444-555555555555} (nvme) disk cache unsafe ` d := ParseShow("apfs-vm", show) if d.DiskController != "nvme" { t.Fatalf("DiskController = %q, want nvme", d.DiskController) } if d.DiskCache != "unsafe" { t.Fatalf("DiskCache = %q, want unsafe", d.DiskCache) } if dDef := ParseShow("plain", "plain\n state stopped\n"); dDef.DiskCache != "default" { t.Fatalf("default DiskCache = %q, want default", dDef.DiskCache) } } func TestLockCoWDisksAllowsRawDevicePath(t *testing.T) { c := &Client{imagesDir: t.TempDir(), Locale: i18n.EnUS} release, err := c.LockCoWDisks(context.Background(), `\\?\Volume{11111111-2222-3333-4444-555555555555}`) if err != nil { t.Fatalf("LockCoWDisks on raw device: %v", err) } release() } func TestCheckCoWBackingUseSkipsRawDeviceVMs(t *testing.T) { vmDir := filepath.Join(t.TempDir(), "raw-vm") if err := os.MkdirAll(vmDir, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(vmDir, "vm.toml"), []byte("disk = \"\\\\\\\\.\\\\Harddisk1Partition2\"\n"), 0o644); err != nil { t.Fatal(err) } c := &Client{Locale: i18n.EnUS} vms := []driver.VMInfo{{Name: "raw-vm", Directory: vmDir}} if err := c.checkCoWBackingUse(vms, "", func(string) bool { return false }); err != nil { t.Fatalf("checkCoWBackingUse returned error for raw device VM: %v", err) } }