How to enable inspec.command('ls') on kitchen-docker?


#1

Per the following page,
http://inspec.io/docs/reference/ruby_usage/

I should be able to run the following in my …/cook_book/test/integration/default/inspec/default_inspe.rb
inspec.command(‘ls’)

But I got
undefined local variable or method `inspec’

How to fix this?


#2

Hi @Victor I am not sure I understand what failed. In your test, your can check for ls in your inspec test

describe command('ls') do
  its('stdout') { should_not eq ''}
end

Is that answering your question?


#3

chris-rock, what I wanted to do was to obtain file names during compile time. Then use the file names during execution time.
The code in your reply won’t run until execution time. I can’t use its stdout in another resource block such as describe file.


#4

Hi Victor,

inspec.command is called from the resource code. If you want to run a command, use the command resource that Chris pointed out. InSpec has only one phase of execution. I believe the code you are looking for looks like this:

apache_files = command('ls -d /etc/apache2/*').stdout.split("\n")

apache_files.each { |apache_file|
  describe file(apache_file) do
    its('owner') { should eq 'root'}
  end
}

It gives me the following output:

$ inspec exec ~/tmp/a.rb

Target:  local://

  File /etc/apache2/extra
     ✔  owner should eq "root"
  File /etc/apache2/httpd.conf
     ✔  owner should eq "root"
  File /etc/apache2/magic
     ✔  owner should eq "root"

Test Summary: 3 successful, 0 failures, 0 skipped

Hope this helps,
Alex


#5

Worked. Thanks!