InSpec - checking files/directories when testing remote hosts

#1

Hey there!
I have an InSpec control, which is executed against remote hosts. What I want to achieve is to check the modification time of all the files in a specific location. Works fine on localhost. However, when running against a remote system, the log file arrays are evaluated locally, which means InSpec looks into the directories on the localhost, fills the arrays and then tries to access all the files on the remote hosts. Some of them exist, but most of them don't so I get a bunch of errors. Is this intended? And how can I achieve that the arrays are filled with actual values on the remote host?
I already tried to put the first to lines into the control, directly before the forEach loop, did not help.
Any ideas? Help would be much appreciated!

nginx_log_files = Dir["/var/log/nginx/."]
mysql_log_files = Dir["/var/log/mysql/."]

control:

control 'testcase-19-os' do
impact 1.0
title 'Log files'
desc 'Check whether log file modification time is <30 days'
nginx_log_files.each do |path|
describe file(path) do
its('mtime') { should >= Time.now.to_i - 30246060 }
end
end
mysql_log_files.each do |path|
describe file(path) do
its('mtime') { should >= Time.now.to_i - 30
246060 }
end
end
end

output:
× testcase-19-os: Log files (13 failed)
× File /var/log/nginx/error.log.1 mtime should >= 1546180951
undefined method >=' for nil:NilClass × File /var/log/nginx/access.log mtime should >= 1546180951 undefined method>=' for nil:NilClass
× File /var/log/nginx/error.log mtime should >= 1546180951
undefined method >=' for nil:NilClass × File /var/log/nginx/access.log.2.gz mtime should >= 1546180951 undefined method>=' for nil:NilClass
× File /var/log/nginx/access.log.1 mtime should >= 1546180951
undefined method >=' for nil:NilClass ✔ File /var/log/mysql/error.log.5.gz mtime should >= 1546180951 × File /var/log/mysql/mysql.log.1.gz mtime should >= 1546180952 undefined method>=' for nil:NilClass
:heavy_check_mark: File /var/log/mysql/error.log.1.gz mtime should >= 1546180952
× File /var/log/mysql/mysql.log mtime should >= 1546180952
undefined method >=' for nil:NilClass × File /var/log/mysql/mysql-slow.log.2.gz mtime should >= 1546180952 undefined method>=' for nil:NilClass
× File /var/log/mysql/mysql-slow.log.3.gz mtime should >= 1546180952
undefined method >=' for nil:NilClass × File /var/log/mysql/mysql.log.2.gz mtime should >= 1546180952 undefined method>=' for nil:NilClass
× File /var/log/mysql/mysql.log.3.gz mtime should >= 1546180952
undefined method >=' for nil:NilClass ✔ File /var/log/mysql/error.log.2.gz mtime should >= 1546180952 ✔ File /var/log/mysql/error.log.3.gz mtime should >= 1546180953 × File /var/log/mysql/mysql-slow.log.1.gz mtime should >= 1546180953 undefined method>=' for nil:NilClass
:heavy_check_mark: File /var/log/mysql/error.log.7.gz mtime should >= 1546180953
:heavy_check_mark: File /var/log/mysql/error.log mtime should >= 1546180953
× File /var/log/mysql/mysql-slow.log mtime should >= 1546180953
undefined method `>=' for nil:NilClass
:heavy_check_mark: File /var/log/mysql/error.log.4.gz mtime should >= 1546180953

InSpec test fails with Dir.foreach
#2

Okay, turns out this is not really InSpec related but just the way the 'Dir' resource within Ruby works.
I found a workaround, not sure if it's the smartest one but I'll share it anyway:

Instead of using the Dir function I invoked a command on the remote host to list all the files in the desired directory and then split it so that I could fill my arrays with it:

nginx_log_files = command('ls /var/log/nginx/.').stdout.split("\n")
mysql_log_files = command('ls /var/log/mysql/.').stdout.split("\n")