Skip to content

Commit 3ca0b03

Browse files
committed
tests: respect ST2_MESSAGING_* env vars
1 parent ac9bcbd commit 3ca0b03

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

pants-plugins/uses_services/rabbitmq_rules.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from dataclasses import dataclass
1717
from textwrap import dedent
18+
from typing import Tuple
1819

1920
from pants.backend.python.goals.pytest_runner import (
2021
PytestPluginSetupRequest,
@@ -27,6 +28,8 @@
2728
VenvPexProcess,
2829
rules as pex_rules,
2930
)
31+
from pants.core.goals.test import TestExtraEnv
32+
from pants.engine.env_vars import EnvironmentVars
3033
from pants.engine.fs import CreateDigest, Digest, FileContent
3134
from pants.engine.rules import collect_rules, Get, MultiGet, rule
3235
from 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
)
8491
async 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

pants-plugins/uses_services/rabbitmq_rules_test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ def run_rabbitmq_is_running(
5151
"--backend-packages=uses_services",
5252
*(extra_args or ()),
5353
],
54-
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
54+
env_inherit={
55+
"PATH",
56+
"PYENV_ROOT",
57+
"HOME",
58+
"ST2_MESSAGING__URL",
59+
"ST2_MESSAGING__PREFIX",
60+
"ST2TESTS_PARALLEL_SLOT",
61+
},
5562
)
5663
result = rule_runner.request(
5764
RabbitMQIsRunning,
@@ -62,7 +69,7 @@ def run_rabbitmq_is_running(
6269

6370
# Warning this requires that rabbitmq be running
6471
def test_rabbitmq_is_running(rule_runner: RuleRunner) -> None:
65-
request = UsesRabbitMQRequest()
72+
request = UsesRabbitMQRequest.from_env(env=rule_runner.environment)
6673
mock_platform = platform(os="TestMock")
6774

6875
# we are asserting that this does not raise an exception

pants.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ extra_env_vars = [
247247
"ST2_DATABASE__CONNECTION_TIMEOUT",
248248
"ST2_DATABASE__USERNAME",
249249
"ST2_DATABASE__PASSWORD",
250+
# Use these to override RabbitMQ connection details
251+
"ST2_MESSAGING__URL",
252+
"ST2_MESSAGING__PREFIX", # Tests will modify this to be "{prefix}{ST2TESTS_PARALLEL_SLOT}"
250253
# Use these to override the redis host and port
251254
"ST2TESTS_REDIS_HOST",
252255
"ST2TESTS_REDIS_PORT",

0 commit comments

Comments
 (0)