Abr*_*m P 4 ruby chef-infra chefspec
我在我的食谱中创建了以下模板文件:
template "my_file" do
path "my_path"
source "my_file.erb"
owner "root"
group "root"
mode "0644"
variables(@template_variables)
notifies :restart, resources(service: "my_service")
end
Run Code Online (Sandbox Code Playgroud)
以及我的ChefSpec测试中的以下断言:
chef_run.should create_file "my_file"
chef_run.file("my_file").should be_owned_by('root', 'root')
Run Code Online (Sandbox Code Playgroud)
这导致以下失败:
No file resource named 'my_file' with action :create found.
Run Code Online (Sandbox Code Playgroud)
这是因为我没有使用file资源而是使用template资源.问题:如何使用ChefSpec测试模板资源的文件创建?
有两种方法可以解决您的问题.
首先,您可以使用create_template匹配器.这将仅匹配运行上下文中的"模板"资源:
expect(chef_run).to create_template('my_file')
Run Code Online (Sandbox Code Playgroud)
此匹配器也是可链接的,因此您可以断言属性:
expect(chef_run).to create_template('my_file')
.with_path('my_path')
.with_owner('root')
Run Code Online (Sandbox Code Playgroud)
但是,此匹配器实际上不会呈现模板.因此,您无法检查是否正确设置了文件特异性.
还有用于任何类型的"文件"的顶级匹配(file,cookbook_file,和template)实际上在内存中呈现的内容:
expect(chef_run).to render_file('my_file').with_content(/^match me$/)
Run Code Online (Sandbox Code Playgroud)
您可以render_file在README中找到更多信息.
小智 5
根据文档(https://github.com/acrmp/chefspec),您应该可以使用:
expect(chef_run).to create_file 'my_file'
Run Code Online (Sandbox Code Playgroud)
我认为最近发生了一些变化(可能是rubygems上的chefspec版本),但是,因为我今天早些时候通过的测试(使用你正在使用的相同语法)现在突然失败了.