@@ -81,25 +81,31 @@ def _create_img(self, fmt, size, subformat=None, options=None,
81
81
shell = True )
82
82
return fn
83
83
84
- def _create_allocated_vmdk (self , size_mb ):
84
+ def _create_allocated_vmdk (self , size_mb , subformat = None ):
85
85
# We need a "big" VMDK file to exercise some parts of the code of the
86
86
# format_inspector. A way to create one is to first create an empty
87
87
# file, and then to convert it with the -S 0 option.
88
- fn = tempfile .mktemp (prefix = 'glance-unittest-formatinspector-' ,
89
- suffix = '.vmdk' )
88
+
89
+ if subformat is None :
90
+ # Matches qemu-img default, see `qemu-img convert -O vmdk -o help`
91
+ subformat = 'monolithicSparse'
92
+
93
+ prefix = 'glance-unittest-formatinspector-%s-' % subformat
94
+ fn = tempfile .mktemp (prefix = prefix , suffix = '.vmdk' )
90
95
self ._created_files .append (fn )
91
- zeroes = tempfile .mktemp (prefix = 'glance-unittest-formatinspector-' ,
92
- suffix = '.zero' )
93
- self ._created_files .append (zeroes )
96
+ raw = tempfile .mktemp (prefix = prefix , suffix = '.raw' )
97
+ self ._created_files .append (raw )
94
98
95
- # Create an empty file
99
+ # Create a file with pseudo-random data, otherwise it will get
100
+ # compressed in the streamOptimized format
96
101
subprocess .check_output (
97
- 'dd if=/dev/zero of=%s bs=1M count=%i' % (zeroes , size_mb ),
102
+ 'dd if=/dev/urandom of=%s bs=1M count=%i' % (raw , size_mb ),
98
103
shell = True )
99
104
100
105
# Convert it to VMDK
101
106
subprocess .check_output (
102
- 'qemu-img convert -f raw -O vmdk -S 0 %s %s' % (zeroes , fn ),
107
+ 'qemu-img convert -f raw -O vmdk -o subformat=%s -S 0 %s %s' % (
108
+ subformat , raw , fn ),
103
109
shell = True )
104
110
return fn
105
111
@@ -118,8 +124,9 @@ def _test_format_at_block_size(self, format_name, img, block_size):
118
124
wrapper .close ()
119
125
return fmt
120
126
121
- def _test_format_at_image_size (self , format_name , image_size ):
122
- img = self ._create_img (format_name , image_size )
127
+ def _test_format_at_image_size (self , format_name , image_size ,
128
+ subformat = None ):
129
+ img = self ._create_img (format_name , image_size , subformat = subformat )
123
130
124
131
# Some formats have internal alignment restrictions making this not
125
132
# always exactly like image_size, so get the real value for comparison
@@ -141,11 +148,12 @@ def _test_format_at_image_size(self, format_name, image_size):
141
148
'Format used more than 512KiB of memory: %s' % (
142
149
fmt .context_info ))
143
150
144
- def _test_format (self , format_name ):
151
+ def _test_format (self , format_name , subformat = None ):
145
152
# Try a few different image sizes, including some odd and very small
146
153
# sizes
147
154
for image_size in (512 , 513 , 2057 , 7 ):
148
- self ._test_format_at_image_size (format_name , image_size * units .Mi )
155
+ self ._test_format_at_image_size (format_name , image_size * units .Mi ,
156
+ subformat = subformat )
149
157
150
158
def test_qcow2 (self ):
151
159
self ._test_format ('qcow2' )
@@ -159,12 +167,30 @@ def test_vhdx(self):
159
167
def test_vmdk (self ):
160
168
self ._test_format ('vmdk' )
161
169
162
- def test_vmdk_bad_descriptor_offset (self ):
170
+ def test_vmdk_stream_optimized (self ):
171
+ self ._test_format ('vmdk' , 'streamOptimized' )
172
+
173
+ def test_from_file_reads_minimum (self ):
174
+ img = self ._create_img ('qcow2' , 10 * units .Mi )
175
+ file_size = os .stat (img ).st_size
176
+ fmt = format_inspector .QcowInspector .from_file (img )
177
+ # We know everything we need from the first 512 bytes of a QCOW image,
178
+ # so make sure that we did not read the whole thing when we inspect
179
+ # a local file.
180
+ self .assertLess (fmt .actual_size , file_size )
181
+
182
+ def test_qed_always_unsafe (self ):
183
+ img = self ._create_img ('qed' , 10 * units .Mi )
184
+ fmt = format_inspector .get_inspector ('qed' ).from_file (img )
185
+ self .assertTrue (fmt .format_match )
186
+ self .assertFalse (fmt .safety_check ())
187
+
188
+ def _test_vmdk_bad_descriptor_offset (self , subformat = None ):
163
189
format_name = 'vmdk'
164
190
image_size = 10 * units .Mi
165
191
descriptorOffsetAddr = 0x1c
166
192
BAD_ADDRESS = 0x400
167
- img = self ._create_img (format_name , image_size )
193
+ img = self ._create_img (format_name , image_size , subformat = subformat )
168
194
169
195
# Corrupt the header
170
196
fd = open (img , 'r+b' )
@@ -184,7 +210,13 @@ def test_vmdk_bad_descriptor_offset(self):
184
210
'size %i block %i' ) % (format_name , image_size ,
185
211
block_size ))
186
212
187
- def test_vmdk_bad_descriptor_mem_limit (self ):
213
+ def test_vmdk_bad_descriptor_offset (self ):
214
+ self ._test_vmdk_bad_descriptor_offset ()
215
+
216
+ def test_vmdk_bad_descriptor_offset_stream_optimized (self ):
217
+ self ._test_vmdk_bad_descriptor_offset (subformat = 'streamOptimized' )
218
+
219
+ def _test_vmdk_bad_descriptor_mem_limit (self , subformat = None ):
188
220
format_name = 'vmdk'
189
221
image_size = 5 * units .Mi
190
222
virtual_size = 5 * units .Mi
@@ -193,7 +225,8 @@ def test_vmdk_bad_descriptor_mem_limit(self):
193
225
twoMBInSectors = (2 << 20 ) // 512
194
226
# We need a big VMDK because otherwise we will not have enough data to
195
227
# fill-up the CaptureRegion.
196
- img = self ._create_allocated_vmdk (image_size // units .Mi )
228
+ img = self ._create_allocated_vmdk (image_size // units .Mi ,
229
+ subformat = subformat )
197
230
198
231
# Corrupt the end of descriptor address so it "ends" at 2MB
199
232
fd = open (img , 'r+b' )
@@ -217,20 +250,11 @@ def test_vmdk_bad_descriptor_mem_limit(self):
217
250
'Format used more than 1.5MiB of memory: %s' % (
218
251
fmt .context_info ))
219
252
220
- def test_qed_always_unsafe (self ):
221
- img = self ._create_img ('qed' , 10 * units .Mi )
222
- fmt = format_inspector .get_inspector ('qed' ).from_file (img )
223
- self .assertTrue (fmt .format_match )
224
- self .assertFalse (fmt .safety_check ())
253
+ def test_vmdk_bad_descriptor_mem_limit (self ):
254
+ self ._test_vmdk_bad_descriptor_mem_limit ()
225
255
226
- def test_from_file_reads_minimum (self ):
227
- img = self ._create_img ('qcow2' , 10 * units .Mi )
228
- file_size = os .stat (img ).st_size
229
- fmt = format_inspector .QcowInspector .from_file (img )
230
- # We know everything we need from the first 512 bytes of a QCOW image,
231
- # so make sure that we did not read the whole thing when we inspect
232
- # a local file.
233
- self .assertLess (fmt .actual_size , file_size )
256
+ def test_vmdk_bad_descriptor_mem_limit_stream_optimized (self ):
257
+ self ._test_vmdk_bad_descriptor_mem_limit (subformat = 'streamOptimized' )
234
258
235
259
def test_qcow2_safety_checks (self ):
236
260
# Create backing and data-file names (and initialize the backing file)
0 commit comments