Skip to content

Commit

Permalink
tests: respect ST2_MESSAGING_* env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Nov 23, 2024
1 parent ac9bcbd commit 3ca0b03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
32 changes: 26 additions & 6 deletions pants-plugins/uses_services/rabbitmq_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from dataclasses import dataclass
from textwrap import dedent
from typing import Tuple

from pants.backend.python.goals.pytest_runner import (
PytestPluginSetupRequest,
Expand All @@ -27,6 +28,8 @@
VenvPexProcess,
rules as pex_rules,
)
from pants.core.goals.test import TestExtraEnv
from pants.engine.env_vars import EnvironmentVars
from pants.engine.fs import CreateDigest, Digest, FileContent
from pants.engine.rules import collect_rules, Get, MultiGet, rule
from pants.engine.process import FallibleProcessResult, ProcessCacheScope
Expand Down Expand Up @@ -54,13 +57,17 @@ class UsesRabbitMQRequest:
# These config opts for integration tests are in:
# conf/st2.tests*.conf st2tests/st2tests/fixtures/conf/st2.tests*.conf
# (changed by setting ST2_CONFIG_PATH env var inside the tests)
# TODO: for unit tests: modify code to pull mq connect settings from env vars
# TODO: for int tests: modify st2.tests*.conf on the fly to set the per-pantsd-slot vhost
# and either add env vars for mq connect settings or modify conf files as well
# These can also be updated via the ST2_MESSAGING_* env vars (which oslo_config reads).
# Integration tests should pass these changes onto subprocesses via the same env vars.

# with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
mq_urls: Tuple[str] = ("amqp://guest:guest@127.0.0.1:5672//",)

mq_urls: tuple[str] = ("amqp://guest:guest@127.0.0.1:5672//",)
@classmethod
def from_env(cls, env: EnvironmentVars) -> UsesRabbitMQRequest:
default = cls()
url = env.get("ST2_MESSAGING__URL", None)
mq_urls = (url,) if url else default.mq_urls
return UsesRabbitMQRequest(mq_urls=mq_urls)


@dataclass(frozen=True)
Expand All @@ -83,9 +90,12 @@ def is_applicable(cls, target: Target) -> bool:
)
async def rabbitmq_is_running_for_pytest(
request: PytestUsesRabbitMQRequest,
test_extra_env: TestExtraEnv,
) -> PytestPluginSetup:
# this will raise an error if rabbitmq is not running
_ = await Get(RabbitMQIsRunning, UsesRabbitMQRequest())
_ = await Get(
RabbitMQIsRunning, UsesRabbitMQRequest.from_env(env=test_extra_env.env)
)

return PytestPluginSetup()

Expand Down Expand Up @@ -167,6 +177,16 @@ async def rabbitmq_is_running(
"""
),
service_start_cmd_generic="systemctl start rabbitmq-server",
env_vars_hint=dedent(
"""\
You can also export the ST2_MESSAGING__URL env var to automatically use any
RabbitMQ host, local or remote, while running unit and integration tests.
If needed, you can also override the default exchange/queue name prefix
by exporting ST2_MESSAGING__PREFIX. Note that tests always add a numeric
suffix to the exchange/queue name prefix so that tests can safely run
in parallel.
"""
),
),
)

Expand Down
11 changes: 9 additions & 2 deletions pants-plugins/uses_services/rabbitmq_rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ def run_rabbitmq_is_running(
"--backend-packages=uses_services",
*(extra_args or ()),
],
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
env_inherit={
"PATH",
"PYENV_ROOT",
"HOME",
"ST2_MESSAGING__URL",
"ST2_MESSAGING__PREFIX",
"ST2TESTS_PARALLEL_SLOT",
},
)
result = rule_runner.request(
RabbitMQIsRunning,
Expand All @@ -62,7 +69,7 @@ def run_rabbitmq_is_running(

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

# we are asserting that this does not raise an exception
Expand Down
3 changes: 3 additions & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ extra_env_vars = [
"ST2_DATABASE__CONNECTION_TIMEOUT",
"ST2_DATABASE__USERNAME",
"ST2_DATABASE__PASSWORD",
# Use these to override RabbitMQ connection details
"ST2_MESSAGING__URL",
"ST2_MESSAGING__PREFIX", # Tests will modify this to be "{prefix}{ST2TESTS_PARALLEL_SLOT}"
# Use these to override the redis host and port
"ST2TESTS_REDIS_HOST",
"ST2TESTS_REDIS_PORT",
Expand Down

0 comments on commit 3ca0b03

Please sign in to comment.