2

Is it possible to use one resource inside other resource in Chef InSpec?

Example:

 describe command('su srijava') do
     describe file ('/app/java/latest') do     
         it{ should exist }
     end
 end

It throws an error like:

`method_missing': undefined method `file' for RSpec::ExampleGroups::CommandSuSriava:Class (NoMethodError)

Actually what I want to do 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 Inspec 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 match('1.7') }
 end

Code without su:

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

How can I do that?

techraf
  • 64,883
  • 27
  • 193
  • 198
saurg
  • 327
  • 1
  • 3
  • 17

2 Answers2

1

As Noah pointed out, nested describe blocks are not supported yet. I also think you do not need those.

result = command('runcommand').stdout
filename = result + '/path'
describe file (filename) do     
  it{ should exist }
end

On the other hand, you could use the bash resource to run multiple commands. command uses the default shell of the user, bash enforces it. This enables you to:

describe bash('su srijava ; cd /app/java; ./java --version') do
  its('stdout') { should match('1.7') }
end
0

No, nor does that make any sense because the two wouldn't be combined even in normal RSpec. If your issue is you need to check a file as root you can use InSpec's sudo options. I'm not sure why you would want to check if a file exists as a certain user, but that isn't supported.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • `su` runs a new shell as the target user. It doesn't magically affect commands later in the same shell. You want something like `sudo -u whatever bash -c "cd /foo; something"` – coderanger Jul 01 '16 at 19:58
  • This is not working. I did it like `describe command('sudo -u whatever bash -c "cd /foo; something" ') do it { should exist } end` and It is saying like `expected command sudo -u whatever bash -c "cd /foo; something" to exist` – saurg Jul 04 '16 at 10:20
  • The `command` resource in InSpec doesn't respond to the `exist` matcher. Commands have matchers for things like stdout/err content and exit status. – coderanger Jul 05 '16 at 00:47
  • `command` resource does have `exist` matcher. See [this](https://docs.chef.io/inspec_reference.html#id28) for reference. – saurg Jul 05 '16 at 05:08