goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.57k stars 472 forks source link

Run command test as specific user other than root #600

Closed diraol closed 4 years ago

diraol commented 4 years ago

Hi, I couldn't find any reference or issue to something like this. If there is already one, please just point me to it!

What I would like to know is if there is a way to run command tests as a specific user other than root (or the one being used).

My context is that I'm building an Image (AWS AMI) and running the tests as root. But I'd like to know if a specific command is available to a user (jenkins) and is executable by this user.

Being more specific, I want to install awscli and yamllint, both are installed using python pip "package manager", but by installing it in the system using root I have faced issues when trying to execute using jenkins user.

So, what I would like to test using goss is running yamllint --version with jenkins user.

Any suggestion?

Thanks! :)

aelsabbahy commented 4 years ago

I would just use su -c or su -lc if you need a login shell.

example:

goss a command 'su someuser -c "echo \$PATH;whoami"'

or if you need a login shell:

goss a command 'su someuser -lc "echo \$PATH;whoami"'

Depending on where some of your $PATH and other things are defined, sometimes you need the -l loginshell.

Let me know if that resolves it for you.

diraol commented 4 years ago

Yeah, that's one of the possibilities that I thought of.

Another approach I'm trying is to run goss twice, one as root with a gossfile specifying tests to be executed as root and another one with the given user passing another gossfile.yml:

su -l jenkins -c "goss ...."

But I guess I'll stick with the first option because the second approach brings the need of merging goss output reports (junit)... it is getting messy... :)

Anyway, thanks for the fast answer!

If I face problems I'll reopen the issue.