Ejemplo n.º 1
0
    def configure_logger_test(self,
                              log_name,
                              log_dest=None,
                              detail_level=None,
                              log_filename=None,
                              error=None):
        """
        Unified test function for the configure_logger function
        """
        if error:
            try:
                configure_logger(log_name,
                                 log_dest=log_dest,
                                 detail_level=detail_level,
                                 log_filename=log_filename)
                self.fail('Exception expected')
            except ValueError:
                pass
        else:
            configure_logger(log_name,
                             log_dest=log_dest,
                             detail_level=detail_level,
                             log_filename=log_filename)

            self.logger_validate(log_name,
                                 log_dest,
                                 detail_level,
                                 log_filename=log_filename)
Ejemplo n.º 2
0
 def test_configure_logger_nameerror(self):
     """
     Test that invalid logger name parameter generates exception. The
     previous test handles valid names
     """
     with pytest.raises(ValueError):
         configure_logger("BadLogName",
                          log_dest='stderr',
                          detail_level="all")
Ejemplo n.º 3
0
    def test_logger_propagate(self, tmp_dir, logger_names, propagate):
        # pylint: disable=redefined-outer-name
        """Test log event propagation behavior."""

        short_name, logger_name = logger_names

        # The testing approach is to log to files and check their contents.
        # Neither LogCapture nor OutputCapture seemed to work with pytest.
        logger_filename = os.path.join(tmp_dir.path, 'pywbem.xxx.log')
        pkg_filename = os.path.join(tmp_dir.path, 'pywbem.log')

        # Create a log handler on the 'pywbem.<xxx>' logger to be tested
        configure_logger(short_name,
                         log_dest='file',
                         log_filename=logger_filename,
                         detail_level='all',
                         connection=True,
                         propagate=propagate)

        # Create a log handler on the 'pywbem' logger (parent)
        pkg_logger = logging.getLogger('pywbem')
        pkg_handler = logging.FileHandler(pkg_filename, encoding="UTF-8")
        pkg_handler.setLevel(logging.DEBUG)
        pkg_formatter = logging.Formatter('%(asctime)s-%(name)s-%(message)s')
        pkg_handler.setFormatter(pkg_formatter)
        pkg_logger.addHandler(pkg_handler)

        # Create a log event
        WBEMConnection('bla')

        # Verify the 'propagate' attribute of the logger to be tested
        logger = logging.getLogger(logger_name)
        assert logger.propagate == propagate

        for h in logger.handlers + pkg_logger.handlers:
            try:
                h.flush()
                h.close()
            except AttributeError:
                pass

        pkg_logger.removeHandler(pkg_handler)

        with open(logger_filename) as logger_fp:
            logger_line = logger_fp.read()
        assert re.match(r'.*-%s\..*-Connection:' % logger_name, logger_line)

        with open(pkg_filename) as pkg_fp:
            pkg_line = pkg_fp.read()
        if propagate:
            assert re.match(r'.*-pywbem.*-Connection:', pkg_line)
        else:
            assert pkg_line == ''

        # Clean up the Python logging configuration
        configure_logger(short_name, connection=False)
Ejemplo n.º 4
0
    def test_configure_logger(self, log_name, log_dest, detail_level,
                              log_filename, exp_except):
        """
        Test variations of the configure_logger method including errors.
        """
        if exp_except:
            with pytest.raises(exp_except):
                configure_logger(log_name, log_dest=log_dest,
                                 detail_level=detail_level,
                                 log_filename=log_filename)

        else:
            configure_logger(log_name, log_dest=log_dest,
                             detail_level=detail_level,
                             log_filename=log_filename)

            self.logger_validate(log_name, log_dest, detail_level,
                                 log_filename=log_filename)