Search code examples
rspecchef-infrainspec

Nested resources in Chef InSpec


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?


Solution

  • 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