chef-infraserverspec

How to use Serverspec to test the utility installed in other user?


My requirement is that I need to run a utility that is installed in other user and I have to check the output returned from that session and verify it. Example :

Code with su :

describe command('su srijava ; cd /app/java; ./java --version') do
  its(:stdout) { should contain('1.7') }
end

Code without su:

describe command('cd /app/java; ./java --version') do
  its(:stdout) { should contain('1.7') }
end

Solution

  • describe command("su -c '/app/java/java --version' srijava") do
      its(:stdout) { should contain('1.7') }
    end
    

    FYI you are using RSpec 2 and soon to be obsolete Serverspec syntax for your matcher. Consider futureproofing it with:

    describe command("su -c '/app/java/java --version' srijava") do
      its(:stdout) { is_expected.to match(/1\.7/) }
    end