| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/sylvinus/agent-vm/internal/cage" |
| 8 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 9 | "github.com/sylvinus/agent-vm/internal/host" |
| 10 | ) |
| 11 | |
| 12 | func TestWindowsAllocationLimits(t *testing.T) { |
| 13 | for _, tc := range []struct{ hostGiB, hostCPUs, wantGiB, wantCPUs int }{ |
| 14 | {1024, 256, 256, 64}, {32, 8, 24, 8}, |
| 15 | } { |
| 16 | maxGiB, maxCPUs := guestos.WindowsVMResourceCeiling(tc.hostGiB, tc.hostCPUs) |
| 17 | mem, cpus, err := windowsAllocationLimits(cage.ResourceAllocation{Automatic: true}, maxGiB, maxCPUs) |
| 18 | if err != nil || mem != tc.wantGiB || cpus != tc.wantCPUs { |
| 19 | t.Fatalf("automatic allocation = %d GiB, %d CPUs, %v", mem, cpus, err) |
| 20 | } |
| 21 | for _, pair := range [][2]int{{2, 2}, {4, 1}, {maxGiB + 1, 2}, {4, maxCPUs + 1}, {0, 0}} { |
| 22 | if _, _, err := windowsAllocationLimits(cage.ResourceAllocation{MemoryGiB: pair[0], CPUs: pair[1]}, maxGiB, maxCPUs); err == nil { |
| 23 | t.Fatalf("accepted out-of-range manual allocation %v", pair) |
| 24 | } |
| 25 | } |
| 26 | if _, _, err := windowsAllocationLimits(cage.ResourceAllocation{MemoryGiB: 4, CPUs: 2}, maxGiB, maxCPUs); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | if _, _, err := windowsAllocationLimits(cage.ResourceAllocation{Automatic: true}, 3, 2); err == nil { |
| 31 | t.Fatal("accepted automatic allocation below the Windows 11 minimum") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestWindowsColdStartUsesManagerAndHostShareLimits(t *testing.T) { |
| 36 | originalMemory, originalCPUs := host.MemGiB, host.CPUs |
| 37 | t.Cleanup(func() { host.MemGiB, host.CPUs = originalMemory, originalCPUs }) |
| 38 | host.MemGiB = func() int { return 128 } |
| 39 | host.CPUs = func() int { return 256 } |
| 40 | s := &Server{configPath: filepath.Join(t.TempDir(), "config.json")} |
| 41 | for _, tc := range []struct { |
| 42 | share string |
| 43 | wantMemory int |
| 44 | }{{"2", 64}, {"1", 96}} { |
| 45 | t.Setenv("AGENT_VM_HOST_SHARE", tc.share) |
| 46 | memory, cpus, err := s.configuredWindowsResources() |
| 47 | if err != nil || memory != tc.wantMemory || cpus != 64 { |
| 48 | t.Fatalf("share %s: got %d GiB / %d CPUs, %v", tc.share, memory, cpus, err) |
| 49 | } |
| 50 | } |
| 51 | if err := writePrivate(cage.ResourceAllocationPath(s.configPath), cage.ResourceAllocation{MemoryGiB: 32, CPUs: 128}); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if _, _, err := s.configuredWindowsResources(); err == nil { |
| 55 | t.Fatal("legacy allocation above manager cap was accepted") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestWindowsDynamicMemoryRequiresExpectedStartupAndMinimum(t *testing.T) { |
| 60 | state := guestos.WindowsVMResourceState{ |
| 61 | DynamicMemory: true, MemoryMinimum: 4 << 30, MemoryStartup: 4 << 30, |
| 62 | MemoryMaximum: 16 << 30, CPUs: 8, |
| 63 | } |
| 64 | if !state.MatchesAllocation(16, 8) { |
| 65 | t.Fatal("current configuration requires an unnecessary restart") |
| 66 | } |
| 67 | state.MemoryMinimum = 2 << 30 |
| 68 | if state.MatchesAllocation(16, 8) { |
| 69 | t.Fatal("ignored the previous minimum memory") |
| 70 | } |
| 71 | state.MemoryMinimum, state.MemoryStartup = 4<<30, 8<<30 |
| 72 | if state.MatchesAllocation(16, 8) { |
| 73 | t.Fatal("ignored the previous startup memory") |
| 74 | } |
| 75 | } |