63 lines
2.6 KiB
Go
63 lines
2.6 KiB
Go
package app
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
)
|
|
|
|
func TestNormalizeDriveSelectionRejectsUnavailableAndExternalDrives(t *testing.T) {
|
|
available := []host.Drive{{Root: `C:\`, System: true}, {Root: `D:\`}, {Root: `E:\`, External: true}}
|
|
got, err := normalizeDriveSelection(available, []string{"d", "D:", `C:\`})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 || got[0] != "C" || got[1] != "D" {
|
|
t.Fatalf("normalized selection = %#v, want [C D]", got)
|
|
}
|
|
for _, selected := range [][]string{{"E"}, {"Z"}, {"../C"}, {"C:/"}} {
|
|
if _, err := normalizeDriveSelection(available, selected); err == nil {
|
|
t.Fatalf("selection %#v unexpectedly accepted", selected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidatedDriveConfigRejectsMissingAndReboundVolumes(t *testing.T) {
|
|
cfg := guesttools.VMConfig{
|
|
DriveSharingEnabled: true, SharedDrives: []string{"C", "D", "E"},
|
|
DriveVolumes: map[string]string{"C": "vol-c", "D": "old-volume-d"},
|
|
}
|
|
available := []host.Drive{
|
|
{Root: `C:\`, VolumeGUID: "vol-c"},
|
|
{Root: `D:\`, VolumeGUID: "new-volume-d"},
|
|
{Root: `E:\`, VolumeGUID: "vol-e", External: true},
|
|
}
|
|
got := validatedDriveConfigForVolumes(cfg, available)
|
|
if len(got.SharedDrives) != 1 || got.SharedDrives[0] != "C" || got.DriveRoots["C"] != `C:\` {
|
|
t.Fatalf("validated config = %#v, want only original C volume", got)
|
|
}
|
|
if len(got.DriveVolumes) != 2 {
|
|
t.Fatalf("runtime validation mutated persisted identities: %#v", got.DriveVolumes)
|
|
}
|
|
cfg.SharedDrives = []string{"C"}
|
|
cfg.DriveVolumes = nil
|
|
if got := validatedDriveConfigForVolumes(cfg, available); len(got.SharedDrives) != 0 {
|
|
t.Fatalf("legacy config without volume identity was trusted: %#v", got)
|
|
}
|
|
}
|
|
func TestMakeDriveShareSettingsDefaultsToReadOnlyAndPerDriveSelection(t *testing.T) {
|
|
available := []host.Drive{{Root: `C:\`, System: true, VolumeGUID: "vol-c"}, {Root: `D:\`, VolumeGUID: "vol-d"}}
|
|
settings := makeDriveShareSettings(guesttools.VMConfig{}, available)
|
|
if settings.Enabled || !settings.ReadOnly || settings.RequiredAgentVersion != guesttools.DriveShareAgentVersion {
|
|
t.Fatalf("unexpected defaults: %#v", settings)
|
|
}
|
|
if settings.Drives == nil || len(settings.Drives) != 2 {
|
|
t.Fatalf("drives = %#v, want two entries", settings.Drives)
|
|
}
|
|
settings = makeDriveShareSettings(guesttools.VMConfig{DriveSharingEnabled: true, SharedDrives: []string{"D"}, DriveVolumes: map[string]string{"D": "vol-d"}}, available)
|
|
if !settings.Enabled || settings.Drives[0].Selected || !settings.Drives[1].Selected || !settings.Drives[0].System {
|
|
t.Fatalf("unexpected settings: %#v", settings)
|
|
}
|
|
}
|