chefspec:render_file无法检查在另一个上下文中删除的模板内容

时间:2017-07-24 15:10:50

标签: chef chefspec

考虑以下方法:

  recipe_name = 'mongodb'
  service_name = if node['szdigi'][recipe_name]['version'].to_f < 2.6
                   'mongodb'
                 else
                   'mongod'
                 end

  conffile = "/etc/#{service_name}.conf"

  # Add configuration
  template conffile do
    source 'mongodb/mongodb-conf.erb'
    owner 'root'
    group 'root'
    mode 0o644
  end

  # Remove old config file
  file '/etc/mongodb.conf' do
    action :delete
    only_if { node['szdigi'][recipe_name]['version'].to_f >= 2.6 }
  end

......以及以下chefspec测试:

require_relative 'spec_helper'

describe 'szdigi::mongo_test' do
  before(:all) do
    @conf_file = '/etc/mongodb.conf'
  end

  context 'Mongo 2.4' do
    let(:chef_run) do
      runner = ChefSpec::SoloRunner.new
      runner.node.normal['szdigi']['mongodb']['version'] = '2.4.14'
      runner.converge(described_recipe)
    end

    it 'creates mongo conf file' do
      expect(chef_run).to create_template(@conf_file)
        .with_mode(0o644)
        .with_owner('root')
        .with_group('root')
    end

    it 'does not remove /etc/mongodb.conf' do
      expect(chef_run).not_to delete_file(@conf_file)
    end

    it 'defines authentication options' do
      expect(chef_run).to render_file(@conf_file).with_content { |content|
        expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
      }
    end
  end

  context 'Mongo 2.6' do
    before :all do
      @conf_file = '/etc/mongod.conf'
    end

    let(:chef_run) do
      runner = ChefSpec::SoloRunner.new
      runner.node.normal['szdigi']['mongodb']['version'] = '2.6.12'
      runner.converge(described_recipe)
    end

    it 'creates mongo conf file' do
      expect(chef_run).to create_template(@conf_file)
        .with_mode(0o644)
        .with_owner('root')
        .with_group('root')
    end

    it 'defines authentication options' do
      expect(chef_run).to render_file(@conf_file).with_content { |content|
        expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
      }
    end

    it 'removes 2.4 configuration file /etc/mongodb.conf' do
      expect(chef_run).to delete_file('/etc/mongodb.conf')
    end
  end
end

配方完全符合我的要求:当版本属性设置为2.4.x时创建/etc/mongodb.conf,而/etc/mongod.conf创建auth = true/etc/mongodb.conf。在后一种情况下,它还删除了现在过时的file '/etc/mongodb.conf'

但是,只要配方包含it 'defines authentication options'资源,chefspec就会在Mongo 2.4上下文中的Failure/Error: expect(chef_run).to render_file(@conf_file).with_content { |content| expect(content).to match(/^\s*auth\s*=\s*true\s*$/) } expected Chef run to render "/etc/mongodb.conf" matching: (the result of a proc) but got: 上失败:

render_file

知道我应该如何在2.6上下文中重写CEFSharp测试才能成功?

由于

帕特里夏

1 个答案:

答案 0 :(得分:0)

我会使用单个顶级if node['szdigi'][recipe_name]['version'].to_f >= 2.6,然后将配方重组为两个分支。使用聪明的flow-y位很好,但不是。