2015-06-14 69 views
0

我有以下表现sudo的用户,加载模板文件如何添加模板文件中的RSpec木偶

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 

得到以下错误,当运行“耙规范“

.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

template是木偶解析器的功能,因此是不可你的单元测试。

如果你真的想要使用你现有的模板作为夹具,你将不得不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 

使用实际有效的模板在验收或集成测试领域更有趣。

+0

谢谢,但我尝试不让(:params),但它的工作。我们是否需要为这种情况创建参数。 – Puppeteer

+0

@Puppeteer我不害怕。 –

+0

下面是我没有param的spec文件,“describe'sudo'do it {should contain_class('sudo')} let(:facts){{:operatingsystemmajrelease => 6}} if(6 <7)context”testing sudo template ('确保'=>'present','owner'=>'root','group'=>'root','mode'= rspec“do it {should contain_file('/ etc/sudoers')。 >'0440')}它{应该包含文件('/ etc/sudoers')。with_content(/ Defaults requiretty /)} end end end“ – Puppeteer

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 

全面介绍:http://rspec-puppet.com/tutorial/

+0

我试过,得到“1)sudo测试与rspec的sudo模板失败/错误:'模式'=>'0440')} Puppet ::错误:无效的参数压缩在节点#上的第4行的类[Sudo]。 /spec/classes/test_spec.rb:16 2)sudo使用rspec测试sudo模板失败/错误:它应该包含文件('/ etc/sudoers')。with_content(/^\ s * compress $ /)Puppet :: Error :在节点#./spec/classes/test_spec.rb:20“第4行的类[Sudo]上无效的参数压缩。在rspec-puppet中加载模板文件是不可能的,因为我的清单使用加载erb文件来获取事实。所以我想用加载模板进行测试。 – Puppeteer