启动 RSpec-puppet 单元测试的正确方法 [英] Correct way to start RSpec-puppet unit tests

查看:42
本文介绍了启动 RSpec-puppet 单元测试的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 Puppet 4 类和一个单元测试,如下(在执行 touch metadata.json; rspec-puppet-init 而在 modules/test/):

I have created a simple Puppet 4 class and a unit test to go along with it as follows (after executing touch metadata.json; rspec-puppet-init while in modules/test/):

# modules/test/manifests/hello_world1.pp
class test::hello_world1 {
  file { "/tmp/hello_world1":
    content => "Hello, world!\n"
  }
}

# modules/test/spec/classes/test__hello_world1_spec.rb
require 'spec_helper'
describe 'test::hello_world1' do
  it { is_expected.to compile }
  it { is_expected.to contain_file('/tmp/hello_world1')\
    .with_content(/^Hello, world!$/) }
end

我可以通过在 modules/test/ 中执行 rspec spec/classes/test__hello_world1_spec.rb 来成功运行单元测试.

I can successfully run the unit test by executing rspec spec/classes/test__hello_world1_spec.rb while in modules/test/.

我现在想继续学习一个稍微高级一点的课程,它使用来自另一个模块的代码,即 concat(模块已经安装在modules/concat):

I would now like to proceed to a slightly more advanced class that uses code from another module, namely concat (the module has arleady been installed in modules/concat):

# modules/test/manifests/hello_world2.pp
class test::hello_world2
{
  concat{ "/tmp/hello_world2":
    ensure => present,
  }
  concat::fragment{ "/tmp/hello_world2_01":
    target  => "/tmp/hello_world2",
    content => "Hello, world!\n",
    order   => '01',
  }
}

# modules/test/spec/classes/test__hello_world2_spec.rb
require 'spec_helper'
describe 'test::hello_world2' do
  it { is_expected.to compile }
  # ...
end

当我在 modules/test 中尝试使用 rspec spec/classes/test__hello_world2_spec.rb 运行此单元测试时,我收到一条错误消息,其中包括:

When I attempt running this unit test with rspec spec/classes/test__hello_world2_spec.rb while in modules/test I receive an error message that includes:

失败/错误:编译过程中出现 { is_expected.to compile } 错误:评估错误:评估资源语句时出错,未知资源类型:'concat'

Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Unknown resource type: 'concat'

我怀疑根本原因是 rspec 找不到其他模块,因为它没有被告知模块路径".

I suspect the root cause is that rspec cannot find the other module(s), because it has not been told a "modulepath".

我的问题是:我应该如何开始单元测试,尤其是需要访问其他模块的单元测试?

My question is this: How exactly am I supposed to start unit tests, especially ones that require access to other modules?

推荐答案

安装 PDK 从它的下载页面为您的平台.使用 pdk new modulepdk new class 重新创建模块,或者按照 指南.

Install the PDK for your platform from its download page. Re-create the module using pdk new module, and pdk new class, or by following the Guide.

现在,我谈到了您的代码中可能存在的直接问题:您的代码依赖于 Puppet Forge 模块 puppetlabs/concat 但您尚未使其可用.PDK 模块模板已经预先配置了 puppetlabs_spec_helper 来为您的模块加载装置.

Now, I come to what is probably the immediate problem in your code: your code depends on a Puppet Forge module, puppetlabs/concat but you haven't made it available. The PDK module template already has pre-configured puppetlabs_spec_helper to load fixtures for your module.

要告诉 puppetlabs_spec_helper 为您获取它,您需要一个包含以下内容的文件 .fixtures.yml:

To tell puppetlabs_spec_helper to get it for you, you need a file .fixtures.yml with the following content:

fixtures:
  forge_modules:
    stdlib: puppetlabs/stdlib
    concat: puppetlabs/concat

请注意,您还需要 puppetlabs/stdlib,因为它是 puppetlabs/concat 的依赖项.

Note that you also need puppetlabs/stdlib, because that is a dependency of puppetlabs/concat.

如果您想探索更多夹具的可能性,请参阅 puppetlabs_spec_helper 的文档.

If you want to explore more fixture possibilities, please refer to puppetlabs_spec_helper's docs.

有了所有这些,并将您发布的代码示例和测试内容集成到 PDLK 提供的初始代码框架中,您的测试现在将在您运行时全部通过:

With all of this in place, and integrating the code samples and test content you posted into the initial code skeletons provided by the PDLK, your tests will all pass now when you run:

$ pdk test unit

请注意,我在博客文章中写了所有有关底层技术的内容,展示了如何从头开始设置 Rspec-puppet 等(ref),它似乎仍然成为有关此主题的最新参考资料.

Note that I have written all about the underlying technologies, in a blog post, showing how to set up Rspec-puppet and more from scratch (ref), and it still appears to be the most up-to-date reference on this subject.

要详细了解 rspec-puppet,请参阅官方 rspec-puppet 文档站点.

To read more about rspec-puppet in general, please refer to the official rspec-puppet docs site.

这篇关于启动 RSpec-puppet 单元测试的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆