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.useras an actual local user on the machine running pytest. That is now configurable with theST2TESTS_SYSTEM_USERenv var added in #6242. If someone forgot to set--or did not know they could set--theST2TESTS_SYSTEM_USERenv 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_servicesplugin, which was added in these PRs (the PR descriptions can be helpful in understanding theuses_servicesplugin):pants-plugins/uses_servicesto check before running tests for required services (like mongo) #5864pants-plugins/uses_servicesto support checking for rabbitmq #5884pants-plugins/uses_servicesto support checking for redis #5893pants-plugins/uses_servicesimprovements #5898has_system_user.pyscript and the rule that runs ithas_system_user.pyis the part that checks to see if the system_user is present on the system.The
has_system_user.pyscript gets opened and run in a pex with the similar rule logic to themongo,redis, andrabbitmqdetection rules.Only
system_userhas to be passed to thehas_system_user.pyscript 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
HasSystemUserwith aUsesSystemUserRequest, pants will also run the rule that generatesPlatform(described in #5864), and then it will run thishas_system_userrule.The
has_system_userrule either returnsHasSystemUser()if the user is present, or raisesServiceMissingErrorif 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 asucceededboolean instead. But, we want to error as early as possible, so this breaks that convention on purpose.wiring up the
testgoal so it runshas_system_userwhenpytestruns on a target with theusesfield.The last piece that ties this all together is a rule that makes sure the
has_system_userrule runs beforepytestruns (if pants is running it on a target with theusesfield). Here is the definition of thehas_system_user_for_pytestrule: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_varssetting defined here:st2/pants.toml
Lines 242 to 247 in 1413e11
Then it just triggers running the
has_system_userrule, passing in the system_user:st2/pants-plugins/uses_services/system_user_rules.py
Lines 80 to 85 in 1413e11
This rule needs the
PytestUsesSystemUserRequestwhich selects targets that have theusesfield.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
usessupport 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 ourusesrules 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_SLOTenv var. I learned about how to do this recently, so I included that update here.Pants is configured to use
ST2TESTS_PARALLEL_SLOThere:st2/pants.toml
Lines 229 to 234 in 1413e11
So, the
mongo_is_running_for_pytestrule 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.pyhere:st2/pants-plugins/uses_services/mongo_rules.py
Line 157 in 1413e11
Finally the db name calculation is moved into
is_mongo_running.pyhere:st2/pants-plugins/uses_services/scripts/is_mongo_running.py
Lines 52 to 56 in 1413e11