InSpec test fails with Dir.foreach


#1

I am using a Dir.foreach 'loop' to run a control on every file in a given directory. When I use the Dir.foreach, the tests fail. If I use a describe block for an individual file however, the tests pass. With this code the controls fail:

Dir.foreach("/var/log") do | x |
  describe file(x) do
    its("owner") { should cmp "root" }
  end
end

However, with this code, with the file /var/log/yum.log for example, the tests pass:

describe file("/var/log/yum.log") do
  its("owner") { should cmp "root" }
end

Does anyone have an idea what the problem might be? I tried printing the value of 'x' on each line, and one thing I found was that yum.log (for example) is instead yum.log-20190101 Would this have anything to do with it?

Thanks in advance


#2

Do you run the tests locally or remotely? I had a problem with the Dir function running remote tests


#3

I'm running the tests remotely, but I just tried to run them remotely on a demo EC2 I have and the control still fails..


#4

For those reading this in the future, I have now fixed this issue. The loop is working fine but it was describing files in the '/' directory, rather than the '/var/log' directory. My code now looks like this and it works fine:

Dir.foreach("/var/log") do |x|
  describe file("/var/log/" + x) do
    its("owner") { should cmp "root" }
  end
end

@netfab