我正在尝试编写一个ChefSpec测试,以检查配方是否仅在目录不存在时才创建该目录。我通过了“创建目录”的第一次测试,但第二次测试失败了。食谱在下面。有没有人能帮我把第二部分弄对?因为如果该目录存在,则第一次测试将失败。我必须删除该目录才能使第一个测试通过,然后第二个测试无论如何都会失败。
require 'spec_helper'
describe 'my_cookbook::default' do
context 'Windows 2012' do
let(:chef_run) do
runner = ChefSpec::ServerRunner.new(platform: 'Windows', version: '2012')
runner.converge(described_recipe)
end
it 'converges successfully' do
expect { chef_run }.to_not raise_error
end
it 'creates directory' do
expect(chef_run).to create_directory('D:\test1\logs')
end
it 'checks directory' do
expect(chef_run).to_not create_directory( ::Dir.exists?("D:\\test1\\logs") )
end
end
end这就是它的配方,它本身就像预期的那样工作,但我似乎无法围绕它写一个测试。
directory "D:\\test1\\logs" do
recursive true
action :create
not_if { ::Dir.exists?("D:\\test1\\logs") }
end发布于 2019-01-24 14:58:28
not_if或only_if是厨师guards
然后使用
属性来告诉chef-client它是否应该继续执行资源
为了使用chefspec测试您的directory resource,您将必须将保护存根,以便在chefspec编译您的资源时,您希望not_if保护的计算结果为真或假。
为了让ChefSpec知道如何评估资源,我们需要告诉它,如果命令在实际计算机上运行,该命令将如何返回此测试:
describe 'something' do
recipe do
execute '/opt/myapp/install.sh' do
# Check if myapp is installed and runnable.
not_if 'myapp --version'
end
end
before do
# Tell ChefSpec the command would have succeeded.
stub_command('myapp --version').and_return(true)
# Tell ChefSpec the command would have failed.
stub_command('myapp --version').and_return(false)
# You can also use a regexp to stub multiple commands at once.
stub_command(/^myapp/).and_return(false)
end
endhttps://stackoverflow.com/questions/54298134
复制相似问题