Skip to content

Commit

Permalink
pants-plugins/uses_services: follow new conventions in redis rules too
Browse files Browse the repository at this point in the history
These changes were already made for MongoDB and RabbitMQ rules.
This removes a TODO comment and slightly refactors to follow the same convention.
  • Loading branch information
cognifloyd committed Nov 23, 2024
1 parent 5cee8c0 commit 500c0f6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
32 changes: 22 additions & 10 deletions pants-plugins/uses_services/redis_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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 All @@ -54,17 +55,30 @@ class UsesRedisRequest:

# These config opts for integration tests are in:
# conf/st2.dev.conf (copied to conf/st2.ci.conf)
# TODO: for int tests: set url by either modifying st2.{dev,ci}.conf on the fly or via env vars.

# with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
# These can also be updated via the ST2TESTS_REDIS_* env vars.
# Integration tests should pass these changes onto subprocesses using the
# ST2_COORDINATION__* env vars (which oslo_config reads).

host: str = "127.0.0.1"
port: str = "6379"
port: int = 6379

@property
def coord_url(self) -> str:
return f"redis://{self.host}:{self.port}?namespace=_st2_test"

@classmethod
def from_env(cls, env: EnvironmentVars) -> UsesRedisRequest:
default = cls()
host = env.get("ST2TESTS_REDIS_HOST", default.host)
port_raw = env.get("ST2TESTS_REDIS_PORT", str(default.port))

try:
port = int(port_raw)
except (TypeError, ValueError):
port = default.port

return cls(host=host, port=port)


@dataclass(frozen=True)
class RedisIsRunning:
Expand All @@ -88,11 +102,8 @@ async def redis_is_running_for_pytest(
request: PytestUsesRedisRequest,
test_extra_env: TestExtraEnv,
) -> PytestPluginSetup:
redis_host = test_extra_env.env.get("ST2TESTS_REDIS_HOST", "127.0.0.1")
redis_port = test_extra_env.env.get("ST2TESTS_REDIS_PORT", "6379")

# this will raise an error if redis is not running
_ = await Get(RedisIsRunning, UsesRedisRequest(host=redis_host, port=redis_port))
_ = await Get(RedisIsRunning, UsesRedisRequest.from_env(env=test_extra_env.env))

return PytestPluginSetup()

Expand Down Expand Up @@ -161,7 +172,7 @@ async def redis_is_running(
not_installed_clause_deb="this is one way to install it:",
install_instructions_deb=dedent(
"""\
sudo apt-get install -y mongodb redis
sudo apt-get install -y redis
# Don't forget to start redis.
"""
),
Expand All @@ -170,7 +181,8 @@ async def redis_is_running(
"""\
You can also export the ST2TESTS_REDIS_HOST and ST2TESTS_REDIS_PORT
env vars to automatically use any redis host, local or remote,
while running unit and integration tests.
while running unit and integration tests. Tests do not use any
ST2_COORDINATION__* vars at this point.
"""
),
),
Expand Down
13 changes: 10 additions & 3 deletions pants-plugins/uses_services/redis_rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ def run_redis_is_running(
"--backend-packages=uses_services",
*(extra_args or ()),
],
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
env_inherit={
"PATH",
"PYENV_ROOT",
"HOME",
"ST2TESTS_REDIS_HOST",
"ST2TESTS_REDIS_PORT",
"ST2TESTS_PARALLEL_SLOT",
},
)
result = rule_runner.request(
RedisIsRunning,
Expand All @@ -62,7 +69,7 @@ def run_redis_is_running(

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

# we are asserting that this does not raise an exception
Expand All @@ -74,7 +81,7 @@ def test_redis_is_running(rule_runner: RuleRunner) -> None:
def test_redis_not_running(rule_runner: RuleRunner, mock_platform: Platform) -> None:
request = UsesRedisRequest(
host="127.100.20.7",
port="10", # 10 is an unassigned port, unlikely to be used
port=10, # 10 is an unassigned port, unlikely to be used
)

with pytest.raises(ExecutionError) as exception_info:
Expand Down
4 changes: 2 additions & 2 deletions pants-plugins/uses_services/scripts/is_redis_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _is_redis_running(coord_url: str) -> bool:
if __name__ == "__main__":
args = dict((k, v) for k, v in enumerate(sys.argv))

# unit tests do not use redis, they use use an in-memory coordinator: "zake://"
# integration tests use this url with a conf file derived from conf/st2.dev.conf
# unit and integration tests require a coordinator, and mostly use this redis url.
# In some cases, unit tests can also use an in-memory coordinator: "zake://"
coord_url = args.get(1, "redis://127.0.0.1:6379?namespace=_st2_test")

is_running = _is_redis_running(coord_url)
Expand Down

0 comments on commit 500c0f6

Please sign in to comment.