2015-06-14 66 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