Updated Branches: refs/heads/trunk 88f513259 -> 0672abab2
AMBARI-3554. Host cleanup commands should allow silent execution. Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/0672abab Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/0672abab Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/0672abab Branch: refs/heads/trunk Commit: 0672abab29adfc2264a9d3ef9e3621959687b187 Parents: 88f5132 Author: Sumit Mohanty <[email protected]> Authored: Tue Oct 22 15:26:38 2013 -0700 Committer: Sumit Mohanty <[email protected]> Committed: Tue Oct 22 15:58:03 2013 -0700 ---------------------------------------------------------------------- .../src/main/python/ambari_agent/HostCleanup.py | 17 +++++--- ambari-agent/src/test/python/TestHostCleanup.py | 42 +++++++++++++++++--- 2 files changed, 47 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/0672abab/ambari-agent/src/main/python/ambari_agent/HostCleanup.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/ambari_agent/HostCleanup.py b/ambari-agent/src/main/python/ambari_agent/HostCleanup.py index 6d07691..2f56376 100644 --- a/ambari-agent/src/main/python/ambari_agent/HostCleanup.py +++ b/ambari-agent/src/main/python/ambari_agent/HostCleanup.py @@ -470,6 +470,10 @@ def main(): parser.add_option("-k", "--skip", dest="skip", help="(packages|users|directories|repositories|processes|alternatives)." + \ " Use , as separator.") + parser.add_option("-s", "--silent", + action="store_true", dest="silent", default=False, + help="Silently accepts default prompt values") + (options, args) = parser.parse_args() # set output file @@ -495,12 +499,13 @@ def main(): if not is_root: raise RuntimeError('HostCleanup needs to be run as root.') - if "users" not in SKIP_LIST: - delete_users = get_YN_input('You have elected to remove all users as well. If it is not intended then use ' - 'option --skip \"users\". Do you want to continue [y/n] (n)', False) - if not delete_users: - print 'Exiting. Use option --skip="users" to skip deleting users' - sys.exit(1) + if not options.silent: + if "users" not in SKIP_LIST: + delete_users = get_YN_input('You have elected to remove all users as well. If it is not intended then use ' + 'option --skip \"users\". Do you want to continue [y/n] (y)', True) + if not delete_users: + print 'Exiting. Use option --skip="users" to skip deleting users' + sys.exit(1) hostcheckfile = options.inputfile propMap = h.read_host_check_file(hostcheckfile) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/0672abab/ambari-agent/src/test/python/TestHostCleanup.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/TestHostCleanup.py b/ambari-agent/src/test/python/TestHostCleanup.py index 401aee9..3534195 100644 --- a/ambari-agent/src/test/python/TestHostCleanup.py +++ b/ambari-agent/src/test/python/TestHostCleanup.py @@ -98,11 +98,12 @@ created = 2013-07-02 20:39:22.162757""" sys.stdout = sys.__stdout__ class HostCleanupOptions: - def __init__(self, outputfile, inputfile, skip, verbose): + def __init__(self, outputfile, inputfile, skip, verbose, silent): self.outputfile = outputfile self.inputfile = inputfile self.skip = skip - self.verbose = False + self.verbose = verbose + self.silent = silent @patch.object(HostCleanup, 'get_YN_input') @patch.object(HostCleanup.HostCleanup, 'do_cleanup') @@ -114,7 +115,8 @@ created = 2013-07-02 20:39:22.162757""" @patch.object(optparse.OptionParser, 'parse_args') def test_options(self, parser_mock, file_handler_mock, logging_mock, read_host_check_file_mock, set_formatter_mock, user_root_mock, do_cleanup_mock, get_yn_input_mock): - parser_mock.return_value = (TestHostCleanup.HostCleanupOptions('/someoutputfile', '/someinputfile', '', False), []) + parser_mock.return_value = (TestHostCleanup.HostCleanupOptions('/someoutputfile', '/someinputfile', '', False, + False), []) file_handler_mock.return_value = logging.FileHandler('') # disable creating real file user_root_mock.return_value = True get_yn_input_mock.return_value = True @@ -128,8 +130,36 @@ created = 2013-07-02 20:39:22.162757""" logging_mock.assert_called_with(level=logging.INFO) # test --in read_host_check_file_mock.assert_called_with('/someinputfile') - - + self.assertTrue(get_yn_input_mock.called) + + + @patch.object(HostCleanup, 'get_YN_input') + @patch.object(HostCleanup.HostCleanup, 'do_cleanup') + @patch.object(HostCleanup.HostCleanup, 'is_current_user_root') + @patch.object(logging.FileHandler, 'setFormatter') + @patch.object(HostCleanup.HostCleanup,'read_host_check_file') + @patch.object(logging,'basicConfig') + @patch.object(logging, 'FileHandler') + @patch.object(optparse.OptionParser, 'parse_args') + def test_options_silent(self, parser_mock, file_handler_mock, logging_mock, read_host_check_file_mock, + set_formatter_mock, user_root_mock, do_cleanup_mock, get_yn_input_mock): + parser_mock.return_value = (TestHostCleanup.HostCleanupOptions('/someoutputfile', '/someinputfile', '', False, + True), []) + file_handler_mock.return_value = logging.FileHandler('') # disable creating real file + user_root_mock.return_value = True + get_yn_input_mock.return_value = True + HostCleanup.main() + + # test --out + file_handler_mock.assert_called_with('/someoutputfile') + # test --skip + self.assertEquals([''],HostCleanup.SKIP_LIST) + #test --verbose + logging_mock.assert_called_with(level=logging.INFO) + # test --in + read_host_check_file_mock.assert_called_with('/someinputfile') + self.assertFalse(get_yn_input_mock.called) + @patch.object(HostCleanup.HostCleanup, 'do_erase_alternatives') @patch.object(HostCleanup.HostCleanup, 'find_repo_files_for_repos') @patch.object(HostCleanup.HostCleanup, 'get_os_type') @@ -396,4 +426,4 @@ name=sd des derft 3.1 sys.stdout = sys.__stdout__ if __name__ == "__main__": - unittest.main(verbosity=2) + unittest.main()
