简体   繁体   English

无法运行具有数据包的Chefspec测试

[英]Unable to run chefspec tests having databags

I am trying to run unit tests using chefspec. 我正在尝试使用Chefspec运行单元测试。 I added databags to my recipe. 我在我的食谱中添加了数据袋。 Without databags, chefspec test running fine. 没有数据包,chefspec测试运行正常。 After adding databags chefspec showing below error: 添加数据袋后,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)>'

I dont know how to handle databags in chefspec. 我不知道如何处理Chefspec中的数据包。 When i execute recipe using chef-client in workstation, it is working fine. 当我在工作站中使用Chef-Client执行配方时,它工作正常。 but the chefspec testing is getting failed. 但是Chefspec测试失败。

recipe spec file: 配方规格文件:

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

recipe file: 配方文件:

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

Could you please suggest how to solve this issue. 您能否建议如何解决此问题。 I am running tests in my chef workstation only. 我仅在我的厨师工作站中运行测试。

You need to create the data bags in the test server, https://github.com/chefspec/chefspec#dsl shows how to do this using ServerRunner or you can use rspec-mocks and mock the load_secret and load methods. 您需要在测试服务器中创建数据包, https://github.com/chefspec/chefspec#dsl展示了如何使用ServerRunner来执行此操作,或者您可以使用rspec-mocks并模拟load_secretload方法。 I prefer the latter as it is faster (in terms of test run time) and more unit-y but it's up to you. 我更喜欢后者,因为它速度更快(就测试运行时间而言)并且单位y更大,但这取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM