def _exit_cleanup_hook(self): """ Disconnect all Fabric connections in addition to shutting down the logging. """ disconnect_all() Application._exit_cleanup_hook(self)
def _handle_error(self): """ Handle KeyboardInterrupt in a special way: don't indicate that it's an error. Returns: Nothing """ self._log_exception() if isinstance(self.exception, KeyboardInterrupt): print >> sys.stderr, "Stopped." sys.exit(0) else: Application._handle_error(self)
def test_warn_utils_prints_out_message(self, logger_mock, log_conf_mock, filesystem_mock): with Application(APPLICATION_NAME): fabric.utils.warn("Test warning.") logger_mock.warn.assert_has_calls([ call('Test warning.\n\nNone\n'), ]) self.assertEqual('\nWarning: Test warning.\n\n', self.test_stderr.getvalue())
def test_log_file_is_created(self): with Application(APPLICATION_NAME): pass log_file_path = os.path.join(get_log_directory(), APPLICATION_NAME + '.log') self.assertTrue(os.path.exists(log_file_path), 'Expected log file does not exist') self.assertTrue( os.path.getsize(log_file_path) > 0, 'Log file is empty')
def test_configures_absolute_path_to_log_file(self, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.return_value = True log_file_path = '/tmp/bar.log' with Application(APPLICATION_NAME, log_file_path=log_file_path): pass self.__assert_logging_setup_with_file(log_file_path, filesystem_mock, logging_mock)
def test_configures_custom_log_file(self, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.return_value = True log_file_path = 'bar.log' with Application(APPLICATION_NAME, log_file_path=log_file_path): pass file_path = os.path.join(get_log_directory(), log_file_path) self.__assert_logging_setup_with_file(file_path, filesystem_mock, logging_mock)
def test_configures_custom_log_file(self, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.return_value = True log_file_path = 'bar.log' with Application(APPLICATION_NAME, log_file_path=log_file_path): pass file_path = os.path.join(constants.PRESTOADMIN_LOG_DIR, log_file_path) self.__assert_logging_setup_with_file(file_path, filesystem_mock, logging_mock)
def test_warn_utils_prints_out_message_with_host(self, logger_mock, log_conf_mock, fs_mock): fabric.api.env.host = 'host' with Application(APPLICATION_NAME): fabric.utils.warn("Test warning.") logger_mock.warn.assert_has_calls([ call('[host] Test warning.\n\nNone\n'), ]) self.assertEqual('\nWarning: [host] Test warning.\n\n', self.test_stderr.getvalue())
def test_configures_default_log_file(self, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.return_value = True with Application(APPLICATION_NAME): pass file_path = os.path.join(get_log_directory(), APPLICATION_NAME + '.log') self.__assert_logging_setup_with_file(file_path, filesystem_mock, logging_mock) path_exists_mock.assert_called_once_with( constants.LOGGING_CONFIG_FILE_NAME)
def test_log_file_is_created(self): with Application(APPLICATION_NAME): pass log_file_path = os.path.join( constants.PRESTOADMIN_LOG_DIR, APPLICATION_NAME + '.log' ) self.assertTrue( os.path.exists(log_file_path), 'Expected log file does not exist' ) self.assertTrue( os.path.getsize(log_file_path) > 0, 'Log file is empty' )
def test_uses_logging_configs_in_order(self, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.side_effect = [False, True] log_file_path = '/tmp/bar.log' with Application(APPLICATION_NAME, log_file_path=log_file_path): pass parent_dirs_mock = filesystem_mock.ensure_parent_directories_exist parent_dirs_mock.assert_called_once_with(log_file_path) file_config_mock = logging_mock.fileConfig file_config_mock.assert_called_once_with( log_file_path + '.ini', defaults={'log_file_path': log_file_path}, disable_existing_loggers=False)
def _execute_operation_test(self, run_command_mock, logger_mock, func): out = fabric.operations._AttributeString('Test warning') out.command = 'echo "Test warning"' out.real_command = '/bin/bash echo "Test warning"' out.stderr = '' run_command_mock.return_value = out fabric.api.env.host_string = 'localhost' with Application(APPLICATION_NAME): func('echo "Test warning"') pass logger_mock.info.assert_has_calls([ call('\nCOMMAND: echo "Test warning"\nFULL COMMAND: /bin/bash' ' echo "Test warning"\nSTDOUT: Test warning\nSTDERR: '), ])
def test_configures_invalid_log_file(self, stderr_mock, path_exists_mock, logging_mock, filesystem_mock): path_exists_mock.return_value = True expected_error = FakeError('Error') logging_mock.fileConfig.side_effect = expected_error try: with Application(APPLICATION_NAME): pass except SystemExit as e: self.assertEqual('Error', e.message) stderr_mock.write.assert_has_calls([ call('Please run %s with sudo.\n' % APPLICATION_NAME), ])
def should_fail(): with Application(APPLICATION_NAME): raise Exception('User facing error message')
def should_exit(): with Application(APPLICATION_NAME): sys.exit()
def should_exit_zero_with_none(): with Application(APPLICATION_NAME): sys.exit(None)
def should_exit_one_with_str(): with Application(APPLICATION_NAME): sys.exit("exit")