我有以下sudo用户的清单,加载模板文件
class sudo {
if $::operatingsystemmajrelease < 7 {
$variable = $::operatingsystemmajrelease ? {
'6' => $::fqdn,
}
file { '/etc/sudoers' :
ensure => present,
owner => 'root',
group => 'root',
mode => '0440',
content => template('sudo/sudoers.erb'),
}
}
}
以下是我的rspec文件
vim test_spec.rb
require 'spec_helper'
describe 'sudo' do
it { should contain_class('sudo')}
let(:facts) {{:operatingsystemmajrelease => 6}}
if (6 < 7)
context "testing sudo template with rspec" do
let(:params) {{:content => template('sudo/sudoers.erb')}}
it {should contain_file('/etc/sudoers').with(
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0440',
'content' => template('sudo/sudoers.erb'))}
end
end
end
在运行&#34; rake spec&#34;
时低于错误.F
Failures:
1) sudo testing sudo template with rspec
Failure/Error: 'content' => template('sudo/sudoers.erb'))}
NoMethodError:
undefined method `template' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x7f5802e70bd8>
# ./spec/classes/test_spec.rb:17
Finished in 0.16067 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/classes/test_spec.rb:12 # sudo testing sudo template with rspec
rake aborted!
ruby -S rspec spec/classes/test_spec.rb failed
任何人都可以指导我如何使用rspec-puppet测试模板。我在网上冲浪也超过两天没有人帮忙。
答案 0 :(得分:1)
您可以选择为内容提供字符串或正则表达式:
context 'with compress => true' do
let(:params) { {:compress => true} }
it do
should contain_file('/etc/logrotate.d/nginx') \
.with_content(/^\s*compress$/)
end
end
context 'with compress => false' do
let(:params) { {:compress => false} }
it do
should contain_file('/etc/logrotate.d/nginx') \
.with_content(/^\s*nocompress$/)
end
end
答案 1 :(得分:0)
template
是Puppet解析器的函数,因此不适用于您的单元测试。
如果确实想要将现有模板用作工具,则必须manually评估其ERB代码。
更好的单元测试方法是在测试代码中硬编码测试数据。
if (6 < 7)
context "testing sudo template with rspec" do
let(:params) {{:content => 'SPEC-CONTENT'}}
it {should contain_file('/etc/sudoers').with(
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0440',
'content' => 'SPEC-CONTENT'}
end
end
在接受或集成测试的领域中,使用实际有效的模板会更有趣。