2017-05-11 36 views
0

如何編寫ruby_block的ChefSpec單元測試?如果局部變量是在配方中聲明的呢?它將如何處理?如何爲ruby_block資源編寫ChefSpec單元測試?

這裏有一個配方代碼:

package 'autofs' do 
    action :install 
end 

src = '/etc/ssh/sshd_config' 

unless ::File.readlines(src).grep(/^PasswordAuthentication yes/).any? 
    Chef::Log.warn "Need to add/change PasswordAuthentication to yes in sshd config." 
    ruby_block 'change_sshd_config' do 
    block do 
     srcfile = Chef::Util::FileEdit.new(src) 
     srcfile.search_file_replace(/^PasswordAuthentication no/, "PasswordAuthentication yes") 
     srcfile.insert_line_if_no_match(/^PasswordAuthentication/, "PasswordAuthentication yes") 
     srcfile.write_file 
    end 
    end 
end 

unless ::File.readlines(src).grep('/^Banner /etc/issue.ssh/').any? 
    Chef::Log.warn "Need to change Banner setting in sshd config." 
    ruby_block 'change_sshd_banner_config' do 
    block do 
     srcfile = Chef::Util::FileEdit.new(src) 
     srcfile.search_file_replace(/^#Banner none/, "Banner /etc/issue.ssh") 
     srcfile.insert_line_if_no_match(/^Banner/, "Banner /etc/issue.ssh") 
     srcfile.write_file 
    end 
    end 
end 

由於我是新來ChefSpec,我能夠編寫代碼的基本資源。我寫了如下單元測試:

require 'chefspec' 

describe 'package::install' do 

    let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '16.04').converge(described_recipe) } 

    it 'install a package autofs' do 
    expect(chef_run).to install_package('autofs') 
    end 

    it 'creates a ruby_block with an change_sshd_config' do 
    expect(chef_run).to run_ruby_block('change_sshd_config') 
    end 

    it 'creates a ruby_block with an change_sshd_banner_config' do 
    expect(chef_run).to run_ruby_block('change_sshd_banner_config') 
    end 

end 

上面的實現是否正確?我無法弄清楚如何將它寫入像ruby block等複雜資源。以及如何在配方中聲明局部變量時應該注意。 在此先感謝..

+0

那麼,這是你可以做的。 ChefSpec不運行ruby_block,它只是檢查,如果ruby_block在資源集合中有正確的動作。爲了測試ruby塊的結果,你必須編寫集成測試,例如使用test-kitchen和inspec。 –

回答

0

單元測試應測試特定的輸入產生預期的輸出。通過期待廚師有run_ruby_block,你實質上是在說「廚師是否按照我期望的方式工作」 - 而不是「我的ruby_block資源是否按我期望的方式工作」。

您應該使用ChefSpec來驗證ruby_block的副作用是您期望的副作用。也就是說,文件被修改。關於RenderFileMatchers的ChefSpec文檔可能是您要查找的內容。

+0

ChefSpec是一個單元測試框架,它不收斂節點並且不運行ruby_block,它只能檢查資源是否在最終資源集合中。 –

+0

呼叫良好。一般來說,這就是爲什麼我試圖避免ChefSpec一起支持諸如[Serverspec](http://serverspec.org/)之類的東西。 它應該是可能的,至少,嘲笑的文件,仍然測試'ruby_block'的副作用雖然,不是? – vase

+1

OP必須模擬':: File.readlines(...)。grep(...)。任何?'調用,然後檢查,如果'ruby_blocks'在資源集合中。 –

1
let(:conf) { double('conf') } 
    before do 
     allow(File).to receive(:readlines).with('/etc/ssh/sshd_config').and_return(["something"]) 
     allow(File).to receive(:readlines).with('/etc/ssh/sshd_config').and_return(["something"]) 
end 

it 'creates a ruby_block with an change_sshd_config' do 
    expect(chef_run).to run_ruby_block('change_sshd_config') 
    expect(Chef::Util::FileEdit).to receive(:new).with('/etc/ssh/sshd_config').and_return(conf) 
    confile = Chef::Util::FileEdit.new('/etc/ssh/sshd_config') 
    end 

對其他紅寶石塊做同樣的事情!大多數情況下,它會工作。

或者

您可以測試使用INSPEC來測試你的系統的狀態。看起來你可能想測試你的系統的狀態,以便在Chef應用config之後配置sshd。你可以用inspec完成類似下面的代碼片段:

describe file('/etc/ssh/sshd_config') do 
    its('content') { should match /whatever/ } 
end 

我希望這有助於!