From fd8e270ce6f035845e15871eb631f09f3e35ff73 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 8 Nov 2024 19:59:20 -0600 Subject: [PATCH] Add st2tests.config.coord_opts_as_env_vars for itests Integration tests need to start subprocesses that use the same redis as the test. This matters when running under pantsbuild, because pants runs several instances of pytest in parallel. This PR prepares to add support, to disambiguate test runs--similar to the database logic--using the env var: ST2TESTS_PARALLEL_SLOT. In any case, when an integration test runs production code in a subprocess, the production code does not use-- and should not use--any ST2TESTS_* vars, meaning the subprocess ends up using what is configured in the conf file instead of the (planned) parallel-safe coordinator. Thanks to a new-ish oslo_config feature, we can now update config via env variables. So, make use of that in integration tests to override the conf-file provided values with test-provided values. --- st2api/tests/integration/test_gunicorn_configs.py | 2 ++ .../tests/integration/test_register_content_script.py | 1 + .../integration/test_service_setup_log_level_filtering.py | 1 + st2reactor/tests/integration/test_garbage_collector.py | 1 + st2reactor/tests/integration/test_sensor_container.py | 1 + st2tests/st2tests/config.py | 7 +++++++ 6 files changed, 13 insertions(+) diff --git a/st2api/tests/integration/test_gunicorn_configs.py b/st2api/tests/integration/test_gunicorn_configs.py index 84fda239f5..c663aa83a7 100644 --- a/st2api/tests/integration/test_gunicorn_configs.py +++ b/st2api/tests/integration/test_gunicorn_configs.py @@ -43,6 +43,7 @@ def test_st2api_wsgi_entry_point(self): env = os.environ.copy() env["ST2_CONFIG_PATH"] = ST2_CONFIG_PATH env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) process = subprocess.Popen(cmd, env=env, shell=True, preexec_fn=os.setsid) try: self.add_process(process=process) @@ -63,6 +64,7 @@ def test_st2auth(self): env = os.environ.copy() env["ST2_CONFIG_PATH"] = ST2_CONFIG_PATH env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) process = subprocess.Popen(cmd, env=env, shell=True, preexec_fn=os.setsid) try: self.add_process(process=process) diff --git a/st2common/tests/integration/test_register_content_script.py b/st2common/tests/integration/test_register_content_script.py index 14abf12e45..c19d91c2a1 100644 --- a/st2common/tests/integration/test_register_content_script.py +++ b/st2common/tests/integration/test_register_content_script.py @@ -212,4 +212,5 @@ def test_register_recreate_virtualenvs(self): def _run_command(cmd): env = os.environ.copy() env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) return run_command(cmd=cmd, env=env) diff --git a/st2common/tests/integration/test_service_setup_log_level_filtering.py b/st2common/tests/integration/test_service_setup_log_level_filtering.py index a4f3cc90a5..03cfa1b6e5 100644 --- a/st2common/tests/integration/test_service_setup_log_level_filtering.py +++ b/st2common/tests/integration/test_service_setup_log_level_filtering.py @@ -231,6 +231,7 @@ def _start_process(config_path, env=None): cwd = os.path.abspath(cwd) env = env or os.environ.copy() env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) process = subprocess.Popen( cmd, env=env, diff --git a/st2reactor/tests/integration/test_garbage_collector.py b/st2reactor/tests/integration/test_garbage_collector.py index 630dad86de..7b8ff0dd10 100644 --- a/st2reactor/tests/integration/test_garbage_collector.py +++ b/st2reactor/tests/integration/test_garbage_collector.py @@ -277,6 +277,7 @@ def _start_garbage_collector(self): subprocess = concurrency.get_subprocess_module() env = os.environ.copy() env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) process = subprocess.Popen( CMD_INQUIRY, stdout=subprocess.PIPE, diff --git a/st2reactor/tests/integration/test_sensor_container.py b/st2reactor/tests/integration/test_sensor_container.py index 0ebae08604..e7d38eda99 100644 --- a/st2reactor/tests/integration/test_sensor_container.py +++ b/st2reactor/tests/integration/test_sensor_container.py @@ -246,6 +246,7 @@ def _start_sensor_container(self, cmd=DEFAULT_CMD): subprocess = concurrency.get_subprocess_module() env = os.environ.copy() env.update(st2tests.config.db_opts_as_env_vars()) + env.update(st2tests.config.coord_opts_as_env_vars()) print("Using command: %s" % (" ".join(cmd))) process = subprocess.Popen( cmd, diff --git a/st2tests/st2tests/config.py b/st2tests/st2tests/config.py index 22c5d62f09..9a2042be4a 100644 --- a/st2tests/st2tests/config.py +++ b/st2tests/st2tests/config.py @@ -165,6 +165,13 @@ def _override_coordinator_opts(noop=False): CONF.set_override(name="lock_timeout", override=1, group="coordination") +def coord_opts_as_env_vars() -> Dict[str, str]: + env = {} + if CONF.coordination.url is not None: + env["ST2_COORDINATION__URL"] = CONF.coordination.url + return env + + def _override_workflow_engine_opts(): cfg.CONF.set_override("retry_stop_max_msec", 200, group="workflow_engine") cfg.CONF.set_override("retry_wait_fixed_msec", 100, group="workflow_engine")