Skip to content

Commit

Permalink
Fix usages of importing bot's module
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-certat committed Sep 28, 2023
1 parent cfdba2f commit fa8f6be
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion intelmq/bin/intelmqctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ def check(self, no_connections=False, check_executables=True):
if bot_id != 'global':
# importable module
try:
bot_module = importlib.import_module(bot_config['module'])
bot_module = importlib.import_module(utils.get_bot_module_name(bot_config['module']))
except ImportError as exc:
check_logger.error('Incomplete installation: Bot %r not importable: %r.', bot_id, exc)
retval = 1
Expand Down
2 changes: 1 addition & 1 deletion intelmq/lib/bot_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_t
self.dryrun = dryrun
self.msg = msg
self.show = show
module = import_module(self.runtime_configuration['module'])
module = import_module(utils.get_bot_module_name(self.runtime_configuration['module']))

if loglevel:
self.leverageLogger(loglevel)
Expand Down
13 changes: 13 additions & 0 deletions intelmq/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,19 @@ def _get_console_entry_points():
return entries.get("console_scripts", []) # it's a dict


def get_bot_module_name(bot_name: str) -> str:
entries = entry_points()
if hasattr(entries, "select"):
entries = entries.select(name=bot_name, group="console_scripts")
else:
entries = [entry for entry in entries.get("console_scripts", []) if entry.name == bot_name]

if not entries:
return None
else:
return entries[0].value.replace(":BOT.run", '')


def list_all_bots() -> dict:
"""
Compile a dictionary with all bots and their parameters.
Expand Down
12 changes: 12 additions & 0 deletions intelmq/tests/bin/test_intelmqctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ def test_check_handles_syntaxerror_when_importing_bots(self):
self.assertIsNotNone(
next(filter(lambda l: "SyntaxError in bot 'test-bot'" in l, captured.output)))

@skip_installation()
@mock.patch.object(utils, "get_bot_module_name", mock.Mock(return_value="mocked-module"))
def test_check_imports_real_bot_module(self):
self._load_default_harmonization()
self._extend_config(self.tmp_runtime, self.BOT_CONFIG)

# raise SyntaxError to stop checking after import
with mock.patch.object(ctl.importlib, "import_module", mock.Mock(side_effect=SyntaxError)) as import_mock:
self.intelmqctl.check(no_connections=True, check_executables=False)

import_mock.assert_called_once_with("mocked-module")


if __name__ == '__main__': # pragma: nocover
unittest.main()
12 changes: 8 additions & 4 deletions intelmq/tests/lib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def test_list_all_bots_filters_entrypoints(self):
)
self.assertEqual(2, import_mock.call_count)

def test_get_bot_module_name_builtin_bot(self):
found_name = utils.get_bot_module_name("intelmq.bots.collectors.api.collector_api")
self.assertEqual("intelmq.bots.collectors.api.collector_api", found_name)

def test_get_bots_settings(self):
with unittest.mock.patch.object(utils, "get_runtime", new_get_runtime):
runtime = utils.get_bots_settings()
Expand Down Expand Up @@ -381,14 +385,14 @@ def test_load_configuration_yaml(self):
filename = os.path.join(os.path.dirname(__file__), '../assets/example.yaml')
self.assertEqual(utils.load_configuration(filename),
{
'some_string': 'Hello World!',
'other_string': 'with a : in it',
'some_string': 'Hello World!',
'other_string': 'with a : in it',
'now more': ['values', 'in', 'a', 'list'],
'types': -4,
'other': True,
'final': 0.5,
}
)
}
)

def test_load_configuration_yaml_invalid(self):
""" Test load_configuration with an invalid YAML file """
Expand Down

0 comments on commit fa8f6be

Please sign in to comment.