InSpec + TestKitchen: How to execute command on behalf of specific user?

953 views Asked by At

My Chef cookbook installs Ruby locally for specific user. I want to write an InSpec test that checks if specific version of Ruby is available and if gems are installed without documentation.

So obviously I somehow need to run commands on behalf of the user which has rbenv installation. I decided to stick with sudo su -c. The test for version works correctly:

describe command('su - rbenv_user -c "ruby -v"') do
  its('stdout') {should match ('ruby 2.3.1')}
end

But when I try to install gem:

describe command('su - rbenv_user -c "gem install bundler"') do
  its('stderr') {should match ('Successfully installed')}
  its('stderr') {should_not match ('Parsing documentation for')}
end

I get an error:

ERROR: While executing gem ... (Errno::EACCES) Permission denied @ rb_sysopen - /apps/rbenv_user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/.codeclimate.yml

I also tried replacing of su - user -c ... with runuser -l user -c ... and it leads to the same result.

I have no idea why this happens and what other options I have to test as another users.

1

There are 1 answers

0
slashpai On

You can specify the user when you run inspec test. Refer https://github.com/chef/inspec/issues/362