Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/qemuimgutil/qemuimgutil_test.go
2662 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package qemuimgutil
5
6
import (
7
"testing"
8
9
"gotest.tools/v3/assert"
10
)
11
12
func TestParseInfo(t *testing.T) {
13
t.Run("qcow2", func(t *testing.T) {
14
// qemu-img create -f qcow2 foo.qcow2 4G
15
// (QEMU 8.0)
16
const s = `{
17
"children": [
18
{
19
"name": "file",
20
"info": {
21
"children": [
22
],
23
"virtual-size": 197120,
24
"filename": "foo.qcow2",
25
"format": "file",
26
"actual-size": 200704,
27
"format-specific": {
28
"type": "file",
29
"data": {
30
}
31
},
32
"dirty-flag": false
33
}
34
}
35
],
36
"virtual-size": 4294967296,
37
"filename": "foo.qcow2",
38
"cluster-size": 65536,
39
"format": "qcow2",
40
"actual-size": 200704,
41
"format-specific": {
42
"type": "qcow2",
43
"data": {
44
"compat": "1.1",
45
"compression-type": "zlib",
46
"lazy-refcounts": false,
47
"refcount-bits": 16,
48
"corrupt": false,
49
"extended-l2": false
50
}
51
},
52
"dirty-flag": false
53
}`
54
55
info, err := parseInfo([]byte(s))
56
assert.NilError(t, err)
57
assert.Equal(t, 1, len(info.Children))
58
assert.Check(t, info.FormatSpecific != nil)
59
qcow2 := info.FormatSpecific.Qcow2()
60
assert.Check(t, qcow2 != nil)
61
assert.Equal(t, qcow2.Compat, "1.1")
62
63
t.Run("diff", func(t *testing.T) {
64
// qemu-img create -f qcow2 -F qcow2 -b foo.qcow2 bar.qcow2
65
// (QEMU 8.0)
66
const s = `{
67
"children": [
68
{
69
"name": "file",
70
"info": {
71
"children": [
72
],
73
"virtual-size": 197120,
74
"filename": "bar.qcow2",
75
"format": "file",
76
"actual-size": 200704,
77
"format-specific": {
78
"type": "file",
79
"data": {
80
}
81
},
82
"dirty-flag": false
83
}
84
}
85
],
86
"backing-filename-format": "qcow2",
87
"virtual-size": 4294967296,
88
"filename": "bar.qcow2",
89
"cluster-size": 65536,
90
"format": "qcow2",
91
"actual-size": 200704,
92
"format-specific": {
93
"type": "qcow2",
94
"data": {
95
"compat": "1.1",
96
"compression-type": "zlib",
97
"lazy-refcounts": false,
98
"refcount-bits": 16,
99
"corrupt": false,
100
"extended-l2": false
101
}
102
},
103
"full-backing-filename": "foo.qcow2",
104
"backing-filename": "foo.qcow2",
105
"dirty-flag": false
106
}`
107
info, err := parseInfo([]byte(s))
108
assert.NilError(t, err)
109
assert.Equal(t, 1, len(info.Children))
110
assert.Equal(t, "foo.qcow2", info.BackingFilename)
111
assert.Equal(t, "bar.qcow2", info.Filename)
112
assert.Check(t, info.FormatSpecific != nil)
113
qcow2 := info.FormatSpecific.Qcow2()
114
assert.Check(t, qcow2 != nil)
115
assert.Equal(t, qcow2.Compat, "1.1")
116
})
117
})
118
t.Run("vmdk", func(t *testing.T) {
119
t.Run("twoGbMaxExtentSparse", func(t *testing.T) {
120
// qemu-img create -f vmdk foo.vmdk 4G -o subformat=twoGbMaxExtentSparse
121
// (QEMU 8.0)
122
const s = `{
123
"children": [
124
{
125
"name": "extents.1",
126
"info": {
127
"children": [
128
],
129
"virtual-size": 327680,
130
"filename": "foo-s002.vmdk",
131
"format": "file",
132
"actual-size": 327680,
133
"format-specific": {
134
"type": "file",
135
"data": {
136
}
137
},
138
"dirty-flag": false
139
}
140
},
141
{
142
"name": "extents.0",
143
"info": {
144
"children": [
145
],
146
"virtual-size": 327680,
147
"filename": "foo-s001.vmdk",
148
"format": "file",
149
"actual-size": 327680,
150
"format-specific": {
151
"type": "file",
152
"data": {
153
}
154
},
155
"dirty-flag": false
156
}
157
},
158
{
159
"name": "file",
160
"info": {
161
"children": [
162
],
163
"virtual-size": 512,
164
"filename": "foo.vmdk",
165
"format": "file",
166
"actual-size": 4096,
167
"format-specific": {
168
"type": "file",
169
"data": {
170
}
171
},
172
"dirty-flag": false
173
}
174
}
175
],
176
"virtual-size": 4294967296,
177
"filename": "foo.vmdk",
178
"cluster-size": 65536,
179
"format": "vmdk",
180
"actual-size": 659456,
181
"format-specific": {
182
"type": "vmdk",
183
"data": {
184
"cid": 918420663,
185
"parent-cid": 4294967295,
186
"create-type": "twoGbMaxExtentSparse",
187
"extents": [
188
{
189
"virtual-size": 2147483648,
190
"filename": "foo-s001.vmdk",
191
"cluster-size": 65536,
192
"format": "SPARSE"
193
},
194
{
195
"virtual-size": 2147483648,
196
"filename": "foo-s002.vmdk",
197
"cluster-size": 65536,
198
"format": "SPARSE"
199
}
200
]
201
}
202
},
203
"dirty-flag": false
204
}`
205
info, err := parseInfo([]byte(s))
206
assert.NilError(t, err)
207
assert.Equal(t, 3, len(info.Children))
208
assert.Equal(t, "foo.vmdk", info.Filename)
209
assert.Check(t, info.FormatSpecific != nil)
210
vmdk := info.FormatSpecific.Vmdk()
211
assert.Check(t, vmdk != nil)
212
assert.Equal(t, vmdk.CreateType, "twoGbMaxExtentSparse")
213
})
214
})
215
}
216
217