0

我正在尝试设置一个 cronjob 来ls每天运行一次命令(在此示例中)。为此,我正在使用cron 资源

问题是我不知道如何使用 Inspect 对其进行测试。我尝试使用crontab,但它不起作用。

这是代码:

// code
cron 'my-ls' do
  minute '1'
  hour '0'
  command 'ls'
end

// test
describe crontab.commands('ls') do
  its('minutes') { should cmp '1' }
  its('hours') { should cmp '0' }
end

它没有说:

×  hours should cmp == "0"

     expected: "0"
          got: []

     (compared using `cmp` matcher)

     ×  minutes should cmp == "1"

     expected: "1"
          got: []

     (compared using `cmp` matcher)

PS:我也尝试过cron_d使用cron 食谱

4

1 回答 1

1

这是我可以使这些测试正常工作的最简单方法:

第 1 步:创建一个cronls.txt以此数据命名的文本文件:

1 0 * * * ls -al

第 2 步:使用以下命令将其转换为 cron 作业:

crontab -a cronls.txt

第 3 步:在您的 Chef 食谱中,将这些控件添加到您的default_test.rb

control 'cron-1' do
  describe crontab do
    its('commands') { should include 'ls -al' }
  end
end

control 'cron-2' do
  describe crontab.commands('ls -al') do
    its('minutes') { should cmp '1' }
    its('hours') { should cmp '0' }
  end
end

第 4 步:执行 InSpec 测试:

inspec exec test/integration/default/default_test.rb

结果是您所期望的:

  ✔  cron-1: crontab for current user
     ✔  crontab for current user commands should include "ls -al"
  ✔  cron-2: crontab for current user with command == "ls -al"
     ✔  crontab for current user with command == "ls -al" minutes should cmp == "1"
     ✔  crontab for current user with command == "ls -al" hours should cmp == "0"

这不是做到这一点的唯一方法(甚至不是最好的方法),但它应该让你继续前进。有关crontab资源的更多选项,请阅读 InSpec 文档:

https://docs.chef.io/resource_cron.html

于 2018-10-01T17:05:32.887 回答