Hi all,
I work on a crate for managing control groups under Linux and I'm trying to add some tests, however most of these tests require running with elevated privileges (read: sudo) because the kernel doesn't allow a normal user to modify the cgroup tree.
Unfortunately, simply running sudo cargo test results in error: no default toolchain configured.
I currently have three workarounds to this:
1) sudo -E cargo test, which keeps the normal user's environment but the effective user will still be the root user.
2) Manually run each generated test binary with sudo target/debug/...
3) I created a script that I run _manually_ before each test run to create the appropriate cgroups and use chown/chmod to give permissions to the user.
Now, neither of these solutions are "perfect" in my mind:
Finally, I see two solutions to this, one of which I have already implemented:
1) Add a new --elevated option to cargo test which executes the test binary under sudo. I have implemented this here.
2) Add a new annotation to tests (i.e., #[elevated]) that automatically executes the tests under sudo. (Which would still ask for a password to confirm).
But maybe there is a better solution to this that I'm not aware of?
Thanks!
Could target.$target.runner be used perhaps? That's a way you can specify an executable to wrap tests and you could probably get something like this working:
# in .cargo/config
[target.x86_64-unknown-linux-gnu]
runner = 'sudo -E'
and then in your project:
cargo test
That's definitely a way more elegant solution than mine, thanks @alexcrichton !
Most helpful comment
Could
target.$target.runnerbe used perhaps? That's a way you can specify an executable to wrap tests and you could probably get something like this working:and then in your project: