40
40
class FakeImgInfo(object):
43
@mock.patch.object(imageutils, 'QemuImgInfo')
44
@mock.patch.object(os.path, 'exists', return_value=False)
43
@mock.patch.object(imageutils, 'QemuImgInfo', autospec=True)
44
@mock.patch.object(os.path, 'exists', return_value=False, autospec=True)
45
45
def test_qemu_img_info_path_doesnt_exist(self, path_exists_mock,
46
46
qemu_img_info_mock):
47
47
images.qemu_img_info('noimg')
48
48
path_exists_mock.assert_called_once_with('noimg')
49
49
qemu_img_info_mock.assert_called_once_with()
51
@mock.patch.object(utils, 'execute', return_value=('out', 'err'))
52
@mock.patch.object(imageutils, 'QemuImgInfo')
53
@mock.patch.object(os.path, 'exists', return_value=True)
51
@mock.patch.object(utils, 'execute', return_value=('out', 'err'),
53
@mock.patch.object(imageutils, 'QemuImgInfo', autospec=True)
54
@mock.patch.object(os.path, 'exists', return_value=True, autospec=True)
54
55
def test_qemu_img_info_path_exists(self, path_exists_mock,
55
56
qemu_img_info_mock, execute_mock):
56
57
images.qemu_img_info('img')
59
60
'qemu-img', 'info', 'img')
60
61
qemu_img_info_mock.assert_called_once_with('out')
62
@mock.patch.object(utils, 'execute')
63
@mock.patch.object(utils, 'execute', autospec=True)
63
64
def test_convert_image(self, execute_mock):
64
65
images.convert_image('source', 'dest', 'out_format')
65
66
execute_mock.assert_called_once_with('qemu-img', 'convert', '-O',
66
67
'out_format', 'source', 'dest',
69
@mock.patch.object(image_service, 'get_image_service')
70
@mock.patch.object(__builtin__, 'open')
70
@mock.patch.object(image_service, 'get_image_service', autospec=True)
71
@mock.patch.object(__builtin__, 'open', autospec=True)
71
72
def test_fetch_no_image_service(self, open_mock, image_service_mock):
72
73
mock_file_handle = mock.MagicMock(spec=file)
73
74
mock_file_handle.__enter__.return_value = 'file'
81
82
image_service_mock.return_value.download.assert_called_once_with(
82
83
'image_href', 'file')
84
@mock.patch.object(__builtin__, 'open')
85
@mock.patch.object(__builtin__, 'open', autospec=True)
85
86
def test_fetch_image_service(self, open_mock):
86
87
mock_file_handle = mock.MagicMock(spec=file)
87
88
mock_file_handle.__enter__.return_value = 'file'
94
95
image_service_mock.download.assert_called_once_with(
95
96
'image_href', 'file')
97
@mock.patch.object(images, 'image_to_raw')
98
@mock.patch.object(__builtin__, 'open')
98
@mock.patch.object(images, 'image_to_raw', autospec=True)
99
@mock.patch.object(__builtin__, 'open', autospec=True)
99
100
def test_fetch_image_service_force_raw(self, open_mock, image_to_raw_mock):
100
101
mock_file_handle = mock.MagicMock(spec=file)
101
102
mock_file_handle.__enter__.return_value = 'file'
111
112
image_to_raw_mock.assert_called_once_with(
112
113
'image_href', 'path', 'path.part')
114
@mock.patch.object(images, 'qemu_img_info')
115
@mock.patch.object(images, 'qemu_img_info', autospec=True)
115
116
def test_image_to_raw_no_file_format(self, qemu_img_info_mock):
116
117
info = self.FakeImgInfo()
117
118
info.file_format = None
122
123
qemu_img_info_mock.assert_called_once_with('path_tmp')
123
124
self.assertIn("'qemu-img info' parsing failed.", str(e))
125
@mock.patch.object(images, 'qemu_img_info')
126
@mock.patch.object(images, 'qemu_img_info', autospec=True)
126
127
def test_image_to_raw_backing_file_present(self, qemu_img_info_mock):
127
128
info = self.FakeImgInfo()
128
129
info.file_format = 'raw'
134
135
qemu_img_info_mock.assert_called_once_with('path_tmp')
135
136
self.assertIn("fmt=raw backed by: backing_file", str(e))
137
@mock.patch.object(os, 'rename')
138
@mock.patch.object(os, 'unlink')
139
@mock.patch.object(images, 'convert_image')
140
@mock.patch.object(images, 'qemu_img_info')
138
@mock.patch.object(os, 'rename', autospec=True)
139
@mock.patch.object(os, 'unlink', autospec=True)
140
@mock.patch.object(images, 'convert_image', autospec=True)
141
@mock.patch.object(images, 'qemu_img_info', autospec=True)
141
142
def test_image_to_raw(self, qemu_img_info_mock, convert_image_mock,
142
143
unlink_mock, rename_mock):
143
144
CONF.set_override('force_raw_images', True)
159
160
unlink_mock.assert_called_once_with('path_tmp')
160
161
rename_mock.assert_called_once_with('path.converted', 'path')
162
@mock.patch.object(os, 'unlink')
163
@mock.patch.object(images, 'convert_image')
164
@mock.patch.object(images, 'qemu_img_info')
163
@mock.patch.object(os, 'unlink', autospec=True)
164
@mock.patch.object(images, 'convert_image', autospec=True)
165
@mock.patch.object(images, 'qemu_img_info', autospec=True)
165
166
def test_image_to_raw_not_raw_after_conversion(self, qemu_img_info_mock,
166
167
convert_image_mock,
179
180
'path.converted', 'raw')
180
181
unlink_mock.assert_called_once_with('path_tmp')
182
@mock.patch.object(os, 'rename')
183
@mock.patch.object(images, 'qemu_img_info')
183
@mock.patch.object(os, 'rename', autospec=True)
184
@mock.patch.object(images, 'qemu_img_info', autospec=True)
184
185
def test_image_to_raw_already_raw_format(self, qemu_img_info_mock,
186
187
info = self.FakeImgInfo()
193
194
qemu_img_info_mock.assert_called_once_with('path_tmp')
194
195
rename_mock.assert_called_once_with('path_tmp', 'path')
196
@mock.patch.object(image_service, 'get_image_service')
197
@mock.patch.object(image_service, 'get_image_service', autospec=True)
197
198
def test_download_size_no_image_service(self, image_service_mock):
198
199
images.download_size('context', 'image_href')
199
200
image_service_mock.assert_called_once_with('image_href',
206
207
images.download_size('context', 'image_href', image_service_mock)
207
208
image_service_mock.show.assert_called_once_with('image_href')
209
@mock.patch.object(images, 'qemu_img_info')
210
@mock.patch.object(images, 'qemu_img_info', autospec=True)
210
211
def test_converted_size(self, qemu_img_info_mock):
211
212
info = self.FakeImgInfo()
212
213
info.virtual_size = 1
215
216
qemu_img_info_mock.assert_called_once_with('path')
216
217
self.assertEqual(1, size)
218
@mock.patch.object(images, 'get_image_properties')
219
@mock.patch.object(glance_utils, 'is_glance_image')
219
@mock.patch.object(images, 'get_image_properties', autospec=True)
220
@mock.patch.object(glance_utils, 'is_glance_image', autospec=True)
220
221
def test_is_whole_disk_image_no_img_src(self, mock_igi, mock_gip):
221
222
instance_info = {'image_source': ''}
222
223
iwdi = images.is_whole_disk_image('context', instance_info)
224
225
self.assertFalse(mock_igi.called)
225
226
self.assertFalse(mock_gip.called)
227
@mock.patch.object(images, 'get_image_properties')
228
@mock.patch.object(glance_utils, 'is_glance_image')
228
@mock.patch.object(images, 'get_image_properties', autospec=True)
229
@mock.patch.object(glance_utils, 'is_glance_image', autospec=True)
229
230
def test_is_whole_disk_image_partition_image(self, mock_igi, mock_gip):
230
231
mock_igi.return_value = True
231
232
mock_gip.return_value = {'kernel_id': 'kernel',
238
239
mock_igi.assert_called_once_with(image_source)
239
240
mock_gip.assert_called_once_with('context', image_source)
241
@mock.patch.object(images, 'get_image_properties')
242
@mock.patch.object(glance_utils, 'is_glance_image')
242
@mock.patch.object(images, 'get_image_properties', autospec=True)
243
@mock.patch.object(glance_utils, 'is_glance_image', autospec=True)
243
244
def test_is_whole_disk_image_whole_disk_image(self, mock_igi, mock_gip):
244
245
mock_igi.return_value = True
245
246
mock_gip.return_value = {}
251
252
mock_igi.assert_called_once_with(image_source)
252
253
mock_gip.assert_called_once_with('context', image_source)
254
@mock.patch.object(images, 'get_image_properties')
255
@mock.patch.object(glance_utils, 'is_glance_image')
255
@mock.patch.object(images, 'get_image_properties', autospec=True)
256
@mock.patch.object(glance_utils, 'is_glance_image', autospec=True)
256
257
def test_is_whole_disk_image_partition_non_glance(self, mock_igi,
258
259
mock_igi.return_value = False
265
266
self.assertFalse(mock_gip.called)
266
267
mock_igi.assert_called_once_with(instance_info['image_source'])
268
@mock.patch.object(images, 'get_image_properties')
269
@mock.patch.object(glance_utils, 'is_glance_image')
269
@mock.patch.object(images, 'get_image_properties', autospec=True)
270
@mock.patch.object(glance_utils, 'is_glance_image', autospec=True)
270
271
def test_is_whole_disk_image_whole_disk_non_glance(self, mock_igi,
272
273
mock_igi.return_value = False
281
282
class FsImageTestCase(base.TestCase):
283
@mock.patch.object(shutil, 'copyfile')
284
@mock.patch.object(os, 'makedirs')
285
@mock.patch.object(os.path, 'dirname')
286
@mock.patch.object(os.path, 'exists')
284
@mock.patch.object(shutil, 'copyfile', autospec=True)
285
@mock.patch.object(os, 'makedirs', autospec=True)
286
@mock.patch.object(os.path, 'dirname', autospec=True)
287
@mock.patch.object(os.path, 'exists', autospec=True)
287
288
def test__create_root_fs(self, path_exists_mock,
288
289
dirname_mock, mkdir_mock, cp_mock):
295
296
'a3': 'sub_dir/b3'}
297
298
path_exists_mock.side_effect = path_exists_mock_func
298
dirname_mock.side_effect = ['root_dir', 'root_dir',
299
'root_dir/sub_dir', 'root_dir/sub_dir']
299
dirname_mock.side_effect = iter(
300
['root_dir', 'root_dir', 'root_dir/sub_dir', 'root_dir/sub_dir'])
300
301
images._create_root_fs('root_dir', files_info)
301
302
cp_mock.assert_any_call('a1', 'root_dir/b1')
302
303
cp_mock.assert_any_call('a2', 'root_dir/b2')
308
309
dirname_mock.assert_any_call('root_dir/sub_dir/b3')
309
310
mkdir_mock.assert_called_once_with('root_dir/sub_dir')
311
@mock.patch.object(images, '_create_root_fs')
312
@mock.patch.object(utils, 'tempdir')
313
@mock.patch.object(utils, 'write_to_file')
314
@mock.patch.object(utils, 'dd')
315
@mock.patch.object(utils, 'umount')
316
@mock.patch.object(utils, 'mount')
317
@mock.patch.object(utils, 'mkfs')
312
@mock.patch.object(images, '_create_root_fs', autospec=True)
313
@mock.patch.object(utils, 'tempdir', autospec=True)
314
@mock.patch.object(utils, 'write_to_file', autospec=True)
315
@mock.patch.object(utils, 'dd', autospec=True)
316
@mock.patch.object(utils, 'umount', autospec=True)
317
@mock.patch.object(utils, 'mount', autospec=True)
318
@mock.patch.object(utils, 'mkfs', autospec=True)
318
319
def test_create_vfat_image(self, mkfs_mock, mount_mock, umount_mock,
319
320
dd_mock, write_mock, tempdir_mock, create_root_fs_mock):
343
344
create_root_fs_mock.assert_called_once_with('tempdir', files_info)
344
345
umount_mock.assert_called_once_with('tempdir')
346
@mock.patch.object(images, '_create_root_fs')
347
@mock.patch.object(utils, 'tempdir')
348
@mock.patch.object(utils, 'dd')
349
@mock.patch.object(utils, 'umount')
350
@mock.patch.object(utils, 'mount')
351
@mock.patch.object(utils, 'mkfs')
347
@mock.patch.object(images, '_create_root_fs', autospec=True)
348
@mock.patch.object(utils, 'tempdir', autospec=True)
349
@mock.patch.object(utils, 'dd', autospec=True)
350
@mock.patch.object(utils, 'umount', autospec=True)
351
@mock.patch.object(utils, 'mount', autospec=True)
352
@mock.patch.object(utils, 'mkfs', autospec=True)
352
353
def test_create_vfat_image_always_umount(self, mkfs_mock, mount_mock,
353
354
umount_mock, dd_mock, tempdir_mock, create_root_fs_mock):
364
365
umount_mock.assert_called_once_with('tempdir')
366
@mock.patch.object(utils, 'dd')
367
@mock.patch.object(utils, 'dd', autospec=True)
367
368
def test_create_vfat_image_dd_fails(self, dd_mock):
369
370
dd_mock.side_effect = processutils.ProcessExecutionError
370
371
self.assertRaises(exception.ImageCreationFailed,
371
372
images.create_vfat_image, 'tgt_file')
373
@mock.patch.object(utils, 'tempdir')
374
@mock.patch.object(utils, 'dd')
375
@mock.patch.object(utils, 'mkfs')
374
@mock.patch.object(utils, 'tempdir', autospec=True)
375
@mock.patch.object(utils, 'dd', autospec=True)
376
@mock.patch.object(utils, 'mkfs', autospec=True)
376
377
def test_create_vfat_image_mkfs_fails(self, mkfs_mock, dd_mock,
384
385
self.assertRaises(exception.ImageCreationFailed,
385
386
images.create_vfat_image, 'tgt_file')
387
@mock.patch.object(images, '_create_root_fs')
388
@mock.patch.object(utils, 'tempdir')
389
@mock.patch.object(utils, 'dd')
390
@mock.patch.object(utils, 'umount')
391
@mock.patch.object(utils, 'mount')
392
@mock.patch.object(utils, 'mkfs')
388
@mock.patch.object(images, '_create_root_fs', autospec=True)
389
@mock.patch.object(utils, 'tempdir', autospec=True)
390
@mock.patch.object(utils, 'dd', autospec=True)
391
@mock.patch.object(utils, 'umount', autospec=True)
392
@mock.patch.object(utils, 'mount', autospec=True)
393
@mock.patch.object(utils, 'mkfs', autospec=True)
393
394
def test_create_vfat_image_umount_fails(self, mkfs_mock, mount_mock,
394
395
umount_mock, dd_mock, tempdir_mock, create_root_fs_mock):
401
402
self.assertRaises(exception.ImageCreationFailed,
402
403
images.create_vfat_image, 'tgt_file')
404
@mock.patch.object(utils, 'umount')
405
@mock.patch.object(utils, 'umount', autospec=True)
405
406
def test__umount_without_raise(self, umount_mock):
407
408
umount_mock.side_effect = processutils.ProcessExecutionError
423
424
self.assertEqual(expected_cfg, cfg)
425
426
def test__generate_grub_cfg(self):
427
427
kernel_params = ['key1=value1', 'key2']
428
428
options = {'linux': '/vmlinuz', 'initrd': '/initrd'}
429
expected_cfg = ("menuentry \"install\" {\n"
430
"linux /vmlinuz key1=value1 key2 --\n"
429
expected_cfg = ("set default=0\n"
431
"set hidden_timeout_quiet=false\n"
433
"menuentry \"boot_partition\" {\n"
434
"linuxefi /vmlinuz key1=value1 key2 --\n"
435
"initrdefi /initrd\n"
434
438
cfg = images._generate_cfg(kernel_params,
437
441
self.assertEqual(expected_cfg, cfg)
439
@mock.patch.object(os.path, 'relpath')
440
@mock.patch.object(os, 'walk')
441
@mock.patch.object(utils, 'mount')
443
@mock.patch.object(os.path, 'relpath', autospec=True)
444
@mock.patch.object(os, 'walk', autospec=True)
445
@mock.patch.object(utils, 'mount', autospec=True)
442
446
def test__mount_deploy_iso(self, mount_mock,
443
447
walk_mock, relpath_mock):
444
448
walk_mock.return_value = [('/tmpdir1/EFI/ubuntu', [], ['grub.cfg']),
445
449
('/tmpdir1/isolinux', [],
446
450
['efiboot.img', 'isolinux.bin',
447
451
'isolinux.cfg'])]
448
relpath_mock.side_effect = ['EFI/ubuntu/grub.cfg',
449
'isolinux/efiboot.img']
452
relpath_mock.side_effect = iter(
453
['EFI/ubuntu/grub.cfg', 'isolinux/efiboot.img'])
451
455
images._mount_deploy_iso('path/to/deployiso', 'tmpdir1')
452
456
mount_mock.assert_called_once_with('path/to/deployiso',
453
457
'tmpdir1', '-o', 'loop')
454
458
walk_mock.assert_called_once_with('tmpdir1')
456
@mock.patch.object(images, '_umount_without_raise')
457
@mock.patch.object(os.path, 'relpath')
458
@mock.patch.object(os, 'walk')
459
@mock.patch.object(utils, 'mount')
460
@mock.patch.object(images, '_umount_without_raise', autospec=True)
461
@mock.patch.object(os.path, 'relpath', autospec=True)
462
@mock.patch.object(os, 'walk', autospec=True)
463
@mock.patch.object(utils, 'mount', autospec=True)
460
464
def test__mount_deploy_iso_fail_no_efibootimg(self, mount_mock,
461
465
walk_mock, relpath_mock,
463
467
walk_mock.return_value = [('/tmpdir1/EFI/ubuntu', [], ['grub.cfg']),
464
468
('/tmpdir1/isolinux', [],
465
469
['isolinux.bin', 'isolinux.cfg'])]
466
relpath_mock.side_effect = ['EFI/ubuntu/grub.cfg']
470
relpath_mock.side_effect = iter(['EFI/ubuntu/grub.cfg'])
468
472
self.assertRaises(exception.ImageCreationFailed,
469
473
images._mount_deploy_iso,
473
477
walk_mock.assert_called_once_with('tmpdir1')
474
478
umount_mock.assert_called_once_with('tmpdir1')
476
@mock.patch.object(images, '_umount_without_raise')
477
@mock.patch.object(os.path, 'relpath')
478
@mock.patch.object(os, 'walk')
479
@mock.patch.object(utils, 'mount')
480
@mock.patch.object(images, '_umount_without_raise', autospec=True)
481
@mock.patch.object(os.path, 'relpath', autospec=True)
482
@mock.patch.object(os, 'walk', autospec=True)
483
@mock.patch.object(utils, 'mount', autospec=True)
480
484
def test__mount_deploy_iso_fails_no_grub_cfg(self, mount_mock,
481
485
walk_mock, relpath_mock,
484
488
('/tmpdir1/isolinux', '',
485
489
['efiboot.img', 'isolinux.bin',
486
490
'isolinux.cfg'])]
487
relpath_mock.side_effect = ['isolinux/efiboot.img']
491
relpath_mock.side_effect = iter(['isolinux/efiboot.img'])
489
493
self.assertRaises(exception.ImageCreationFailed,
490
494
images._mount_deploy_iso,
494
498
walk_mock.assert_called_once_with('tmpdir1')
495
499
umount_mock.assert_called_once_with('tmpdir1')
497
@mock.patch.object(utils, 'mount')
501
@mock.patch.object(utils, 'mount', autospec=True)
498
502
def test__mount_deploy_iso_fail_with_ExecutionError(self, mount_mock):
499
503
mount_mock.side_effect = processutils.ProcessExecutionError
500
504
self.assertRaises(exception.ImageCreationFailed,
501
505
images._mount_deploy_iso,
502
506
'path/to/deployiso', 'tmpdir1')
504
@mock.patch.object(images, '_umount_without_raise')
505
@mock.patch.object(images, '_create_root_fs')
506
@mock.patch.object(utils, 'write_to_file')
507
@mock.patch.object(utils, 'execute')
508
@mock.patch.object(images, '_mount_deploy_iso')
509
@mock.patch.object(utils, 'tempdir')
510
@mock.patch.object(images, '_generate_cfg')
508
@mock.patch.object(images, '_umount_without_raise', autospec=True)
509
@mock.patch.object(images, '_create_root_fs', autospec=True)
510
@mock.patch.object(utils, 'write_to_file', autospec=True)
511
@mock.patch.object(utils, 'execute', autospec=True)
512
@mock.patch.object(images, '_mount_deploy_iso', autospec=True)
513
@mock.patch.object(utils, 'tempdir', autospec=True)
514
@mock.patch.object(images, '_generate_cfg', autospec=True)
511
515
def test_create_isolinux_image_for_uefi(self, gen_cfg_mock,
512
516
tempdir_mock, mount_mock, execute_mock,
513
517
write_to_file_mock,
541
545
mock_file_handle.__enter__.return_value = 'tmpdir'
542
546
mock_file_handle1 = mock.MagicMock(spec=file)
543
547
mock_file_handle1.__enter__.return_value = 'mountdir'
544
tempdir_mock.side_effect = [mock_file_handle,
548
tempdir_mock.side_effect = iter(
549
[mock_file_handle, mock_file_handle1])
546
550
mount_mock.return_value = (uefi_path_info,
547
551
e_img_rel_path, grub_rel_path)
566
570
'-no-emul-boot', '-o', 'tgt_file', 'tmpdir')
567
571
umount_mock.assert_called_once_with('mountdir')
569
@mock.patch.object(images, '_create_root_fs')
570
@mock.patch.object(utils, 'write_to_file')
571
@mock.patch.object(utils, 'tempdir')
572
@mock.patch.object(utils, 'execute')
573
@mock.patch.object(images, '_generate_cfg')
573
@mock.patch.object(images, '_create_root_fs', autospec=True)
574
@mock.patch.object(utils, 'write_to_file', autospec=True)
575
@mock.patch.object(utils, 'tempdir', autospec=True)
576
@mock.patch.object(utils, 'execute', autospec=True)
577
@mock.patch.object(images, '_generate_cfg', autospec=True)
574
578
def test_create_isolinux_image_for_bios(self, gen_cfg_mock,
576
580
tempdir_mock, write_to_file_mock,
609
613
'4', '-boot-info-table', '-b', 'isolinux/isolinux.bin',
610
614
'-o', 'tgt_file', 'tmpdir')
612
@mock.patch.object(images, '_umount_without_raise')
613
@mock.patch.object(images, '_create_root_fs')
614
@mock.patch.object(utils, 'tempdir')
615
@mock.patch.object(utils, 'execute')
616
@mock.patch.object(os, 'walk')
616
@mock.patch.object(images, '_umount_without_raise', autospec=True)
617
@mock.patch.object(images, '_create_root_fs', autospec=True)
618
@mock.patch.object(utils, 'tempdir', autospec=True)
619
@mock.patch.object(utils, 'execute', autospec=True)
620
@mock.patch.object(os, 'walk', autospec=True)
617
621
def test_create_isolinux_image_uefi_rootfs_fails(self, walk_mock,
624
628
mock_file_handle.__enter__.return_value = 'tmpdir'
625
629
mock_file_handle1 = mock.MagicMock(spec=file)
626
630
mock_file_handle1.__enter__.return_value = 'mountdir'
627
tempdir_mock.side_effect = [mock_file_handle,
631
tempdir_mock.side_effect = iter(
632
[mock_file_handle, mock_file_handle1])
629
633
create_root_fs_mock.side_effect = IOError
631
635
self.assertRaises(exception.ImageCreationFailed,
635
639
'path/to/ramdisk')
636
640
umount_mock.assert_called_once_with('mountdir')
638
@mock.patch.object(images, '_create_root_fs')
639
@mock.patch.object(utils, 'tempdir')
640
@mock.patch.object(utils, 'execute')
641
@mock.patch.object(os, 'walk')
642
@mock.patch.object(images, '_create_root_fs', autospec=True)
643
@mock.patch.object(utils, 'tempdir', autospec=True)
644
@mock.patch.object(utils, 'execute', autospec=True)
645
@mock.patch.object(os, 'walk', autospec=True)
642
646
def test_create_isolinux_image_bios_rootfs_fails(self, walk_mock,
650
654
'tgt_file', 'path/to/kernel',
651
655
'path/to/ramdisk')
653
@mock.patch.object(images, '_umount_without_raise')
654
@mock.patch.object(images, '_create_root_fs')
655
@mock.patch.object(utils, 'write_to_file')
656
@mock.patch.object(utils, 'tempdir')
657
@mock.patch.object(utils, 'execute')
658
@mock.patch.object(images, '_mount_deploy_iso')
659
@mock.patch.object(images, '_generate_cfg')
657
@mock.patch.object(images, '_umount_without_raise', autospec=True)
658
@mock.patch.object(images, '_create_root_fs', autospec=True)
659
@mock.patch.object(utils, 'write_to_file', autospec=True)
660
@mock.patch.object(utils, 'tempdir', autospec=True)
661
@mock.patch.object(utils, 'execute', autospec=True)
662
@mock.patch.object(images, '_mount_deploy_iso', autospec=True)
663
@mock.patch.object(images, '_generate_cfg', autospec=True)
660
664
def test_create_isolinux_image_mkisofs_fails(self,
669
673
mock_file_handle.__enter__.return_value = 'tmpdir'
670
674
mock_file_handle1 = mock.MagicMock(spec=file)
671
675
mock_file_handle1.__enter__.return_value = 'mountdir'
672
tempdir_mock.side_effect = [mock_file_handle,
676
tempdir_mock.side_effect = iter(
677
[mock_file_handle, mock_file_handle1])
674
678
mount_mock.return_value = ({'a': 'a'}, 'b', 'c')
675
679
utils_mock.side_effect = processutils.ProcessExecutionError
679
683
'tgt_file', 'path/to/deployiso',
680
684
'path/to/kernel',
681
685
'path/to/ramdisk')
682
umount_mock.assert_called_once_wth('mountdir')
686
umount_mock.assert_called_once_with('mountdir')
684
@mock.patch.object(images, '_create_root_fs')
685
@mock.patch.object(utils, 'write_to_file')
686
@mock.patch.object(utils, 'tempdir')
687
@mock.patch.object(utils, 'execute')
688
@mock.patch.object(images, '_generate_cfg')
688
@mock.patch.object(images, '_create_root_fs', autospec=True)
689
@mock.patch.object(utils, 'write_to_file', autospec=True)
690
@mock.patch.object(utils, 'tempdir', autospec=True)
691
@mock.patch.object(utils, 'execute', autospec=True)
692
@mock.patch.object(images, '_generate_cfg', autospec=True)
689
693
def test_create_isolinux_image_bios_mkisofs_fails(self,
702
706
'tgt_file', 'path/to/kernel',
703
707
'path/to/ramdisk')
705
@mock.patch.object(images, 'create_isolinux_image_for_uefi')
706
@mock.patch.object(images, 'fetch')
707
@mock.patch.object(utils, 'tempdir')
709
@mock.patch.object(images, 'create_isolinux_image_for_uefi', autospec=True)
710
@mock.patch.object(images, 'fetch', autospec=True)
711
@mock.patch.object(utils, 'tempdir', autospec=True)
708
712
def test_create_boot_iso_for_uefi(self, tempdir_mock, fetch_images_mock,
709
713
create_isolinux_mock):
710
714
mock_file_handle = mock.MagicMock(spec=file)
727
731
'tmpdir/deploy_iso-uuid', 'tmpdir/kernel-uuid',
728
732
'tmpdir/ramdisk-uuid', params)
730
@mock.patch.object(images, 'create_isolinux_image_for_bios')
731
@mock.patch.object(images, 'fetch')
732
@mock.patch.object(utils, 'tempdir')
734
@mock.patch.object(images, 'create_isolinux_image_for_bios', autospec=True)
735
@mock.patch.object(images, 'fetch', autospec=True)
736
@mock.patch.object(utils, 'tempdir', autospec=True)
733
737
def test_create_boot_iso_for_bios(self, tempdir_mock, fetch_images_mock,
734
738
create_isolinux_mock):
735
739
mock_file_handle = mock.MagicMock(spec=file)
744
748
'tmpdir/kernel-uuid')
745
749
fetch_images_mock.assert_any_call('ctx', 'ramdisk-uuid',
746
750
'tmpdir/ramdisk-uuid')
747
fetch_images_mock.assert_not_called_with('ctx', 'deploy_iso-uuid',
748
'tmpdir/deploy_iso-uuid')
751
# Note (NobodyCam): the orginal assert asserted that fetch_images
752
# was not called with parameters, this did not
753
# work, So I instead assert that there were only
754
# Two calls to the mock validating the above
756
self.assertEqual(2, fetch_images_mock.call_count)
750
758
params = ['root=UUID=root-uuid', 'kernel-params']
751
759
create_isolinux_mock.assert_called_once_with('output_file',
753
761
'tmpdir/ramdisk-uuid',
756
@mock.patch.object(images, 'create_isolinux_image_for_bios')
757
@mock.patch.object(images, 'fetch')
758
@mock.patch.object(utils, 'tempdir')
764
@mock.patch.object(images, 'create_isolinux_image_for_bios', autospec=True)
765
@mock.patch.object(images, 'fetch', autospec=True)
766
@mock.patch.object(utils, 'tempdir', autospec=True)
759
767
def test_create_boot_iso_for_bios_with_no_boot_mode(self, tempdir_mock,
760
768
fetch_images_mock,
761
769
create_isolinux_mock):