Flo*_*uer 5 rspec chef-infra chefspec
我创建了一个Chef资源,它"扩展"了chef的部署资源.基本思想是检查是否存在deploy/crontab类似于deploy/after_restart.rb要部署的源中的机制的文件,并从中创建cronjobs.
虽然这种机制应该可行(参见https://github.com/fh/easybib-cookbooks/blob/t/cron-tests/easybib/providers/deploy.rb#L11-L14),但我正在努力基于ChefSpec的测试.我目前正在尝试使用FakeFS- 但是当我在Chef运行之前模拟Filesystem时,运行失败,因为没有找到cookbook,因为它们不存在于模拟文件系统中.如果我不deploy/crontab这样做,显然没有找到模拟文件,因此提供者不会做任何事情.我目前的方法是在chef_run FakeFS.activate!之前直接触发runner.converge(described_recipe).
我很想听听一些建议如何在这里进行正确的测试:是否有可能只在部署资源运行之前直接启用FakeFS,或者仅部分模拟文件系统?
我在存根文件系统类时遇到了类似的问题。我一直在解决这个问题的方法如下。
::File.stub(:exists?).with(anything).and_call_original
::File.stub(:exists?).with('/tmp/deploy/crontab').and_return true
open_file = double('file')
allow(open_file).to receive(:each_line).and_yield('line1').and_yield('line2')
::File.stub(:open).and_call_original
::File.stub(:open).with('/tmp/deploy/crontab').and_return open_file
Run Code Online (Sandbox Code Playgroud)
由于 punkle 的解决方案在语法上已被弃用,并且缺少一些部分,我将尝试给出一个新的解决方案:
require 'spec_helper'
describe 'cookbook::recipe' do
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
file_content = <<-EOF
...here goes your
multiline
file content
EOF
describe 'describe what your recipe should do' do
before(:each) do
allow(File).to receive(:exists?).with(anything).and_call_original
allow(File).to receive(:exists?).with('/path/to/file/that/should/exist').and_return true
allow(File).to receive(:read).with(anything).and_call_original
allow(File).to receive(:read).with('/path/to/file/that/should/exist').and_return file_content
end
it 'describe what should happen with the file...' do
expect(chef_run).to #...
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4045 次 |
| 最近记录: |