StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
6.11k stars 746 forks source link

Fix unit tests to not require the `stanley` system_user? #6203

Open cognifloyd opened 6 months ago

cognifloyd commented 6 months ago

I'm working on getting pants to run our unit tests via pytest (I figure we can deal with integration tests later once all unit tests are running). I've got most of the st2common unit tests passing, but I've run into an assumption in our test suite that I would like to get rid of. The question is how.

Problematic assumption: That the system running our tests has the system_user (typically stanley).

For GHA, we had to add setup tasks that modify the conf files to use runner instead of stanley... But then we also create the stanley user "for ssh". For vagrant, adding a user is not a big ask. Similar for docker containers. But for local development (eg with vim, or with PyCharm, or whatever people prefer), I want to be able to at least run the unit tests without creating that user.

What's worse, the tests fail without indicating that the reason they failed was because the stanley user is not present. I noticed in the log output that the core.echo action was triggering the command: sudo -E -H -u stanley -- bash -c 'echo "<% ctx().msg1 %>"' which was weird because the sudo parameter was false in the test's sample workflow. Apparently, the local-command-runner will use sudo -u <system_user> if the current user is not the same as system_user.

How can we make the default system_user match the current user, at least for unit tests? Maybe whatever solution we come up with will lead to removing those GHA user setup tasks for the unit tests (requiring the user for integration tests doesn't bother me).

cognifloyd commented 6 months ago

Here's one solution: https://github.com/StackStorm/st2/pull/6202/commits/769e741c614c9ab79eee380e36885c5366c2660c

It doesn't set system_user.user to the current user by default, so people will still get weird errors until they figure out they need this and then they set the ST2TESTS_SYSTEM_USER env var with something like export ST2TESTS_SYSTEM_USER=${USER}. But, it's an improvement.