@@ -51,38 +51,51 @@ def tearDown(self):
51
51
except Exception :
52
52
pass
53
53
54
- def _create_img (self , fmt , size ):
54
+ def _create_img (self , fmt , size , subformat = None ):
55
55
if fmt == 'vhd' :
56
56
# QEMU calls the vhd format vpc
57
57
fmt = 'vpc'
58
58
59
- fn = tempfile .mktemp (prefix = 'glance-unittest-formatinspector-' ,
59
+ opt = ''
60
+ prefix = 'glance-unittest-formatinspector-'
61
+
62
+ if subformat :
63
+ opt = ' -o subformat=%s' % subformat
64
+ prefix += subformat + '-'
65
+
66
+ fn = tempfile .mktemp (prefix = prefix ,
60
67
suffix = '.%s' % fmt )
61
68
self ._created_files .append (fn )
62
69
subprocess .check_output (
63
- 'qemu-img create -f %s %s %i' % (fmt , fn , size ),
70
+ 'qemu-img create -f %s %s %s % i' % (fmt , opt , fn , size ),
64
71
shell = True )
65
72
return fn
66
73
67
- def _create_allocated_vmdk (self , size_mb ):
74
+ def _create_allocated_vmdk (self , size_mb , subformat = None ):
68
75
# We need a "big" VMDK file to exercise some parts of the code of the
69
76
# format_inspector. A way to create one is to first create an empty
70
77
# file, and then to convert it with the -S 0 option.
71
- fn = tempfile .mktemp (prefix = 'glance-unittest-formatinspector-' ,
72
- suffix = '.vmdk' )
78
+
79
+ if subformat is None :
80
+ # Matches qemu-img default, see `qemu-img convert -O vmdk -o help`
81
+ subformat = 'monolithicSparse'
82
+
83
+ prefix = 'glance-unittest-formatinspector-%s-' % subformat
84
+ fn = tempfile .mktemp (prefix = prefix , suffix = '.vmdk' )
73
85
self ._created_files .append (fn )
74
- zeroes = tempfile .mktemp (prefix = 'glance-unittest-formatinspector-' ,
75
- suffix = '.zero' )
76
- self ._created_files .append (zeroes )
86
+ raw = tempfile .mktemp (prefix = prefix , suffix = '.raw' )
87
+ self ._created_files .append (raw )
77
88
78
- # Create an empty file
89
+ # Create a file with pseudo-random data, otherwise it will get
90
+ # compressed in the streamOptimized format
79
91
subprocess .check_output (
80
- 'dd if=/dev/zero of=%s bs=1M count=%i' % (zeroes , size_mb ),
92
+ 'dd if=/dev/urandom of=%s bs=1M count=%i' % (raw , size_mb ),
81
93
shell = True )
82
94
83
95
# Convert it to VMDK
84
96
subprocess .check_output (
85
- 'qemu-img convert -f raw -O vmdk -S 0 %s %s' % (zeroes , fn ),
97
+ 'qemu-img convert -f raw -O vmdk -o subformat=%s -S 0 %s %s' % (
98
+ subformat , raw , fn ),
86
99
shell = True )
87
100
return fn
88
101
@@ -101,8 +114,9 @@ def _test_format_at_block_size(self, format_name, img, block_size):
101
114
wrapper .close ()
102
115
return fmt
103
116
104
- def _test_format_at_image_size (self , format_name , image_size ):
105
- img = self ._create_img (format_name , image_size )
117
+ def _test_format_at_image_size (self , format_name , image_size ,
118
+ subformat = None ):
119
+ img = self ._create_img (format_name , image_size , subformat = subformat )
106
120
107
121
# Some formats have internal alignment restrictions making this not
108
122
# always exactly like image_size, so get the real value for comparison
@@ -124,11 +138,12 @@ def _test_format_at_image_size(self, format_name, image_size):
124
138
'Format used more than 512KiB of memory: %s' % (
125
139
fmt .context_info ))
126
140
127
- def _test_format (self , format_name ):
141
+ def _test_format (self , format_name , subformat = None ):
128
142
# Try a few different image sizes, including some odd and very small
129
143
# sizes
130
144
for image_size in (512 , 513 , 2057 , 7 ):
131
- self ._test_format_at_image_size (format_name , image_size * units .Mi )
145
+ self ._test_format_at_image_size (format_name , image_size * units .Mi ,
146
+ subformat = subformat )
132
147
133
148
def test_qcow2 (self ):
134
149
self ._test_format ('qcow2' )
@@ -142,12 +157,15 @@ def test_vhdx(self):
142
157
def test_vmdk (self ):
143
158
self ._test_format ('vmdk' )
144
159
145
- def test_vmdk_bad_descriptor_offset (self ):
160
+ def test_vmdk_stream_optimized (self ):
161
+ self ._test_format ('vmdk' , 'streamOptimized' )
162
+
163
+ def _test_vmdk_bad_descriptor_offset (self , subformat = None ):
146
164
format_name = 'vmdk'
147
165
image_size = 10 * units .Mi
148
166
descriptorOffsetAddr = 0x1c
149
167
BAD_ADDRESS = 0x400
150
- img = self ._create_img (format_name , image_size )
168
+ img = self ._create_img (format_name , image_size , subformat = subformat )
151
169
152
170
# Corrupt the header
153
171
fd = open (img , 'r+b' )
@@ -167,7 +185,13 @@ def test_vmdk_bad_descriptor_offset(self):
167
185
'size %i block %i' ) % (format_name , image_size ,
168
186
block_size ))
169
187
170
- def test_vmdk_bad_descriptor_mem_limit (self ):
188
+ def test_vmdk_bad_descriptor_offset (self ):
189
+ self ._test_vmdk_bad_descriptor_offset ()
190
+
191
+ def test_vmdk_bad_descriptor_offset_stream_optimized (self ):
192
+ self ._test_vmdk_bad_descriptor_offset (subformat = 'streamOptimized' )
193
+
194
+ def _test_vmdk_bad_descriptor_mem_limit (self , subformat = None ):
171
195
format_name = 'vmdk'
172
196
image_size = 5 * units .Mi
173
197
virtual_size = 5 * units .Mi
@@ -176,7 +200,8 @@ def test_vmdk_bad_descriptor_mem_limit(self):
176
200
twoMBInSectors = (2 << 20 ) // 512
177
201
# We need a big VMDK because otherwise we will not have enough data to
178
202
# fill-up the CaptureRegion.
179
- img = self ._create_allocated_vmdk (image_size // units .Mi )
203
+ img = self ._create_allocated_vmdk (image_size // units .Mi ,
204
+ subformat = subformat )
180
205
181
206
# Corrupt the end of descriptor address so it "ends" at 2MB
182
207
fd = open (img , 'r+b' )
@@ -200,6 +225,12 @@ def test_vmdk_bad_descriptor_mem_limit(self):
200
225
'Format used more than 1.5MiB of memory: %s' % (
201
226
fmt .context_info ))
202
227
228
+ def test_vmdk_bad_descriptor_mem_limit (self ):
229
+ self ._test_vmdk_bad_descriptor_mem_limit ()
230
+
231
+ def test_vmdk_bad_descriptor_mem_limit_stream_optimized (self ):
232
+ self ._test_vmdk_bad_descriptor_mem_limit (subformat = 'streamOptimized' )
233
+
203
234
def test_vdi (self ):
204
235
self ._test_format ('vdi' )
205
236
0 commit comments