Update pants-plugins/uses_services
to support checking for redis
#5893
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
This is another part of introducing pants, as discussed in various TSC meetings.
Related PRs can be found in:
Overview of this PR
This PR builds on #5864 and #5884 to further improve the DX (developer experience) by failing as early as possible if the development (or CI) environment does not have the required services.
This PR adds checks for
redis
, like #5864 addedmongo
checks and #5884 addedrabbitmq
checks.Please see #5864 for a more detailed description of
pants-plugins/uses_services
.is_redis_running.py
script and the rule that runs itis_redis_running.py
is the part that checks to see if redis is running.The pants plugin cannot import anything from our other
st2*
code, sois_redis_running.py
is a minimal self-contained script that mirrors how st2 connects to redis. It should be as minimal as possible so that keeping it up-to-date with the core st2 code is not onerous.The
is_redis_running.py
rule gets opened and run in a pex with the same rule logic asis_mongo_running.py
andinspect_platform.py
(see #5864).Redis, like rabbitmq, embeds all auth stuff in the connection url. So, there are not as many settings to worry about as there were with mongo. For now, this only supports the url hard-coded in the tests. Here is where the rule defines the default url:
st2/pants-plugins/uses_services/redis_rules.py
Lines 47 to 62 in 06aedfa
Here is the definition of the rule that runs
is_redis_running.py
:st2/pants-plugins/uses_services/redis_rules.py
Lines 96 to 98 in 06aedfa
So, when another rule Gets
RedisIsRunning
with aUsesRedisRequest
, pants will also run the rule that generatesPlatform
(described in #5864), and then it will run thisis_redis_running
rule.The
is_redis_running
rule either returnsRedisIsRunning()
if it is running, or raisesServiceMissingError
if it is not. By raising an error, this actually breaks a convention for pants rules. Exceptions stop everything and get shown to the user right away, and for most goals, pants wants to see some dataclass returned that has something like asucceeded
boolean instead. But, we want to error as early as possible, so this breaks that convention on purpose.wiring up the
test
goal so it runsis_redis_running
whenpytest
runs on a target with theuses
field.The last piece that ties this all together is a rule that makes sure the
is_redis_running
rule runs beforepytest
runs (if pants is running it on a target with theuses
field). Here is the definition of theredis_is_running_for_pytest
rule:st2/pants-plugins/uses_services/redis_rules.py
Lines 83 to 85 in 06aedfa
This rule is very simple. It just triggers running the
is_redis_running
rule:st2/pants-plugins/uses_services/redis_rules.py
Lines 86 to 89 in 06aedfa
This rule needs the
PytestUsesRedisRequest
which selects targets that have theuses
field.st2/pants-plugins/uses_services/redis_rules.py
Lines 70 to 76 in 06aedfa
This request will be made by the pants-internal pytest rules thanks to this
UnionRule
, which is a way to declare that our class "implements" thePytestPluginSetupRequest
:st2/pants-plugins/uses_services/redis_rules.py
Line 168 in 06aedfa
If we need to add
uses
support to other targets, we will need to find similar UnionRules where we can inject our rule logic. For now, I've only wired this up so ouruses
rules will run for pytest.