14
14
# License for the specific language governing permissions and limitations
15
15
# under the License.
22
22
from oslo.config import cfg
23
from oslo_concurrency import processutils
24
25
from ironic.common import disk_partitioner
25
26
from ironic.common import exception
27
from ironic.common import images
26
28
from ironic.common import utils as common_utils
27
29
from ironic.drivers.modules import deploy_utils as utils
28
30
from ironic.drivers.modules import image_cache
29
from ironic.openstack.common import processutils
30
31
from ironic.tests import base as tests_base
32
33
_PXECONF_DEPLOY = """
161
162
name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi',
162
163
'logout_iscsi', 'delete_iscsi', 'make_partitions',
163
'is_block_device', 'dd', 'mkswap', 'block_uuid',
164
'notify', 'destroy_disk_metadata']
164
'is_block_device', 'populate_image', 'mkswap',
165
'block_uuid', 'notify', 'destroy_disk_metadata']
165
166
parent_mock = self._mock_calls(name_list)
166
167
parent_mock.get_dev.return_value = dev
167
168
parent_mock.get_image_mb.return_value = 1
181
182
mock.call.is_block_device(root_part),
182
183
mock.call.is_block_device(swap_part),
183
mock.call.dd(image_path, root_part),
184
mock.call.populate_image(image_path, root_part),
184
185
mock.call.mkswap(swap_part),
185
186
mock.call.block_uuid(root_part),
186
187
mock.call.logout_iscsi(address, port, iqn),
214
215
name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi',
215
216
'logout_iscsi', 'delete_iscsi', 'make_partitions',
216
'is_block_device', 'dd', 'block_uuid',
217
'is_block_device', 'populate_image', 'block_uuid',
217
218
'notify', 'destroy_disk_metadata']
218
219
parent_mock = self._mock_calls(name_list)
219
220
parent_mock.get_dev.return_value = dev
233
234
mock.call.is_block_device(root_part),
234
mock.call.dd(image_path, root_part),
235
mock.call.populate_image(image_path, root_part),
235
236
mock.call.block_uuid(root_part),
236
237
mock.call.logout_iscsi(address, port, iqn),
237
238
mock.call.delete_iscsi(address, port, iqn)]
266
267
name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi',
267
268
'logout_iscsi', 'delete_iscsi', 'make_partitions',
268
'is_block_device', 'dd', 'mkswap', 'block_uuid',
269
'notify', 'mkfs_ephemeral',
269
'is_block_device', 'populate_image', 'mkswap',
270
'block_uuid', 'notify', 'mkfs_ephemeral',
270
271
'destroy_disk_metadata']
271
272
parent_mock = self._mock_calls(name_list)
272
273
parent_mock.get_dev.return_value = dev
288
289
mock.call.is_block_device(root_part),
289
290
mock.call.is_block_device(swap_part),
290
291
mock.call.is_block_device(ephemeral_part),
291
mock.call.dd(image_path, root_part),
292
mock.call.populate_image(image_path, root_part),
292
293
mock.call.mkswap(swap_part),
293
294
mock.call.mkfs_ephemeral(ephemeral_part,
294
295
ephemeral_format),
326
327
name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi',
327
328
'logout_iscsi', 'delete_iscsi', 'make_partitions',
328
'is_block_device', 'dd', 'mkswap', 'block_uuid',
329
'notify', 'mkfs_ephemeral', 'get_dev_block_size']
329
'is_block_device', 'populate_image', 'mkswap',
330
'block_uuid', 'notify', 'mkfs_ephemeral',
331
'get_dev_block_size']
330
332
parent_mock = self._mock_calls(name_list)
331
333
parent_mock.get_dev.return_value = dev
332
334
parent_mock.get_image_mb.return_value = 1
347
349
mock.call.is_block_device(root_part),
348
350
mock.call.is_block_device(swap_part),
349
351
mock.call.is_block_device(ephemeral_part),
350
mock.call.dd(image_path, root_part),
352
mock.call.populate_image(image_path, root_part),
351
353
mock.call.mkswap(swap_part),
352
354
mock.call.block_uuid(root_part),
353
355
mock.call.logout_iscsi(address, port, iqn),
469
471
actual = utils.get_dev('1.2.3.4', 5678, 'iqn.fake', 9)
470
472
self.assertEqual(expected, actual)
472
def test_get_image_mb(self):
474
@mock.patch.object(os.path, 'getsize')
475
@mock.patch.object(images, 'converted_size')
476
def test_get_image_mb(self, mock_csize, mock_getsize):
476
def fake_getsize(path):
479
self.useFixture(fixtures.MonkeyPatch('os.path.getsize', fake_getsize))
481
self.assertEqual(0, utils.get_image_mb('x'))
483
self.assertEqual(1, utils.get_image_mb('x'))
485
self.assertEqual(1, utils.get_image_mb('x'))
487
self.assertEqual(2, utils.get_image_mb('x'))
479
mock_getsize.return_value = 0
480
mock_csize.return_value = 0
481
self.assertEqual(0, utils.get_image_mb('x', False))
482
self.assertEqual(0, utils.get_image_mb('x', True))
483
mock_getsize.return_value = 1
484
mock_csize.return_value = 1
485
self.assertEqual(1, utils.get_image_mb('x', False))
486
self.assertEqual(1, utils.get_image_mb('x', True))
487
mock_getsize.return_value = mb
488
mock_csize.return_value = mb
489
self.assertEqual(1, utils.get_image_mb('x', False))
490
self.assertEqual(1, utils.get_image_mb('x', True))
491
mock_getsize.return_value = mb + 1
492
mock_csize.return_value = mb + 1
493
self.assertEqual(2, utils.get_image_mb('x', False))
494
self.assertEqual(2, utils.get_image_mb('x', True))
490
497
@mock.patch.object(disk_partitioner.DiskPartitioner, 'commit', lambda _: None)
678
685
mock_exec.assert_has_calls(expected_call)
688
@mock.patch.object(utils, 'dd')
689
@mock.patch.object(images, 'qemu_img_info')
690
@mock.patch.object(images, 'convert_image')
691
class PopulateImageTestCase(tests_base.TestCase):
694
super(PopulateImageTestCase, self).setUp()
696
def test_populate_raw_image(self, mock_cg, mock_qinfo, mock_dd):
697
type(mock_qinfo.return_value).file_format = mock.PropertyMock(
699
utils.populate_image('src', 'dst')
700
mock_dd.assert_called_once_with('src', 'dst')
701
self.assertFalse(mock_cg.called)
703
def test_populate_qcow2_image(self, mock_cg, mock_qinfo, mock_dd):
704
type(mock_qinfo.return_value).file_format = mock.PropertyMock(
705
return_value='qcow2')
706
utils.populate_image('src', 'dst')
707
mock_cg.assert_called_once_with('src', 'dst', 'raw', True)
708
self.assertFalse(mock_dd.called)
681
711
@mock.patch.object(utils, 'is_block_device', lambda d: True)
682
712
@mock.patch.object(utils, 'block_uuid', lambda p: 'uuid')
683
713
@mock.patch.object(utils, 'dd', lambda *_: None)
714
@mock.patch.object(images, 'convert_image', lambda *_: None)
684
715
@mock.patch.object(common_utils, 'mkfs', lambda *_: None)
685
716
# NOTE(dtantsur): destroy_disk_metadata resets file size, disabling it
686
717
@mock.patch.object(utils, 'destroy_disk_metadata', lambda *_: None)
760
791
mock_clean_up_caches.assert_called_once_with(None, 'master_dir',
761
792
[('uuid', 'path')])
762
793
mock_cache.fetch_image.assert_called_once_with('uuid', 'path',
765
797
@mock.patch.object(image_cache, 'clean_up_caches')
766
798
def test_fetch_images_fail(self, mock_clean_up_caches):