~ubuntu-branches/ubuntu/vivid/ironic/vivid-proposed

« back to all changes in this revision

Viewing changes to ironic/tests/test_images.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-04-17 09:28:31 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20150417092831-wu2awfbqomnzpeim
Tags: 2015.1~rc1-0ubuntu1
* New upstream milestone release:
  - d/control: Align with upstream dependencies
  - d/p/fix-requirements.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    class FakeImgInfo(object):
41
41
        pass
42
42
 
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()
50
50
 
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'),
 
52
                       autospec=True)
 
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')
61
62
 
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',
67
68
                                             run_as_root=False)
68
69
 
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')
83
84
 
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')
96
97
 
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')
113
114
 
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))
124
125
 
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))
136
137
 
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')
161
162
 
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,
167
168
                                                   unlink_mock):
179
180
                                                   'path.converted', 'raw')
180
181
        unlink_mock.assert_called_once_with('path_tmp')
181
182
 
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,
185
186
                                             rename_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')
195
196
 
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')
208
209
 
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)
217
218
 
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)
226
227
 
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)
240
241
 
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)
253
254
 
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,
257
258
                                                      mock_gip):
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'])
267
268
 
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,
271
272
                                                       mock_gip):
272
273
        mock_igi.return_value = False
280
281
 
281
282
class FsImageTestCase(base.TestCase):
282
283
 
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):
289
290
 
295
296
                'a3': 'sub_dir/b3'}
296
297
 
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')
310
311
 
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):
320
321
 
343
344
        create_root_fs_mock.assert_called_once_with('tempdir', files_info)
344
345
        umount_mock.assert_called_once_with('tempdir')
345
346
 
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):
354
355
 
363
364
 
364
365
        umount_mock.assert_called_once_with('tempdir')
365
366
 
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):
368
369
 
369
370
        dd_mock.side_effect = processutils.ProcessExecutionError
370
371
        self.assertRaises(exception.ImageCreationFailed,
371
372
                          images.create_vfat_image, 'tgt_file')
372
373
 
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,
377
378
                                          tempdir_mock):
378
379
 
384
385
        self.assertRaises(exception.ImageCreationFailed,
385
386
                          images.create_vfat_image, 'tgt_file')
386
387
 
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):
395
396
 
401
402
        self.assertRaises(exception.ImageCreationFailed,
402
403
                          images.create_vfat_image, 'tgt_file')
403
404
 
404
 
    @mock.patch.object(utils, 'umount')
 
405
    @mock.patch.object(utils, 'umount', autospec=True)
405
406
    def test__umount_without_raise(self, umount_mock):
406
407
 
407
408
        umount_mock.side_effect = processutils.ProcessExecutionError
423
424
        self.assertEqual(expected_cfg, cfg)
424
425
 
425
426
    def test__generate_grub_cfg(self):
426
 
 
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"
431
 
                        "initrd /initrd\n"
 
429
        expected_cfg = ("set default=0\n"
 
430
                        "set timeout=5\n"
 
431
                        "set hidden_timeout_quiet=false\n"
 
432
                        "\n"
 
433
                        "menuentry \"boot_partition\" {\n"
 
434
                        "linuxefi /vmlinuz key1=value1 key2 --\n"
 
435
                        "initrdefi /initrd\n"
432
436
                        "}")
433
437
 
434
438
        cfg = images._generate_cfg(kernel_params,
436
440
                                   options)
437
441
        self.assertEqual(expected_cfg, cfg)
438
442
 
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'])
450
454
 
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')
455
459
 
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,
462
466
                                                  umount_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'])
467
471
 
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')
475
479
 
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,
482
486
                                                 umount_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'])
488
492
 
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')
496
500
 
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')
503
507
 
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,
524
528
        cfg_file = 'tmpdir/isolinux/isolinux.cfg'
525
529
        grubcfg = "grubcfg"
526
530
        grub_file = 'tmpdir/relpath/to/grub.cfg'
527
 
        gen_cfg_mock.side_effect = [cfg, grubcfg]
 
531
        gen_cfg_mock.side_effect = iter([cfg, grubcfg])
528
532
 
529
533
        params = ['a=b', 'c']
530
534
        isolinux_options = {'kernel': '/vmlinuz',
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,
545
 
                                    mock_file_handle1]
 
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)
548
552
 
566
570
                 '-no-emul-boot', '-o', 'tgt_file', 'tmpdir')
567
571
        umount_mock.assert_called_once_with('mountdir')
568
572
 
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,
575
579
                                   execute_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')
611
615
 
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,
618
622
                                                     utils_mock,
619
623
                                                     tempdir_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,
628
 
                                    mock_file_handle1]
 
631
        tempdir_mock.side_effect = iter(
 
632
            [mock_file_handle, mock_file_handle1])
629
633
        create_root_fs_mock.side_effect = IOError
630
634
 
631
635
        self.assertRaises(exception.ImageCreationFailed,
635
639
                          'path/to/ramdisk')
636
640
        umount_mock.assert_called_once_with('mountdir')
637
641
 
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,
643
647
                                                     utils_mock,
644
648
                                                     tempdir_mock,
650
654
                          'tgt_file', 'path/to/kernel',
651
655
                          'path/to/ramdisk')
652
656
 
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,
661
665
                                                 gen_cfg_mock,
662
666
                                                 mount_mock,
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,
673
 
                                    mock_file_handle1]
 
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
676
680
 
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')
683
687
 
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,
690
694
                                                      gen_cfg_mock,
691
695
                                                      utils_mock,
702
706
                          'tgt_file', 'path/to/kernel',
703
707
                          'path/to/ramdisk')
704
708
 
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)
729
733
 
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
 
755
        #                   asserts.
 
756
        self.assertEqual(2, fetch_images_mock.call_count)
749
757
 
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',
754
762
                                                     params)
755
763
 
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):
778
786
                                                     'tmpdir/ramdisk-uuid',
779
787
                                                     params)
780
788
 
781
 
    @mock.patch.object(image_service, 'get_image_service')
 
789
    @mock.patch.object(image_service, 'get_image_service', autospec=True)
782
790
    def test_get_glance_image_properties_no_such_prop(self,
783
791
                                                      image_service_mock):
784
792
 
796
804
                          'p2': 'v2',
797
805
                          'p3': None}, ret_val)
798
806
 
799
 
    @mock.patch.object(image_service, 'get_image_service')
 
807
    @mock.patch.object(image_service, 'get_image_service', autospec=True)
800
808
    def test_get_glance_image_properties_default_all(
801
809
            self, image_service_mock):
802
810
 
812
820
        self.assertEqual({'p1': 'v1',
813
821
                          'p2': 'v2'}, ret_val)
814
822
 
815
 
    @mock.patch.object(image_service, 'get_image_service')
 
823
    @mock.patch.object(image_service, 'get_image_service', autospec=True)
816
824
    def test_get_glance_image_properties_with_prop_subset(
817
825
            self, image_service_mock):
818
826
 
830
838
        self.assertEqual({'p1': 'v1',
831
839
                          'p3': 'v3'}, ret_val)
832
840
 
833
 
    @mock.patch.object(image_service, 'GlanceImageService')
 
841
    @mock.patch.object(image_service, 'GlanceImageService', autospec=True)
834
842
    def test_get_temp_url_for_glance_image(self, image_service_mock):
835
843
 
836
844
        direct_url = 'swift+http://host/v1/AUTH_xx/con/obj'