【问题标题】:Chefspec: override attribute doesn't workChefspec:覆盖属性不起作用
【发布时间】:2020-02-14 17:07:34
【问题描述】:

我正在尝试使用 bundle exec rspec 检查我的规范测试以进行以下测试

require 'spec_helper'

describe 'my_recipe::default' do
  windows_platforms = {
    windows: %w(2008R2 2012 2012R2 2016 2019)
  }

  windows_platforms.each do |platform, versions|
    versions.each do |version|
      context "When all attributes are default, on #{platform} #{version}" do
        let(:chef_run) do
          runner = ChefSpec::ServerRunner.new(platform: platform.to_s, version: version) do |node|
            node.override['domain'] = 'mydomain.com'
          end
          runner.converge(described_recipe)
        end

        it 'converges successfully' do
          expect { chef_run }.to_not raise_error
        end
      end
    end
  end
end

这是我的默认属性文件的示例:

default['wsus'] =
  case node['domain']
  when 'mydomain.com'
    "do something"
  else
    raise 'Domain cannot be determined.'
  end

据我了解,'mydomain.com' 应该被指定为默认值['domain'],但似乎并非如此。

这是我得到的错误:

expected no Exception, got #<RuntimeError: Domain cannot be determined.> with backtrace:

有没有人建议为什么测试不会采用被覆盖的属性?

附言如果我没有道理,请原谅我。这是我在 stackoverflow 上的第一篇文章 :(

【问题讨论】:

    标签: rspec chef-infra chefspec


    【解决方案1】:

    您的代码很好,它应该可以按预期使用许多其他属性,但不是domain。要点是 - domain特殊 - 它是 Ohai 设置的自动属性(或在 Fauxhai 的测试用例中),它具有最高优先级,不能被 override 属性覆盖。要模拟自动属性,您需要 automatic 键。

    runner = ChefSpec::ServerRunner.new(platform: platform.to_s, version: version) do |node|
      node.automatic['domain'] = 'mydomain.com'
    end
    

    Chef docs on Ohai 中提供了完整的自动属性列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多