def test_configure_no_logging(self): config = {"logging": {"path": False, "console": False}} opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 1) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0])) self.assertEqual(rootlogger.handlers[0].level, logging.CRITICAL)
def test_configure_no_logging(): config = {"logging": {"path": False, "console": False}} opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers) == 1 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.CRITICAL
def test_configure_console_logging(): config = {"path": False, "level": "error", "console": True} opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers) == 1 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.ERROR
def test_configure_whitelist_and_blacklist(capsys, white_and_black_config): opsdroid.configure_logging(white_and_black_config) captured = capsys.readouterr() log1 = "Both whitelist and blacklist filters found in configuration." log2 = "Only one can be used at a time - only the whitelist filter will be used." assert log1 in captured.err assert log2 in captured.err
def test_configure_default_logging(self): config = {} opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 2) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0])) self.assertEqual(rootlogger.handlers[0].level, logging.INFO) self.assertEqual(logging.FileHandler, type(rootlogger.handlers[1])) self.assertEqual(rootlogger.handlers[1].level, logging.INFO) self.assertLogs("_LOGGER", "info")
def test_configure_file_logging(): with tempfile.TemporaryDirectory() as tmp_dir: config = { "logging": {"path": os.path.join(tmp_dir, "output.log"), "console": False} } opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers), 2 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level, logging.CRITICAL assert isinstance(rootlogger.handlers[1], logging.handlers.RotatingFileHandler) assert rootlogger.handlers[1].level == logging.INFO
def test_configure_default_logging(capsys): config = {} opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers), 2 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.INFO assert isinstance(rootlogger.handlers[1], logging.handlers.RotatingFileHandler) assert rootlogger.handlers[1].level == logging.INFO captured = capsys.readouterr() assert "INFO opsdroid.logging: Started opsdroid" in captured.err
def test_configure_file_logging_directory_not_exists(mocker): logmock = mocker.patch("logging.getLogger") mocklogger = mocker.MagicMock() mocklogger.handlers = [True] logmock.return_value = mocklogger with tempfile.TemporaryDirectory() as tmp_dir: config = { "path": os.path.join(tmp_dir, "mynonexistingdirectory", "output.log"), "console": False, } opsdroid.configure_logging(config)
def test_configure_logging_with_timestamp(capsys): config = { "logging": {"path": False, "level": "info", "console": True, "timestamp": True} } opsdroid.configure_logging(config) rootlogger = logging.getLogger() captured = capsys.readouterr() assert len(rootlogger.handlers) == 1 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) # Regex to match timestamp: 2020-12-02 17:46:33,158 regex = r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} INFO opsdroid\.logging:" assert re.match(regex, captured.err)
def test_configure_extended_logging(self): config = { "logging": { "path": False, "level": "error", "console": True, "extended": True, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 1) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0]))
def test_configure_logging_formatter(capsys): config = { "path": False, "console": True, "formatter": "%(name)s:", } opsdroid.configure_logging(config) captured = capsys.readouterr() log = "opsdroid.logging:\nopsdroid.logging:\n" assert log in captured.err
def test_configure_file_logging(self): config = { "logging": { "path": os.path.join(self._tmp_dir, "output.log"), "console": False, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 2) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0])) self.assertEqual(rootlogger.handlers[0].level, logging.CRITICAL) self.assertEqual(logging.FileHandler, type(rootlogger.handlers[1])) self.assertEqual(rootlogger.handlers[1].level, logging.INFO)
def test_configure_console_blacklist(self): config = { "logging": { "path": False, "level": "error", "console": True, "filter": {"blacklist": "opsdroid"}, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 1) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0])) self.assertLogs("_LOGGER", None)
def test_configure_console_whitelist(): config = { "logging": { "path": False, "level": "info", "console": True, "filter": {"whitelist": "opsdroid.logging"}, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers) == 1 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.INFO
def start(): """Start the opsdroid bot.""" check_dependencies() config = Loader.load_config_file([ "configuration.yaml", DEFAULT_CONFIG_PATH, "/etc/opsdroid/configuration.yaml" ]) configure_lang(config) configure_logging(config) welcome_message(config) with OpsDroid(config=config) as opsdroid: opsdroid.run()
def test_configure_file_logging_directory_not_exists(self): with mock.patch("logging.getLogger") as logmock: mocklogger = mock.MagicMock() mocklogger.handlers = [True] logmock.return_value = mocklogger config = { "logging": { "path": os.path.join( self._tmp_dir, "mynonexistingdirectory", "output.log" ), "console": False, } } opsdroid.configure_logging(config)
def white_and_black_config(): # Set up config = { "logging": { "path": False, "level": "info", "console": True, "filter": {"whitelist": ["opsdroid"], "blacklist": ["opsdroid.core"]}, } } yield config # Tear down config = {} opsdroid.configure_logging(config)
def start(path): """Start the opsdroid bot. If the `-f` flag is used with this command, opsdroid will load the configuration specified on that path otherwise it will use the default configuration. """ check_dependencies() config = load_config_file([path] if path else DEFAULT_CONFIG_LOCATIONS) configure_lang(config) configure_logging(config) welcome_message(config) with OpsDroid(config=config) as opsdroid: opsdroid.run()
def test_configure_file_blacklist(self): config = { "logging": { "path": os.path.join(self._tmp_dir, "output.log"), "console": False, "filter": { "blacklist": "opsdroid.logging" }, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() self.assertEqual(len(rootlogger.handlers), 2) self.assertEqual(logging.StreamHandler, type(rootlogger.handlers[0])) self.assertEqual(rootlogger.handlers[0].level, logging.CRITICAL) self.assertEqual(logging.handlers.RotatingFileHandler, type(rootlogger.handlers[1])) self.assertLogs("_LOGGER", None)
def test_configure_console_blacklist(capsys): config = { "logging": { "path": False, "level": "error", "console": True, "filter": {"blacklist": "opsdroid"}, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers) == 1 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.ERROR captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_configure_file_blacklist(capsys): with tempfile.TemporaryDirectory() as tmp_dir: config = { "logging": { "path": os.path.join(tmp_dir, "output.log"), "console": False, "filter": {"blacklist": "opsdroid.logging"}, } } opsdroid.configure_logging(config) rootlogger = logging.getLogger() assert len(rootlogger.handlers) == 2 assert isinstance(rootlogger.handlers[0], logging.StreamHandler) assert rootlogger.handlers[0].level == logging.CRITICAL assert isinstance(rootlogger.handlers[1], logging.handlers.RotatingFileHandler) captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def main(): """Opsdroid is a chat bot framework written in Python. It is designed to be extendable, scalable and simple. See https://opsdroid.github.io/ for more information. """ check_dependencies() config = Loader.load_config_file([ "configuration.yaml", DEFAULT_CONFIG_PATH, "/etc/opsdroid/configuration.yaml" ]) configure_lang(config) configure_logging(config) welcome_message(config) with OpsDroid(config=config) as opsdroid: opsdroid.load() opsdroid.run()
def build_config(ctx, params): """Load configuration, load modules and install dependencies. This function loads the configuration and install all necessary dependencies defined on a `requirements.txt` file inside the module. If the flag `--verbose` is passed the logging level will be set as debug and all logs will be shown to the user. Args: ctx (:obj:`click.Context`): The current click cli context. params (dict): a dictionary of all parameters pass to the click context when invoking this function as a callback. Returns: int: the exit code. Always returns 0 in this case. """ click.echo("Opsdroid will build modules from config.") path = params.get("path") with contextlib.suppress(Exception): check_dependencies() config = load_config_file([path] if path else DEFAULT_CONFIG_LOCATIONS) if params["verbose"]: config["logging"] = {"level": "debug"} configure_logging(config) with OpsDroid(config=config) as opsdroid: opsdroid.loader.load_modules_from_config(config) click.echo(click.style("SUCCESS:", bg="green", bold=True), nl=False) click.echo(" Opsdroid modules successfully built from config.")
def test_configure_whitelist_and_blacklist(self): opsdroid.configure_logging(self.config) self.assertLogs("_LOGGER", "warning")
def tearDown(self): self.config = {} opsdroid.configure_logging(self.config)