2017-08-21 43 views
2

我創建了一個簡單的木偶4類和單元測試與它一起去如下(執行後touch metadata.json; rspec-puppet-init而在modules/test/):正確啓動RSpec的木偶單元方法進行試驗,

# modules/test/manifests/hello_world1.pp 
class test::hello_world1 { 
    file { "/tmp/hello_world1": 
    content => "Hello, world!\n" 
    } 
} 

# modules/test/spec/classes/test__hello_world1_spec.rb 
require 'spec_helper' 
describe 'test::hello_world1' do 
    it { is_expected.to compile } 
    it { is_expected.to contain_file('/tmp/hello_world1')\ 
    .with_content(/^Hello, world!$/) } 
end 

我可以成功通過在modules/test/中執行rspec spec/classes/test__hello_world1_spec.rb來運行單元測試。

我現在想繼續使用代碼從另一個模塊一個稍微更先進的階級,即concat(模塊已arleady被安裝在modules/concat):

# modules/test/manifests/hello_world2.pp 
class test::hello_world2 
{ 
    concat{ "/tmp/hello_world2": 
    ensure => present, 
    } 
    concat::fragment{ "/tmp/hello_world2_01": 
    target => "/tmp/hello_world2", 
    content => "Hello, world!\n", 
    order => '01', 
    } 
} 

# modules/test/spec/classes/test__hello_world2_spec.rb 
require 'spec_helper' 
describe 'test::hello_world2' do 
    it { is_expected.to compile } 
    # ... 
end 

當我試圖運行這個單元測試與rspec spec/classes/test__hello_world2_spec.rb而在modules/test我接收包括的錯誤消息:

Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Unknown resource type: 'concat'

我懷疑的根本原因是,rspec找不到其它模塊(多個) ,因爲它沒有被告知「模塊路徑」。

我的問題是:我該如何開始單元測試,尤其是那些需要訪問其他模塊的單元測試?

回答

3

從您的平臺download page安裝PDK。使用pdk new modulepdk new class或者按照Guide重新創建模塊。

現在,我來談談代碼中可能存在的問題:代碼取決於Puppet Forge模塊puppetlabs/concat,但您尚未將其提供。 PDK模塊模板已經預先配置puppetlabs_spec_helper來爲您的模塊加載燈具。

告訴puppetlabs_spec_helper得到它你,你需要一個文件.fixtures.yml,內容如下:

fixtures: 
    forge_modules: 
    stdlib: puppetlabs/stdlib 
    concat: puppetlabs/concat 

請注意,您還需要puppetlabs/stdlib,因爲那是puppetlabs/concat的依賴。

如果你想探索更多的夾具可能性,請參閱puppetlabs_spec_helper's docs

有了這一切到位,並整合您發佈到由PDLK提供的初始代碼框架代碼樣本和測試的內容,你的測試都將現在通過在運行:

$ pdk test unit 

注意我已經在博客文章中撰寫了關於底層技術的所有內容,展示瞭如何從頭開始設置Rspec-puppet和更多(ref),它似乎仍然是此主題上最新的參考。

要了解有關rspec-puppet的更多信息,請參考官方rspec-puppet docs site