forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage_test.go
100 lines (89 loc) · 2.45 KB
/
storage_test.go
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
package vz_test
import (
"path/filepath"
"strings"
"testing"
"github.com/Code-Hex/vz/v3"
)
func TestBlockDeviceIdentifier(t *testing.T) {
if vz.Available(12.3) {
t.Skip("VirtioBlockDeviceConfiguration.SetBlockDeviceIdentifier is supported from macOS 12.3")
}
dir := t.TempDir()
path := filepath.Join(dir, "disk.img")
if err := vz.CreateDiskImage(path, 512); err != nil {
t.Fatal(err)
}
attachment, err := vz.NewDiskImageStorageDeviceAttachment(path, false)
if err != nil {
t.Fatal(err)
}
config, err := vz.NewVirtioBlockDeviceConfiguration(attachment)
if err != nil {
t.Fatal(err)
}
got1, err := config.BlockDeviceIdentifier()
if err != nil {
t.Fatal(err)
}
if got1 != "" {
t.Fatalf("want empty by default: %q", got1)
}
invalidID := strings.Repeat("h", 25)
if err := config.SetBlockDeviceIdentifier(invalidID); err == nil {
t.Fatal("want error")
} else {
nserr, ok := err.(*vz.NSError)
if !ok {
t.Fatalf("unexpected error: %v", err)
}
if nserr.Domain != "VZErrorDomain" {
t.Errorf("unexpected NSError domain: %v", nserr)
}
if nserr.Code != int(vz.ErrorInvalidVirtualMachineConfiguration) {
t.Errorf("unexpected NSError code: %v", nserr)
}
}
want := "hello"
if err := config.SetBlockDeviceIdentifier(want); err != nil {
t.Fatal(err)
}
got2, err := config.BlockDeviceIdentifier()
if err != nil {
t.Fatal(err)
}
if got2 != want {
t.Fatalf("want %q but got %q", want, got2)
}
}
func TestBlockDeviceWithCacheAndSyncMode(t *testing.T) {
if vz.Available(12) {
t.Skip("vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync is supported from macOS 12")
}
container := newVirtualizationMachine(t,
func(vmc *vz.VirtualMachineConfiguration) error {
dir := t.TempDir()
path := filepath.Join(dir, "disk.img")
if err := vz.CreateDiskImage(path, 512); err != nil {
t.Fatal(err)
}
attachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(path, false, vz.DiskImageCachingModeAutomatic, vz.DiskImageSynchronizationModeFsync)
if err != nil {
t.Fatal(err)
}
config, err := vz.NewVirtioBlockDeviceConfiguration(attachment)
if err != nil {
t.Fatal(err)
}
vmc.SetStorageDevicesVirtualMachineConfiguration([]vz.StorageDeviceConfiguration{
config,
})
return nil
},
)
defer container.Close()
vm := container.VirtualMachine
if got := vm.State(); vz.VirtualMachineStateRunning != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateRunning, got)
}
}