pants: Add system_user
detection to pants-plugins/uses_services
(+ mongo
detection improvements)
#6244
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.
Some of the tests require the
system_user.user
as an actual local user on the machine running pytest. That is now configurable with theST2TESTS_SYSTEM_USER
env var added in #6242. If someone forgot to set--or did not know they could set--theST2TESTS_SYSTEM_USER
env var, they will get really odd errors that are difficult to interpret and debug. So, this PR adds a pants-plugin feature to fail with an error message about setting that env var before pytest even runs tests that require the system_user.This improves on the
pants-plugins/uses_services
plugin, which was added in these PRs (the PR descriptions can be helpful in understanding theuses_services
plugin):pants-plugins/uses_services
to check before running tests for required services (like mongo) #5864pants-plugins/uses_services
to support checking for rabbitmq #5884pants-plugins/uses_services
to support checking for redis #5893pants-plugins/uses_services
improvements #5898has_system_user.py
script and the rule that runs ithas_system_user.py
is the part that checks to see if the system_user is present on the system.The
has_system_user.py
script gets opened and run in a pex with the similar rule logic to themongo
,redis
, andrabbitmq
detection rules.Only
system_user
has to be passed to thehas_system_user.py
script to check for that user:st2/pants-plugins/uses_services/system_user_rules.py
Lines 46 to 55 in 1413e11
Here is the definition of the rule that runs
has_system_user.py
:st2/pants-plugins/uses_services/system_user_rules.py
Lines 92 to 94 in 1413e11
So, when another rule Gets
HasSystemUser
with aUsesSystemUserRequest
, pants will also run the rule that generatesPlatform
(described in #5864), and then it will run thishas_system_user
rule.The
has_system_user
rule either returnsHasSystemUser()
if the user is present, or raisesServiceMissingError
if it is not (the same error that the other uses_services rules raise). 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 runshas_system_user
whenpytest
runs on a target with theuses
field.The last piece that ties this all together is a rule that makes sure the
has_system_user
rule runs beforepytest
runs (if pants is running it on a target with theuses
field). Here is the definition of thehas_system_user_for_pytest
rule:st2/pants-plugins/uses_services/system_user_rules.py
Lines 76 to 79 in 1413e11
This rule is fairly simple. It looks up the system_user based on the
[test].extra_env_vars
setting defined here:st2/pants.toml
Lines 242 to 247 in 1413e11
Then it just triggers running the
has_system_user
rule, passing in the system_user:st2/pants-plugins/uses_services/system_user_rules.py
Lines 80 to 85 in 1413e11
This rule needs the
PytestUsesSystemUserRequest
which selects targets that have theuses
field.st2/pants-plugins/uses_services/system_user_rules.py
Lines 63 to 69 in 1413e11
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/system_user_rules.py
Line 156 in 1413e11
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.update mongo bits to use pants idiom
This PR also updates the mongo detection rules to use pants to grab the
ST2TESTS_PARALLEL_SLOT
env var. I learned about how to do this recently, so I included that update here.Pants is configured to use
ST2TESTS_PARALLEL_SLOT
here:st2/pants.toml
Lines 229 to 234 in 1413e11
So, the
mongo_is_running_for_pytest
rule passes that var name here:st2/pants-plugins/uses_services/mongo_rules.py
Lines 110 to 113 in 1413e11
And this line tells pants to include the var in the env used when running
is_mongo_running.py
here:st2/pants-plugins/uses_services/mongo_rules.py
Line 157 in 1413e11
Finally the db name calculation is moved into
is_mongo_running.py
here:st2/pants-plugins/uses_services/scripts/is_mongo_running.py
Lines 52 to 56 in 1413e11