【问题标题】:Unable to run chefspec tests having databags无法运行具有数据包的 chefspec 测试
【发布时间】:2017-06-16 13:48:16
【问题描述】:

我正在尝试使用 chefspec 运行单元测试。我在我的食谱中添加了数据包。没有数据包,chefspec 测试运行良好。添加 databags chefspec 后显示以下错误:

 1) database::prerequisites installs a package
     Failure/Error: let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'oracle', version: '7.2').converge(described_recipe) }
 Net::HTTPServerException:
   404 "Not Found "
 # /tmp/chefspec20170616-8187-olziw2file_cache_path/cookbooks/database/recipes/prerequisites.rb:9:in `from_file'
 # ./prerequisites_spec.rb:4:in `block (2 levels) in <top (required)>'
 # ./prerequisites_spec.rb:25:in `block (2 levels) in <top (required)>'

我不知道如何在 chefspec 中处理数据包。当我在工作站中使用厨师客户端执行食谱时,它工作正常。但是 chefspec 测试失败了。

配方规格文件:

require 'chefspec'

describe 'database::prerequisites' do
  let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'oracle', version: '7.2').converge(described_recipe) }



    before(:each) do
      stub_command("cat /etc/oracle-release | grep 7.*").and_return(true)
    end

    before(:each) do
      stub_command("cat /etc/oracle-release | grep 6.*").and_return(true)
    end

     before(:each) do
      stub_command("cat /etc/selinux/config | grep SELINUX=disabled").and_return(true)
    end

    before(:each) do
      stub_command("sestatus | grep enabled").and_return(true)
    end

  it 'installs a package ' do
    expect(chef_run).to install_package('oracle-rdbms-server-12cR1-preinstall')
    expect(chef_run).to install_package('net-tools')
    expect(chef_run).to install_package('xorg-x11-apps')

  end

   it 'creates a directory with the default action' do
    expect(chef_run).to create_directory('/u01/app/oraInventory')
    expect(chef_run).to create_directory('/oradata')
  end  

end

配方文件:

my_secret_key = Chef::EncryptedDataBagItem.load_secret("/etc/secret_key")
passwords = Chef::EncryptedDataBagItem.load("databags", "databag_passwords", my_secret_key)

您能否建议如何解决此问题。我只在我的厨师工作站上运行测试。

【问题讨论】:

  • 为什么不使用data_bag_item配方DSL方法(Documentation here)?
  • 感谢您的建议。但我仍然遇到同样的错误。厨师食谱运行顺利。但测试给出了同样的错误。

标签: rspec chef-infra chefspec databags


【解决方案1】:

您需要在测试服务器中创建数据包,https://github.com/chefspec/chefspec#dsl 显示如何使用 ServerRunner 执行此操作,或者您可以使用 rspec-mocks 并模拟 load_secretload 方法。我更喜欢后者,因为它更快(就测试运行时间而言)和更多单元,但这取决于你。

【讨论】:

    最近更新 更多