标签: chefspec

ChefSpec和测试厨房

我正在调查ChefSpec报告(带覆盖范围)和测试厨房报告是否可以生成为junit格式,以便Jenkins可以解析它们以发布结果.或者如果有其他友好的方式,这些结果可以由詹金斯发布.

谢谢.

编辑1:对不起,如果我解释不正确,但我知道chefspec是单元测试,测试厨房用于集成测试.我在这个问题中的意思是如何以Jenkins解析的格式(例如junit格式)获取测试结果报告(对于chefspec和另一个手工测试厨房).

chef-infra chefspec test-kitchen

12
推荐指数
2
解决办法
2976
查看次数

NoMethodError:使用ChefSpec测试角色时未定义的方法`run_list_for'

我正在尝试使用ChefSpec测试一个角色.我宁愿不使用Chef Zero(通过要求'chefspec/server'),因为它比ChefSpec自己运行起来要慢一些.

也许我正在阅读文档错误,但看起来不需要Chef Zero来测试角色.但是,我对当前的配置没有任何好运.这是我的测试:

require 'chefspec'

RSpec.configure do |config|
  config.cookbook_path = 'C:\projects\chef\cookbooks'
  config.role_path = 'C:\projects\chef\roles'
  config.log_level = :debug
  config.platform = 'ubuntu'
  config.version = '12.04'
end

describe 'my_cookbook::default' do
  let(:chef_run) do
    ChefSpec::Runner.new.converge('role[my_role]')
  end

  it 'runs without failing' do
    expect(chef_run)
  end
end
Run Code Online (Sandbox Code Playgroud)

角色(位于角色/ my_role.json):

{
    "name": "my_role",
    "description": "My role",
    "default_attributes": {
    },
    "run_list": [
        "recipe[my_cookbook::default]"
    ]
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我收到:

NoMethodError: undefined method `run_list_for' for #<Hash:0x4fa3280>
./spec/role_spec.rb:13:in `block (2 levels) in <top (required)>'
./spec/role_spec.rb:17:in `block (2 levels) in <top …
Run Code Online (Sandbox Code Playgroud)

chef-infra chefspec

7
推荐指数
1
解决办法
2467
查看次数

关于Ruby/ChefSpec编码风格的反馈

我对Ruby很陌生,但过去两周我一直在做大量的厨师测试研究.这个测试使用了ChefSpec和Fauxhai,但它看起来并不是"ruby-ish",我希望社区可以给我一些关于编码风格的指示.有没有更好的方法来编写这样的嵌套循环?

食谱/富/食谱/ default.rb

package "foo" do
  action :install
end
Run Code Online (Sandbox Code Playgroud)

食谱/富/规格/ default_spec.rb

require 'chefspec'

describe 'foo::default' do
  platforms = { 
    "debian"   => ['6.0.5'],
    "ubuntu"   => ['12.04', '10.04'],
    "centos"   => ['5.8', '6.0', '6.3'],
    "redhat"   => ['5.8', '6.3'],
    "mac_os_x" => ['10.6.8', '10.7.4', '10.8.2'],
    "windows"  => ['2008R2']
  }

  platforms.each do |platform,versions|
    versions.each do |version|
      context "on #{platform} #{version}" do
        before do
          Fauxhai.mock(platform: platform, version: version)
        end

        it 'should install foo' do
          @runner = ChefSpec::ChefRunner.new.converge('foo::default')
          @runner.should install_package 'foo'
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

欢迎任何和所有反馈.谢谢!

ruby rspec chef-infra chefspec

6
推荐指数
1
解决办法
1310
查看次数

ChefSpec - 无法设置节点属性

我有一个简单的测试nginx食谱:

require 'spec_helper'

describe 'my_cookbook::nginx' do
  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      node.set['nginx']['dir'] = '/etc/nginx'
    end.converge(described_recipe)
  end

  it 'should create configuration directory' do
    expect(chef_run).to create_directory("#{node['nginx']['dir']}")
  end

end
Run Code Online (Sandbox Code Playgroud)

哪个失败了:

Failures:

  1) my_cookbook::nginx should create configuration directory
     Failure/Error: expect(chef_run).to create_directory("#{node['nginx']['dir']}")
     NameError:
       undefined local variable or method `node' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000007993570>
Run Code Online (Sandbox Code Playgroud)

我正在尝试按照文档中的描述设置节点属性,是否有一些明显我缺少的东西?

chef-infra chefspec

6
推荐指数
1
解决办法
3595
查看次数

如何使用ChefSpec测试我的LWRP?

我创建了自定义LWRP,但是当我运行ChefSpec单元测试时.它不知道我的LWRP动作.

这是我的资源:

actions :install, :uninstall
default_action :install

attribute :version, :kind_of => String
attribute :options, :kind_of => String
Run Code Online (Sandbox Code Playgroud)

这是我的提供者:

def whyrun_supported?
  true
end

action :install do
  version = @new_resource.version
  options = @new_resource.options
  e = execute "sudo apt-get install postgresql-#{version} #{options}"
  @new_resource.updated_by_last_action(e.updated_by_last_action?)
end
Run Code Online (Sandbox Code Playgroud)

这是我的ChefSpec单元测试:

require_relative '../spec_helper'

describe 'app_cookbook::postgresql' do

  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      node.set[:app][:postgresql][:database_user] = 'test'
      node.set[:app][:postgresql][:database_password] = 'test'
      node.set[:app][:postgresql][:database_name] = 'testdb'
    end.converge(described_recipe)
  end

  it 'installs postgresql 9.1 package' do
    expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 …
Run Code Online (Sandbox Code Playgroud)

unit-testing chef-infra chefspec

6
推荐指数
1
解决办法
5944
查看次数

ChefSpec不应该测试包含的食谱

我已经建立了一个用于安装Jenkins CI的食谱.它使用了食谱中的资源keyrepository资源yum,所以我最终得到了以下食谱:

yum_key "RPM-GPG-KEY-jenkins" do
  url "http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key"
  action :add
end

yum_repository "jenkins" do
  description "Jenkins-CI 3rd party repository"
  url "http://pkg.jenkins-ci.org/redhat"
  key "RPM-GPG-KEY-jenkins"
  action :add
end
Run Code Online (Sandbox Code Playgroud)

当我将这个食谱包含在另一个食谱中:

include_recipe 'sp_jenkins::default'
Run Code Online (Sandbox Code Playgroud)

我用以下的ChefSpec测试来测试它

it 'includes the `sp_jenkins::default` recipe' do
  expect(chef_run).to include_recipe('sp_jenkins::install')
end
Run Code Online (Sandbox Code Playgroud)

我的ChefSpec测试失败,输出如下:

NameError:
  Cannot find a resource for yum_key on chefspec version 0.6.1
Run Code Online (Sandbox Code Playgroud)

(我不确定为什么它说版本0.6.1,gem list告诉我它使用的是3.0.2)

sp_jenkins食谱确实取决于yum食谱(metadata.rb),并运行正常,但是,我目前正在写不依赖于菜谱yum食谱,因此不具备yum_keyyum_repository方法可用.

有没有办法阻止ChefSpec"降序"到包含的食谱/烹饪书中,只测试当前的食谱?

ruby rspec chef-infra chefspec

5
推荐指数
1
解决办法
2581
查看次数

单元测试Chef提供者

如何为Chef提供商编写单元测试?

到目前为止,我们的单元测试策略使用ChefSpec作为配方,我们为库中的提供程序填充了大部分有趣的逻辑,使逻辑更易于测试.但是,我们仍遇到供应商调用其他资源(以及其他简单的逻辑问题)的问题.例如:

action :run do

   helper = Helper.new
   template '/etc/hosts' do
     source 'hosts.erb'
     variables ({
              "host" => @new_resource.host,
              "ip_address" => node['ipaddress']
          })
     only_if { helper.update_hosts }
   end

   service 'httpd' do
      action :restart
   end
end
Run Code Online (Sandbox Code Playgroud)

(这不是真正的代码,只是一个简单的例子)

我们要做的是单独测试此提供程序以检查逻辑错误.ChefSpec有能力进入LWRP,但看起来这会迫使我们将LWRP放入配方中,而我们的许多烹饪书基本上都是LWRP库而没有配方.我们还想在我们的测试中保持一个干净的分离,所以通过查看文件名很明显哪个组件失败了.

另外,如果LWRP定义中存在任何语法错误,测试会自动失败,那将是很好的.例如:

action :run do
   template '/etc/hosts/' do
     source_whoops 'hosts.erb'
     action :whoops
   end
end
Run Code Online (Sandbox Code Playgroud)

如果由于属性名称定义不正确而导致测试失败,并且操作名称不存在(就像ChefSpec一样),那将是非常好的.

我提出的唯一解决方案是基本上创建一个"测试食谱" - 一个单独的食谱,用一个食谱定义每个LWRP 1:1,所以ChefSpec可以这样进入它.这似乎是一个合理但不太理想的解决方案.

unit-testing chef-infra chefspec

5
推荐指数
1
解决办法
1424
查看次数

在ChefSpec运行期间模拟文件

我创建了一个Chef资源,它"扩展"了chef的部署资源.基本思想是检查是否存在deploy/crontab类似于deploy/after_restart.rb要部署的源中的机制的文件,并从中创建cronjobs.

虽然这种机制应该可行(参见https://github.com/fh/easybib-cookbooks/blob/t/cron-tests/easybib/providers/deploy.rb#L11-L14),但我正在努力基于ChefSpec的测试.我目前正在尝试使用FakeFS- 但是当我在Chef运行之前模拟Filesystem时,运行失败,因为没有找到cookbook,因为它们不存在于模拟文件系统中.如果我不deploy/crontab这样做,显然没有找到模拟文件,因此提供者不会做任何事情.我目前的方法是在chef_run FakeFS.activate!之前直接触发runner.converge(described_recipe).

我很想听听一些建议如何在这里进行正确的测试:是否有可能只在部署资源运行之前直接启用FakeFS,或者仅部分模拟文件系统?

rspec chef-infra chefspec

5
推荐指数
2
解决办法
4045
查看次数

Chefspec重复加载库并发出警告"已初始化的常量CONSTANT"

我有一个带图书馆的厨师食谱,例如library.rb.它包含一个CONSTANT:

CONSTANT = 'constant'
Run Code Online (Sandbox Code Playgroud)

当我为这本食谱编写单元测试时,它总是给我警告:

(Some prefix...)warning: already initialized constant CONSTANT
(Some prefix...)warning: previous definition of CONSTANT was here
Run Code Online (Sandbox Code Playgroud)

警告反复出现,与示例(测试用例)的数量减去一样多.我认为这是因为chefspec为每个示例加载一次库.有人能告诉我如何只加载一次库,或者如何禁用警告信息?

ruby chef-infra chefspec

5
推荐指数
1
解决办法
679
查看次数

使用ChefSpec基于模板测试文件初始化

我在我的食谱中创建了以下模板文件:

template "my_file" do
  path "my_path"
  source "my_file.erb"
  owner "root"
  group "root"
  mode "0644"
  variables(@template_variables)
  notifies :restart, resources(service: "my_service")
end
Run Code Online (Sandbox Code Playgroud)

以及我的ChefSpec测试中的以下断言:

  chef_run.should create_file "my_file"
  chef_run.file("my_file").should be_owned_by('root', 'root')
Run Code Online (Sandbox Code Playgroud)

这导致以下失败:

  No file resource named 'my_file' with action :create found.
Run Code Online (Sandbox Code Playgroud)

这是因为我没有使用file资源而是使用template资源.问题:如何使用ChefSpec测试模板资源的文件创建?

ruby chef-infra chefspec

4
推荐指数
2
解决办法
2400
查看次数

标签 统计

chef-infra ×10

chefspec ×10

ruby ×4

rspec ×3

unit-testing ×2

test-kitchen ×1