-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig_test.go
More file actions
116 lines (101 loc) · 3.94 KB
/
config_test.go
File metadata and controls
116 lines (101 loc) · 3.94 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
package firecracker
import (
"os"
"path/filepath"
"testing"
"github.com/kernel/hypeman/lib/hypervisor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestToDriveConfigs(t *testing.T) {
cfg := hypervisor.VMConfig{
Disks: []hypervisor.DiskConfig{
{Path: "/rootfs.raw", Readonly: true, IOBps: 1024, IOBurstBps: 4096},
{Path: "/overlay.raw", Readonly: false},
},
}
drives := toDriveConfigs(cfg)
require.Len(t, drives, 2)
assert.Equal(t, "rootfs", drives[0].DriveID)
assert.True(t, drives[0].IsRootDevice)
assert.True(t, drives[0].IsReadOnly)
require.NotNil(t, drives[0].RateLimiter)
require.NotNil(t, drives[0].RateLimiter.Bandwidth)
assert.Equal(t, int64(1024), drives[0].RateLimiter.Bandwidth.Size)
assert.Equal(t, int64(1000), drives[0].RateLimiter.Bandwidth.RefillTime)
require.NotNil(t, drives[0].RateLimiter.Bandwidth.OneTimeBurst)
assert.Equal(t, int64(3072), *drives[0].RateLimiter.Bandwidth.OneTimeBurst)
assert.Equal(t, "disk1", drives[1].DriveID)
assert.False(t, drives[1].IsRootDevice)
assert.False(t, drives[1].IsReadOnly)
assert.Nil(t, drives[1].RateLimiter)
}
func TestToNetworkInterfaces(t *testing.T) {
cfg := hypervisor.VMConfig{
Networks: []hypervisor.NetworkConfig{
{
TAPDevice: "hype-abc123",
MAC: "02:00:00:00:00:01",
DownloadBps: 1_000_000,
UploadBps: 2_000_000,
},
},
}
nets := toNetworkInterfaces(cfg)
require.Len(t, nets, 1)
assert.Equal(t, "eth0", nets[0].IfaceID)
assert.Equal(t, "hype-abc123", nets[0].HostDevName)
assert.Equal(t, "02:00:00:00:00:01", nets[0].GuestMAC)
require.NotNil(t, nets[0].RxRateLimiter)
require.NotNil(t, nets[0].TxRateLimiter)
assert.Equal(t, int64(1_000_000), nets[0].RxRateLimiter.Bandwidth.Size)
assert.Equal(t, int64(2_000_000), nets[0].TxRateLimiter.Bandwidth.Size)
}
func TestSnapshotParamPaths(t *testing.T) {
t.Run("uses full snapshots when no retained base exists", func(t *testing.T) {
snapshotDir := filepath.Join(t.TempDir(), "snapshot-latest")
create := toSnapshotCreateParams(snapshotDir)
assert.Equal(t, filepath.Join(snapshotDir, "state"), create.SnapshotPath)
assert.Equal(t, filepath.Join(snapshotDir, "memory"), create.MemFilePath)
assert.Equal(t, "Full", create.SnapshotType)
})
t.Run("uses diff snapshots when retained base memory exists", func(t *testing.T) {
snapshotDir := filepath.Join(t.TempDir(), "snapshot-latest")
require.NoError(t, os.MkdirAll(snapshotDir, 0755))
require.NoError(t, os.WriteFile(filepath.Join(snapshotDir, "memory"), []byte("base"), 0644))
create := toSnapshotCreateParams(snapshotDir)
assert.Equal(t, filepath.Join(snapshotDir, "state"), create.SnapshotPath)
assert.Equal(t, filepath.Join(snapshotDir, "memory"), create.MemFilePath)
assert.Equal(t, "Diff", create.SnapshotType)
})
load := toSnapshotLoadParams("/tmp/snapshot-latest", []networkOverride{
{IfaceID: "eth0", HostDevName: "hype-abc123"},
}, "")
assert.Equal(t, "/tmp/snapshot-latest/state", load.SnapshotPath)
assert.Equal(t, "/tmp/snapshot-latest/memory", load.MemFilePath)
assert.Nil(t, load.MemBackend)
assert.True(t, load.EnableDiffSnapshots)
assert.False(t, load.ResumeVM)
require.Len(t, load.NetworkOverrides, 1)
loadUffd := toSnapshotLoadParams("/tmp/snapshot-latest", nil, "/run/uffd/abc.sock")
assert.Equal(t, "", loadUffd.MemFilePath, "mem_file_path must be empty when a uffd backend is set")
require.NotNil(t, loadUffd.MemBackend)
assert.Equal(t, "Uffd", loadUffd.MemBackend.BackendType)
assert.Equal(t, "/run/uffd/abc.sock", loadUffd.MemBackend.BackendPath)
}
func TestToBalloonConfig(t *testing.T) {
cfg := hypervisor.VMConfig{
GuestMemory: hypervisor.GuestMemoryConfig{
EnableBalloon: true,
DeflateOnOOM: true,
FreePageHinting: true,
FreePageReporting: true,
},
}
b := toBalloonConfig(cfg)
require.NotNil(t, b)
assert.Equal(t, int64(0), b.AmountMib)
assert.True(t, b.DeflateOnOOM)
assert.True(t, b.FreePageHinting)
assert.True(t, b.FreePageReporting)
}