0

テンプレートファイルを読み込んでいる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

「rake spec」を実行するとエラーを下回る

.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 を使用してテンプレートをテストする方法を教えてください。ネットサーフィンも頭を壊しましたが、2日以上は役に立ちませんでした。

4

2 に答える 2

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/

于 2015-06-14T22:06:58.423 に答える
0

templateは Puppet パーサーの機能であるため、単体テストでは使用できません。

既存のテンプレートをフィクスチャとして本当に使用したい場合は、その 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

実際の有効なテンプレートを使用することは、受け入れテストやおそらく統合テストの領域でより興味深いものになります。

于 2015-06-16T14:17:10.560 に答える