From dd37eac34239345734ff6af1df0ea2541a680b59 Mon Sep 17 00:00:00 2001 From: Alexander Gordeev Date: Wed, 15 Jul 2015 16:21:39 +0300 Subject: [PATCH] IBP: add do_bootloader unit tests for fuel-agent First portion of its tests was introduced by https://review.openstack.org/#/c/201639/ Change-Id: I69d5731a6fe3c1979f89fece660fab07bc8c6e09 Closes-Bug: #1449609 --- fuel_agent/manager.py | 6 +- fuel_agent/tests/test_manager.py | 99 ++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/fuel_agent/manager.py b/fuel_agent/manager.py index 2d81056..cbdd38b 100644 --- a/fuel_agent/manager.py +++ b/fuel_agent/manager.py @@ -393,8 +393,6 @@ class Manager(object): continue fu.umount_fs(chroot + fs.mount) - # TODO(kozhukalov): write tests for this method - # https://bugs.launchpad.net/fuel/+bug/1449609 def do_bootloader(self): LOG.debug('--- Installing bootloader (do_bootloader) ---') chroot = '/tmp/target' @@ -406,6 +404,10 @@ class Manager(object): 'blkid', '-o', 'value', '-s', 'UUID', fs.device, check_exit_code=[0])[0].strip() + if '/' not in mount2uuid: + raise errors.WrongPartitionSchemeError( + 'Error: device with / mountpoint has not been found') + grub = self.driver.grub grub.version = gu.guess_grub_version(chroot=chroot) diff --git a/fuel_agent/tests/test_manager.py b/fuel_agent/tests/test_manager.py index f1d20f3..27bffc1 100644 --- a/fuel_agent/tests/test_manager.py +++ b/fuel_agent/tests/test_manager.py @@ -47,6 +47,105 @@ class TestManager(test_base.BaseTestCase): mock_lbd.return_value = test_nailgun.LIST_BLOCK_DEVICES_SAMPLE self.mgr = manager.Manager(test_nailgun.PROVISION_SAMPLE_DATA) + @mock.patch('fuel_agent.manager.open', + create=True, new_callable=mock.mock_open) + @mock.patch('fuel_agent.manager.gu', create=True) + @mock.patch('fuel_agent.manager.utils', create=True) + @mock.patch.object(manager.Manager, 'mount_target') + @mock.patch.object(manager.Manager, 'umount_target') + def test_do_bootloader_grub1_kernel_initrd_guessed(self, mock_umount, + mock_mount, mock_utils, + mock_gu, mock_open): + mock_utils.execute.return_value = ('', '') + mock_gu.guess_grub_version.return_value = 1 + # grub has kernel_name and initrd_name both set to None + self.mgr.driver.grub.kernel_name = None + self.mgr.driver.grub.initrd_name = None + self.mgr.driver.grub.kernel_params = 'fake_kernel_params' + self.mgr.driver.grub.kernel_regexp = 'fake_kernel_regexp' + self.mgr.driver.grub.initrd_regexp = 'fake_initrd_regexp' + mock_gu.guess_kernel.return_value = 'guessed_kernel' + mock_gu.guess_initrd.return_value = 'guessed_initrd' + self.mgr.do_bootloader() + self.assertFalse(mock_gu.grub2_cfg.called) + self.assertFalse(mock_gu.grub2_install.called) + mock_gu.grub1_cfg.assert_called_once_with( + kernel_params='fake_kernel_params root=UUID= ', + initrd='guessed_initrd', kernel='guessed_kernel', + chroot='/tmp/target') + mock_gu.grub1_install.assert_called_once_with( + ['/dev/sda', '/dev/sdb', '/dev/sdc'], + '/dev/sda3', chroot='/tmp/target') + mock_gu.guess_initrd.assert_called_once_with( + regexp='fake_initrd_regexp', chroot='/tmp/target') + mock_gu.guess_kernel.assert_called_once_with( + regexp='fake_kernel_regexp', chroot='/tmp/target') + + @mock.patch('fuel_agent.manager.open', + create=True, new_callable=mock.mock_open) + @mock.patch('fuel_agent.manager.gu', create=True) + @mock.patch('fuel_agent.manager.utils', create=True) + @mock.patch.object(manager.Manager, 'mount_target') + @mock.patch.object(manager.Manager, 'umount_target') + def test_do_bootloader_grub1_kernel_initrd_set(self, mock_umount, + mock_mount, mock_utils, + mock_gu, mock_open): + mock_utils.execute.return_value = ('', '') + mock_gu.guess_grub_version.return_value = 1 + self.mgr.driver.grub.kernel_params = 'fake_kernel_params' + # grub has kernel_name and initrd_name set + self.mgr.driver.grub.kernel_name = 'kernel_name' + self.mgr.driver.grub.initrd_name = 'initrd_name' + self.mgr.do_bootloader() + self.assertFalse(mock_gu.grub2_cfg.called) + self.assertFalse(mock_gu.grub2_install.called) + mock_gu.grub1_cfg.assert_called_once_with( + kernel_params='fake_kernel_params root=UUID= ', + initrd='initrd_name', kernel='kernel_name', chroot='/tmp/target') + mock_gu.grub1_install.assert_called_once_with( + ['/dev/sda', '/dev/sdb', '/dev/sdc'], + '/dev/sda3', chroot='/tmp/target') + self.assertFalse(mock_gu.guess_initrd.called) + self.assertFalse(mock_gu.guess_kernel.called) + + @mock.patch('fuel_agent.objects.bootloader.Grub', autospec=True) + @mock.patch('fuel_agent.manager.open', + create=True, new_callable=mock.mock_open) + @mock.patch('fuel_agent.manager.gu', create=True) + @mock.patch('fuel_agent.manager.utils', create=True) + @mock.patch.object(manager.Manager, 'mount_target') + @mock.patch.object(manager.Manager, 'umount_target') + def test_do_bootloader_rootfs_uuid(self, mock_umount, mock_mount, + mock_utils, mock_gu, mock_open, + mock_grub): + def _fake_uuid(*args, **kwargs): + if args[5] == '/dev/mapper/os-root': + return ('FAKE_ROOTFS_UUID', None) + else: + return ('FAKE_UUID', None) + mock_utils.execute.side_effect = _fake_uuid + mock_gu.guess_grub_version.return_value = 2 + mock_grub.kernel_name = 'fake_kernel_name' + mock_grub.initrd_name = 'fake_initrd_name' + mock_grub.kernel_params = 'fake_kernel_params' + self.mgr.driver._grub = mock_grub + self.mgr.do_bootloader() + mock_grub.append_kernel_params.assert_called_once_with( + 'root=UUID=FAKE_ROOTFS_UUID ') + self.assertEqual(2, mock_grub.version) + + @mock.patch('fuel_agent.manager.utils', create=True) + @mock.patch.object(manager.Manager, 'mount_target') + def test_do_bootloader_rootfs_not_found(self, mock_umount, mock_utils): + mock_utils.execute.return_value = ('fake', 'fake') + self.mgr.driver._partition_scheme = objects.PartitionScheme() + self.mgr.driver.partition_scheme.add_fs( + device='fake', mount='/boot', fs_type='ext2') + self.mgr.driver.partition_scheme.add_fs( + device='fake', mount='swap', fs_type='swap') + self.assertRaises(errors.WrongPartitionSchemeError, + self.mgr.do_bootloader) + @mock.patch('fuel_agent.manager.open', create=True, new_callable=mock.mock_open) @mock.patch('fuel_agent.manager.gu', create=True)