rspec为厨师资源

时间:2017-03-23 14:47:02

标签: rspec automation chef

我是rspec的新手,我试着写一本规格测试我的食谱,但它失败了。当我尝试测试"厨师执行时,它无法找到资源和输出:"找不到厨师资源,跳过覆盖率计算..."。

以下是代码和规范:

group sq_group do
  system true
end

user sq_user do
  gid sq_group
  system true
end

以上代码适用于组和用户,下面是我的测试用户和组的rspec。

require 'spec_helper'
ChefSpec::Coverage.start!

describe 'recipes::default' do
  context 'Mostly defaults' do
    let(:chef_run) do
      ChefSpec::SoloRunner.new(platform: 'platform_name') do |node|
        default['cookbookname']['user'] = 'user'
        default['cookbook']['group'] = 'group'
      end.converge(default)
    end
  end
end

1 个答案:

答案 0 :(得分:1)

让我们假设您的食谱名为“user.rb”,您将拥有以下规格:

require 'spec_helper'

describe 'lecturio_ds::user' do
  context 'When all attributes are default, on an unspecified platform' do
    let(:chef_run) do
      runner = ChefSpec::ServerRunner.new
      runner.converge(described_recipe)
     end

    it 'creates group' do
      expect(chef_run).to create_group('sq_group')
      .with(
         system: true
       )
    end

    it 'creates user' do
      expect(chef_run).to create_user('sq_user')
      .with(
         gid: 'sq_group'
         system: true
       )
    end

    it 'converges successfully' do
     chef_run # This should not raise an error
    end

  end
end

当您创建新配方时,您应该使用chef generate recipe cookbooks/[cookbook_name] [recipe_name]并获得自动生成的配方文件,您需要填写正确的测试。