diff --git a/coriolis/tests/osmorphing/test_debian.py b/coriolis/tests/osmorphing/test_debian.py index d4c9a0c4..5bac1ec8 100644 --- a/coriolis/tests/osmorphing/test_debian.py +++ b/coriolis/tests/osmorphing/test_debian.py @@ -211,6 +211,33 @@ def test_set_net_config_no_dhcp( mock_write_file_sudo.assert_not_called() mock_exec_cmd_chroot.assert_not_called() + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_get_installed_packages(self, mock_exec_cmd_chroot): + mock_exec_cmd_chroot.return_value = \ + "package1\npackage2".encode('utf-8') + + self.morpher.get_installed_packages() + + self.assertEqual( + self.morpher.installed_packages, + ['package1', 'package2'] + ) + mock_exec_cmd_chroot.assert_called_once_with( + "dpkg-query -f '${binary:Package}\\n' -W") + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_get_installed_packages_none(self, mock_exec_cmd_chroot): + mock_exec_cmd_chroot.side_effect = exception.CoriolisException() + + self.morpher.get_installed_packages() + + self.assertEqual( + self.morpher.installed_packages, + [] + ) + mock_exec_cmd_chroot.assert_called_once_with( + "dpkg-query -f '${binary:Package}\\n' -W") + @mock.patch.object(base.BaseLinuxOSMorphingTools, 'pre_packages_install') @mock.patch.object(debian.BaseDebianMorphingTools, '_exec_cmd_chroot') def test_pre_packages_install(self, mock_exec_cmd_chroot, diff --git a/coriolis/tests/osmorphing/test_manager.py b/coriolis/tests/osmorphing/test_manager.py index fd425b24..2012e413 100644 --- a/coriolis/tests/osmorphing/test_manager.py +++ b/coriolis/tests/osmorphing/test_manager.py @@ -153,6 +153,8 @@ def check_os_supported(cls, detected_os_info): self.assertIsNone(result) class MockOSMorphingToolsClass: + installed_packages = [] + def __init__(self, *args, **kwargs): pass @@ -174,6 +176,9 @@ def post_packages_uninstall(self, packages_remove): def pre_packages_install(self, packages_add): pass + def get_installed_packages(self): + pass + def set_net_config(self, nics_info, dhcp): pass diff --git a/coriolis/tests/osmorphing/test_redhat.py b/coriolis/tests/osmorphing/test_redhat.py index c624969e..e916896b 100644 --- a/coriolis/tests/osmorphing/test_redhat.py +++ b/coriolis/tests/osmorphing/test_redhat.py @@ -364,27 +364,33 @@ def test_set_net_config_no_dhcp( mock_get_net_ifaces_info.return_value) @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') - def test_has_package_installed(self, mock_exec_cmd_chroot): - result = self.morphing_tools._has_package_installed("mock_package") + def test_get_installed_packages(self, mock_exec_cmd_chroot): + mock_exec_cmd_chroot.return_value = \ + "package1\npackage2".encode('utf-8') + + self.morphing_tools.get_installed_packages() self.assertEqual( - True, - result + self.morphing_tools.installed_packages, + ['package1', 'package2'] ) - mock_exec_cmd_chroot.assert_called_once_with("rpm -q mock_package") + mock_exec_cmd_chroot.assert_called_once_with( + 'rpm -qa --qf "%{NAME}\\n"') - mock_exec_cmd_chroot.reset_mock() + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_get_installed_packages_none(self, mock_exec_cmd_chroot): mock_exec_cmd_chroot.side_effect = exception.CoriolisException() with self.assertLogs( 'coriolis.osmorphing.redhat', level=logging.DEBUG): - result = self.morphing_tools._has_package_installed("mock_package") + self.morphing_tools.get_installed_packages() self.assertEqual( - False, - result + self.morphing_tools.installed_packages, + [] ) - mock_exec_cmd_chroot.assert_called_once_with("rpm -q mock_package") + mock_exec_cmd_chroot.assert_called_once_with( + 'rpm -qa --qf "%{NAME}\\n"') @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') def test__yum_install(self, mock_exec_cmd_chroot): @@ -472,15 +478,12 @@ def test__find_yum_repos_not_found(self, mock_read_file, mock_list_dir): with self.assertLogs('coriolis.osmorphing.redhat', level=logging.WARN): self.morphing_tools._find_yum_repos(repos_to_enable) - @mock.patch.object(redhat.BaseRedHatMorphingTools, - '_has_package_installed') @mock.patch.object(redhat.BaseRedHatMorphingTools, '_yum_install') @mock.patch.object(redhat.BaseRedHatMorphingTools, '_yum_clean_all') @mock.patch.object(base.BaseLinuxOSMorphingTools, 'pre_packages_install') def test_pre_packages_install(self, mock_pre_packages_install, - mock_yum_clean_all, mock_yum_install, - mock_has_package_installed): - mock_has_package_installed.return_value = False + mock_yum_clean_all, mock_yum_install): + self.morphing_tools.installed_packages = [] self.morphing_tools.pre_packages_install(self.package_names) @@ -488,15 +491,12 @@ def test_pre_packages_install(self, mock_pre_packages_install, mock_yum_clean_all.assert_called_once() mock_yum_install.assert_called_once_with(['grubby']) - @mock.patch.object(redhat.BaseRedHatMorphingTools, - '_has_package_installed') @mock.patch.object(redhat.BaseRedHatMorphingTools, '_yum_install') @mock.patch.object(redhat.BaseRedHatMorphingTools, '_yum_clean_all') @mock.patch.object(base.BaseLinuxOSMorphingTools, 'pre_packages_install') def test_pre_packages_install_has_grubby( - self, mock_pre_packages_install, mock_yum_clean_all, mock_yum_install, - mock_has_package_installed): - mock_has_package_installed.return_value = True + self, mock_pre_packages_install, mock_yum_clean_all, mock_yum_install): + self.morphing_tools.installed_packages = ['grubby'] self.morphing_tools.pre_packages_install(self.package_names) diff --git a/coriolis/tests/osmorphing/test_suse.py b/coriolis/tests/osmorphing/test_suse.py index 765d5dff..f1ca436b 100644 --- a/coriolis/tests/osmorphing/test_suse.py +++ b/coriolis/tests/osmorphing/test_suse.py @@ -76,6 +76,35 @@ def test_check_os_not_supported(self): self.assertFalse(result) + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_get_installed_packages(self, mock_exec_cmd_chroot): + mock_exec_cmd_chroot.return_value = \ + "package1\npackage2".encode('utf-8') + + self.morphing_tools.get_installed_packages() + + self.assertEqual( + self.morphing_tools.installed_packages, + ['package1', 'package2'] + ) + mock_exec_cmd_chroot.assert_called_once_with( + 'rpm -qa --qf "%{NAME}\\n"') + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_get_installed_packages_none(self, mock_exec_cmd_chroot): + mock_exec_cmd_chroot.side_effect = exception.CoriolisException() + + with self.assertLogs( + 'coriolis.osmorphing.suse', level=logging.DEBUG): + self.morphing_tools.get_installed_packages() + + self.assertEqual( + self.morphing_tools.installed_packages, + [] + ) + mock_exec_cmd_chroot.assert_called_once_with( + 'rpm -qa --qf "%{NAME}\\n"') + @mock.patch.object( suse.BaseSUSEMorphingTools, '_get_grub2_cfg_location' )