可以在 Inspec 中使用 Chef 变量吗?

Possible to use Chef variables within Inspec?

我正在学习为我的 Chef 食谱编写集成测试。是否可以在我的测试中引用 attributes 文件夹中的变量?

这是我的测试,以确保 httpdphp 安装正确。但是,我还有其他包裹要检查。

test/smoke/default/install.rb

%w(httpd php).each do |rpm_package|
  describe package(rpm_package) do
    it { should be_installed }
  end
end

attributes/default.rb

default['ic_apachephp']['php_packages'] = [
                              'php-mysqlnd',
                              'php-mbstring',
                              'php-gd',
                              'php-xml',
                              'php'
                            ]

是的,这是可能的,但不是 "directly"。 Matt Wrock 在 blog entry.

中对其进行了描述

必要的步骤是:

  • 添加食谱(例如 export-node,在 test/fixtures/cookbooks/ 下方),其中包含具有以下内容的食谱(将节点属性转储到指定的 JSON 文件中):

    ruby_block "Save node attributes" do
      block do
        IO.write("/tmp/kitchen_chef_node.json", node.to_json)
      end
    end
    
  • 将此食谱添加到 .kitchen.yml

  • 中的 运行 列表
  • 使用

    在您的 inspec 测试中加载 node 对象
    node = json('/tmp/kitchen_chef_node.json').params
    

请注意,您必须手动从正确的属性优先级别 (automatic/default/normal/override) 中进行选择,因为它们不会合并。

你也可以在这本食谱中找到我的例子:TYPO3-cookbooks/t3-pdns

编辑:我忘记了告诉 Berkshelf 关于那本食谱的步骤。添加到您的 Berksfile:

group :integration do
  cookbook 'export-node', path: 'test/fixtures/cookbooks/export-node'
end