Пример #1
0
    def test_get_logger_different_log_file(self, getFileHandler,
                                           getQueueLogger):
        queueLogger = log.QueueLogger('virtwho')
        queueLogger.logger.handlers = []
        mockQueueLogger = Mock(wraps=queueLogger)
        getQueueLogger.return_value = mockQueueLogger

        config = Mock()
        config.name = 'test'
        config.log_file = 'test.log'
        config.log_dir = '/test/'

        options = Mock()
        options.debug = False
        options.background = True
        options.log_per_config = True
        options.log_dir = ''
        options.log_file = ''
        log.init(options)
        test_logger = log.getLogger(name='test', config=config)

        self.assertTrue(test_logger.name == 'virtwho.test')
        self.assertTrue(len(test_logger.handlers) == 1)
        self.assertTrue(len(queueLogger.logger.handlers) == 1)
        getFileHandler.assert_called_with(name=test_logger.name, config=config)
Пример #2
0
 def test_get_logger_no_config(self, open, getQueueLogger, isdir):
     open.return_value = None
     isdir.return_value = True
     queueLogger = log.QueueLogger('virtwho')
     queueLogger.logger.handlers = []
     mockQueueLogger = Mock(wraps=queueLogger)
     getQueueLogger.return_value = mockQueueLogger
     conf_values = {
         'global': {
             'debug': False,
             'background': True,
             'log_file': log.DEFAULT_LOG_FILE,
             'log_dir': log.DEFAULT_LOG_DIR,
             'log_per_config': False
         }
     }
     config = StubEffectiveConfig(conf_values)
     log.init(config)
     main_logger = log.getLogger(name='main')
     self.assertTrue(main_logger.name == 'virtwho.main')
     self.assertTrue(len(main_logger.handlers) == 1)
     self.assertTrue(isinstance(main_logger.handlers[0], log.QueueHandler))
     queue_handlers = queueLogger.logger.handlers
     self.assertTrue(len(queue_handlers) == 2)
     self.assertEqual(queue_handlers[0].baseFilename, '%s/%s' % (log.DEFAULT_LOG_DIR, log.DEFAULT_LOG_FILE))
Пример #3
0
    def test_queue_logger(self, getLogger, queue):
        fake_queue = sentinel.queue
        name = sentinel.name
        logger = sentinel.logger
        queue.return_value = fake_queue
        getLogger.return_value = logger
        queueLogger = log.QueueLogger(name)
        getLogger.assert_called_with(name)

        self.assertTrue(
            isinstance(queueLogger._logging_thread, threading.Thread))
        thread = queueLogger._logging_thread
        self.assertTrue(
            thread.__dict__['_Thread__target'] == log.QueueLogger._log)
        self.assertTrue(thread.__dict__['_Thread__args'] == (logger,
                                                             fake_queue))
        self.assertTrue(queueLogger.queue == fake_queue)
        self.assertTrue(queueLogger.logger == logger)
        self.assertTrue(queueLogger.name == name)
Пример #4
0
    def test_get_logger_different_log_file(self, getFileHandler, getQueueLogger):
        queueLogger = log.QueueLogger('virtwho')
        queueLogger.logger.handlers = []
        mockQueueLogger = Mock(wraps=queueLogger)
        getQueueLogger.return_value = mockQueueLogger

        options = {
            'global': {
                'debug': False,
                'background': True,
                'log_per_config': True,
                'log_dir': '/test/',
                'log_file': 'test.log',
            },
        }
        log.init(options)
        test_logger = log.getLogger(config=options)

        self.assertTrue(test_logger.name == 'virtwho.test_log')
        self.assertTrue(len(test_logger.handlers) == 1)
        self.assertTrue(len(queueLogger.logger.handlers) == 2)
        getFileHandler.assert_called_with(name=test_logger.name, config=options)
Пример #5
0
 def test_get_logger_no_config(self, open, getQueueLogger, isdir):
     open.return_value = None
     isdir.return_value = True
     queueLogger = log.QueueLogger('virtwho')
     queueLogger.logger.handlers = []
     mockQueueLogger = Mock(wraps=queueLogger)
     getQueueLogger.return_value = mockQueueLogger
     options = Mock()
     options.debug = False
     options.background = True
     options.log_file = log.DEFAULT_LOG_FILE
     options.log_dir = log.DEFAULT_LOG_DIR
     options.log_per_config = False
     log.init(options)
     main_logger = log.getLogger(name='main')
     self.assertTrue(main_logger.name == 'virtwho.main')
     self.assertTrue(len(main_logger.handlers) == 1)
     self.assertTrue(isinstance(main_logger.handlers[0], log.QueueHandler))
     queue_handlers = queueLogger.logger.handlers
     self.assertTrue(len(queue_handlers) == 1)
     self.assertEquals(
         queue_handlers[0].baseFilename,
         '%s/%s' % (log.DEFAULT_LOG_DIR, log.DEFAULT_LOG_FILE))
Пример #6
0
    def test_queue_logger(self, getLogger, queue):
        fake_queue = sentinel.queue
        name = sentinel.name
        logger = sentinel.logger
        queue.return_value = fake_queue
        getLogger.return_value = logger
        queueLogger = log.QueueLogger(name)
        getLogger.assert_called_with(name)

        self.assertTrue(isinstance(queueLogger._logging_thread,
                                   threading.Thread))
        thread = queueLogger._logging_thread

        target_attr = '_target'
        args_attr = '_args'
        if not six.PY3:
            target_attr = '_Thread_' + target_attr
            args_attr = '_Thread_' + args_attr

        self.assertTrue(getattr(thread, target_attr) == log.QueueLogger._log)
        self.assertTrue(getattr(thread, args_attr) == (logger, fake_queue))
        self.assertTrue(queueLogger.queue == fake_queue)
        self.assertTrue(queueLogger.logger == logger)
        self.assertTrue(queueLogger.name == name)