def test_install_hbase_master_with_version(self, get_provider): from ambari_commons.os_check import OSConst from ambari_commons.repo_manager import ManagerFactory pkg_manager = ManagerFactory.get_new_instance(OSConst.REDHAT_FAMILY) with patch.object(pkg_manager, "all_packages") as all_packages,\ patch.object(pkg_manager, "available_packages") as available_packages, \ patch.object(pkg_manager, "installed_packages") as installed_packages: all_packages.return_value = [["hbase_2_3_0_1_1234", "1.0", "testrepo"]] available_packages.return_value = [["hbase_2_3_0_1_1234", "1.0", "testrepo"]] installed_packages.return_value = [["hbase_2_3_0_1_1234", "1.0", "testrepo"]] get_provider.return_value = pkg_manager config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/hbase_with_phx.json" with open(config_file, "r") as f: json_content = json.load(f) version = '2.3.0.1-1234' # the json file is not a "well formed" install command json_content['roleCommand'] = 'INSTALL' json_content['commandParams']['version'] = version json_content['commandParams']['package_list'] = "[{\"name\":\"hbase_${stack_version}\",\"condition\":\"\",\"skipUpgrade\":false}]" self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/hbase_master.py", classname = "HbaseMaster", command = "install", config_dict = json_content, stack_version = self.STACK_VERSION, target = RMFTestCase.TARGET_COMMON_SERVICES, try_install=True, os_type=('Redhat', '6.4', 'Final'), checked_call_mocks = [(0, "OK.", "")], available_packages_in_repos = ['hbase_2_3_0_1_1234'], ) # only assert that the correct package is trying to be installed self.assertResourceCalled('Package', 'hbase_2_3_0_1_1234', retry_count=5, retry_on_repo_unavailability=False) def test_configure_default(self): self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/hbase_master.py", classname = "HbaseMaster", command = "configure", config_file="default.json", stack_version = self.STACK_VERSION, target = RMFTestCase.TARGET_COMMON_SERVICES ) self.assert_configure_default() self.assertNoMoreResources()
class TestPackageResource(TestCase): @patch.object(ac_shell, "process_executor") @patch.object(ManagerFactory, "get", new=MagicMock(return_value=ManagerFactory.get_new_instance( OSConst.REDHAT_FAMILY))) def test_action_install_pattern_rhel(self, shell_mock): shell_mock.return_value.__enter__.return_value = [] sys.modules['rpm'] = MagicMock() sys.modules['rpm'].TransactionSet.return_value = MagicMock() sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [ { 'name': 'some_packag' } ] with Environment('/') as env: Package("some_package*", logoutput=False) self.assertEquals(shell_mock.call_args_list[0][0][0], [ '/usr/bin/yum', '-d', '0', '-e', '0', '-y', 'install', 'some_package*' ]) @patch.object(ac_shell, "process_executor") @patch.object(ManagerFactory, "get", new=MagicMock(return_value=ManagerFactory.get_new_instance( OSConst.REDHAT_FAMILY))) def test_action_install_pattern_installed_rhel(self, shell_mock): shell_mock.return_value = (0, '') sys.modules['yum'] = MagicMock() sys.modules['yum'].YumBase.return_value = MagicMock() sys.modules['yum'].YumBase.return_value.rpmdb = MagicMock() sys.modules[ 'yum'].YumBase.return_value.rpmdb.simplePkgList.return_value = [ ('some_package_1_2_3', ) ] with Environment('/') as env: Package("some_package*", logoutput=False) self.assertEqual(shell_mock.call_count, 0, "shell.checked_call shouldn't be called") @patch.object(ac_shell, "process_executor") @patch.object( ManagerFactory, "get", new=MagicMock( return_value=ManagerFactory.get_new_instance(OSConst.SUSE_FAMILY))) def test_action_install_pattern_suse(self, shell_mock, call_mock): call_mock.side_effect = [ (0, None), (0, "Loading repository data...\nReading installed packages...\n\nS | Name\n--+-----\n | Pack" ) ] with Environment('/') as env: Package("some_package*", ) call_mock.assert_has_calls([ call( "installed_pkgs=`rpm -qa 'some_package*'` ; [ ! -z \"$installed_pkgs\" ]" ), call( "zypper --non-interactive search --type package --uninstalled-only --match-exact 'some_package*'" ) ]) self.assertEquals(shell_mock.call_args_list[0][0][0], [ '/usr/bin/zypper', '--quiet', 'install', '--auto-agree-with-licenses', '--no-confirm', 'some_package*' ]) @patch.object(ac_shell, "process_executor") @patch.object( ManagerFactory, "get", new=MagicMock( return_value=ManagerFactory.get_new_instance(OSConst.SUSE_FAMILY))) def test_action_install_pattern_suse(self, shell_mock): sys.modules['rpm'] = MagicMock() sys.modules['rpm'].TransactionSet.return_value = MagicMock() sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [ { 'name': 'some_packagetest' } ] with Environment('/') as env: Package("some_package*", ) self.assertEqual(shell_mock.call_count, 0, "shell.checked_call shouldn't be called") @patch.object(ac_shell, "process_executor") @patch.object(ManagerFactory, "get", new=MagicMock(return_value=ManagerFactory.get_new_instance( OSConst.REDHAT_FAMILY))) def test_action_install_existent_rhel(self, shell_mock): sys.modules['rpm'] = MagicMock() sys.modules['rpm'].TransactionSet.return_value = MagicMock() sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [ { 'name': 'some_package' } ] with Environment('/') as env: Package("some_package", ) self.assertFalse(shell_mock.call_count > 0) @patch.object(ac_shell, "process_executor") @patch.object( ManagerFactory, "get", new=MagicMock( return_value=ManagerFactory.get_new_instance(OSConst.SUSE_FAMILY))) def test_action_install_existent_suse(self, shell_mock): sys.modules['rpm'] = MagicMock() sys.modules['rpm'].TransactionSet.return_value = MagicMock() sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [ { 'name': 'some_package' } ] with Environment('/') as env: Package("some_package", ) self.assertFalse(shell_mock.call_count > 0) @patch.object(ac_shell, "process_executor") @patch.object(ManagerFactory, "get", new=MagicMock(return_value=ManagerFactory.get_new_instance( OSConst.REDHAT_FAMILY))) def test_action_remove_rhel(self, shell_mock): sys.modules['rpm'] = MagicMock() sys.modules['rpm'].TransactionSet.return_value = MagicMock() sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [ { 'name': 'some_package' } ] with Environment('/') as env: Package("some_package", action="remove", logoutput=False) self.assertEquals(shell_mock.call_args_list[0][0][0], [ '/usr/bin/yum', '-d', '0', '-e', '0', '-y', 'erase', 'some_package' ]) @replace_underscores def func_to_test(self, name): return name def testReplaceUnderscore(self): self.assertEqual("-", self.func_to_test("_")) self.assertEqual("hadoop-x-x-x-*", self.func_to_test("hadoop_x_x_x-*")) self.assertEqual("hadoop", self.func_to_test("hadoop"))