3

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 :

  • I installed java as srijava user
  • Now in Serverspec I wrote the command to test the Java version (Assume that the java -version runs only in that user and not as root).
  • if I use su srijava, then I do not get the output returned back to the root session and the test fails.
  • If I run without su srijava then my utility will throw an error that the user is not SriJava

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
mkobit
  • 43,979
  • 12
  • 156
  • 150
Sri
  • 41
  • 4

1 Answers1

2
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
mkobit
  • 43,979
  • 12
  • 156
  • 150
Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • Should with no receiver isn't deprecated AFAIK, it's the `object.should` that got whacked. – coderanger Jun 13 '16 at 20:22
  • @coderanger I didn't say anything about deprecated anything. What are you referring to? – Matthew Schuchard Jun 13 '16 at 21:42
  • You said "Rspec 2" and "obsolete", I think because you confused the two forms of using `should`. The receiverless is still present in Rspec 3 and is not obsolete (and is used in all Serverspec examples). As such, you should remove that part of the answer as it is incorrect :) – coderanger Jun 13 '16 at 23:14
  • @coderanger I still have no idea what you are talking about. I said it was using RSpec 2 (`should`) and soon to be obsolete Serverspec (`contain`) matcher syntax. This is absolutely and unequivocally correct. I recommend reading RSpec and Serverspec documentation if this is still unclear because I don't want to have a big side instruction in the comments here. Thanks. – Matthew Schuchard Jun 14 '16 at 11:09