Skip to content

Commit 2f258b8

Browse files
authored
Merge pull request #173 from stackhpc/upstream/2024.1-2026-02-19
Synchronise 2024.1 with upstream
2 parents 598d32e + 9c7fac9 commit 2f258b8

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

nova/tests/unit/virt/disk/test_api.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from oslo_concurrency import processutils
2020
from oslo_utils import units
2121

22+
from nova import exception
2223
from nova import test
2324
from nova.virt.disk import api
2425
from 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()

nova/virt/disk/api.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,21 @@ def extend(image, size):
125125
nova.privsep.libvirt.ploop_resize(image.path, size)
126126
return
127127

128-
processutils.execute('qemu-img', 'resize', image.path, size)
128+
# NOTE(danms): We should not call qemu-img without a format, and
129+
# only qcow2 and raw are supported. So check which one we're being
130+
# told this is supposed to be and pass that to qemu-img. Also note
131+
# that we need to pass the qemu format string to this command, which
132+
# may or may not be the same as the FORMAT_* constant, so be
133+
# explicit here.
134+
if image.format == imgmodel.FORMAT_RAW:
135+
format = 'raw'
136+
elif image.format == imgmodel.FORMAT_QCOW2:
137+
format = 'qcow2'
138+
else:
139+
LOG.warning('Attempting to resize image %s with format %s, '
140+
'which is not supported', image.path, image.format)
141+
raise exception.InvalidDiskFormat(disk_format=image.format)
142+
processutils.execute('qemu-img', 'resize', '-f', format, image.path, size)
129143

130144
if (image.format != imgmodel.FORMAT_RAW and
131145
not CONF.resize_fs_using_block_device):

0 commit comments

Comments
 (0)