1515
1616from dataclasses import dataclass
1717from textwrap import dedent
18+ from typing import Tuple
1819
1920from pants .backend .python .goals .pytest_runner import (
2021 PytestPluginSetupRequest ,
2728 VenvPexProcess ,
2829 rules as pex_rules ,
2930)
31+ from pants .core .goals .test import TestExtraEnv
32+ from pants .engine .env_vars import EnvironmentVars
3033from pants .engine .fs import CreateDigest , Digest , FileContent
3134from pants .engine .rules import collect_rules , Get , MultiGet , rule
3235from pants .engine .process import FallibleProcessResult , ProcessCacheScope
@@ -54,13 +57,17 @@ class UsesRabbitMQRequest:
5457 # These config opts for integration tests are in:
5558 # conf/st2.tests*.conf st2tests/st2tests/fixtures/conf/st2.tests*.conf
5659 # (changed by setting ST2_CONFIG_PATH env var inside the tests)
57- # TODO: for unit tests: modify code to pull mq connect settings from env vars
58- # TODO: for int tests: modify st2.tests*.conf on the fly to set the per-pantsd-slot vhost
59- # and either add env vars for mq connect settings or modify conf files as well
60+ # These can also be updated via the ST2_MESSAGING_* env vars (which oslo_config reads).
61+ # Integration tests should pass these changes onto subprocesses via the same env vars.
6062
61- # with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
63+ mq_urls : Tuple [ str ] = ( "amqp://guest:guest@127.0.0.1:5672//" ,)
6264
63- mq_urls : tuple [str ] = ("amqp://guest:guest@127.0.0.1:5672//" ,)
65+ @classmethod
66+ def from_env (cls , env : EnvironmentVars ) -> UsesRabbitMQRequest :
67+ default = cls ()
68+ url = env .get ("ST2_MESSAGING__URL" , None )
69+ mq_urls = (url ,) if url else default .mq_urls
70+ return UsesRabbitMQRequest (mq_urls = mq_urls )
6471
6572
6673@dataclass (frozen = True )
@@ -83,9 +90,12 @@ def is_applicable(cls, target: Target) -> bool:
8390)
8491async def rabbitmq_is_running_for_pytest (
8592 request : PytestUsesRabbitMQRequest ,
93+ test_extra_env : TestExtraEnv ,
8694) -> PytestPluginSetup :
8795 # this will raise an error if rabbitmq is not running
88- _ = await Get (RabbitMQIsRunning , UsesRabbitMQRequest ())
96+ _ = await Get (
97+ RabbitMQIsRunning , UsesRabbitMQRequest .from_env (env = test_extra_env .env )
98+ )
8999
90100 return PytestPluginSetup ()
91101
@@ -167,6 +177,16 @@ async def rabbitmq_is_running(
167177 """
168178 ),
169179 service_start_cmd_generic = "systemctl start rabbitmq-server" ,
180+ env_vars_hint = dedent (
181+ """\
182+ You can also export the ST2_MESSAGING__URL env var to automatically use any
183+ RabbitMQ host, local or remote, while running unit and integration tests.
184+ If needed, you can also override the default exchange/queue name prefix
185+ by exporting ST2_MESSAGING__PREFIX. Note that tests always add a numeric
186+ suffix to the exchange/queue name prefix so that tests can safely run
187+ in parallel.
188+ """
189+ ),
170190 ),
171191 )
172192
0 commit comments