|
| 1 | +package vz_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/Code-Hex/vz/v3" |
| 8 | +) |
| 9 | + |
| 10 | +func TestVirtioTraditionalMemoryBalloonDeviceConfiguration(t *testing.T) { |
| 11 | + // Create memory balloon device configuration |
| 12 | + config, err := vz.NewVirtioTraditionalMemoryBalloonDeviceConfiguration() |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("failed to create memory balloon device configuration: %v", err) |
| 15 | + } |
| 16 | + if config == nil { |
| 17 | + t.Fatal("memory balloon configuration should not be nil") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestMemoryBalloonDevices(t *testing.T) { |
| 22 | + // Create a simple VM configuration |
| 23 | + bootLoader, err := vz.NewLinuxBootLoader( |
| 24 | + "./testdata/Image", |
| 25 | + vz.WithCommandLine("console=hvc0"), |
| 26 | + ) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("failed to create boot loader: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + config, err := vz.NewVirtualMachineConfiguration( |
| 32 | + bootLoader, |
| 33 | + 1, |
| 34 | + 256*1024*1024, |
| 35 | + ) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("failed to create virtual machine configuration: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Create and add a memory balloon device |
| 41 | + memoryBalloonConfig, err := vz.NewVirtioTraditionalMemoryBalloonDeviceConfiguration() |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("failed to create memory balloon device configuration: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + config.SetMemoryBalloonDevicesVirtualMachineConfiguration([]vz.MemoryBalloonDeviceConfiguration{ |
| 47 | + memoryBalloonConfig, |
| 48 | + }) |
| 49 | + |
| 50 | + // Create the VM |
| 51 | + vm, err := vz.NewVirtualMachine(config) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("failed to create virtual machine: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Get memory balloon devices |
| 57 | + balloonDevices := vm.MemoryBalloonDevices() |
| 58 | + if len(balloonDevices) != 1 { |
| 59 | + t.Fatalf("expected 1 memory balloon device, got %d", len(balloonDevices)) |
| 60 | + } |
| 61 | + |
| 62 | + // Verify we can access the balloon device |
| 63 | + balloonDevice := balloonDevices[0] |
| 64 | + if balloonDevice == nil { |
| 65 | + t.Fatal("memory balloon device should not be nil") |
| 66 | + } |
| 67 | + |
| 68 | + // Verify we can cast to VirtioTraditionalMemoryBalloonDevice |
| 69 | + traditionalDevice := vz.AsVirtioTraditionalMemoryBalloonDevice(balloonDevice) |
| 70 | + if traditionalDevice == nil { |
| 71 | + t.Fatal("failed to cast to VirtioTraditionalMemoryBalloonDevice") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestMemoryBalloonTargetSizeAdjustment(t *testing.T) { |
| 76 | + // Create a VM with a memory balloon device |
| 77 | + bootLoader, err := vz.NewLinuxBootLoader( |
| 78 | + "./testdata/Image", |
| 79 | + vz.WithCommandLine("console=hvc0"), |
| 80 | + vz.WithInitrd("./testdata/initramfs.cpio.gz"), |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("failed to create boot loader: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + startingMemory := uint64(512 * 1024 * 1024) |
| 87 | + targetMemory := uint64(300 * 1024 * 1024) |
| 88 | + |
| 89 | + t.Logf("Starting memory: %d bytes", startingMemory) |
| 90 | + t.Logf("Target memory: %d bytes", targetMemory) |
| 91 | + |
| 92 | + config, err := vz.NewVirtualMachineConfiguration( |
| 93 | + bootLoader, |
| 94 | + 1, |
| 95 | + startingMemory, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("failed to create virtual machine configuration: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Create memory balloon device |
| 102 | + memoryBalloonConfig, err := vz.NewVirtioTraditionalMemoryBalloonDeviceConfiguration() |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("failed to create memory balloon device configuration: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Add memory balloon device to VM configuration |
| 108 | + config.SetMemoryBalloonDevicesVirtualMachineConfiguration([]vz.MemoryBalloonDeviceConfiguration{ |
| 109 | + memoryBalloonConfig, |
| 110 | + }) |
| 111 | + |
| 112 | + // Validate the configuration |
| 113 | + valid, err := config.Validate() |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("configuration validation failed: %v", err) |
| 116 | + } |
| 117 | + if !valid { |
| 118 | + t.Fatal("configuration is not valid") |
| 119 | + } |
| 120 | + |
| 121 | + // Create the VM |
| 122 | + vm, err := vz.NewVirtualMachine(config) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("failed to create virtual machine: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + // Check memory balloon devices |
| 128 | + balloonDevices := vm.MemoryBalloonDevices() |
| 129 | + if len(balloonDevices) != 1 { |
| 130 | + t.Fatalf("expected 1 memory balloon device, got %d", len(balloonDevices)) |
| 131 | + } |
| 132 | + |
| 133 | + // Cast to VirtioTraditionalMemoryBalloonDevice |
| 134 | + balloonDevice := vz.AsVirtioTraditionalMemoryBalloonDevice(balloonDevices[0]) |
| 135 | + if balloonDevice == nil { |
| 136 | + t.Fatal("failed to cast to VirtioTraditionalMemoryBalloonDevice") |
| 137 | + } |
| 138 | + |
| 139 | + // Start the VM |
| 140 | + t.Log("Starting virtual machine...") |
| 141 | + err = vm.Start() |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("failed to start virtual machine: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + defer func() { |
| 147 | + if vm.CanStop() { |
| 148 | + _ = vm.Stop() // Cleanup VM |
| 149 | + } |
| 150 | + }() |
| 151 | + |
| 152 | + // Wait until the VM is running |
| 153 | + err = waitUntilState(10*time.Second, vm, vz.VirtualMachineStateRunning) |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("failed to wait for VM to start: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + // Get the current target memory size |
| 159 | + currentMemoryBefore := balloonDevice.GetTargetVirtualMachineMemorySize() |
| 160 | + |
| 161 | + if currentMemoryBefore != startingMemory { |
| 162 | + t.Fatalf("expected starting memory size to be %d, got %d", startingMemory, currentMemoryBefore) |
| 163 | + } |
| 164 | + |
| 165 | + // Set a new target memory size |
| 166 | + balloonDevice.SetTargetVirtualMachineMemorySize(targetMemory) |
| 167 | + |
| 168 | + // Verify the new memory size was set |
| 169 | + currentMemoryAfter := balloonDevice.GetTargetVirtualMachineMemorySize() |
| 170 | + |
| 171 | + if currentMemoryAfter != targetMemory { |
| 172 | + t.Fatalf("expected memory size after adjustment to be %d, got %d", targetMemory, currentMemoryAfter) |
| 173 | + } |
| 174 | +} |
0 commit comments