6

我对 Ruby 比较陌生,但在过去的两周里,我一直在对 Chef 测试进行大量研究。这个测试使用 ChefSpec 和 Fauxhai,但它看起来不是很“红宝石”,我希望社区能给我一些关于编码风格的建议。有没有更好的方法来编写这样的嵌套循环?

食谱/foo/recipes/default.rb

package "foo" do
  action :install
end

食谱/foo/spec/default_spec.rb

require 'chefspec'

describe 'foo::default' do
  platforms = { 
    "debian"   => ['6.0.5'],
    "ubuntu"   => ['12.04', '10.04'],
    "centos"   => ['5.8', '6.0', '6.3'],
    "redhat"   => ['5.8', '6.3'],
    "mac_os_x" => ['10.6.8', '10.7.4', '10.8.2'],
    "windows"  => ['2008R2']
  }

  platforms.each do |platform,versions|
    versions.each do |version|
      context "on #{platform} #{version}" do
        before do
          Fauxhai.mock(platform: platform, version: version)
        end

        it 'should install foo' do
          @runner = ChefSpec::ChefRunner.new.converge('foo::default')
          @runner.should install_package 'foo'
        end
      end
    end
  end
end

欢迎任何和所有反馈。谢谢!

4

1 回答 1

6

首先,一种常见的做法是将ChefRunner实例化提取到let帮助程序。您还可以在其中包含所有 Fauxhai 配置:

let(:chef_run) do
  ChefSpec::ChefRunner.new(platform: platform, version: version) do |node|
    node.set['foo']['bar'] = 'baz'
    # ....
  end.converge('foo::default')
end

it "installs foo" do
  expect(chef_run).to install_package 'foo'
end

expect语法似乎被推荐超过should. 但在本例中,我将使用单线:

subject do
  ChefSpec::ChefRunner.new(platform: platform, version: version).converge('foo::default')
end
it { should install_package 'foo' }

要稍微清理一下循环,您可以使用RSpec 的共享示例。一个更扩展的例子:

require 'chefspec'

shared_examples 'foo' do |platform, version|
  context "on #{platform} #{version}" do
    let(:users) { %w[user1 user2] }
    let(:chef_run) do
      ChefSpec::ChefRunner.new(platform: platform, version: version) do |node|
        node.set['foo']['users'] = users
      end.converge('foo::default')
    end
    subject { chef_run }

    it { should install_package 'foo' }

    it "creates specified users" do
      users.each { |u| expect(chef_run).to create_user u }
    end
  end
end

describe 'foo::default' do
  platforms = {
    'debian'   => ['6.0.5'],
    'ubuntu'   => ['12.04', '10.04'],
    'centos'   => ['5.8', '6.0', '6.3'],
    'redhat'   => ['5.8', '6.3'],
    'mac_os_x' => ['10.6.8', '10.7.4', '10.8.2'],
    'windows'  => ['2008R2']
  }

  platforms.each do |platform, versions|
    versions.each do |version|
      include_examples 'foo', platform, version
    end
  end
end
于 2013-07-02T17:28:35.417 回答