1919from oslo_concurrency import processutils
2020from oslo_utils import units
2121
22+ from nova import exception
2223from nova import test
2324from nova .virt .disk import api
2425from nova .virt .disk .mount import api as mount
@@ -128,7 +129,7 @@ def test_extend_qcow_success(self, mock_exec, mock_inst, mock_resize,
128129
129130 mock_can_resize .assert_called_once_with (imgfile , imgsize )
130131 mock_exec .assert_called_once_with ('qemu-img' , 'resize' ,
131- imgfile , imgsize )
132+ '-f' , 'qcow2' , imgfile , imgsize )
132133 mock_extendable .assert_called_once_with (image )
133134 mock_inst .assert_called_once_with (image , None , None )
134135 mock_resize .assert_called_once_with (mounter .device ,
@@ -154,8 +155,8 @@ def test_extend_qcow_no_resize(self, mock_execute, mock_extendable,
154155 api .extend (image , imgsize )
155156
156157 mock_can_resize_image .assert_called_once_with (imgfile , imgsize )
157- mock_execute .assert_called_once_with ('qemu-img' , 'resize' , imgfile ,
158- imgsize )
158+ mock_execute .assert_called_once_with ('qemu-img' , 'resize' , '-f' ,
159+ 'qcow2' , imgfile , imgsize )
159160 self .assertFalse (mock_extendable .called )
160161
161162 @mock .patch .object (api , 'can_resize_image' , autospec = True ,
@@ -186,8 +187,34 @@ def test_extend_raw_success(self, mock_exec, mock_resize,
186187 api .extend (image , imgsize )
187188
188189 mock_exec .assert_has_calls (
189- [mock .call ('qemu-img' , 'resize' , imgfile , imgsize ),
190+ [mock .call ('qemu-img' , 'resize' , '-f' , 'raw' , imgfile , imgsize ),
190191 mock .call ('e2label' , image .path )])
191192 mock_resize .assert_called_once_with (imgfile , run_as_root = False ,
192193 check_exit_code = [0 ])
193194 mock_can_resize .assert_called_once_with (imgfile , imgsize )
195+
196+ @mock .patch .object (api , 'can_resize_image' , autospec = True ,
197+ return_value = True )
198+ @mock .patch .object (api , 'resize2fs' , autospec = True )
199+ @mock .patch ('oslo_concurrency.processutils.execute' , autospec = True )
200+ def test_extend_vmdk_failure (self , mock_exec , mock_resize ,
201+ mock_can_resize ):
202+
203+ imgfile = tempfile .NamedTemporaryFile ()
204+ self .addCleanup (imgfile .close )
205+ imgsize = 10
206+ # NOTE(danms): There is no image.model.FORMAT_VMDK, but since the
207+ # code initializes this directly from Image.disk_format without using
208+ # the constant (tsk), this can actually happen at runtime.
209+ self .assertRaises (exception .InvalidImageFormat ,
210+ imgmodel .LocalFileImage , imgfile , 'vmdk' )
211+
212+ # Patch ALL_FORMATS to include vmdk as if it got added at some point
213+ with mock .patch ('nova.virt.image.model.ALL_FORMATS' ,
214+ new = ['vmdk' ]):
215+ image = imgmodel .LocalFileImage (imgfile , 'vmdk' )
216+
217+ # Make sure that we still don't call qemu-img resize on the image
218+ self .assertRaises (exception .InvalidDiskFormat ,
219+ api .extend , image , imgsize )
220+ mock_exec .assert_not_called ()
0 commit comments