From 977baaf58334612f9f853625f07638d94ec3a4ec Mon Sep 17 00:00:00 2001 From: jk464 Date: Fri, 24 May 2024 16:20:02 +0100 Subject: [PATCH] Enable completely offline st2-rule-tester --- CHANGELOG.rst | 2 ++ st2reactor/st2reactor/cmd/rule_tester.py | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3cf9d948b0..aec623e490 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -35,6 +35,8 @@ Added Contributed by @amanda11 * Ensure `.pth` files in the st2 virtualenv get loaded by pack virtualenvs. #6183 Contributed by @cognifloyd +* Allow `st2-rule-tester` to run without a mongo connection if user is testing against local `rule`/`trigger-instance` files. #6208 + Contributed by @jk464 3.8.1 - December 13, 2023 ------------------------- diff --git a/st2reactor/st2reactor/cmd/rule_tester.py b/st2reactor/st2reactor/cmd/rule_tester.py index b346168cb5..29b2e2999d 100644 --- a/st2reactor/st2reactor/cmd/rule_tester.py +++ b/st2reactor/st2reactor/cmd/rule_tester.py @@ -21,6 +21,7 @@ from st2common import config from st2common import log as logging from st2common.config import do_register_cli_opts +from st2common.service_setup import db_setup from st2common.script_setup import setup as common_setup from st2common.script_setup import teardown as common_teardown from st2reactor.rules.tester import RuleTester @@ -46,13 +47,29 @@ def _register_cli_opts(): default=None, help="Id of the Trigger Instance to use for validation.", ), + cfg.BoolOpt( + "offline", + default=False, + help="Run st2-rule-tester without DB connection - can only be used in connection with 'rule' and 'trigger-instance' options", + ), ] do_register_cli_opts(cli_opts) def main(): _register_cli_opts() - common_setup(config=config, setup_db=True, register_mq_exchanges=False) + + common_setup(config=config, setup_db=False, register_mq_exchanges=False) + + # Setup DB if not running offline + if not cfg.CONF.offline: + db_setup() + # If running offline check that neither rule_ref or trigger_instance_id are provided as they require the DB. + elif cfg.CONF.rule_ref or cfg.CONF.trigger_instance_id: + LOG.critical( + "'rule-ref' and/or 'trigger-instance-id' cannot be used in 'offline' mode" + ) + sys.exit(2) try: tester = RuleTester(