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